Skip to content

Commit

Permalink
feat: add per-project hook file! If setting enableHooksFile is enable…
Browse files Browse the repository at this point in the history
…d you can add .vscode/ts-essentials.js file for taking full control of any languageService methods
  • Loading branch information
zardoy committed Nov 12, 2023
1 parent ea46dc1 commit b7352ee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/configurationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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+ = "[^ ]+"
Expand Down
28 changes: 28 additions & 0 deletions typescript/src/decorateProxy.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ const plugin = ({ typescript }: Parameters<ts.server.PluginModuleFactory>[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
Expand Down

0 comments on commit b7352ee

Please sign in to comment.