add notification handlers
parent
0f9ce93857
commit
8eaa5c9326
|
@ -4910,6 +4910,12 @@
|
|||
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==",
|
||||
"dev": true
|
||||
},
|
||||
"mitt": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/mitt/-/mitt-1.2.0.tgz",
|
||||
"integrity": "sha512-r6lj77KlwqLhIUku9UWYes7KJtsczvolZkzp8hbaDPPaE24OmWl5s539Mytlj22siEQKosZ26qCBgda2PKwoJw==",
|
||||
"dev": true
|
||||
},
|
||||
"mixin-deep": {
|
||||
"version": "1.3.2",
|
||||
"resolved": "https://registry.npmjs.org/mixin-deep/-/mixin-deep-1.3.2.tgz",
|
||||
|
|
|
@ -91,6 +91,7 @@
|
|||
"import-from": "^3.0.0",
|
||||
"jest": "^25.5.4",
|
||||
"line-column": "^1.0.2",
|
||||
"mitt": "^1.2.0",
|
||||
"mkdirp": "^1.0.3",
|
||||
"pkg-up": "^3.1.0",
|
||||
"postcss": "^7.0.27",
|
||||
|
|
|
@ -21,6 +21,7 @@ import { registerConfigErrorHandler } from './lib/registerConfigErrorHandler'
|
|||
import { DEFAULT_LANGUAGES } from './lib/languages'
|
||||
import isObject from './util/isObject'
|
||||
import { dedupe, equal } from './util/array'
|
||||
import { createEmitter } from './lib/emitter'
|
||||
|
||||
const CLIENT_ID = 'tailwindcss-intellisense'
|
||||
const CLIENT_NAME = 'Tailwind CSS IntelliSense'
|
||||
|
@ -147,7 +148,8 @@ export function activate(context: ExtensionContext) {
|
|||
)
|
||||
|
||||
client.onReady().then(() => {
|
||||
registerConfigErrorHandler(client)
|
||||
let emitter = createEmitter(client)
|
||||
registerConfigErrorHandler(emitter)
|
||||
})
|
||||
|
||||
client.start()
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
import mitt from 'mitt'
|
||||
import { LanguageClient } from 'vscode-languageclient'
|
||||
import crypto from 'crypto'
|
||||
|
||||
export interface NotificationEmitter {
|
||||
on: (name: string, handler: (args: any) => void) => void
|
||||
off: (name: string, handler: (args: any) => void) => void
|
||||
emit: (name: string, args: any) => Promise<any>
|
||||
}
|
||||
|
||||
export function createEmitter(client: LanguageClient): NotificationEmitter {
|
||||
const emitter = mitt()
|
||||
const registered: string[] = []
|
||||
|
||||
const on = (name: string, handler: (args: any) => void) => {
|
||||
if (!registered.includes(name)) {
|
||||
registered.push(name)
|
||||
client.onNotification(`tailwindcss/${name}`, (args) =>
|
||||
emitter.emit(name, args)
|
||||
)
|
||||
}
|
||||
emitter.on(name, handler)
|
||||
}
|
||||
|
||||
const off = (name: string, handler: (args: any) => void) => {
|
||||
emitter.off(name, handler)
|
||||
}
|
||||
|
||||
const emit = (name: string, params: any) => {
|
||||
return new Promise((resolve, _reject) => {
|
||||
const id = crypto.randomBytes(16).toString('hex')
|
||||
on(`${name}Response`, (result) => {
|
||||
const { _id, ...rest } = result
|
||||
if (_id === id) {
|
||||
resolve(rest)
|
||||
}
|
||||
})
|
||||
client.sendNotification(`tailwindcss/${name}`, {
|
||||
_id: id,
|
||||
...params,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
on,
|
||||
off,
|
||||
emit,
|
||||
}
|
||||
}
|
|
@ -1,10 +1,8 @@
|
|||
import { LanguageClient } from 'vscode-languageclient'
|
||||
import { window, Uri, Range, Position } from 'vscode'
|
||||
import { NotificationEmitter } from './emitter'
|
||||
|
||||
export function registerConfigErrorHandler(client: LanguageClient) {
|
||||
client.onNotification(
|
||||
'tailwindcss/configError',
|
||||
async ({ message, file, line }) => {
|
||||
export function registerConfigErrorHandler(emitter: NotificationEmitter) {
|
||||
emitter.on('configError', async ({ message, file, line }) => {
|
||||
const actions: string[] = file ? ['View'] : []
|
||||
const action = await window.showErrorMessage(
|
||||
`Tailwind CSS: ${message}`,
|
||||
|
@ -18,6 +16,5 @@ export function registerConfigErrorHandler(client: LanguageClient) {
|
|||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
import { Connection } from 'vscode-languageserver'
|
||||
|
||||
export function onMessage(
|
||||
connection: Connection,
|
||||
name: string,
|
||||
handler: (params: any) => any
|
||||
): void {
|
||||
connection.onNotification(`tailwindcss/${name}`, async (params: any) => {
|
||||
const { _id, ...rest } = params
|
||||
connection.sendNotification(`tailwindcss/${name}Response`, {
|
||||
_id,
|
||||
...(await handler(rest)),
|
||||
})
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue