diff --git a/buildTsPlugin.mjs b/buildTsPlugin.mjs index 4989d4b1..6f60299c 100644 --- a/buildTsPlugin.mjs +++ b/buildTsPlugin.mjs @@ -2,8 +2,9 @@ import buildTsPlugin from '@zardoy/vscode-utils/build/buildTypescriptPlugin.js' import { build, analyzeMetafile } from 'esbuild' -build({ - // bundle: true, +await build({ + bundle: true, + external: ['typescript-essential-plugins'], // minify: !watch, entryPoints: ['./typescript/src/volarConfig.ts'], outfile: './out/volarConfig.js', diff --git a/integration/index.ts b/integration/index.ts index 418fac41..1f2cd137 100644 --- a/integration/index.ts +++ b/integration/index.ts @@ -6,7 +6,7 @@ export const run = async () => { const mocha = new Mocha({ color: true, parallel: false, - timeout: process.env.CI ? 4500 : 2000, + timeout: process.env.CI ? 5200 : 2000, }) const testsRoot = join(__dirname, './suite') await new Promise(resolve => { diff --git a/typescript/src/decorateFindRenameLocations.ts b/typescript/src/decorateFindRenameLocations.ts new file mode 100644 index 00000000..10071969 --- /dev/null +++ b/typescript/src/decorateFindRenameLocations.ts @@ -0,0 +1,59 @@ +import { RequestOptionsTypes } from './ipcTypes' +import { GetConfig } from './types' +import { findChildContainingExactPosition } from './utils' + +export const overrideRenameRequest = { + value: undefined as undefined | RequestOptionsTypes['acceptRenameWithParams'], +} + +export default (proxy: ts.LanguageService, languageService: ts.LanguageService, c: GetConfig) => { + proxy.findRenameLocations = ( + ...args: [ + fileName: string, + position: number, + findInStrings: boolean, + findInComments: boolean, + providePrefixAndSuffixTextForRename?: boolean | ts.UserPreferences, + ] + ) => { + if (overrideRenameRequest.value) { + const { comments, strings, alias } = overrideRenameRequest.value + if (comments !== undefined) { + args[3] = comments + } + if (strings !== undefined) { + args[2] = strings + } + if (alias !== undefined) { + if (typeof args[4] === 'object') { + args[4] = { + ...args[4], + providePrefixAndSuffixTextForRename: alias, + } + } else { + args[4] = alias + } + } + + overrideRenameRequest.value = undefined + } + + //@ts-expect-error + const renameLocations = languageService.findRenameLocations(...args) + if (!renameLocations) return renameLocations + // const firstLocation = renameLocations[0] + // if (firstLocation?.fileName === args[0]) { + // const node = findChildContainingExactPosition(languageService.getProgram()!.getSourceFile(args[0])!, firstLocation.textSpan.start) + // if ( + // node && + // ts.isIdentifier(node) && + // ts.isArrayBindingPattern(node.parent) && + // node.parent.elements.length === 2 && + // ts.isVariableDeclaration(node.parent.parent) + // ) { + // // firstLocation. + // } + // } + return renameLocations + } +} diff --git a/typescript/src/decorateProxy.ts b/typescript/src/decorateProxy.ts index 704d5d4a..3f83b747 100644 --- a/typescript/src/decorateProxy.ts +++ b/typescript/src/decorateProxy.ts @@ -18,6 +18,7 @@ import decorateFormatFeatures from './decorateFormatFeatures' import libDomPatching from './libDomPatching' import decorateSignatureHelp from './decorateSignatureHelp' import { approveCast, findChildContainingExactPosition } from './utils' +import decorateFindRenameLocations from './decorateFindRenameLocations' /** @internal */ export const thisPluginMarker = '__essentialPluginsMarker__' @@ -31,10 +32,6 @@ export const getInitialProxy = (languageService: ts.LanguageService, proxy = Obj return proxy } -export const overrideRequestPreferences = { - rename: undefined as undefined | RequestOptionsTypes['acceptRenameWithParams'], -} - export const decorateLanguageService = ( { languageService, languageServiceHost }: ts.server.PluginCreateInfo, existingProxy: ts.LanguageService | undefined, @@ -145,23 +142,7 @@ export const decorateLanguageService = ( decorateWorkspaceSymbolSearch(proxy, languageService, c, languageServiceHost) decorateFormatFeatures(proxy, languageService, languageServiceHost, c) decorateSignatureHelp(proxy, languageService, languageServiceHost, c) - proxy.findRenameLocations = (fileName, position, findInStrings, findInComments, providePrefixAndSuffixTextForRename) => { - if (overrideRequestPreferences.rename) { - try { - const { comments, strings, alias } = overrideRequestPreferences.rename - return languageService.findRenameLocations( - fileName, - position, - strings ?? findInStrings, - comments ?? findInComments, - alias ?? providePrefixAndSuffixTextForRename, - ) - } finally { - overrideRequestPreferences.rename = undefined - } - } - return languageService.findRenameLocations(fileName, position, findInStrings, findInComments, providePrefixAndSuffixTextForRename) - } + decorateFindRenameLocations(proxy, languageService, c) libDomPatching(languageServiceHost, c) diff --git a/typescript/src/specialCommands/handle.ts b/typescript/src/specialCommands/handle.ts index e1261327..355b8981 100644 --- a/typescript/src/specialCommands/handle.ts +++ b/typescript/src/specialCommands/handle.ts @@ -1,11 +1,10 @@ import { compact } from '@zardoy/utils' import { getExtendedCodeActions } from '../codeActions/getCodeActions' -import constructMethodSnippet from '../constructMethodSnippet' -import { overrideRequestPreferences } from '../decorateProxy' import { NodeAtPositionResponse, RequestOptionsTypes, RequestResponseTypes, TriggerCharacterCommand, triggerCharacterCommands } from '../ipcTypes' import { GetConfig } from '../types' import { findChildContainingExactPosition, findChildContainingPosition, getNodePath } from '../utils' import { lastResolvedCompletion } from '../completionEntryDetails' +import { overrideRenameRequest } from '../decorateFindRenameLocations' import getEmmetCompletions from './emmet' import objectIntoArrayConverters from './objectIntoArrayConverters' @@ -200,7 +199,7 @@ export default ( } if (specialCommand === 'acceptRenameWithParams') { changeType(specialCommandArg) - overrideRequestPreferences.rename = specialCommandArg + overrideRenameRequest.value = specialCommandArg return undefined } if (specialCommand === 'pickAndInsertFunctionArguments') { diff --git a/typescript/src/volarConfig.ts b/typescript/src/volarConfig.ts index 1c60af61..e163198a 100644 --- a/typescript/src/volarConfig.ts +++ b/typescript/src/volarConfig.ts @@ -1,12 +1,13 @@ /* eslint-disable @typescript-eslint/no-require-imports */ -import { compact } from '@zardoy/utils' -import { get } from 'lodash' +import get from 'lodash/get' import type { Configuration } from './types' // will be required from ./node_modules/typescript-essential-plugins/index.js const originalPluginFactory = require('typescript-essential-plugins') +const compact = (arr: Array): T[] => arr.filter(Boolean) as T[] + const plugin = ((context, { typescript: tsModule } = {}) => { if (!context) throw new Error('Not recieve context') const { typescript } = context