Repository navigation
Expand file tree
/
Copy pathDocumentHighlight.js
More file actions
177 lines (160 loc) · 6.41 KB
/
Copy pathDocumentHighlight.js
File metadata and controls
177 lines (160 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/**
* DocumentHighlight - highlights every occurrence of the symbol under the cursor in the active
* editor, via the LSP `textDocument/documentHighlight` request. This is the generic, multi-language
* replacement for the Tern-only "highlight references under cursor" feature
* (extensions/default/JavaScriptRefactoring/HighLightReferences.js).
*
* It works for any registered language server that advertises `documentHighlightProvider`. Marks
* reuse the same visual style as the legacy feature (Editor.getMarkOptionMatchingRefs()).
*
* @module languageTools/DocumentHighlight
*/
define(function (require, exports, module) {
const EditorManager = require("editor/EditorManager"),
Editor = require("editor/Editor").Editor;
const HIGHLIGHT_MARKER = "lsp-document-highlight";
const REQUEST_DEBOUNCE_MS = 150;
// Token types we never highlight on (comments/strings/numbers aren't symbols).
const SKIP_TOKEN_TYPES = ["comment", "string", "number"];
let initialized = false;
const registeredClients = []; // LanguageClient[]
let lastTokenKey = null;
let debounceTimer = null;
let requestSeq = 0;
function _clientForEditor(editor) {
if (!editor) {
return null;
}
const langId = editor.document.getLanguage().getId();
for (let i = 0; i < registeredClients.length; i++) {
const c = registeredClients[i];
if (c.capabilities && c.capabilities.documentHighlightProvider &&
c.languages.indexOf(langId) !== -1) {
return c;
}
}
return null;
}
function _hasSingleCursor(editor) {
const selections = editor.getSelections();
if (selections.length > 1) {
return false; // multi-cursor: don't highlight
}
const start = selections[0].start, end = selections[0].end;
return start.line === end.line && start.ch === end.ch; // a caret, not a range
}
function _cursorActivity(_evt, editor) {
const client = _clientForEditor(editor);
if (!client) {
return;
}
if (!_hasSingleCursor(editor)) {
editor.clearAllMarks(HIGHLIGHT_MARKER);
lastTokenKey = null;
return;
}
const pos = editor.getCursorPos();
const token = editor.getToken(pos);
const tokenKey = pos.line + ":" + (token ? token.start + ":" + token.string : pos.ch);
if (tokenKey === lastTokenKey) {
return; // still on the same token - keep existing marks
}
editor.clearAllMarks(HIGHLIGHT_MARKER);
lastTokenKey = tokenKey;
if (!token || !token.string || !/[\w$]/.test(token.string) ||
(token.type && SKIP_TOKEN_TYPES.indexOf(token.type) !== -1)) {
return;
}
if (debounceTimer) {
clearTimeout(debounceTimer);
}
const seq = ++requestSeq;
debounceTimer = setTimeout(function () {
debounceTimer = null;
client.documentHighlight({ filePath: editor.document.file._path, cursorPos: pos })
.done(function (highlights) {
// Ignore stale responses (cursor moved / another request issued / editor changed).
if (seq !== requestSeq || EditorManager.getActiveEditor() !== editor) {
return;
}
if (!highlights || !highlights.length) {
return;
}
editor.operation(function () {
highlights.forEach(function (h) {
if (!h || !h.range) {
return;
}
editor.markText(HIGHLIGHT_MARKER,
{ line: h.range.start.line, ch: h.range.start.character },
{ line: h.range.end.line, ch: h.range.end.character },
Editor.getMarkOptionMatchingRefs());
});
});
})
.fail(function () { /* no highlights for this position */ });
}, REQUEST_DEBOUNCE_MS);
}
function _activeEditorChanged(evt, current, previous) {
if (previous) {
previous.off("cursorActivity.lspHighlight");
}
if (current) {
current.off("cursorActivity.lspHighlight");
current.on("cursorActivity.lspHighlight", _cursorActivity);
lastTokenKey = null;
_cursorActivity(evt, current);
}
}
/**
* Attach the active-editor / cursor listeners. Safe to call multiple times.
*/
function init() {
if (initialized) {
return;
}
initialized = true;
EditorManager.on("activeEditorChange", _activeEditorChanged);
const editor = EditorManager.getActiveEditor();
if (editor) {
_activeEditorChanged(null, editor, null);
}
}
/**
* Register a client so its languages get cursor-based occurrence highlighting.
* @param {Object} client - a LanguageClient
*/
function registerClient(client) {
if (registeredClients.indexOf(client) === -1) {
registeredClients.push(client);
}
// Re-evaluate the current editor now that a new server is available.
const editor = EditorManager.getActiveEditor();
if (editor) {
lastTokenKey = null;
_cursorActivity(null, editor);
}
}
exports.init = init;
exports.registerClient = registerClient;
exports.HIGHLIGHT_MARKER = HIGHLIGHT_MARKER;
});