Skip to content

Commit

Permalink
Merge pull request #132 from zardoy/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy authored Jun 1, 2023
2 parents 83cd6d2 + eedfc9c commit a98aaae
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 29 deletions.
5 changes: 3 additions & 2 deletions buildTsPlugin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>(resolve => {
Expand Down
59 changes: 59 additions & 0 deletions typescript/src/decorateFindRenameLocations.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
23 changes: 2 additions & 21 deletions typescript/src/decorateProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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__'
Expand All @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 2 additions & 3 deletions typescript/src/specialCommands/handle.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -200,7 +199,7 @@ export default (
}
if (specialCommand === 'acceptRenameWithParams') {
changeType<RequestOptionsTypes['acceptRenameWithParams']>(specialCommandArg)
overrideRequestPreferences.rename = specialCommandArg
overrideRenameRequest.value = specialCommandArg
return undefined
}
if (specialCommand === 'pickAndInsertFunctionArguments') {
Expand Down
5 changes: 3 additions & 2 deletions typescript/src/volarConfig.ts
Original file line number Diff line number Diff line change
@@ -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 = <T>(arr: Array<T | undefined>): T[] => arr.filter(Boolean) as T[]

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

0 comments on commit a98aaae

Please sign in to comment.