Skip to content

Commit

Permalink
feat: Speed up a bit JSX linked editing by simply caching previous re…
Browse files Browse the repository at this point in the history
…sponse
  • Loading branch information
zardoy committed Nov 28, 2023
1 parent 9bf21eb commit 5a1a41a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/configurationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,12 @@ export type Configuration = {
* @default false
*/
'experiments.enableInsertNameOfSuggestionFix': boolean
/**
* Speed up JSX linked editing by not using actual tsserver command when possible, which in theory may introduce some inconsistencies.
* Note that currently it doesn't really help if you have `"typescript.tsserver.useSyntaxServer": "auto"` in the settings.
* @default true
*/
'experiments.speedLinkedEditing': boolean
/**
* Map *symbol - array of modules* to change sorting of imports - first available takes precedence in auto import code fixes (+ import all action)
*
Expand Down
37 changes: 37 additions & 0 deletions typescript/src/decorateLinkedEditing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { GetConfig } from './types'

export default (proxy: ts.LanguageService, languageService: ts.LanguageService, languageServiceHost: ts.LanguageServiceHost, c: GetConfig) => {
// patch JSX tag linked editing to improve performance (needed for great user experience)

let lastLinkedEditingRangeRequest:
| {
pos: number
fileName: string
result: ts.LinkedEditingInfo
}
| undefined
proxy.getLinkedEditingRangeAtPosition = (fileName, position) => {
if (
c('experiments.speedLinkedEditing') &&
lastLinkedEditingRangeRequest &&
lastLinkedEditingRangeRequest.pos === position - 1 &&
lastLinkedEditingRangeRequest.fileName === fileName
) {
lastLinkedEditingRangeRequest.pos = position
lastLinkedEditingRangeRequest.result.ranges[0]!.length++
lastLinkedEditingRangeRequest.result.ranges[1]!.start++
lastLinkedEditingRangeRequest.result.ranges[1]!.length++
return lastLinkedEditingRangeRequest.result
}
lastLinkedEditingRangeRequest = undefined

const prior = languageService.getLinkedEditingRangeAtPosition(fileName, position)
if (!prior) return
lastLinkedEditingRangeRequest = {
pos: position,
fileName,
result: globalThis.structuredClone(prior),
}
return prior
}
}
2 changes: 2 additions & 0 deletions typescript/src/decorateProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import decorateSignatureHelp from './decorateSignatureHelp'
import decorateFindRenameLocations from './decorateFindRenameLocations'
import decorateQuickInfoAtPosition from './decorateQuickInfoAtPosition'
import decorateEditsForFileRename from './decorateEditsForFileRename'
import decorateLinkedEditing from './decorateLinkedEditing'

/** @internal */
export const thisPluginMarker = '__essentialPluginsMarker__'
Expand Down Expand Up @@ -102,6 +103,7 @@ export const decorateLanguageService = (
decorateSignatureHelp(proxy, languageService, languageServiceHost, c)
decorateFindRenameLocations(proxy, languageService, c)
decorateQuickInfoAtPosition(proxy, languageService, languageServiceHost, c)
decorateLinkedEditing(proxy, languageService, languageServiceHost, c)

libDomPatching(languageServiceHost, c)

Expand Down

0 comments on commit 5a1a41a

Please sign in to comment.