Skip to content

Commit

Permalink
fix: fix jsx attribute shortcut replace range
Browse files Browse the repository at this point in the history
fixes #195
  • Loading branch information
zardoy committed Apr 21, 2024
1 parent 5e20047 commit 23cf01a
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion typescript/src/completions/jsxAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default (
sourceFile: ts.SourceFile,
jsxCompletionsMap: Configuration['jsxCompletionsMap'],
): ts.CompletionEntry[] => {
const originalNode = node
// ++ patch with jsxCompletionsMap
// -- don't
// <div| - identifier, not attribute --
Expand Down Expand Up @@ -78,13 +79,24 @@ export default (
const enableJsxAttributesShortcuts = sharedCompletionContext.c('jsxAttributeShortcutCompletions.enable')
if (enableJsxAttributesShortcuts !== 'disable') {
const locals = collectLocalSymbols(node, sharedCompletionContext.typeChecker)
let attrib = originalNode.parent as ts.JsxAttribute | undefined
if (!ts.isJsxAttribute(attrib!)) {
attrib = undefined
}
entries = entries.flatMap(entry => {
if (locals.includes(entry.name)) {
const insertText = `${entry.name}={${entry.name}}`
const additionalSuggestions = {
const pos = attrib ? attrib.end - attrib.getWidth() : 0
const additionalSuggestions: ts.CompletionEntry = {
...entry,
name: insertText,
insertText,
replacementSpan: attrib
? {
start: pos,
length: attrib.end - pos,
}
: undefined,
}
return enableJsxAttributesShortcuts === 'after' ? [entry, additionalSuggestions] : [additionalSuggestions, entry]
}
Expand Down

0 comments on commit 23cf01a

Please sign in to comment.