From f36441a64610590e9774abd1d7c7438f4b2d5785 Mon Sep 17 00:00:00 2001 From: machty Date: Thu, 9 Jan 2025 02:39:27 -0500 Subject: [PATCH] reinstate watchFiles --- packages/core/src/volar/language-server.ts | 57 +++++++++------------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/packages/core/src/volar/language-server.ts b/packages/core/src/volar/language-server.ts index 5b05a6b6..11349a50 100644 --- a/packages/core/src/volar/language-server.ts +++ b/packages/core/src/volar/language-server.ts @@ -22,6 +22,7 @@ import GlimmerASTMappingTree from '../transform/template/glimmer-ast-mapping-tre import { Directive, TransformedModule } from '../transform/index.js'; import { Range } from '../transform/template/transformed-module.js'; import { offsetToPosition } from '../language-server/util/position.js'; +import { Disposable } from '@volar/language-service'; const connection = createConnection(); @@ -33,10 +34,17 @@ const server = createServer(connection); * other initialization params needed by the server. */ connection.onInitialize((parameters) => { - const project = createTypeScriptProject(ts, undefined, (projectContext) => { + // Not sure how tsLocalized is used. + const tsLocalized = undefined; + const watchingExtensions = new Set(); + let fileWatcher: Promise | undefined; + + const project = createTypeScriptProject(ts, tsLocalized, (projectContext) => { const configFileName = projectContext.configFileName; const languagePlugins = []; + updateFileWatcher(); + // I don't remember why but there are some contexts where a configFileName is not known, // in which case we cannot fully activate all of the language plugins. if (configFileName) { @@ -102,6 +110,20 @@ connection.onInitialize((parameters) => { } }), ); + + function updateFileWatcher() { + const extensions = ['js', 'ts', 'gjs', 'gts', 'hbs']; + const newExtensions = extensions.filter((ext) => !watchingExtensions.has(ext)); + if (newExtensions.length) { + for (const ext of newExtensions) { + watchingExtensions.add(ext); + } + fileWatcher?.then((dispose) => dispose.dispose()); + fileWatcher = server.fileWatcher.watchFiles([ + '**/*.{' + [...watchingExtensions].join(',') + '}', + ]); + } + } }); function filterAndAugmentDiagnostics( @@ -271,42 +293,11 @@ function filterAndAugmentDiagnostics( return allDiagnostics; } -// connection.onRequest('mdx/toggleDelete', async (parameters) => { -// const commands = await getCommands(parameters.uri) -// return commands.toggleDelete(parameters) -// }) - -// connection.onRequest('mdx/toggleEmphasis', async (parameters) => { -// const commands = await getCommands(parameters.uri) -// return commands.toggleEmphasis(parameters) -// }) - -// connection.onRequest('mdx/toggleInlineCode', async (parameters) => { -// const commands = await getCommands(parameters.uri) -// return commands.toggleInlineCode(parameters) -// }) - -// connection.onRequest('mdx/toggleStrong', async (parameters) => { -// const commands = await getCommands(parameters.uri) -// return commands.toggleStrong(parameters) -// }) - /** - * Invoked when client has sent `initialized` notification. Volar takes this - * opportunity to finish initializing, and we tell the client which extensions - * it should add file-watchers for (technically file-watchers could eagerly - * be set up on the client (e.g. when the extension activates), but since Volar - * capabilities use dynamic/deferredregistration, we have the server tell the - * client which files to watch via the deferred `registerCapability` message - * within `watchFiles()`). + * Invoked when client has sent `initialized` notification. */ connection.onInitialized(() => { server.initialized(); - - const extensions = ['js', 'ts', 'gjs', 'gts', 'hbs']; - - // TODO: figure out how to reinstate (and what this does exactly) - // server.watchFiles([`**.*.{${extensions.join(',')}}`]); }); connection.listen();