add notification handlers

master
Brad Cornes 2020-05-09 17:24:56 +01:00
parent 0f9ce93857
commit 8eaa5c9326
6 changed files with 91 additions and 20 deletions

6
package-lock.json generated
View File

@ -4910,6 +4910,12 @@
"integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==",
"dev": true "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": { "mixin-deep": {
"version": "1.3.2", "version": "1.3.2",
"resolved": "https://registry.npmjs.org/mixin-deep/-/mixin-deep-1.3.2.tgz", "resolved": "https://registry.npmjs.org/mixin-deep/-/mixin-deep-1.3.2.tgz",

View File

@ -91,6 +91,7 @@
"import-from": "^3.0.0", "import-from": "^3.0.0",
"jest": "^25.5.4", "jest": "^25.5.4",
"line-column": "^1.0.2", "line-column": "^1.0.2",
"mitt": "^1.2.0",
"mkdirp": "^1.0.3", "mkdirp": "^1.0.3",
"pkg-up": "^3.1.0", "pkg-up": "^3.1.0",
"postcss": "^7.0.27", "postcss": "^7.0.27",

View File

@ -21,6 +21,7 @@ import { registerConfigErrorHandler } from './lib/registerConfigErrorHandler'
import { DEFAULT_LANGUAGES } from './lib/languages' import { DEFAULT_LANGUAGES } from './lib/languages'
import isObject from './util/isObject' import isObject from './util/isObject'
import { dedupe, equal } from './util/array' import { dedupe, equal } from './util/array'
import { createEmitter } from './lib/emitter'
const CLIENT_ID = 'tailwindcss-intellisense' const CLIENT_ID = 'tailwindcss-intellisense'
const CLIENT_NAME = 'Tailwind CSS IntelliSense' const CLIENT_NAME = 'Tailwind CSS IntelliSense'
@ -147,7 +148,8 @@ export function activate(context: ExtensionContext) {
) )
client.onReady().then(() => { client.onReady().then(() => {
registerConfigErrorHandler(client) let emitter = createEmitter(client)
registerConfigErrorHandler(emitter)
}) })
client.start() client.start()

50
src/lib/emitter.ts 100644
View File

@ -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,
}
}

View File

@ -1,23 +1,20 @@
import { LanguageClient } from 'vscode-languageclient'
import { window, Uri, Range, Position } from 'vscode' import { window, Uri, Range, Position } from 'vscode'
import { NotificationEmitter } from './emitter'
export function registerConfigErrorHandler(client: LanguageClient) { export function registerConfigErrorHandler(emitter: NotificationEmitter) {
client.onNotification( emitter.on('configError', async ({ message, file, line }) => {
'tailwindcss/configError', const actions: string[] = file ? ['View'] : []
async ({ message, file, line }) => { const action = await window.showErrorMessage(
const actions: string[] = file ? ['View'] : [] `Tailwind CSS: ${message}`,
const action = await window.showErrorMessage( ...actions
`Tailwind CSS: ${message}`, )
...actions if (action === 'View') {
) window.showTextDocument(Uri.file(file), {
if (action === 'View') { selection: new Range(
window.showTextDocument(Uri.file(file), { new Position(line - 1, 0),
selection: new Range( new Position(line - 1, 0)
new Position(line - 1, 0), ),
new Position(line - 1, 0) })
),
})
}
} }
) })
} }

View File

@ -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)),
})
})
}