Skip to content

Commit a51ec55

Browse files
committed
jsxCompletionsMap fixes
1 parent b34233c commit a51ec55

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

typescript/src/completions/jsxAttributes.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@ export default (
1111
sourceFile: tslib.SourceFile,
1212
jsxCompletionsMap: Configuration['jsxCompletionsMap'],
1313
): tslib.CompletionEntry[] => {
14-
// TODO refactor to findNodeAtPosition
14+
// ++ patch with jsxCompletionsMap
15+
// -- don't
16+
// <div| - identifier, not attribute --
17+
// <div | - not identifier, not attribute ++
18+
// <div t| - identifier -> attribute ++
19+
// <div a={} t={} - in attributes ++
20+
// <div t={|} - isn't attribute, so doesn't matter --
21+
let jsxAttributeCandidate = !ts.isIdentifier(node)
1522
if (ts.isIdentifier(node)) node = node.parent
23+
// fix builtin jsx attribute completion
1624
if (ts.isJsxAttribute(node) && node.initializer) {
1725
entries = entries.map(entry => {
1826
return {
@@ -21,9 +29,15 @@ export default (
2129
}
2230
})
2331
}
24-
if (ts.isJsxAttribute(node)) node = node.parent
25-
if (ts.isJsxAttributes(node)) node = node.parent
26-
if (Object.keys(jsxCompletionsMap).length && (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node))) {
32+
if (ts.isJsxAttribute(node)) {
33+
jsxAttributeCandidate = true
34+
node = node.parent
35+
}
36+
if (ts.isJsxAttributes(node)) {
37+
jsxAttributeCandidate = true
38+
node = node.parent
39+
}
40+
if (jsxAttributeCandidate && Object.keys(jsxCompletionsMap).length && (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node))) {
2741
const tagName = node.tagName.getText()
2842
// TODO use the same perf optimization for replaceSuggestions
2943
const patchEntries: Record<number, Configuration['jsxCompletionsMap'][string]> = {}
@@ -34,7 +48,7 @@ export default (
3448
if (comparingTagName && comparingTagName !== tagName) continue
3549
const comparingName = key.slice(splitTagNameIdx + 1)
3650
if (comparingName.includes('*')) {
37-
const regexMatch = new RegExp(escapeStringRegexp(comparingName).replaceAll('\\*', '.*'))
51+
const regexMatch = new RegExp(`^${escapeStringRegexp(comparingName).replaceAll('\\*', '.*')}$`)
3852
entries.forEach(({ name, kind }, index) => {
3953
if (kind === ts.ScriptElementKind.memberVariableElement && regexMatch.test(name)) {
4054
patchEntries[index] = patchMethod

typescript/src/completionsAtPosition.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,14 @@ export const getCompletionsAtPosition = (
3232
if (!program || !sourceFile) return
3333
if (!scriptSnapshot || isInBannedPosition(position, scriptSnapshot, sourceFile)) return
3434
let prior = languageService.getCompletionsAtPosition(fileName, position, options)
35-
// console.log(
36-
// 'raw prior',
37-
// prior?.entries.map(entry => entry.name),
38-
// )
3935
const ensurePrior = () => {
4036
if (!prior) prior = { entries: [], isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false }
4137
return true
4238
}
4339
const node = findChildContainingPosition(ts, sourceFile, position)
40+
const leftNode = findChildContainingPosition(ts, sourceFile, position - 1)
4441
if (['.jsx', '.tsx'].some(ext => fileName.endsWith(ext))) {
45-
// JSX Features
42+
// #region JSX tag improvements
4643
if (node) {
4744
const { SyntaxKind } = ts
4845
const emmetSyntaxKinds = [SyntaxKind.JsxFragment, SyntaxKind.JsxElement, SyntaxKind.JsxText]
@@ -61,6 +58,7 @@ export const getCompletionsAtPosition = (
6158
entry => isStartingWithUpperCase(entry.name) && ![ts.ScriptElementKind.enumElement].includes(entry.kind),
6259
)
6360
}
61+
// #endregion
6462

6563
if (
6664
c('jsxEmmet.type') !== 'disabled' &&
@@ -210,7 +208,7 @@ export const getCompletionsAtPosition = (
210208
})
211209
}
212210

213-
if (c('improveJsxCompletions') && node) prior.entries = improveJsxCompletions(ts, prior.entries, node, position, sourceFile, c('jsxCompletionsMap'))
211+
if (c('improveJsxCompletions') && leftNode) prior.entries = improveJsxCompletions(ts, prior.entries, leftNode, position, sourceFile, c('jsxCompletionsMap'))
214212

215213
for (const rule of c('replaceSuggestions')) {
216214
let foundIndex: number
@@ -240,7 +238,7 @@ export const getCompletionsAtPosition = (
240238

241239
if (c('correctSorting.enable')) prior.entries = prior.entries.map((entry, index) => ({ ...entry, sortText: `${entry.sortText ?? ''}${index}` }))
242240

243-
console.log('signatureHelp', JSON.stringify(languageService.getSignatureHelpItems(fileName, position, {})))
241+
// console.log('signatureHelp', JSON.stringify(languageService.getSignatureHelpItems(fileName, position, {})))
244242
return {
245243
completions: prior,
246244
prevCompletionsMap,

0 commit comments

Comments
 (0)