diff --git a/src/extension.js b/src/extension.js index 10e2c9d..bb57035 100644 --- a/src/extension.js +++ b/src/extension.js @@ -28,6 +28,30 @@ function activate(context) { ); let timeout = null; + + const runnerConfigMap = { + php: { + runner: phpRunner, + runnerOptions: undefined + }, + typescript: { + runner: typescriptRunner, + runnerOptions: { language: ts.ScriptKind.TS } + }, + typescriptreact: { + runner: typescriptRunner, + runnerOptions: { language: ts.ScriptKind.TSX } + }, + javascript: { + runner: typescriptRunner, + runnerOptions: { language: ts.ScriptKind.JS } + }, + javascriptreact: { + runner: typescriptRunner, + runnerOptions: { language: ts.ScriptKind.JSX } + } + } + const trigger = (identifier, editor, force, time = 100) => { if (currentRunner && !currentRunner.state.done) { currentRunner.reject(); @@ -36,64 +60,27 @@ function activate(context) { clearTimeout(timeout); } timeout = setTimeout(() => { - if (editor && (isEnabled() || force)) { - if (languagesEnabled().includes("php") && editor.document.languageId === 'php') { - currentRunner = runner(phpRunner, editor, hints => { - if (hints !== false && isEnabled()) { - if (hints.length) { - editor.setDecorations(hintDecorationType, hints); - } else { - editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); - } - - } - }) - } else if (languagesEnabled().includes("typescript") && editor.document.languageId === 'typescript') { - currentRunner = runner(typescriptRunner, editor, hints => { - if (hints !== false && isEnabled()) { - if (hints.length) { - editor.setDecorations(hintDecorationType, hints); - } else { - editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); - } - - } - }, { language: ts.ScriptKind.TS }) - } else if (languagesEnabled().includes("typescriptreact") && editor.document.languageId === 'typescriptreact') { - currentRunner = runner(typescriptRunner, editor, hints => { - if (hints !== false && isEnabled()) { - if (hints.length) { - editor.setDecorations(hintDecorationType, hints); - } else { - editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); - } - - } - }, { language: ts.ScriptKind.TSX }) - } else if (languagesEnabled().includes("javascript") && editor.document.languageId === 'javascript') { - currentRunner = runner(typescriptRunner, editor, hints => { - if (hints !== false && isEnabled()) { - if (hints.length) { - editor.setDecorations(hintDecorationType, hints); - } else { - editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); - } - - } - }, { language: ts.ScriptKind.JS }) - } else if (languagesEnabled().includes("javascriptreact") && editor.document.languageId === 'javascriptreact') { - currentRunner = runner(typescriptRunner, editor, hints => { - if (hints !== false && isEnabled()) { - if (hints.length) { - editor.setDecorations(hintDecorationType, hints); - } else { - editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); - } - - } - }, { language: ts.ScriptKind.JSX }) + if (!editor || !(isEnabled() || force)) { + return; + } + + for(const [language, config] of Object.entries(runnerConfigMap)) { + if(!languagesEnabled().includes(language) || editor.document.languageId !== language) { + continue; } + currentRunner = runner(config.runner, editor, hints => { + if (hints === false || !isEnabled()) { + return; + } + if (hints.length) { + editor.setDecorations(hintDecorationType, hints); + } else { + editor.setDecorations(hintDecorationType, [new vscode.Range(0, 0, 0, 0)]); + } + }, config.runnerOptions) + break; } + }, time); } const clear = (editor) => {