Skip to content

Commit 23cf01a

Browse files
committed
fix: fix jsx attribute shortcut replace range
fixes #195
1 parent 5e20047 commit 23cf01a

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

typescript/src/completions/jsxAttributes.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default (
1111
sourceFile: ts.SourceFile,
1212
jsxCompletionsMap: Configuration['jsxCompletionsMap'],
1313
): ts.CompletionEntry[] => {
14+
const originalNode = node
1415
// ++ patch with jsxCompletionsMap
1516
// -- don't
1617
// <div| - identifier, not attribute --
@@ -78,13 +79,24 @@ export default (
7879
const enableJsxAttributesShortcuts = sharedCompletionContext.c('jsxAttributeShortcutCompletions.enable')
7980
if (enableJsxAttributesShortcuts !== 'disable') {
8081
const locals = collectLocalSymbols(node, sharedCompletionContext.typeChecker)
82+
let attrib = originalNode.parent as ts.JsxAttribute | undefined
83+
if (!ts.isJsxAttribute(attrib!)) {
84+
attrib = undefined
85+
}
8186
entries = entries.flatMap(entry => {
8287
if (locals.includes(entry.name)) {
8388
const insertText = `${entry.name}={${entry.name}}`
84-
const additionalSuggestions = {
89+
const pos = attrib ? attrib.end - attrib.getWidth() : 0
90+
const additionalSuggestions: ts.CompletionEntry = {
8591
...entry,
8692
name: insertText,
8793
insertText,
94+
replacementSpan: attrib
95+
? {
96+
start: pos,
97+
length: attrib.end - pos,
98+
}
99+
: undefined,
88100
}
89101
return enableJsxAttributesShortcuts === 'after' ? [entry, additionalSuggestions] : [additionalSuggestions, entry]
90102
}

0 commit comments

Comments
 (0)