Skip to content

Commit

Permalink
fix: fixed a few bugs from #1050
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Mar 18, 2022
1 parent 259740e commit 60b2d08
Show file tree
Hide file tree
Showing 20 changed files with 179 additions and 178 deletions.
5 changes: 5 additions & 0 deletions extensions/vscode-typescript-vue-plugin/scripts/build-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ require('esbuild').build({
const pathEsm = pathUmdMay.replace('/umd/', '/esm/')
return { path: pathEsm }
})
build.onResolve({ filter: /\@vue\/compiler-sfc/ }, args => {
const pathUmdMay = require.resolve(args.path, { paths: [args.resolveDir] })
const pathEsm = pathUmdMay.replace('compiler-sfc.cjs.js', 'compiler-sfc.esm-browser.js')
return { path: pathEsm }
})
},
}],
}).catch(() => process.exit(1))
5 changes: 5 additions & 0 deletions extensions/vscode-vue-language-features/scripts/build-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ require('esbuild').build({
const pathEsm = pathUmdMay.replace('/umd/', '/esm/')
return { path: pathEsm }
})
build.onResolve({ filter: /\@vue\/compiler-sfc/ }, args => {
const pathUmdMay = require.resolve(args.path, { paths: [args.resolveDir] })
const pathEsm = pathUmdMay.replace('compiler-sfc.cjs.js', 'compiler-sfc.esm-browser.js')
return { path: pathEsm }
})
},
}],
}).catch(() => process.exit(1))
15 changes: 6 additions & 9 deletions packages/typescript-language-service/src/services/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ export interface Data {
uri: string,
fileName: string,
offset: number,
source: string | undefined,
name: string,
tsData: any,
originalItem: ts.CompletionEntry
}

