|
| 1 | +import { window, workspace, ExtensionContext, TextEditor } from 'vscode' |
| 2 | +import { NotificationEmitter } from './emitter' |
| 3 | +import { LanguageClient } from 'vscode-languageclient' |
| 4 | +import debounce from 'debounce' |
| 5 | + |
| 6 | +const colorDecorationType = window.createTextEditorDecorationType({ |
| 7 | + before: { |
| 8 | + width: '0.8em', |
| 9 | + height: '0.8em', |
| 10 | + contentText: ' ', |
| 11 | + border: '0.1em solid', |
| 12 | + margin: '0.1em 0.2em 0', |
| 13 | + }, |
| 14 | + dark: { |
| 15 | + before: { |
| 16 | + borderColor: '#eeeeee', |
| 17 | + }, |
| 18 | + }, |
| 19 | + light: { |
| 20 | + before: { |
| 21 | + borderColor: '#000000', |
| 22 | + }, |
| 23 | + }, |
| 24 | +}) |
| 25 | + |
| 26 | +export function registerColorDecorator( |
| 27 | + client: LanguageClient, |
| 28 | + context: ExtensionContext, |
| 29 | + emitter: NotificationEmitter |
| 30 | +) { |
| 31 | + let activeEditor = window.activeTextEditor |
| 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 preference = |
| 51 | + workspace.getConfiguration('tailwindCSS', editor.document) |
| 52 | + .colorDecorators || 'inherit' |
| 53 | + |
| 54 | + let enabled: boolean = |
| 55 | + preference === 'inherit' |
| 56 | + ? Boolean(workspace.getConfiguration('editor').colorDecorators) |
| 57 | + : preference === 'on' |
| 58 | + |
| 59 | + if (!enabled) { |
| 60 | + editor.setDecorations(colorDecorationType, []) |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + let { colors } = await emitter.emit('getDocumentColors', { |
| 65 | + document: editor.document.uri.toString(), |
| 66 | + }) |
| 67 | + |
| 68 | + editor.setDecorations( |
| 69 | + colorDecorationType, |
| 70 | + colors |
| 71 | + .filter(({ color }) => color !== 'rgba(0, 0, 0, 0.01)') |
| 72 | + .map(({ range, color }) => ({ |
| 73 | + range, |
| 74 | + renderOptions: { before: { backgroundColor: color } }, |
| 75 | + })) |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + const triggerUpdateDecorations = debounce(updateDecorations, 200) |
| 80 | + |
| 81 | + if (activeEditor) { |
| 82 | + triggerUpdateDecorations() |
| 83 | + } |
| 84 | + |
| 85 | + window.onDidChangeActiveTextEditor( |
| 86 | + (editor) => { |
| 87 | + activeEditor = editor |
| 88 | + if (editor) { |
| 89 | + triggerUpdateDecorations() |
| 90 | + } |
| 91 | + }, |
| 92 | + null, |
| 93 | + context.subscriptions |
| 94 | + ) |
| 95 | + |
| 96 | + workspace.onDidChangeTextDocument( |
| 97 | + (event) => { |
| 98 | + if (activeEditor && event.document === activeEditor.document) { |
| 99 | + triggerUpdateDecorations() |
| 100 | + } |
| 101 | + }, |
| 102 | + null, |
| 103 | + context.subscriptions |
| 104 | + ) |
| 105 | + |
| 106 | + workspace.onDidOpenTextDocument( |
| 107 | + (document) => { |
| 108 | + if (activeEditor && document === activeEditor.document) { |
| 109 | + triggerUpdateDecorations() |
| 110 | + } |
| 111 | + }, |
| 112 | + null, |
| 113 | + context.subscriptions |
| 114 | + ) |
| 115 | + |
| 116 | + workspace.onDidChangeConfiguration((e) => { |
| 117 | + if ( |
| 118 | + e.affectsConfiguration('editor.colorDecorators') || |
| 119 | + e.affectsConfiguration('tailwindCSS.colorDecorators') |
| 120 | + ) { |
| 121 | + window.visibleTextEditors.forEach(updateDecorationsInEditor) |
| 122 | + } |
| 123 | + }) |
| 124 | + |
| 125 | + emitter.on('configUpdated', () => { |
| 126 | + window.visibleTextEditors.forEach(updateDecorationsInEditor) |
| 127 | + }) |
| 128 | + |
| 129 | + emitter.on('configError', () => { |
| 130 | + window.visibleTextEditors.forEach(updateDecorationsInEditor) |
| 131 | + }) |
| 132 | +} |
0 commit comments