Skip to content

Commit

Permalink
feat(vscode): integrate native typescript editor features
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Dec 26, 2023
1 parent 2999cdf commit a8d33ec
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 97 deletions.
84 changes: 24 additions & 60 deletions extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -606,16 +606,6 @@
}
},
"commands": [
{
"command": "volar.action.restartServer",
"title": "Restart Vue server",
"category": "Volar"
},
{
"command": "volar.action.reloadProject",
"title": "Reload Project",
"category": "Volar"
},
{
"command": "volar.action.doctor",
"title": "Doctor",
Expand All @@ -636,27 +626,37 @@
"title": "Split <script>, <template>, <style> Editors",
"category": "Volar",
"icon": "images/split-editors.png"
},
{
"command": "volar.selectTypeScriptVersion",
"title": "Select TypeScript Version...",
"category": "Volar"
},
{
"command": "volar.vue.findAllFileReferences",
"title": "Vue: Find File References",
"category": "Volar"
}
],
"menus": {
"editor/context": [
{
"command": "typescript.goToSourceDefinition",
"when": "tsSupportsSourceDefinition && resourceLangId == vue",
"group": "navigation@9"
}
],
"explorer/context": [
{
"command": "typescript.findAllFileReferences",
"when": "tsSupportsFileReferences && resourceLangId == vue",
"group": "4_search"
}
],
"editor/title/context": [
{
"command": "typescript.findAllFileReferences",
"when": "tsSupportsFileReferences && resourceLangId == vue"
}
],
"commandPalette": [
{
"command": "volar.action.restartServer",
"when": "volar.activated"
"command": "typescript.reloadProjects",
"when": "editorLangId == vue && typescript.isManagedFile"
},
{
"command": "volar.action.reloadProject",
"when": "volar.activated"
"command": "typescript.goToProjectConfig",
"when": "editorLangId == vue"
},
{
"command": "volar.action.doctor",
Expand All @@ -669,10 +669,6 @@
{
"command": "volar.action.splitEditors",
"when": "editorLangId == vue"
},
{
"command": "volar.selectTypeScriptVersion",
"when": "volar.activated"
}
],
"editor/title": [
Expand All @@ -681,38 +677,6 @@
"when": "resourceLangId == vue && config.vue.splitEditors.icon",
"group": "navigation"
}
],
"explorer/context": [
{
"command": "volar.vue.findAllFileReferences",
"when": "resourceLangId == typescript",
"group": "4_search"
},
{
"command": "volar.vue.findAllFileReferences",
"when": "resourceLangId == typescriptreact",
"group": "4_search"
},
{
"command": "volar.vue.findAllFileReferences",
"when": "resourceLangId == javascript",
"group": "4_search"
},
{
"command": "volar.vue.findAllFileReferences",
"when": "resourceLangId == javascriptreact",
"group": "4_search"
},
{
"command": "volar.vue.findAllFileReferences",
"when": "resourceLangId == vue",
"group": "4_search"
},
{
"command": "volar.vue.findAllFileReferences",
"when": "resourceLangId == html",
"group": "4_search"
}
]
},
"problemMatchers": [
Expand Down
39 changes: 2 additions & 37 deletions extensions/vscode/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import {
activateAutoInsertion,
// activateDocumentDropEdit,
// activateFindFileReferences,
// activateReloadProjects,
activateServerSys,
// activateTsConfigStatusItem,
// activateTsVersionStatusItem,
// activateWriteVirtualFiles,
activateWriteVirtualFiles,
getTsdk,
} from '@volar/vscode';
import { DiagnosticModel, VueServerInitializationOptions } from '@vue/language-server';
Expand Down Expand Up @@ -93,38 +89,7 @@ async function doActivate(context: vscode.ExtensionContext, createLc: CreateLang

activateAutoInsertion([client], document => supportedLanguages[document.languageId]); // TODO: implement auto insert .value
// activateDocumentDropEdit(selectors, client);
// activateWriteVirtualFiles('volar.action.writeVirtualFiles', client);
// activateFindFileReferences('volar.vue.findAllFileReferences', client);
// activateTsConfigStatusItem('volar.openTsconfig', client,
// document => {
// return document.languageId === 'vue'
// || (config.server.vitePress.supportMdFile && document.languageId === 'markdown')
// || (config.server.petiteVue.supportHtmlFile && document.languageId === 'html')
// || (
// takeOverModeActive(context)
// && ['javascript', 'typescript', 'javascriptreact', 'typescriptreact'].includes(document.languageId)
// );
// },
// );
// activateReloadProjects('volar.action.reloadProject', [client]);
// activateTsVersionStatusItem('volar.selectTypeScriptVersion', context, client,
// document => {
// return document.languageId === 'vue'
// || (config.server.vitePress.supportMdFile && document.languageId === 'markdown')
// || (config.server.petiteVue.supportHtmlFile && document.languageId === 'html')
// || (
// takeOverModeActive(context)
// && ['javascript', 'typescript', 'javascriptreact', 'typescriptreact'].includes(document.languageId)
// );
// },
// text => {
// if (takeOverModeActive(context)) {
// text += ' (takeover)';
// }
// return text;
// },
// false,
// );
activateWriteVirtualFiles('volar.action.writeVirtualFiles', client);

activateServerSys(client);

Expand Down
25 changes: 25 additions & 0 deletions extensions/vscode/src/nodeClientMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as lsp from 'vscode-languageclient/node';
import { activate as commonActivate, deactivate as commonDeactivate } from './common';
import { config } from './config';
import { middleware } from './middleware';
import * as fs from 'fs';

export async function activate(context: vscode.ExtensionContext) {

Expand Down Expand Up @@ -181,3 +182,27 @@ function updateProviders(client: lsp.LanguageClient) {
return initializeFeatures.call(client, ...args);
};
}

try {
const tsExtension = vscode.extensions.getExtension('vscode.typescript-language-features')!;
const readFileSync = fs.readFileSync;
const extensionJsPath = require.resolve('./dist/extension.js', { paths: [tsExtension.extensionPath] });

// @ts-expect-error
fs.readFileSync = (...args) => {
if (args[0] === extensionJsPath) {
// @ts-expect-error
let text = readFileSync(...args) as string;

// patch jsTsLanguageModes
text = text.replace('t.$u=[t.$r,t.$s,t.$p,t.$q]', s => s + '.concat("vue")');

// patch isSupportedLanguageMode
text = text.replace('s.languages.match([t.$p,t.$q,t.$r,t.$s]', s => s + '.concat("vue")');

return text;
}
// @ts-expect-error
return readFileSync(...args);
};
} catch { }

0 comments on commit a8d33ec

Please sign in to comment.