|
| 1 | +import { window, workspace, ExtensionContext, TextEditor } from 'vscode' |
| 2 | +import { NotificationEmitter } from './emitter' |
| 3 | +import { LanguageClient } from 'vscode-languageclient' |
| 4 | + |
| 5 | +const colorDecorationType = window.createTextEditorDecorationType({ |
| 6 | + before: { |
| 7 | + width: '0.8em', |
| 8 | + height: '0.8em', |
| 9 | + contentText: ' ', |
| 10 | + border: '0.1em solid', |
| 11 | + margin: '0.1em 0.2em 0', |
| 12 | + }, |
| 13 | + dark: { |
| 14 | + before: { |
| 15 | + borderColor: '#eeeeee', |
| 16 | + }, |
| 17 | + }, |
| 18 | + light: { |
| 19 | + before: { |
| 20 | + borderColor: '#000000', |
| 21 | + }, |
| 22 | + }, |
| 23 | +}) |
| 24 | + |
| 25 | +export function registerColorDecorator( |
| 26 | + client: LanguageClient, |
| 27 | + context: ExtensionContext, |
| 28 | + emitter: NotificationEmitter |
| 29 | +) { |
| 30 | + let activeEditor = window.activeTextEditor |
| 31 | + let timeout: NodeJS.Timer | undefined = undefined |
| 32 | + |
| 33 | + async function updateDecorations() { |
| 34 | + return updateDecorationsInEditor(activeEditor) |
| 35 | + } |
| 36 | + |
| 37 | + async function updateDecorationsInEditor(editor: TextEditor) { |
| 38 | + if (!editor) return |
| 39 | + if (editor.document.uri.scheme !== 'file') return |
| 40 | + |
| 41 | + let workspaceFolder = workspace.getWorkspaceFolder(editor.document.uri) |
| 42 | + if ( |
| 43 | + !workspaceFolder || |
| 44 | + workspaceFolder.uri.toString() !== |
| 45 | + client.clientOptions.workspaceFolder.uri.toString() |
| 46 | + ) { |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + let settings = workspace.getConfiguration( |
| 51 | + 'tailwindCSS.colorDecorators', |
| 52 | + editor.document |
| 53 | + ) |
| 54 | + |
| 55 | + if (settings.enabled !== true) { |
| 56 | + editor.setDecorations(colorDecorationType, []) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + let { colors } = await emitter.emit('getDocumentColors', { |
| 61 | + document: editor.document.uri.toString(), |
| 62 | + classes: settings.classes, |
| 63 | + cssHelpers: settings.cssHelpers, |
| 64 | + }) |
| 65 | + |
| 66 | + editor.setDecorations( |
| 67 | + colorDecorationType, |
| 68 | + colors |
| 69 | + .filter(({ color }) => color !== 'rgba(0, 0, 0, 0.01)') |
| 70 | + .map(({ range, color }) => ({ |
| 71 | + range, |
| 72 | + renderOptions: { before: { backgroundColor: color } }, |
| 73 | + })) |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + function triggerUpdateDecorations() { |
| 78 | + if (timeout) { |
| 79 | + clearTimeout(timeout) |
| 80 | + timeout = undefined |
| 81 | + } |
| 82 | + timeout = setTimeout(updateDecorations, 500) |
| 83 | + } |
| 84 | + |
| 85 | + if (activeEditor) { |
| 86 | + triggerUpdateDecorations() |
| 87 | + } |
| 88 | + |
| 89 | + window.onDidChangeActiveTextEditor( |
| 90 | + (editor) => { |
| 91 | + activeEditor = editor |
| 92 | + if (editor) { |
| 93 | + triggerUpdateDecorations() |
| 94 | + } |
| 95 | + }, |
| 96 | + null, |
| 97 | + context.subscriptions |
| 98 | + ) |
| 99 | + |
| 100 | + workspace.onDidChangeTextDocument( |
| 101 | + (event) => { |
| 102 | + if (activeEditor && event.document === activeEditor.document) { |
| 103 | + triggerUpdateDecorations() |
| 104 | + } |
| 105 | + }, |
| 106 | + null, |
| 107 | + context.subscriptions |
| 108 | + ) |
| 109 | + |
| 110 | + workspace.onDidOpenTextDocument( |
| 111 | + (document) => { |
| 112 | + if (activeEditor && document === activeEditor.document) { |
| 113 | + triggerUpdateDecorations() |
| 114 | + } |
| 115 | + }, |
| 116 | + null, |
| 117 | + context.subscriptions |
| 118 | + ) |
| 119 | + |
| 120 | + workspace.onDidChangeConfiguration((e) => { |
| 121 | + if (e.affectsConfiguration('tailwindCSS.colorDecorators')) { |
| 122 | + window.visibleTextEditors.forEach(updateDecorationsInEditor) |
| 123 | + } |
| 124 | + }) |
| 125 | +} |
0 commit comments