export function getTriggerCharacters(tsVersion: string) {
Expand Down Expand Up @@ -133,14 +131,13 @@ export function register(
uri,
fileName,
offset,
source: tsEntry.source,
name: tsEntry.name,
tsData: tsEntry.data,
originalItem: tsEntry,
};
// @ts-expect-error
item.data = data;

return item;
return {
...item,
data: data as any,
};
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ export function register(
) {
return async (item: vscode.CompletionItem, newPosition?: vscode.Position): Promise<vscode.CompletionItem> => {

// @ts-expect-error
const data: Data = item.data;
const data: Data = item.data as any;
const fileName = data.fileName;
const name = data.name;
const source = data.source;
let offset = data.offset;
const document = getTextDocument(data.uri);

Expand All @@ -35,7 +32,7 @@ export function register(

let details: ts.CompletionEntryDetails | undefined;
try {
details = languageService.getCompletionEntryDetails(fileName, offset, name, formatOptions, source, preferences, data.tsData);
details = languageService.getCompletionEntryDetails(fileName, offset, data.originalItem.name, formatOptions, data.originalItem.source, preferences, data.originalItem.data);
}
catch (err) {
item.detail = `[TS Error] ${err}`;
Expand Down
1 change: 1 addition & 0 deletions packages/vue-language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@volar/shared": "0.33.2",
"@volar/vue-language-service": "0.33.2",
"@volar/vue-typescript": "0.33.2",
"@vue/shared": "^3.2.27",
"request-light": "^0.5.7",
"upath": "^2.0.1",
Expand Down
3 changes: 1 addition & 2 deletions packages/vue-language-server/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { createLsConfigs } from './configs';
import { getInferredCompilerOptions } from './inferredCompilerOptions';
import { createProjects } from './projects';
import type { FileSystemProvider } from 'vscode-html-languageservice';
import { EmbeddedLanguagePlugin } from '@volar/vue-language-service';

export interface RuntimeEnvironment {
loadTypescript: (initOptions: shared.ServerInitializationOptions) => typeof import('typescript/lib/tsserverlibrary'),
Expand Down Expand Up @@ -129,7 +128,7 @@ export function createLanguageServer(connection: vscode.Connection, runtimeEnv:
export function loadCustomPlugins(dir: string) {
try {
const configPath = require.resolve('./volar.config.js', { paths: [dir] });
const config: { plugins?: EmbeddedLanguagePlugin[] } = require(configPath);
const config: { plugins?: vue.EmbeddedLanguagePlugin[] } = require(configPath);
return config.plugins ?? []
}
catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions packages/vue-language-service/src/documentFeatures/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function register(context: DocumentServiceRuntimeContext) {

const sourceMap = vueDocument.sourceMapsMap.get(embedded.self);

if (embedded.self.file.lsType === 'template')
if (embedded.inheritParentIndent)
toPatchIndent = {
lsType: sourceMap.embeddedFile.lsType,
sourceMapEmbeddedDocumentUri: sourceMap.mappedDocument.uri,
Expand Down Expand Up @@ -71,7 +71,7 @@ export function register(context: DocumentServiceRuntimeContext) {
applyEdits(edits);
}

if (toPatchIndent !== undefined) {
if (toPatchIndent) {

tryUpdateVueDocument();

Expand Down
36 changes: 21 additions & 15 deletions packages/vue-language-service/src/documentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,29 @@ export function getDocumentService(

let vueDoc = vueDocuments.get(document);

if (!vueDoc) {

const vueFile = vueTs.createVueFile(
shared.uriToFsPath(document.uri),
document.getText(),
document.version.toString(),
vueTsPlugins,
{},
context.typescript,
services.getCssVBindRanges,
services.getCssClasses,
);
vueDoc = parseVueDocument(vueFile);

vueDocuments.set(document, vueDoc);
if (vueDoc) {

if (vueDoc.file.getVersion() !== document.version.toString()) {
vueDoc.file.update(document.getText(), document.version.toString());
}

return vueDoc;
}

const vueFile = vueTs.createVueFile(
shared.uriToFsPath(document.uri),
document.getText(),
document.version.toString(),
vueTsPlugins,
{},
context.typescript,
services.getCssVBindRanges,
services.getCssClasses,
);
vueDoc = parseVueDocument(vueFile);

vueDocuments.set(document, vueDoc);

return vueDoc;
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/vue-language-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ export { margeWorkspaceEdits } from './languageFuatures/rename';
export * from './languageService';
export * from './types';
export { convertTagNameCasingCommand, ConvertTagNameCasingCommandArgs } from './vuePlugins/tagNameCasingConversions';

25 changes: 15 additions & 10 deletions packages/vue-language-service/src/languageFuatures/codeLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,24 @@ export function register(context: LanguageServiceRuntimeContext) {
const codeLens = await plugin.doCodeLens?.(document);

if (codeLens) {
return codeLens.map(item => (<vscode.CodeLens>{
...item,
command: item.command ? {
...item.command,
command: executePluginCommand,
arguments: <ExecutePluginCommandArgs>[uri, plugin.id, item.command],
} : undefined,
data: <PluginCodeLensData>{
return codeLens.map(item => {
const data: PluginCodeLensData = {
uri,
originalItem: item,
pluginId: plugin.id,
} as any,
}));
}
const commandArgs: ExecutePluginCommandArgs | undefined = item.command ? [uri, plugin.id, item.command] : undefined;
const codeLens: vscode.CodeLens = {
...item,
command: item.command && commandArgs ? {
...item.command,
command: executePluginCommand,
arguments: commandArgs as any,
} : undefined,
data: data as any,
};
return codeLens;
});
}
},
(data, sourceMap) => data.map(codeLens => {
Expand Down
64 changes: 38 additions & 26 deletions packages/vue-language-service/src/languageFuatures/complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,24 @@ export function register(context: LanguageServiceRuntimeContext) {

cacheData.list = {
...embeddedCompletionList,
items: embeddedCompletionList.items.map(item => ({
...transformCompletionItem(
item,
embeddedRange => sourceMap.getSourceRange(embeddedRange.start, embeddedRange.end)?.[0],
),
data: <PluginCompletionData>{
items: embeddedCompletionList.items.map(item => {
const data: PluginCompletionData = {
uri,
originalItem: item,
pluginId: cacheData.plugin.id,
sourceMap: {
lsType: sourceMap.embeddedFile.lsType,
embeddedDocumentUri: sourceMap.mappedDocument.uri,
},
} as any,
})),
};
return {
...transformCompletionItem(
item,
embeddedRange => sourceMap.getSourceRange(embeddedRange.start, embeddedRange.end)?.[0],
),
data: data as any,
};
}),
};
}
}
Expand All @@ -99,15 +102,18 @@ export function register(context: LanguageServiceRuntimeContext) {

cacheData.list = {
...completionList,
items: completionList.items.map(item => ({
...item,
data: <PluginCompletionData>{
items: completionList.items.map(item => {
const data: PluginCompletionData = {
uri,
originalItem: item,
pluginId: cacheData.plugin.id,
sourceMap: undefined,
} as any,
}))
};
return {
...item,
data: data as any,
};
})
};
}
}
Expand Down Expand Up @@ -154,21 +160,24 @@ export function register(context: LanguageServiceRuntimeContext) {

const completionList: vscode.CompletionList = {
...embeddedCompletionList,
items: embeddedCompletionList.items.map(item => ({
...transformCompletionItem(
item,
embeddedRange => sourceMap.getSourceRange(embeddedRange.start, embeddedRange.end)?.[0],
),
data: <PluginCompletionData>{
items: embeddedCompletionList.items.map(item => {
const data: PluginCompletionData = {
uri,
originalItem: item,
pluginId: plugin.id,
sourceMap: {
lsType: sourceMap.embeddedFile.lsType,
embeddedDocumentUri: sourceMap.mappedDocument.uri,
}
} as any,
})),
};
return {
...transformCompletionItem(
item,
embeddedRange => sourceMap.getSourceRange(embeddedRange.start, embeddedRange.end)?.[0],
),
data: data as any,
};
}),
};

cache!.data.push({
Expand Down Expand Up @@ -215,15 +224,18 @@ export function register(context: LanguageServiceRuntimeContext) {
plugin,
list: {
...completionList,
items: completionList.items.map(item => ({
...item,
data: <PluginCompletionData>{
items: completionList.items.map(item => {
const data: PluginCompletionData = {
uri,
originalItem: item,
pluginId: plugin.id,
sourceMap: undefined,
} as any,
}))
};
return {
...item,
data: data as any,
};
})
},
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function register(context: LanguageServiceRuntimeContext) {
&& edit.range.end.line === 0
&& edit.range.end.character === 0
) {
edit.newText = (context.vueLsHost.getNewLine?.() ?? '\n') + edit.newText;
edit.newText = '\n' + edit.newText;
}
}
}
Expand Down
26 changes: 11 additions & 15 deletions packages/vue-language-service/src/languageService.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import * as shared from '@volar/shared';
import * as ts2 from '@volar/typescript-language-service';
import { EmbeddedLanguagePlugin } from '@volar/vue-language-service-types';
import { createTypeScriptRuntime } from '@volar/vue-typescript';
import { isGloballyWhitelisted } from '@vue/shared';
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as upath from 'upath';
import type * as html from 'vscode-html-languageservice';
import * as json from 'vscode-json-languageservice';
import * as vscode from 'vscode-languageserver-protocol';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { createBasicRuntime } from './basicRuntime';
import useCssPlugin, { triggerCharacters as cssTriggerCharacters } from './commonPlugins/css';
import useDirectiveCommentPlugin, { triggerCharacters as directiveCommentTriggerCharacters } from './commonPlugins/directiveComment';
import useEmmetPlugin, { triggerCharacters as emmetTriggerCharacters } from './commonPlugins/emmet';
import useHtmlPlugin, { triggerCharacters as htmlTriggerCharacters } from './commonPlugins/html';
import useJsDocPlugin, { triggerCharacters as jsDocTriggerCharacters } from './commonPlugins/jsDoc';
import useJsonPlugin, { triggerCharacters as jsonTriggerCharacters } from './commonPlugins/json';
import usePugPlugin, { triggerCharacters as pugTriggerCharacters, createPugDocuments } from './commonPlugins/pug';
import usePugPlugin, { createPugDocuments, triggerCharacters as pugTriggerCharacters } from './commonPlugins/pug';
import useTsPlugin, { getSemanticTokenLegend as getTsSemanticTokenLegend, getTriggerCharacters as getTsTriggerCharacters } from './commonPlugins/typescript';
// import * as d3 from './ideFeatures/d3';
import * as tagNameCase from './ideFeatures/tagNameCase';
import * as autoInsert from './languageFuatures/autoInsert';
import * as callHierarchy from './languageFuatures/callHierarchy';
Expand All @@ -34,8 +40,9 @@ import * as renamePrepare from './languageFuatures/renamePrepare';
import * as signatureHelp from './languageFuatures/signatureHelp';
import * as diagnostics from './languageFuatures/validation';
import * as workspaceSymbol from './languageFuatures/workspaceSymbols';
import { getTsSettings } from './tsConfigs';
import { LanguageServiceHost, LanguageServiceRuntimeContext } from './types';
import { EmbeddedLanguagePlugin } from '@volar/vue-language-service-types';
import { parseVueDocuments } from './vueDocuments';
import useAutoDotValuePlugin from './vuePlugins/autoCompleteRefs';
import useHtmlPugConversionsPlugin from './vuePlugins/htmlPugConversions';
import useReferencesCodeLensPlugin from './vuePlugins/referencesCodeLens';
Expand All @@ -44,16 +51,7 @@ import useScriptSetupConversionsPlugin from './vuePlugins/scriptSetupConversions
import useTagNameCasingConversionsPlugin from './vuePlugins/tagNameCasingConversions';
import useVuePlugin, { triggerCharacters as vueTriggerCharacters } from './vuePlugins/vue';
import useVueTemplateLanguagePlugin, { semanticTokenTypes as vueTemplateSemanticTokenTypes, triggerCharacters as vueTemplateLanguageTriggerCharacters } from './vuePlugins/vueTemplateLanguage';
import * as json from 'vscode-json-languageservice';
import { TextDocument } from 'vscode-languageserver-textdocument';
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as upath from 'upath';

import type * as html from 'vscode-html-languageservice';
import type * as _1 from 'vscode-css-languageservice';
import { getTsSettings } from './tsConfigs';
import { createBasicRuntime } from './basicRuntime';
import { parseVueDocuments } from './vueDocuments';
// import * as d3 from './ideFeatures/d3';

export interface LanguageService extends ReturnType<typeof createLanguageService> { }

Expand Down Expand Up @@ -296,10 +294,8 @@ export function createLanguageService(
}

const context: LanguageServiceRuntimeContext = {
vueLsHost: vueLsHost,
vueDocuments,
getTsLs: lsType => lsType === 'template' ? templateTsLs! : scriptTsLs,
typescript: ts,
getTextDocument,
getPlugins: lsType => {
let plugins = [
Expand Down
Loading

0 comments on commit 60b2d08

Please sign in to comment.