-
-
Notifications
You must be signed in to change notification settings - Fork 952
/
complete.ts
87 lines (81 loc) · 2.92 KB
/
complete.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { CompletionItem, CompletionItemKind, InsertTextFormat, Position } from 'vscode-languageserver-types'
import { SnippetParser } from '../snippets/parser'
import { CompleteOption } from '../types'
import { byteSlice, characterIndex } from './string'
const logger = require('./logger')('util-complete')
export function getPosition(opt: CompleteOption): Position {
let { line, linenr, colnr } = opt
let part = byteSlice(line, 0, colnr - 1)
return {
line: linenr - 1,
character: part.length
}
}
export function getWord(item: CompletionItem, opt: CompleteOption, invalidInsertCharacters: string[]): string {
// tslint:disable-next-line: deprecation
let { label, data, insertTextFormat, insertText, textEdit } = item
let word: string
let newText: string
if (data && data.word) return data.word
if (textEdit) {
let { range } = textEdit
newText = textEdit.newText
if (range && range.start.line == range.end.line) {
let { line, col, colnr } = opt
let character = characterIndex(line, col)
if (range.start.character > character) {
let before = line.slice(character - range.start.character)
newText = before + newText
} else {
let start = line.slice(range.start.character, character)
if (start.length && newText.startsWith(start)) {
newText = newText.slice(start.length)
}
}
character = characterIndex(line, colnr - 1)
if (range.end.character > character) {
let end = line.slice(character, range.end.character)
if (newText.endsWith(end)) {
newText = newText.slice(0, - end.length)
}
}
}
} else {
newText = insertText
}
if (insertTextFormat == InsertTextFormat.Snippet
&& newText
&& newText.indexOf('$') !== -1) {
let parser = new SnippetParser()
let snippet = parser.text(newText)
word = snippet ? getValidWord(snippet, invalidInsertCharacters) : label
} else {
word = getValidWord(newText, invalidInsertCharacters) || label
}
return word
}
export function getDocumentation(item: CompletionItem): string {
let { documentation } = item
if (!documentation) return ''
if (typeof documentation === 'string') return documentation
return documentation.value
}
export function completionKindString(kind: CompletionItemKind, map: Map<CompletionItemKind, string>, defaultValue = ''): string {
return map.get(kind) || defaultValue
}
export function getSnippetDocumentation(languageId: string, body: string): string {
languageId = languageId.replace(/react$/, '')
let str = body.replace(/\$\d+/g, '').replace(/\$\{\d+(?::([^{]+))?\}/, '$1')
str = '``` ' + languageId + '\n' + str + '\n' + '```'
return str
}
export function getValidWord(text: string, invalidChars: string[]): string {
if (!text) return ''
for (let i = 0; i < text.length; i++) {
let c = text[i]
if (invalidChars.indexOf(c) !== -1) {
return text.slice(0, i)
}
}
return text
}