Skip to content

Commit

Permalink
Only watch TS related file extensions if needed (#432)
Browse files Browse the repository at this point in the history
If TypeScript isn’t enabled in the language server, there’s no need to
watch file extension other than `mdx`.
  • Loading branch information
remcohaszing committed May 2, 2024
1 parent 8747672 commit 7b7c0ae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-lies-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mdx-js/language-server": patch
---

Avoid redundant file watchers when TypeScript is disabled.
19 changes: 10 additions & 9 deletions packages/language-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ process.title = 'mdx-language-server'
const defaultPlugins = [[remarkFrontmatter, ['toml', 'yaml']], remarkGfm]
const connection = createConnection()
const server = createServer(connection)
let tsEnabled = false

connection.onInitialize(async (parameters) => {
const tsdk = parameters.initializationOptions?.typescript?.tsdk
const tsEnabled = Boolean(
parameters.initializationOptions?.typescript?.enabled
)
tsEnabled = Boolean(parameters.initializationOptions?.typescript?.enabled)
assert(
typeof tsdk === 'string',
'Missing initialization option typescript.tsdk'
Expand Down Expand Up @@ -146,21 +145,23 @@ connection.onRequest('mdx/toggleStrong', async (parameters) => {
})

connection.onInitialized(() => {
server.initialized()
server.watchFiles([
`**/*.{${[
const extensions = ['mdx']
if (tsEnabled) {
extensions.push(
'cjs',
'cts',
'js',
'jsx',
'json',
'mdx',
'mjs',
'mts',
'ts',
'tsx'
].join(',')}}`
])
)
}

server.initialized()
server.watchFiles([`**/*.{${extensions.join(',')}}`])
})

connection.listen()
Expand Down

0 comments on commit 7b7c0ae

Please sign in to comment.