-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcompletionsAtPosition.ts
More file actions
249 lines (230 loc) · 11.2 KB
/
completionsAtPosition.ts
File metadata and controls
249 lines (230 loc) · 11.2 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import _ from 'lodash'
import type tslib from 'typescript/lib/tsserverlibrary'
import * as emmet from '@vscode/emmet-helper'
import isInBannedPosition from './isInBannedPosition'
import { GetConfig } from './types'
export type PrevCompletionMap = Record<string, { originalName?: string; documentationOverride?: string | tslib.SymbolDisplayPart[] }>
export const getCompletionsAtPosition = (
fileName: string,
position: number,
options: ts.GetCompletionsAtPositionOptions | undefined,
c: GetConfig,
languageService: ts.LanguageService,
scriptSnapshot: ts.IScriptSnapshot,
ts: typeof tslib,
) => {
const prevCompletionsMap: PrevCompletionMap = {}
const program = languageService.getProgram()
const sourceFile = program?.getSourceFile(fileName)
if (!program || !sourceFile) return
const node = findChildContainingPosition(ts, sourceFile, position)
if (!scriptSnapshot || isInBannedPosition(position, fileName, scriptSnapshot, sourceFile, languageService, ts, program, node)) return
let prior = languageService.getCompletionsAtPosition(fileName, position, options)
// console.log(
// 'raw prior',
// prior?.entries.map(entry => entry.name),
// )
if (['.jsx', '.tsx'].some(ext => fileName.endsWith(ext))) {
// JSX Features
if (node) {
const { SyntaxKind } = ts
const emmetSyntaxKinds = [SyntaxKind.JsxFragment, SyntaxKind.JsxElement, SyntaxKind.JsxText]
const emmetClosingSyntaxKinds = [SyntaxKind.JsxClosingElement, SyntaxKind.JsxClosingFragment]
// TODO maybe allow fragment?
const correntComponentSuggestionsKinds = [SyntaxKind.JsxOpeningElement, SyntaxKind.JsxSelfClosingElement]
const nodeText = node.getFullText().slice(0, position - node.pos)
if (correntComponentSuggestionsKinds.includes(node.kind) && c('jsxImproveElementsSuggestions.enabled') && !nodeText.includes(' ') && prior) {
let lastPart = nodeText.split('.').at(-1)!
if (lastPart.startsWith('<')) lastPart = lastPart.slice(1)
const isStartingWithUpperCase = (str: string) => str[0] === str[0]?.toUpperCase()
// check if starts with lowercase
if (isStartingWithUpperCase(lastPart))
// TODO! compare with suggestions from lib.dom
prior.entries = prior.entries.filter(
entry => isStartingWithUpperCase(entry.name) && ![ts.ScriptElementKind.enumElement].includes(entry.kind),
)
}
if (
c('jsxEmmet.type') !== 'disabled' &&
(emmetSyntaxKinds.includes(node.kind) || /* Just before closing tag */ (emmetClosingSyntaxKinds.includes(node.kind) && nodeText.length === 0))
) {
// const { textSpan } = proxy.getSmartSelectionRange(fileName, position)
// let existing = scriptSnapshot.getText(textSpan.start, textSpan.start + textSpan.length)
// if (existing.includes('\n')) existing = ''
if (!prior) prior = { entries: [], isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false }
// if (existing.startsWith('.')) {
// const className = existing.slice(1)
// prior.entries.push({
// kind: typescript.ScriptElementKind.label,
// name: className,
// sortText: '!5',
// insertText: `<div className="${className}">$1</div>`,
// isSnippet: true,
// })
// } else if (!existing[0] || existing[0].match(/\w/)) {
if (c('jsxEmmet.type') === 'realEmmet') {
const sendToEmmet = nodeText.split(' ').at(-1)!
const emmetCompletions = emmet.doComplete(
{
getText: () => sendToEmmet,
languageId: 'html',
lineCount: 1,
offsetAt: position => position.character,
positionAt: offset => ({ line: 0, character: offset }),
uri: '/',
version: 1,
},
{ line: 0, character: sendToEmmet.length },
'html',
{},
) ?? { items: [] }
for (const completion of emmetCompletions.items)
prior.entries.push({
kind: ts.ScriptElementKind.label,
name: completion.label.slice(1),
sortText: '!5',
// insertText: `${completion.label.slice(1)} ${completion.textEdit?.newText}`,
insertText: completion.textEdit?.newText,
isSnippet: true,
sourceDisplay: completion.detail !== undefined ? [{ kind: 'text', text: completion.detail }] : undefined,
// replacementSpan: { start: position - 5, length: 5 },
})
} else {
const tags = c('jsxPseudoEmmet.tags')
for (let [tag, value] of Object.entries(tags)) {
if (value === true) value = `<${tag}>$1</${tag}>`
prior.entries.push({
kind: ts.ScriptElementKind.label,
name: tag,
sortText: '!5',
insertText: value,
isSnippet: true,
})
}
}
}
}
}
if (!prior) return
// const fullText = scriptSnapshot.getText(0, scriptSnapshot.getLength())
// const matchImport = /(import (.*)from )['"].*['"]/.exec(fullText.split('\n')[line]!)?.[1]
// if (matchImport && character <= `import${matchImport}`.length) {
// console.log('override')
// return
// }
// prior.isGlobalCompletion
// prior.entries[0]
const entryNames = new Set(prior.entries.map(({ name }) => name))
if (c('removeUselessFunctionProps.enable')) prior.entries = prior.entries.filter(e => !['Symbol', 'caller', 'prototype'].includes(e.name))
if (['bind', 'call', 'caller'].every(name => entryNames.has(name)) && c('highlightNonFunctionMethods.enable')) {
const standardProps = new Set(['Symbol', 'apply', 'arguments', 'bind', 'call', 'caller', 'length', 'name', 'prototype', 'toString'])
// TODO lift up!
prior.entries = prior.entries.map(entry => {
if (!standardProps.has(entry.name) && entry.kind !== ts.ScriptElementKind.warning) {
const newName = `☆${entry.name}`
prevCompletionsMap[newName] = {
originalName: entry.name,
}
return {
...entry,
insertText: entry.insertText ?? entry.name,
name: newName,
}
}
return entry
})
}
if (c('patchToString.enable')) {
// const indexToPatch = arrayMoveItemToFrom(
// prior.entries,
// ({ name }) => name === 'toExponential',
// ({ name }) => name === 'toString',
// )
const indexToPatch = prior.entries.findIndex(({ name }) => name === 'toString')
if (indexToPatch !== -1) {
prior.entries[indexToPatch]!.insertText = `${prior.entries[indexToPatch]!.insertText ?? prior.entries[indexToPatch]!.name}()`
prior.entries[indexToPatch]!.kind = ts.ScriptElementKind.constElement
// prior.entries[indexToPatch]!.isSnippet = true
}
}
const banAutoImportPackages = c('suggestions.banAutoImportPackages')
if (banAutoImportPackages?.length)
prior.entries = prior.entries.filter(entry => {
if (!entry.sourceDisplay) return true
const text = entry.sourceDisplay.map(item => item.text).join('')
if (text.startsWith('.')) return true
// TODO change to startsWith?
return !banAutoImportPackages.includes(text)
})
if (c('suggestions.keywordsInsertText') === 'space') {
const charAhead = scriptSnapshot.getText(position, position + 1)
const bannedKeywords = [
'true',
'false',
'undefined',
'null',
'never',
'unknown',
'any',
'symbol',
'string',
'number',
'boolean',
'object',
'this',
'catch',
'constructor',
'continue',
'break',
'debugger',
'default',
'super',
]
prior.entries = prior.entries.map(entry => {
if (entry.kind !== ts.ScriptElementKind.keyword || charAhead === ' ' || bannedKeywords.includes(entry.name)) return entry
return { ...entry, insertText: `${entry.name} ` }
})
}
for (const rule of c('replaceSuggestions')) {
let foundIndex: number
const suggestion = prior.entries.find(({ name, kind }, index) => {
if (rule.suggestion !== name) return false
if (rule.filter?.kind && kind !== rule.filter.kind) return false
foundIndex = index
return true
})
if (!suggestion) continue
if (rule.delete) prior.entries.splice(foundIndex!, 1)
if (rule.duplicateOriginal) prior.entries.splice(rule.duplicateOriginal === 'above' ? foundIndex! : foundIndex! + 1, 0, { ...suggestion })
Object.assign(suggestion, rule.patch ?? {})
if (rule.patch?.insertText) suggestion.isSnippet = true
}
if (c('correctSorting.enable')) prior.entries = prior.entries.map((entry, index) => ({ ...entry, sortText: `${entry.sortText ?? ''}${index}` }))
// console.log('signatureHelp', JSON.stringify(info.languageService.getSignatureHelpItems(fileName, position, {})))
return {
completions: prior,
prevCompletionsMap,
}
}
type ArrayPredicate<T> = (value: T, index: number) => boolean
const arrayMoveItemToFrom = <T>(array: T[], originalItem: ArrayPredicate<T>, itemToMove: ArrayPredicate<T>) => {
const originalItemIndex = array.findIndex(originalItem)
if (originalItemIndex === -1) return undefined
const itemToMoveIndex = array.findIndex(itemToMove)
if (itemToMoveIndex === -1) return undefined
array.splice(originalItemIndex, 0, array[itemToMoveIndex]!)
array.splice(itemToMoveIndex + 1, 1)
return originalItemIndex
}
const patchText = (input: string, start: number, end: number, newText: string) => input.slice(0, start) + newText + input.slice(end)
function findChildContainingPosition(
typescript: typeof import('typescript/lib/tsserverlibrary'),
sourceFile: tslib.SourceFile,
position: number,
): tslib.Node | undefined {
function find(node: ts.Node): ts.Node | undefined {
if (position >= node.getStart() && position < node.getEnd()) return typescript.forEachChild(node, find) || node
return
}
return find(sourceFile)
}