Skip to content

Commit b7352ee

Browse files
committed
feat: add per-project hook file! If setting enableHooksFile is enabled you can add .vscode/ts-essentials.js file for taking full control of any languageService methods
1 parent ea46dc1 commit b7352ee

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/configurationType.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,8 @@ export type Configuration = {
647647
typeAlias: string
648648
interface: string
649649
}
650+
/** @default false */
651+
enableHooksFile: boolean
650652
}
651653

652654
// scrapped using search editor. config: caseInsensitive, context lines: 0, regex: const fix\w+ = "[^ ]+"

typescript/src/decorateProxy.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import lodashGet from 'lodash.get'
22
import { getCompletionsAtPosition, PrevCompletionMap, PrevCompletionsAdditionalData } from './completionsAtPosition'
33
import { RequestInputTypes, TriggerCharacterCommand } from './ipcTypes'
4+
import { findChildContainingExactPosition, nodeModules } from './utils'
45
import { getNavTreeItems } from './getPatchedNavTree'
56
import decorateCodeActions from './codeActions/decorateProxy'
67
import decorateSemanticDiagnostics from './semanticDiagnostics'
@@ -116,5 +117,32 @@ export const decorateLanguageService = (
116117
}
117118

118119
languageService[thisPluginMarker] = true
120+
121+
if (!__WEB__ && c('enableHooksFile')) {
122+
const projectRoot = languageServiceHost.getCurrentDirectory()
123+
const hooksFilePath = nodeModules!.path.join(projectRoot, '.vscode/ts-essentials.js')
124+
if (languageServiceHost.fileExists(hooksFilePath)) {
125+
try {
126+
proxy['original-proxy'] ??= { ...proxy }
127+
const ls = proxy['original-proxy']
128+
// eslint-disable-next-line @typescript-eslint/no-require-imports
129+
const hooks = require(hooksFilePath)({
130+
ts,
131+
ls,
132+
languageService: ls,
133+
languageServiceHost,
134+
c,
135+
config,
136+
utils: {
137+
getNodeAtPosition: findChildContainingExactPosition,
138+
},
139+
})
140+
Object.assign(proxy, hooks)
141+
} catch (err) {
142+
console.warn('Failed to load hooks file', err) // todo issue
143+
}
144+
}
145+
}
146+
119147
return proxy
120148
}

typescript/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ const plugin = ({ typescript }: Parameters<ts.server.PluginModuleFactory>[0]) =>
2424

2525
// #region watch enablePlugin setting
2626
let prevPluginEnabledSetting = _configObj.config.enablePlugin
27+
let prevHooksFile = _configObj.config.enableHooksFile
2728
updateConfigListeners.push(() => {
2829
if ((prevPluginEnabledSetting === true || prevPluginEnabledSetting === undefined) && !_configObj.config.enablePlugin) {
2930
// plugin got disabled, restore original languageService methods
3031
// todo resetting doesn't work after tsconfig changes
3132
getInitialProxy(info.languageService, proxy)
32-
} else if (prevPluginEnabledSetting === false && _configObj.config.enablePlugin) {
33+
} else if ((prevPluginEnabledSetting === false && _configObj.config.enablePlugin) || prevHooksFile !== _configObj.config.enableHooksFile) {
3334
// plugin got enabled
3435
decorateLanguageService(info, proxy, _configObj, _configObj.config?.['_additionalPluginOptions'])
36+
prevHooksFile = _configObj.config.enableHooksFile
3537
}
3638

3739
prevPluginEnabledSetting = _configObj.config.enablePlugin

0 commit comments

Comments
 (0)