diff --git a/src/configurationType.ts b/src/configurationType.ts index 46b26ee..b2ac606 100644 --- a/src/configurationType.ts +++ b/src/configurationType.ts @@ -647,6 +647,8 @@ export type Configuration = { typeAlias: string interface: string } + /** @default false */ + enableHooksFile: boolean } // scrapped using search editor. config: caseInsensitive, context lines: 0, regex: const fix\w+ = "[^ ]+" diff --git a/typescript/src/decorateProxy.ts b/typescript/src/decorateProxy.ts index 2d930bd..387c85c 100644 --- a/typescript/src/decorateProxy.ts +++ b/typescript/src/decorateProxy.ts @@ -1,6 +1,7 @@ import lodashGet from 'lodash.get' import { getCompletionsAtPosition, PrevCompletionMap, PrevCompletionsAdditionalData } from './completionsAtPosition' import { RequestInputTypes, TriggerCharacterCommand } from './ipcTypes' +import { findChildContainingExactPosition, nodeModules } from './utils' import { getNavTreeItems } from './getPatchedNavTree' import decorateCodeActions from './codeActions/decorateProxy' import decorateSemanticDiagnostics from './semanticDiagnostics' @@ -116,5 +117,32 @@ export const decorateLanguageService = ( } languageService[thisPluginMarker] = true + + if (!__WEB__ && c('enableHooksFile')) { + const projectRoot = languageServiceHost.getCurrentDirectory() + const hooksFilePath = nodeModules!.path.join(projectRoot, '.vscode/ts-essentials.js') + if (languageServiceHost.fileExists(hooksFilePath)) { + try { + proxy['original-proxy'] ??= { ...proxy } + const ls = proxy['original-proxy'] + // eslint-disable-next-line @typescript-eslint/no-require-imports + const hooks = require(hooksFilePath)({ + ts, + ls, + languageService: ls, + languageServiceHost, + c, + config, + utils: { + getNodeAtPosition: findChildContainingExactPosition, + }, + }) + Object.assign(proxy, hooks) + } catch (err) { + console.warn('Failed to load hooks file', err) // todo issue + } + } + } + return proxy } diff --git a/typescript/src/index.ts b/typescript/src/index.ts index 8d4a927..cfa89ea 100644 --- a/typescript/src/index.ts +++ b/typescript/src/index.ts @@ -24,14 +24,16 @@ const plugin = ({ typescript }: Parameters[0]) => // #region watch enablePlugin setting let prevPluginEnabledSetting = _configObj.config.enablePlugin + let prevHooksFile = _configObj.config.enableHooksFile updateConfigListeners.push(() => { if ((prevPluginEnabledSetting === true || prevPluginEnabledSetting === undefined) && !_configObj.config.enablePlugin) { // plugin got disabled, restore original languageService methods // todo resetting doesn't work after tsconfig changes getInitialProxy(info.languageService, proxy) - } else if (prevPluginEnabledSetting === false && _configObj.config.enablePlugin) { + } else if ((prevPluginEnabledSetting === false && _configObj.config.enablePlugin) || prevHooksFile !== _configObj.config.enableHooksFile) { // plugin got enabled decorateLanguageService(info, proxy, _configObj, _configObj.config?.['_additionalPluginOptions']) + prevHooksFile = _configObj.config.enableHooksFile } prevPluginEnabledSetting = _configObj.config.enablePlugin