From 1ac357497d03c20b3672667774e09abb4cbe21c0 Mon Sep 17 00:00:00 2001 From: chemzqm Date: Thu, 1 Nov 2018 02:18:04 +0800 Subject: [PATCH] add commitCharacters to completionItems --- src/server/features/completionItemProvider.ts | 20 +++++--- src/server/utils/completionItem.ts | 51 ++++++++++++++++++- 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/src/server/features/completionItemProvider.ts b/src/server/features/completionItemProvider.ts index 0f983d2..aff75c4 100644 --- a/src/server/features/completionItemProvider.ts +++ b/src/server/features/completionItemProvider.ts @@ -114,15 +114,20 @@ export default class TypeScriptCompletionItemProvider implements CompletionItemP triggerCharacter: this.getTsTriggerCharacter(context) } - let msg: Proto.CompletionEntry[] | undefined - try { + let msg: ReadonlyArray | undefined + + let isNewIdentifierLocation = true + if (this.client.apiVersion.gte(API.v300)) { + const response = await this.client.execute('completionInfo', args, token) + if (response.type !== 'response' || !response.body) { + return null + } + isNewIdentifierLocation = response.body.isNewIdentifierLocation + msg = response.body.entries + } else { const response = await this.client.execute('completions', args, token) msg = response.body - if (!msg) { - return [] - } - } catch { - return [] + if (!msg) return [] } const completionItems: CompletionItem[] = [] @@ -135,6 +140,7 @@ export default class TypeScriptCompletionItemProvider implements CompletionItemP uri, position, completeOption.completeFunctionCalls, + isNewIdentifierLocation ) completionItems.push(item) } diff --git a/src/server/utils/completionItem.ts b/src/server/utils/completionItem.ts index 803275a..6c1cfde 100644 --- a/src/server/utils/completionItem.ts +++ b/src/server/utils/completionItem.ts @@ -7,6 +7,12 @@ import { Document, workspace } from 'coc.nvim' import * as Proto from '../protocol' import * as PConst from '../protocol.const' +interface CommitCharactersSettings { + readonly isNewIdentifierLocation: boolean + readonly isInValidCommitCharacterContext: boolean + readonly useCodeSnippetsOnMethodSuggest: boolean +} + export function resolveItem( item: CompletionItem, document: Document, @@ -56,7 +62,8 @@ export function convertCompletionEntry( tsEntry: Proto.CompletionEntry, uri: string, position: Position, - useCodeSnippetsOnMethodSuggest: boolean + useCodeSnippetsOnMethodSuggest: boolean, + isNewIdentifierLocation: boolean ): CompletionItem { let label = tsEntry.name let sortText = tsEntry.sortText @@ -80,14 +87,18 @@ export function convertCompletionEntry( let textEdit: TextEdit = null let insertText = tsEntry.insertText + let document = workspace.getDocument(uri) if (insertText) { - let document = workspace.getDocument(uri) textEdit = { range: document.getWordRangeAtPosition(position), newText: insertText } insertText = null } + let preText = document.getline(position.line).slice(0, position.character) + const isInValidCommitCharacterContext = preText.match(/(^|[a-z_$\(\)\[\]\{\}]|[^.]\.)\s*$/ig) !== null + + let commitCharacters = getCommitCharacters(tsEntry, { isNewIdentifierLocation, isInValidCommitCharacterContext, useCodeSnippetsOnMethodSuggest }) let optional = tsEntry.kindModifiers && tsEntry.kindModifiers.match(/\boptional\b/) return { label, @@ -96,6 +107,7 @@ export function convertCompletionEntry( textEdit, insertTextFormat, sortText, + commitCharacters, data: { uri, optional, @@ -147,3 +159,38 @@ function convertKind(kind: string): CompletionItemKind { } return CompletionItemKind.Variable } + +function getCommitCharacters(tsEntry: Proto.CompletionEntry, settings: CommitCharactersSettings): string[] | undefined { + if (settings.isNewIdentifierLocation || !settings.isInValidCommitCharacterContext) { + return undefined + } + const commitCharacters: string[] = [] + switch (tsEntry.kind) { + case PConst.Kind.memberGetAccessor: + case PConst.Kind.memberSetAccessor: + case PConst.Kind.constructSignature: + case PConst.Kind.callSignature: + case PConst.Kind.indexSignature: + case PConst.Kind.enum: + case PConst.Kind.interface: + commitCharacters.push('.', ';') + break + case PConst.Kind.module: + case PConst.Kind.alias: + case PConst.Kind.const: + case PConst.Kind.let: + case PConst.Kind.variable: + case PConst.Kind.localVariable: + case PConst.Kind.memberVariable: + case PConst.Kind.class: + case PConst.Kind.function: + case PConst.Kind.memberFunction: + case PConst.Kind.keyword: + commitCharacters.push('.', ',', ';') + if (settings.useCodeSnippetsOnMethodSuggest) { + commitCharacters.push('(') + } + break + } + return commitCharacters.length === 0 ? undefined : commitCharacters +}