Skip to content

Commit 14f1c4f

Browse files
committed
Support spawning LSP binary in tests
Ideally we would never need this but Vitest hooking into imports and stuff makes this necessary to properly test a small number things w/o outside interference
1 parent 8ef1e60 commit 14f1c4f

File tree

1 file changed

+27
-2
lines changed
  • packages/tailwindcss-language-server/tests

1 file changed

+27
-2
lines changed

packages/tailwindcss-language-server/tests/common.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'node:path'
22
import { beforeAll, describe } from 'vitest'
3-
import { connect } from './connection'
3+
import { connect, launch } from './connection'
44
import {
55
CompletionRequest,
66
ConfigurationRequest,
@@ -12,6 +12,7 @@ import {
1212
RegistrationRequest,
1313
InitializeParams,
1414
DidOpenTextDocumentParams,
15+
MessageType,
1516
} from 'vscode-languageserver-protocol'
1617
import type { ClientCapabilities, ProtocolConnection } from 'vscode-languageclient'
1718
import type { Feature } from '@tailwindcss/language-service/src/features'
@@ -44,6 +45,14 @@ interface FixtureContext
4445
}
4546

4647
export interface InitOptions {
48+
/**
49+
* How to connect to the LSP:
50+
* - `in-band` runs the server in the same process (default)
51+
* - `spawn` launches the binary as a separate process, connects via stdio,
52+
* and requires a rebuild of the server after making changes.
53+
*/
54+
mode?: 'in-band' | 'spawn'
55+
4756
/**
4857
* Extra initialization options to pass to the LSP
4958
*/
@@ -57,7 +66,23 @@ export async function init(
5766
let settings = {}
5867
let docSettings = new Map<string, Settings>()
5968

60-
const { client } = await connect()
69+
const { client } = opts?.mode === 'spawn' ? await launch() : await connect()
70+
71+
if (opts?.mode === 'spawn') {
72+
client.onNotification('window/logMessage', ({ message, type }) => {
73+
if (type === MessageType.Error) {
74+
console.error(message)
75+
} else if (type === MessageType.Warning) {
76+
console.warn(message)
77+
} else if (type === MessageType.Info) {
78+
console.info(message)
79+
} else if (type === MessageType.Log) {
80+
console.log(message)
81+
} else if (type === MessageType.Debug) {
82+
console.debug(message)
83+
}
84+
})
85+
}
6186

6287
const capabilities: ClientCapabilities = {
6388
textDocument: {

0 commit comments

Comments
 (0)