Skip to content

Commit

Permalink
Fix workspace initialization
Browse files Browse the repository at this point in the history
Closes GH-27.

Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
remcohaszing committed Jan 24, 2022
1 parent 033ebba commit 723ceba
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 22 deletions.
41 changes: 23 additions & 18 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export function configureUnifiedLanguageServer(
) {
/** @type {Set<string>} */
const workspaces = new Set()
let hasWorkspaceFolderCapability = false

/**
* Process various LSP text documents using unified and send back the
Expand Down Expand Up @@ -325,22 +326,10 @@ export function configureUnifiedLanguageServer(
workspaces.add(event.rootUri)
}

if (
hasWorkspaceFolderCapability = Boolean(
event.capabilities.workspace &&
event.capabilities.workspace.workspaceFolders
) {
connection.workspace.onDidChangeWorkspaceFolders(function (event) {
for (const workspace of event.removed) {
workspaces.delete(workspace.uri)
}

for (const workspace of event.added) {
workspaces.add(workspace.uri)
}

checkDocuments(...documents.all())
})
}
event.capabilities.workspace.workspaceFolders
)

return {
capabilities: {
Expand All @@ -350,13 +339,29 @@ export function configureUnifiedLanguageServer(
codeActionKinds: [CodeActionKind.QuickFix],
resolveProvider: true
},
workspace: {
workspaceFolders: {supported: true, changeNotifications: true}
}
workspace: hasWorkspaceFolderCapability
? {workspaceFolders: {supported: true, changeNotifications: true}}
: undefined
}
}
})

connection.onInitialized(() => {
if (hasWorkspaceFolderCapability) {
connection.workspace.onDidChangeWorkspaceFolders((event) => {
for (const workspace of event.removed) {
workspaces.delete(workspace.uri)
}

for (const workspace of event.added) {
workspaces.add(workspace.uri)
}

checkDocuments(...documents.all())
})
}
})

connection.onDocumentFormatting(async (event) => {
const document = documents.get(event.textDocument.uri)

Expand Down
70 changes: 66 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as exports from 'unified-language-server'

const sleep = promisify(setTimeout)

const delay = process.platform === 'win32' ? 600 : 300
const delay = process.platform === 'win32' ? 800 : 400
const timeout = 10_000

test('exports', (t) => {
Expand Down Expand Up @@ -51,6 +51,61 @@ test('`initialize`', async (t) => {
assert(promise.stdout)
promise.stdout.on('data', () => setImmediate(() => stdin.end()))

try {
await promise
t.fail('should reject')
} catch (error) {
const exception = /** @type {ExecError} */ (error)
const messages = fromMessages(exception.stdout)
t.equal(messages.length, 1, 'should emit messages')
const parameters = messages[0].result

t.deepEqual(
parameters,
{
capabilities: {
textDocumentSync: 1,
documentFormattingProvider: true,
codeActionProvider: {
codeActionKinds: ['quickfix'],
resolveProvider: true
}
}
},
'should emit an introduction on `initialize`'
)
}

t.end()
})

test('`initialize` workspace capabilities', async (t) => {
const stdin = new PassThrough()
const promise = execa('node', ['remark.js', '--stdio'], {
cwd: fileURLToPath(new URL('.', import.meta.url)),
input: stdin,
timeout
})

stdin.write(
toMessage({
method: 'initialize',
id: 0,
/** @type {import('vscode-languageserver').InitializeParams} */
params: {
processId: null,
rootUri: null,
capabilities: {workspace: {workspaceFolders: true}},
workspaceFolders: null
}
})
)

await sleep(delay)

assert(promise.stdout)
promise.stdout.on('data', () => setImmediate(() => stdin.end()))

try {
await promise
t.fail('should reject')
Expand Down Expand Up @@ -754,9 +809,6 @@ test('`textDocument/codeAction` (and diagnostics)', async (t) => {
codeActionProvider: {
codeActionKinds: ['quickfix'],
resolveProvider: true
},
workspace: {
workspaceFolders: {supported: true, changeNotifications: true}
}
}
}
Expand Down Expand Up @@ -1066,6 +1118,16 @@ test('`workspace/didChangeWorkspaceFolders`', async (t) => {

await sleep(delay)

stdin.write(
toMessage({
method: 'initialized',
/** @type {import('vscode-languageserver').InitializedParams} */
params: {}
})
)

await sleep(delay)

const otherCwd = new URL('./folder/', processCwd)

stdin.write(
Expand Down

0 comments on commit 723ceba

Please sign in to comment.