|
| 1 | +import { Extension, StateField } from '@codemirror/state'; |
| 2 | +import { |
| 3 | + Decoration, |
| 4 | + DecorationSet, |
| 5 | + EditorView, |
| 6 | + ViewPlugin, |
| 7 | + ViewUpdate, |
| 8 | +} from '@codemirror/view'; |
| 9 | +import { DocumentHighlightKind } from 'vscode-languageserver-protocol'; |
| 10 | + |
| 11 | +import { languageServerPlugin } from './plugin'; |
| 12 | +import { offsetToPos, posToOffset } from './pos'; |
| 13 | + |
| 14 | +const highlightField = StateField.define<DecorationSet>({ |
| 15 | + create() { |
| 16 | + return Decoration.none; |
| 17 | + }, |
| 18 | + update(decorations, tr) { |
| 19 | + for (const effect of tr.effects) { |
| 20 | + if (effect.is(setHighlightsEffect)) { |
| 21 | + return effect.value; |
| 22 | + } |
| 23 | + } |
| 24 | + if (tr.docChanged) { |
| 25 | + return Decoration.none; |
| 26 | + } |
| 27 | + return decorations; |
| 28 | + }, |
| 29 | + provide: (field) => EditorView.decorations.from(field), |
| 30 | +}); |
| 31 | + |
| 32 | +import { StateEffect } from '@codemirror/state'; |
| 33 | + |
| 34 | +const setHighlightsEffect = StateEffect.define<DecorationSet>(); |
| 35 | + |
| 36 | +const textDecoration = Decoration.mark({ |
| 37 | + class: 'cm-lsp-highlight-text', |
| 38 | +}); |
| 39 | +const readDecoration = Decoration.mark({ |
| 40 | + class: 'cm-lsp-highlight-read', |
| 41 | +}); |
| 42 | +const writeDecoration = Decoration.mark({ |
| 43 | + class: 'cm-lsp-highlight-write', |
| 44 | +}); |
| 45 | + |
| 46 | +function decorationForKind(kind?: DocumentHighlightKind) { |
| 47 | + switch (kind) { |
| 48 | + case DocumentHighlightKind.Read: |
| 49 | + return readDecoration; |
| 50 | + case DocumentHighlightKind.Write: |
| 51 | + return writeDecoration; |
| 52 | + default: |
| 53 | + return textDecoration; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +const highlightPlugin = ViewPlugin.fromClass( |
| 58 | + class { |
| 59 | + private pending: number = -1; |
| 60 | + |
| 61 | + update(update: ViewUpdate) { |
| 62 | + if (!update.selectionSet && !update.docChanged) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (update.docChanged) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + clearTimeout(this.pending); |
| 71 | + this.pending = setTimeout( |
| 72 | + () => this.requestHighlights(update.view), |
| 73 | + 100, |
| 74 | + ) as any; |
| 75 | + } |
| 76 | + |
| 77 | + async requestHighlights(view: EditorView) { |
| 78 | + const plugin = view.plugin(languageServerPlugin); |
| 79 | + if (!plugin) return; |
| 80 | + |
| 81 | + const { state } = view; |
| 82 | + const pos = state.selection.main.head; |
| 83 | + |
| 84 | + if ( |
| 85 | + !plugin.client.ready || |
| 86 | + !plugin.client.capabilities?.documentHighlightProvider |
| 87 | + ) { |
| 88 | + view.dispatch({ effects: setHighlightsEffect.of(Decoration.none) }); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const result = |
| 93 | + await plugin.client.textDocumentDocumentHighlight({ |
| 94 | + textDocument: { uri: plugin.documentUri }, |
| 95 | + position: offsetToPos(state.doc, pos), |
| 96 | + }); |
| 97 | + |
| 98 | + if (view.state.selection.main.head !== pos) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if (!result || result.length === 0) { |
| 103 | + view.dispatch({ effects: setHighlightsEffect.of(Decoration.none) }); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const decorations = result |
| 108 | + .map((highlight) => { |
| 109 | + const from = posToOffset( |
| 110 | + view.state.doc, |
| 111 | + highlight.range.start, |
| 112 | + ); |
| 113 | + const to = posToOffset( |
| 114 | + view.state.doc, |
| 115 | + highlight.range.end, |
| 116 | + ); |
| 117 | + if (from == null || to == null) return null; |
| 118 | + return decorationForKind(highlight.kind).range(from, to); |
| 119 | + }) |
| 120 | + .filter((d) => d != null) |
| 121 | + .sort((a, b) => a.from - b.from); |
| 122 | + |
| 123 | + view.dispatch({ |
| 124 | + effects: setHighlightsEffect.of(Decoration.set(decorations)), |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + destroy() { |
| 129 | + clearTimeout(this.pending); |
| 130 | + } |
| 131 | + }, |
| 132 | +); |
| 133 | + |
| 134 | +export const documentHighlight = (): Extension => [ |
| 135 | + highlightField, |
| 136 | + highlightPlugin, |
| 137 | +]; |
0 commit comments