Skip to content

Commit 8cc3788

Browse files
committed
fix: fix invalid linked editing ranges sometimes
feat: Add custom code action to fix closing/opening tag name from the current location
1 parent 2b084c7 commit 8cc3788

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { matchParents } from '../../utils'
2+
import { CodeAction } from '../getCodeActions'
3+
4+
export default {
5+
id: 'fixOppositeTagName',
6+
kind: 'quickfix',
7+
name: 'Fix opposite tag name',
8+
tryToApply(sourceFile, position, range, node) {
9+
const elem = matchParents(node, ['Identifier', 'JsxOpeningElement']) ?? matchParents(node, ['Identifier', 'JsxClosingElement'])
10+
if (!elem) return
11+
const tagNamesDiffers = elem.parent.openingElement.tagName.getText() !== elem.parent.closingElement.tagName.getText()
12+
if (tagNamesDiffers) {
13+
const isCurrentlyAtOpening = elem.parent.openingElement === elem
14+
const oppositeElem = isCurrentlyAtOpening ? elem.parent.closingElement.tagName : elem.parent.openingElement.tagName
15+
return [
16+
{
17+
start: oppositeElem.getStart(),
18+
length: oppositeElem.getWidth(),
19+
newText: elem.tagName.getText(),
20+
},
21+
]
22+
}
23+
return
24+
},
25+
} satisfies CodeAction

typescript/src/codeActions/extended/declareMissingProperties.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,3 @@ export default {
8383
return
8484
},
8585
} as ExtendedCodeAction
86-
87-
const testCode = () => {
88-
const tester = (code: string) => {
89-
// ^ - problem location in which quickfix needs to be tested (applied)
90-
// | - cursor position after quickfix is applied
91-
// [[...]] - applied part of the code
92-
/* TODO */
93-
}
94-
95-
tester(/* ts */ `
96-
const b = ({ b, ^a }: { b[[, a/*|*/]] }) => {}
97-
`)
98-
}

typescript/src/codeActions/getCodeActions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import declareMissingProperties from './extended/declareMissingProperties'
1010
import { renameParameterToNameFromType, renameAllParametersToNameFromType } from './custom/renameParameterToNameFromType'
1111
import addDestructure_1 from './custom/addDestructure/addDestructure'
1212
import fromDestructure_1 from './custom/fromDestructure/fromDestructure'
13+
import fixClosingTagName from './custom/fixClosingTagName'
1314

1415
const codeActions: CodeAction[] = [
1516
addDestructure_1,
@@ -19,6 +20,7 @@ const codeActions: CodeAction[] = [
1920
splitDeclarationAndInitialization,
2021
renameParameterToNameFromType,
2122
renameAllParametersToNameFromType,
23+
fixClosingTagName,
2224
]
2325
const extendedCodeActions: ExtendedCodeAction[] = [declareMissingProperties]
2426

typescript/src/decorateLinkedEditing.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
2323
lastLinkedEditingRangeRequest.fileName === fileName
2424
) {
2525
lastLinkedEditingRangeRequest.pos = position
26-
lastLinkedEditingRangeRequest.result.ranges[0]!.length++
26+
const startRange = lastLinkedEditingRangeRequest.result.ranges[0]!
27+
const endRange = lastLinkedEditingRangeRequest.result.ranges[1]!
28+
startRange.length++
2729
lastLinkedEditingRangeRequest.result.ranges[1]!.start++
30+
2831
lastLinkedEditingRangeRequest.result.ranges[1]!.length++
29-
return lastLinkedEditingRangeRequest.result
32+
const leadingText = fileContent.slice(startRange.start, startRange.start + startRange.length)
33+
const endingText = fileContent.slice(endRange.start, endRange.start + endRange.length)
34+
if (leadingText === endingText) {
35+
return lastLinkedEditingRangeRequest.result
36+
}
3037
}
3138
lastLinkedEditingRangeRequest = undefined
3239

0 commit comments

Comments
 (0)