Skip to content

Commit 8eaa5c9

Browse files
author
Brad Cornes
committed
add notification handlers
1 parent 0f9ce93 commit 8eaa5c9

File tree

6 files changed

+91
-20
lines changed

6 files changed

+91
-20
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"import-from": "^3.0.0",
9292
"jest": "^25.5.4",
9393
"line-column": "^1.0.2",
94+
"mitt": "^1.2.0",
9495
"mkdirp": "^1.0.3",
9596
"pkg-up": "^3.1.0",
9697
"postcss": "^7.0.27",

src/extension.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { registerConfigErrorHandler } from './lib/registerConfigErrorHandler'
2121
import { DEFAULT_LANGUAGES } from './lib/languages'
2222
import isObject from './util/isObject'
2323
import { dedupe, equal } from './util/array'
24+
import { createEmitter } from './lib/emitter'
2425

2526
const CLIENT_ID = 'tailwindcss-intellisense'
2627
const CLIENT_NAME = 'Tailwind CSS IntelliSense'
@@ -147,7 +148,8 @@ export function activate(context: ExtensionContext) {
147148
)
148149

149150
client.onReady().then(() => {
150-
registerConfigErrorHandler(client)
151+
let emitter = createEmitter(client)
152+
registerConfigErrorHandler(emitter)
151153
})
152154

153155
client.start()

src/lib/emitter.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import mitt from 'mitt'
2+
import { LanguageClient } from 'vscode-languageclient'
3+
import crypto from 'crypto'
4+
5+
export interface NotificationEmitter {
6+
on: (name: string, handler: (args: any) => void) => void
7+
off: (name: string, handler: (args: any) => void) => void
8+
emit: (name: string, args: any) => Promise<any>
9+
}
10+
11+
export function createEmitter(client: LanguageClient): NotificationEmitter {
12+
const emitter = mitt()
13+
const registered: string[] = []
14+
15+
const on = (name: string, handler: (args: any) => void) => {
16+
if (!registered.includes(name)) {
17+
registered.push(name)
18+
client.onNotification(`tailwindcss/${name}`, (args) =>
19+
emitter.emit(name, args)
20+
)
21+
}
22+
emitter.on(name, handler)
23+
}
24+
25+
const off = (name: string, handler: (args: any) => void) => {
26+
emitter.off(name, handler)
27+
}
28+
29+
const emit = (name: string, params: any) => {
30+
return new Promise((resolve, _reject) => {
31+
const id = crypto.randomBytes(16).toString('hex')
32+
on(`${name}Response`, (result) => {
33+
const { _id, ...rest } = result
34+
if (_id === id) {
35+
resolve(rest)
36+
}
37+
})
38+
client.sendNotification(`tailwindcss/${name}`, {
39+
_id: id,
40+
...params,
41+
})
42+
})
43+
}
44+
45+
return {
46+
on,
47+
off,
48+
emit,
49+
}
50+
}

src/lib/registerConfigErrorHandler.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
import { LanguageClient } from 'vscode-languageclient'
21
import { window, Uri, Range, Position } from 'vscode'
2+
import { NotificationEmitter } from './emitter'
33

4-
export function registerConfigErrorHandler(client: LanguageClient) {
5-
client.onNotification(
6-
'tailwindcss/configError',
7-
async ({ message, file, line }) => {
8-
const actions: string[] = file ? ['View'] : []
9-
const action = await window.showErrorMessage(
10-
`Tailwind CSS: ${message}`,
11-
...actions
12-
)
13-
if (action === 'View') {
14-
window.showTextDocument(Uri.file(file), {
15-
selection: new Range(
16-
new Position(line - 1, 0),
17-
new Position(line - 1, 0)
18-
),
19-
})
20-
}
4+
export function registerConfigErrorHandler(emitter: NotificationEmitter) {
5+
emitter.on('configError', async ({ message, file, line }) => {
6+
const actions: string[] = file ? ['View'] : []
7+
const action = await window.showErrorMessage(
8+
`Tailwind CSS: ${message}`,
9+
...actions
10+
)
11+
if (action === 'View') {
12+
window.showTextDocument(Uri.file(file), {
13+
selection: new Range(
14+
new Position(line - 1, 0),
15+
new Position(line - 1, 0)
16+
),
17+
})
2118
}
22-
)
19+
})
2320
}

src/lsp/notifications.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Connection } from 'vscode-languageserver'
2+
3+
export function onMessage(
4+
connection: Connection,
5+
name: string,
6+
handler: (params: any) => any
7+
): void {
8+
connection.onNotification(`tailwindcss/${name}`, async (params: any) => {
9+
const { _id, ...rest } = params
10+
connection.sendNotification(`tailwindcss/${name}Response`, {
11+
_id,
12+
...(await handler(rest)),
13+
})
14+
})
15+
}

0 commit comments

Comments
 (0)