Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'open in vscode' for TT #163

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions apps/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@
"Linters"
],
"activationEvents": [
"onLanguage:typescript",
"onLanguage:typescriptreact",
"onLanguage:javascript",
"onLanguage:javascriptreact",
"onLanguage:vue",
"onLanguage:svelte",
"onLanguage:astro"
"*"
],
"main": "./out/extension.js",
"browser": "./out/extension.js",
Expand Down
2 changes: 2 additions & 0 deletions apps/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { defaultOptions } from './defaultOptions';
import { initDiagnostics } from './initDiagnostics';
import { showBeginnerQuestion } from './showBeginnerQuestion';
import { showTipsQuestion } from './showTipsQuestion';
import { initTotalTypeScriptCommands } from './totalTypeScriptCommands';

const languages = [
'typescript',
Expand Down Expand Up @@ -80,6 +81,7 @@ const isTipComplete = (tipType: Tip['type']) => {

export async function activate(context: vscode.ExtensionContext) {
initDiagnostics(context);
await initTotalTypeScriptCommands(context);

const updateHiddenTips = () => {
updateOptions();
Expand Down
123 changes: 123 additions & 0 deletions apps/vscode/src/totalTypeScriptCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import * as vscode from 'vscode';

class UriEventHandler
extends vscode.EventEmitter<vscode.Uri>
implements vscode.UriHandler
{
public handleUri(uri: vscode.Uri): vscode.ProviderResult<void> {
this.fire(uri);
}
}

const terminals = new Set<vscode.Terminal>();

const uriHandler = new UriEventHandler();

export const initTotalTypeScriptCommands = async (
context: vscode.ExtensionContext,
) => {
const openFileAndRunTests = async (config: {
script: string;
fileUri: vscode.Uri;
}) => {
await vscode.window.showTextDocument(config.fileUri);

terminals.forEach((terminal) => {
try {
terminal.dispose();
} catch (e) {}
});

terminals.clear();

const terminal = vscode.window.createTerminal();

terminals.add(terminal);

terminal.sendText(config.script);
terminal.show(true);
};

context.subscriptions.push(
vscode.window.registerUriHandler(uriHandler),
uriHandler.event(async (uri) => {
const info = parseQuery<{
filename: string;
repo: string;
script: string;
}>(uri);

for (const folder of vscode.workspace.workspaceFolders || []) {
if (folder.name === info.repo) {
await openFileAndRunTests({
fileUri: vscode.Uri.joinPath(folder.uri, info.filename),
script: info.script,
});
return;
}
}

// Tries scanning other workspaces at the same level for the right folder

const candidateRepos = await vscode.workspace.fs.readDirectory(
vscode.Uri.joinPath(
vscode.Uri.parse(vscode.workspace.rootPath!),
'../',
),
);

const candidateRepo = candidateRepos.find(([name]) => name === info.repo);

console.log(candidateRepo, info.repo);

if (candidateRepo) {
const rootUri = vscode.Uri.joinPath(
vscode.Uri.parse(vscode.workspace.rootPath!),
'../',
candidateRepo[0],
);

await vscode.workspace.updateWorkspaceFolders(
vscode.workspace.workspaceFolders?.length || 0,
0,
{
uri: rootUri,
},
);

await openFileAndRunTests({
fileUri: vscode.Uri.joinPath(rootUri, info.filename),
script: info.script,
});
return;
}

vscode.window
.showErrorMessage(
`Could not find the right file in this repository.`,
{
detail: `Looks like this isn't total-typescript/${info.repo}. Would you like to clone it?`,
modal: true,
},
'Clone from GitHub',
)
.then((action) => {
if (action === 'Clone from GitHub') {
vscode.env.openExternal(
vscode.Uri.parse(
`https://github.com/total-typescript/${info.repo}`,
),
);
}
});
}),
);
};

function parseQuery<T>(uri: vscode.Uri): T {
return uri.query.split('&').reduce((prev: any, current) => {
const queryString = current.split('=');
prev[queryString[0]] = queryString[1];
return prev;
}, {});
}