Skip to content

Commit

Permalink
feat: add getScriptKind()
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed May 26, 2021
1 parent 14403a5 commit 0386094
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 36 deletions.
68 changes: 40 additions & 28 deletions packages/typescript-vue-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,48 @@ import { createLanguageService } from 'vscode-vue-languageservice'
import { sleep } from '@volar/shared';
import * as path from 'upath';

export = function init(modules: { typescript: typeof import('typescript/lib/tsserverlibrary') }) {
const ts = modules.typescript;
const init: ts.server.PluginModuleFactory = (modules) => {
const { typescript: ts } = modules;
const vueFilesGetter = new WeakMap<ts.server.Project, () => string[]>();

return {
create,
getExternalFiles,
};

function create(info: ts.server.PluginCreateInfo): ts.LanguageService {

const proxyHost = createProxyHost(ts, info);
const vueLs = createLanguageService({ typescript: ts }, proxyHost.host, true);

vueFilesGetter.set(info.project, proxyHost.getVueFiles);

return new Proxy(info.languageService, {
get: (target: any, property: keyof ts.LanguageService) => {
return vueLs.__internal__.tsPlugin[property] || target[property];
},
});
}
function getExternalFiles(project: ts.server.Project): string[] {
const getVueFiles = vueFilesGetter.get(project);
if (!getVueFiles) {
return [];
}
return getVueFiles().filter(fileName => project.fileExists(fileName));
const pluginModule: ts.server.PluginModule = {
create(info) {

// fix: https://github.com/johnsoncodehk/volar/issues/205
info.project.getScriptKind = fileName => {
switch (path.extname(fileName)) {
case '.vue': return ts.ScriptKind.JSON; // can't use External, Unknown
case '.js': return ts.ScriptKind.JS;
case '.jsx': return ts.ScriptKind.JSX;
case '.ts': return ts.ScriptKind.TS;
case '.tsx': return ts.ScriptKind.TSX;
case '.json': return ts.ScriptKind.JSON;
default: return ts.ScriptKind.Unknown;
}
};

const proxyHost = createProxyHost(ts, info);
const vueLs = createLanguageService(modules, proxyHost.host);

vueFilesGetter.set(info.project, proxyHost.getVueFiles);

return new Proxy(info.languageService, {
get: (target: any, property: keyof ts.LanguageService) => {
return vueLs.__internal__.tsPlugin[property] || target[property];
},
});
},
getExternalFiles(project) {
const getVueFiles = vueFilesGetter.get(project);
if (!getVueFiles) {
return [];
}
return getVueFiles().filter(fileName => project.fileExists(fileName));
},
}
}
return pluginModule;
};

export = init;

function createProxyHost(ts: typeof import('typescript/lib/tsserverlibrary'), info: ts.server.PluginCreateInfo) {

Expand Down
21 changes: 13 additions & 8 deletions packages/vscode-vue-languageservice/src/languageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export function getDocumentLanguageService({ typescript: ts }: Dependencies) {
export function createLanguageService(
{ typescript: ts }: Dependencies,
vueHost: LanguageServiceHost,
isTsPlugin = false,
) {

let vueProjectVersion: string | undefined;
Expand All @@ -107,7 +106,7 @@ export function createLanguageService(
const resolveResult = ts.resolveModuleName(ref, base, vueHost.getCompilationSettings(), compilerHost);
const failedLookupLocations: string[] = (resolveResult as any).failedLookupLocations;
const dirs = new Set<string>();

for (const failed of failedLookupLocations) {
let path = failed;
if (path.endsWith('index.d.ts')) {
Expand Down Expand Up @@ -417,6 +416,17 @@ export function createLanguageService(
}
return result;
},
getScriptKind(fileName) {
switch (upath.extname(fileName)) {
case '.vue': return ts.ScriptKind.JSON; // can't use External, Unknown
case '.js': return ts.ScriptKind.JS;
case '.jsx': return ts.ScriptKind.JSX;
case '.ts': return ts.ScriptKind.TS;
case '.tsx': return ts.ScriptKind.TSX;
case '.json': return ts.ScriptKind.JSON;
default: return ts.ScriptKind.Unknown;
}
},
};

return tsHost;
Expand All @@ -432,12 +442,7 @@ export function createLanguageService(
tsFileNames.push(uriToFsPath(uri)); // virtual .ts
}
}
if (isTsPlugin) {
tsFileNames.push(fileName); // .vue + .ts
}
else if (!sourceFile && !fileName.endsWith('.vue')) {
tsFileNames.push(fileName); // .ts
}
tsFileNames.push(fileName); // .vue + .ts
}
return tsFileNames;
}
Expand Down

0 comments on commit 0386094

Please sign in to comment.