From 1766c21de68d0c7af3a3365aab37d13df26188b2 Mon Sep 17 00:00:00 2001 From: ktsn Date: Wed, 2 Oct 2019 00:45:31 +0800 Subject: [PATCH 01/19] Move ts to language server kind utils --- server/src/modes/script/javascript.ts | 70 +--------------- server/src/services/typescriptService/util.ts | 79 +++++++++++++++++++ 2 files changed, 82 insertions(+), 67 deletions(-) diff --git a/server/src/modes/script/javascript.ts b/server/src/modes/script/javascript.ts index 48b51709f3..864c1f8a1e 100644 --- a/server/src/modes/script/javascript.ts +++ b/server/src/modes/script/javascript.ts @@ -1,7 +1,6 @@ import { LanguageModelCache, getLanguageModelCache } from '../../embeddedSupport/languageModelCache'; import { SymbolInformation, - SymbolKind, CompletionItem, Location, SignatureHelp, @@ -41,6 +40,7 @@ import { getComponentInfo } from './componentInfo'; import { DependencyService, T_TypeScript, State } from '../../services/dependencyService'; import { RefactorAction } from '../../types'; import { IServiceHost } from '../../services/typescriptService/serviceHost'; +import { toCompletionItemKind, toSymbolKind } from '../../services/typescriptService/util'; // Todo: After upgrading to LS server 4.0, use CompletionContext for filtering trigger chars // https://microsoft.github.io/language-server-protocol/specification#completion-request-leftwards_arrow_with_hook @@ -163,7 +163,7 @@ export async function getJavascriptMode( label, detail, sortText: entry.sortText + index, - kind: convertKind(entry.kind), + kind: toCompletionItemKind(entry.kind), textEdit: range && TextEdit.replace(range, entry.name), data: { // data used for resolving item details (see 'doResolve') @@ -339,7 +339,7 @@ export async function getJavascriptMode( if (item.kind !== 'script' && !existing[sig]) { const symbol: SymbolInformation = { name: item.text, - kind: convertSymbolKind(item.kind), + kind: toSymbolKind(item.kind), location: { uri: doc.uri, range: convertRange(scriptDoc, item.spans[0]) @@ -648,70 +648,6 @@ function convertRange(document: TextDocument, span: ts.TextSpan): Range { return Range.create(startPosition, endPosition); } -function convertKind(kind: ts.ScriptElementKind): CompletionItemKind { - switch (kind) { - case 'primitive type': - case 'keyword': - return CompletionItemKind.Keyword; - case 'var': - case 'local var': - return CompletionItemKind.Variable; - case 'property': - case 'getter': - case 'setter': - return CompletionItemKind.Field; - case 'function': - case 'method': - case 'construct': - case 'call': - case 'index': - return CompletionItemKind.Function; - case 'enum': - return CompletionItemKind.Enum; - case 'module': - return CompletionItemKind.Module; - case 'class': - return CompletionItemKind.Class; - case 'interface': - return CompletionItemKind.Interface; - case 'warning': - return CompletionItemKind.File; - case 'script': - return CompletionItemKind.File; - case 'directory': - return CompletionItemKind.Folder; - } - - return CompletionItemKind.Property; -} - -function convertSymbolKind(kind: ts.ScriptElementKind): SymbolKind { - switch (kind) { - case 'var': - case 'local var': - case 'const': - return SymbolKind.Variable; - case 'function': - case 'local function': - return SymbolKind.Function; - case 'enum': - return SymbolKind.Enum; - case 'module': - return SymbolKind.Module; - case 'class': - return SymbolKind.Class; - case 'interface': - return SymbolKind.Interface; - case 'method': - return SymbolKind.Method; - case 'property': - case 'getter': - case 'setter': - return SymbolKind.Property; - } - return SymbolKind.Variable; -} - function convertOptions( formatSettings: ts.FormatCodeSettings, options: FormattingOptions, diff --git a/server/src/services/typescriptService/util.ts b/server/src/services/typescriptService/util.ts index d57fec3419..5d3631abc5 100644 --- a/server/src/services/typescriptService/util.ts +++ b/server/src/services/typescriptService/util.ts @@ -1,3 +1,6 @@ +import * as ts from 'typescript'; +import { CompletionItemKind, SymbolKind } from 'vscode-languageserver'; + export function isVueFile(path: string) { return path.endsWith('.vue'); } @@ -17,3 +20,79 @@ export function isVirtualVueFile(path: string) { export function isVirtualVueTemplateFile(path: string) { return path.endsWith('.vue.template'); } + +export function findNodeByOffset(root: ts.Node, offset: number): ts.Node | undefined { + if (offset < root.getStart() || root.getEnd() < offset) { + return undefined; + } + + const childMatch = root.getChildren().reduce((matched, child) => { + return matched || findNodeByOffset(child, offset); + }, undefined); + + return childMatch ? childMatch : root; +} + +export function toCompletionItemKind(kind: ts.ScriptElementKind): CompletionItemKind { + switch (kind) { + case 'primitive type': + case 'keyword': + return CompletionItemKind.Keyword; + case 'var': + case 'local var': + return CompletionItemKind.Variable; + case 'property': + case 'getter': + case 'setter': + return CompletionItemKind.Field; + case 'function': + case 'method': + case 'construct': + case 'call': + case 'index': + return CompletionItemKind.Function; + case 'enum': + return CompletionItemKind.Enum; + case 'module': + return CompletionItemKind.Module; + case 'class': + return CompletionItemKind.Class; + case 'interface': + return CompletionItemKind.Interface; + case 'warning': + return CompletionItemKind.File; + case 'script': + return CompletionItemKind.File; + case 'directory': + return CompletionItemKind.Folder; + } + + return CompletionItemKind.Property; +} + +export function toSymbolKind(kind: ts.ScriptElementKind): SymbolKind { + switch (kind) { + case 'var': + case 'local var': + case 'const': + return SymbolKind.Variable; + case 'function': + case 'local function': + return SymbolKind.Function; + case 'enum': + return SymbolKind.Enum; + case 'module': + return SymbolKind.Module; + case 'class': + return SymbolKind.Class; + case 'interface': + return SymbolKind.Interface; + case 'method': + return SymbolKind.Method; + case 'property': + case 'getter': + case 'setter': + return SymbolKind.Property; + } + return SymbolKind.Variable; +} From 2255bc51103a7bcbdf2da94896ade00e9c046adf Mon Sep 17 00:00:00 2001 From: ktsn Date: Wed, 2 Oct 2019 00:46:28 +0800 Subject: [PATCH 02/19] Fix invalid assertion in test case --- test/interpolation/completion/basic.test.ts | 2 ++ test/interpolation/completion/helper.ts | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/test/interpolation/completion/basic.test.ts b/test/interpolation/completion/basic.test.ts index 2769a4fc62..a8398c8435 100644 --- a/test/interpolation/completion/basic.test.ts +++ b/test/interpolation/completion/basic.test.ts @@ -63,6 +63,7 @@ describe('Should autocomplete interpolation for