Skip to content

Commit 60aa0f4

Browse files
committed
handle proper rename features decoration for future ts
lint
1 parent b471838 commit 60aa0f4

File tree

4 files changed

+64
-25
lines changed

4 files changed

+64
-25
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { RequestOptionsTypes } from './ipcTypes'
2+
import { GetConfig } from './types'
3+
import { findChildContainingExactPosition } from './utils'
4+
5+
export const overrideRenameRequest = {
6+
value: undefined as undefined | RequestOptionsTypes['acceptRenameWithParams'],
7+
}
8+
9+
export default (proxy: ts.LanguageService, languageService: ts.LanguageService, c: GetConfig) => {
10+
proxy.findRenameLocations = (
11+
...args: [
12+
fileName: string,
13+
position: number,
14+
findInStrings: boolean,
15+
findInComments: boolean,
16+
providePrefixAndSuffixTextForRename?: boolean | ts.UserPreferences,
17+
]
18+
) => {
19+
if (overrideRenameRequest.value) {
20+
const { comments, strings, alias } = overrideRenameRequest.value
21+
if (comments !== undefined) {
22+
args[3] = comments
23+
}
24+
if (strings !== undefined) {
25+
args[2] = strings
26+
}
27+
if (alias !== undefined) {
28+
if (typeof args[4] === 'object') {
29+
args[4] = {
30+
...args[4],
31+
providePrefixAndSuffixTextForRename: alias,
32+
}
33+
} else {
34+
args[4] = alias
35+
}
36+
}
37+
38+
overrideRenameRequest.value = undefined
39+
}
40+
41+
//@ts-expect-error
42+
const renameLocations = languageService.findRenameLocations(...args)
43+
if (!renameLocations) return renameLocations
44+
// const firstLocation = renameLocations[0]
45+
// if (firstLocation?.fileName === args[0]) {
46+
// const node = findChildContainingExactPosition(languageService.getProgram()!.getSourceFile(args[0])!, firstLocation.textSpan.start)
47+
// if (
48+
// node &&
49+
// ts.isIdentifier(node) &&
50+
// ts.isArrayBindingPattern(node.parent) &&
51+
// node.parent.elements.length === 2 &&
52+
// ts.isVariableDeclaration(node.parent.parent)
53+
// ) {
54+
// // firstLocation.
55+
// }
56+
// }
57+
return renameLocations
58+
}
59+
}

typescript/src/decorateProxy.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import decorateFormatFeatures from './decorateFormatFeatures'
1818
import libDomPatching from './libDomPatching'
1919
import decorateSignatureHelp from './decorateSignatureHelp'
2020
import { approveCast, findChildContainingExactPosition } from './utils'
21+
import decorateFindRenameLocations from './decorateFindRenameLocations'
2122

2223
/** @internal */
2324
export const thisPluginMarker = '__essentialPluginsMarker__'
@@ -31,10 +32,6 @@ export const getInitialProxy = (languageService: ts.LanguageService, proxy = Obj
3132
return proxy
3233
}
3334

34-
export const overrideRequestPreferences = {
35-
rename: undefined as undefined | RequestOptionsTypes['acceptRenameWithParams'],
36-
}
37-
3835
export const decorateLanguageService = (
3936
{ languageService, languageServiceHost }: ts.server.PluginCreateInfo,
4037
existingProxy: ts.LanguageService | undefined,
@@ -145,23 +142,7 @@ export const decorateLanguageService = (
145142
decorateWorkspaceSymbolSearch(proxy, languageService, c, languageServiceHost)
146143
decorateFormatFeatures(proxy, languageService, languageServiceHost, c)
147144
decorateSignatureHelp(proxy, languageService, languageServiceHost, c)
148-
proxy.findRenameLocations = (fileName, position, findInStrings, findInComments, providePrefixAndSuffixTextForRename) => {
149-
if (overrideRequestPreferences.rename) {
150-
try {
151-
const { comments, strings, alias } = overrideRequestPreferences.rename
152-
return languageService.findRenameLocations(
153-
fileName,
154-
position,
155-
strings ?? findInStrings,
156-
comments ?? findInComments,
157-
alias ?? providePrefixAndSuffixTextForRename,
158-
)
159-
} finally {
160-
overrideRequestPreferences.rename = undefined
161-
}
162-
}
163-
return languageService.findRenameLocations(fileName, position, findInStrings, findInComments, providePrefixAndSuffixTextForRename)
164-
}
145+
decorateFindRenameLocations(proxy, languageService, c)
165146

166147
libDomPatching(languageServiceHost, c)
167148

typescript/src/specialCommands/handle.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { compact } from '@zardoy/utils'
22
import { getExtendedCodeActions } from '../codeActions/getCodeActions'
3-
import constructMethodSnippet from '../constructMethodSnippet'
4-
import { overrideRequestPreferences } from '../decorateProxy'
53
import { NodeAtPositionResponse, RequestOptionsTypes, RequestResponseTypes, TriggerCharacterCommand, triggerCharacterCommands } from '../ipcTypes'
64
import { GetConfig } from '../types'
75
import { findChildContainingExactPosition, findChildContainingPosition, getNodePath } from '../utils'
86
import { lastResolvedCompletion } from '../completionEntryDetails'
7+
import { overrideRenameRequest } from '../decorateFindRenameLocations'
98
import getEmmetCompletions from './emmet'
109
import objectIntoArrayConverters from './objectIntoArrayConverters'
1110

@@ -200,7 +199,7 @@ export default (
200199
}
201200
if (specialCommand === 'acceptRenameWithParams') {
202201
changeType<RequestOptionsTypes['acceptRenameWithParams']>(specialCommandArg)
203-
overrideRequestPreferences.rename = specialCommandArg
202+
overrideRenameRequest.value = specialCommandArg
204203
return undefined
205204
}
206205
if (specialCommand === 'pickAndInsertFunctionArguments') {

typescript/src/volarConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { Configuration } from './types'
66
// will be required from ./node_modules/typescript-essential-plugins/index.js
77
const originalPluginFactory = require('typescript-essential-plugins')
88

9-
const compact = <T>(arr: (T | undefined)[]): T[] => arr.filter(Boolean) as T[]
9+
const compact = <T>(arr: Array<T | undefined>): T[] => arr.filter(Boolean) as T[]
1010

1111
const plugin = ((context, { typescript: tsModule } = {}) => {
1212
if (!context) throw new Error('Not recieve context')

0 commit comments

Comments
 (0)