add commitCharacters to completionItems
This commit is contained in:
parent
c5c1630d85
commit
1ac357497d
2 changed files with 62 additions and 9 deletions
|
@ -114,15 +114,20 @@ export default class TypeScriptCompletionItemProvider implements CompletionItemP
|
||||||
triggerCharacter: this.getTsTriggerCharacter(context)
|
triggerCharacter: this.getTsTriggerCharacter(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
let msg: Proto.CompletionEntry[] | undefined
|
let msg: ReadonlyArray<Proto.CompletionEntry> | undefined
|
||||||
try {
|
|
||||||
|
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)
|
const response = await this.client.execute('completions', args, token)
|
||||||
msg = response.body
|
msg = response.body
|
||||||
if (!msg) {
|
if (!msg) return []
|
||||||
return []
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
return []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const completionItems: CompletionItem[] = []
|
const completionItems: CompletionItem[] = []
|
||||||
|
@ -135,6 +140,7 @@ export default class TypeScriptCompletionItemProvider implements CompletionItemP
|
||||||
uri,
|
uri,
|
||||||
position,
|
position,
|
||||||
completeOption.completeFunctionCalls,
|
completeOption.completeFunctionCalls,
|
||||||
|
isNewIdentifierLocation
|
||||||
)
|
)
|
||||||
completionItems.push(item)
|
completionItems.push(item)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,12 @@ import { Document, workspace } from 'coc.nvim'
|
||||||
import * as Proto from '../protocol'
|
import * as Proto from '../protocol'
|
||||||
import * as PConst from '../protocol.const'
|
import * as PConst from '../protocol.const'
|
||||||
|
|
||||||
|
interface CommitCharactersSettings {
|
||||||
|
readonly isNewIdentifierLocation: boolean
|
||||||
|
readonly isInValidCommitCharacterContext: boolean
|
||||||
|
readonly useCodeSnippetsOnMethodSuggest: boolean
|
||||||
|
}
|
||||||
|
|
||||||
export function resolveItem(
|
export function resolveItem(
|
||||||
item: CompletionItem,
|
item: CompletionItem,
|
||||||
document: Document,
|
document: Document,
|
||||||
|
@ -56,7 +62,8 @@ export function convertCompletionEntry(
|
||||||
tsEntry: Proto.CompletionEntry,
|
tsEntry: Proto.CompletionEntry,
|
||||||
uri: string,
|
uri: string,
|
||||||
position: Position,
|
position: Position,
|
||||||
useCodeSnippetsOnMethodSuggest: boolean
|
useCodeSnippetsOnMethodSuggest: boolean,
|
||||||
|
isNewIdentifierLocation: boolean
|
||||||
): CompletionItem {
|
): CompletionItem {
|
||||||
let label = tsEntry.name
|
let label = tsEntry.name
|
||||||
let sortText = tsEntry.sortText
|
let sortText = tsEntry.sortText
|
||||||
|
@ -80,14 +87,18 @@ export function convertCompletionEntry(
|
||||||
|
|
||||||
let textEdit: TextEdit = null
|
let textEdit: TextEdit = null
|
||||||
let insertText = tsEntry.insertText
|
let insertText = tsEntry.insertText
|
||||||
|
let document = workspace.getDocument(uri)
|
||||||
if (insertText) {
|
if (insertText) {
|
||||||
let document = workspace.getDocument(uri)
|
|
||||||
textEdit = {
|
textEdit = {
|
||||||
range: document.getWordRangeAtPosition(position),
|
range: document.getWordRangeAtPosition(position),
|
||||||
newText: insertText
|
newText: insertText
|
||||||
}
|
}
|
||||||
insertText = null
|
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/)
|
let optional = tsEntry.kindModifiers && tsEntry.kindModifiers.match(/\boptional\b/)
|
||||||
return {
|
return {
|
||||||
label,
|
label,
|
||||||
|
@ -96,6 +107,7 @@ export function convertCompletionEntry(
|
||||||
textEdit,
|
textEdit,
|
||||||
insertTextFormat,
|
insertTextFormat,
|
||||||
sortText,
|
sortText,
|
||||||
|
commitCharacters,
|
||||||
data: {
|
data: {
|
||||||
uri,
|
uri,
|
||||||
optional,
|
optional,
|
||||||
|
@ -147,3 +159,38 @@ function convertKind(kind: string): CompletionItemKind {
|
||||||
}
|
}
|
||||||
return CompletionItemKind.Variable
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue