Skip to content

Commit

Permalink
fix: fix invalid linked editing ranges sometimes
Browse files Browse the repository at this point in the history
feat: Add custom code action to fix closing/opening tag name from the current location
  • Loading branch information
zardoy committed Apr 21, 2024
1 parent 2b084c7 commit 8cc3788
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
25 changes: 25 additions & 0 deletions typescript/src/codeActions/custom/fixClosingTagName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { matchParents } from '../../utils'
import { CodeAction } from '../getCodeActions'

export default {
id: 'fixOppositeTagName',
kind: 'quickfix',
name: 'Fix opposite tag name',
tryToApply(sourceFile, position, range, node) {
const elem = matchParents(node, ['Identifier', 'JsxOpeningElement']) ?? matchParents(node, ['Identifier', 'JsxClosingElement'])
if (!elem) return
const tagNamesDiffers = elem.parent.openingElement.tagName.getText() !== elem.parent.closingElement.tagName.getText()
if (tagNamesDiffers) {
const isCurrentlyAtOpening = elem.parent.openingElement === elem
const oppositeElem = isCurrentlyAtOpening ? elem.parent.closingElement.tagName : elem.parent.openingElement.tagName
return [
{
start: oppositeElem.getStart(),
length: oppositeElem.getWidth(),
newText: elem.tagName.getText(),
},
]
}
return
},
} satisfies CodeAction
13 changes: 0 additions & 13 deletions typescript/src/codeActions/extended/declareMissingProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,3 @@ export default {
return
},
} as ExtendedCodeAction

const testCode = () => {
const tester = (code: string) => {
// ^ - problem location in which quickfix needs to be tested (applied)
// | - cursor position after quickfix is applied
// [[...]] - applied part of the code
/* TODO */
}

tester(/* ts */ `
const b = ({ b, ^a }: { b[[, a/*|*/]] }) => {}
`)
}
2 changes: 2 additions & 0 deletions typescript/src/codeActions/getCodeActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import declareMissingProperties from './extended/declareMissingProperties'
import { renameParameterToNameFromType, renameAllParametersToNameFromType } from './custom/renameParameterToNameFromType'
import addDestructure_1 from './custom/addDestructure/addDestructure'
import fromDestructure_1 from './custom/fromDestructure/fromDestructure'
import fixClosingTagName from './custom/fixClosingTagName'

const codeActions: CodeAction[] = [
addDestructure_1,
Expand All @@ -19,6 +20,7 @@ const codeActions: CodeAction[] = [
splitDeclarationAndInitialization,
renameParameterToNameFromType,
renameAllParametersToNameFromType,
fixClosingTagName,
]
const extendedCodeActions: ExtendedCodeAction[] = [declareMissingProperties]

Expand Down
11 changes: 9 additions & 2 deletions typescript/src/decorateLinkedEditing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
lastLinkedEditingRangeRequest.fileName === fileName
) {
lastLinkedEditingRangeRequest.pos = position
lastLinkedEditingRangeRequest.result.ranges[0]!.length++
const startRange = lastLinkedEditingRangeRequest.result.ranges[0]!
const endRange = lastLinkedEditingRangeRequest.result.ranges[1]!
startRange.length++
lastLinkedEditingRangeRequest.result.ranges[1]!.start++

lastLinkedEditingRangeRequest.result.ranges[1]!.length++
return lastLinkedEditingRangeRequest.result
const leadingText = fileContent.slice(startRange.start, startRange.start + startRange.length)
const endingText = fileContent.slice(endRange.start, endRange.start + endRange.length)
if (leadingText === endingText) {
return lastLinkedEditingRangeRequest.result
}
}
lastLinkedEditingRangeRequest = undefined

Expand Down

0 comments on commit 8cc3788

Please sign in to comment.