add commitCharacters to completionItems

This commit is contained in:
chemzqm 2018-11-01 02:18:04 +08:00
parent c5c1630d85
commit 1ac357497d
2 changed files with 62 additions and 9 deletions

View file

@ -114,15 +114,20 @@ export default class TypeScriptCompletionItemProvider implements CompletionItemP
triggerCharacter: this.getTsTriggerCharacter(context)
}
let msg: Proto.CompletionEntry[] | undefined
try {
let msg: ReadonlyArray<Proto.CompletionEntry> | 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)
}

View file

@ -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
}