Repository navigation
Expand file tree
/
Copy pathHighLightReferences.js
More file actions
150 lines (128 loc) · 5.08 KB
/
Copy pathHighLightReferences.js
File metadata and controls
150 lines (128 loc) · 5.08 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
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2013 - 2021 Adobe Systems Incorporated. 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.
*
*/
define(function (require, exports, module) {
let EditorManager = brackets.getModule("editor/EditorManager"),
ScopeManager = brackets.getModule("JSUtils/ScopeManager"),
Session = brackets.getModule("JSUtils/Session"),
MessageIds = JSON.parse(brackets.getModule("text!JSUtils/MessageIds.json")),
Editor = brackets.getModule("editor/Editor").Editor;
let session = null;
//Create new session
function initializeSession(editor) {
session = new Session(editor);
}
//Post message to tern node domain that will request tern server to find refs
function getRefs(fileInfo, offset) {
ScopeManager.postMessage({
type: MessageIds.TERN_REFS,
fileInfo: fileInfo,
offset: offset
});
return ScopeManager.addPendingRequest(fileInfo.name, offset, MessageIds.TERN_REFS);
}
//Create info required to find reference
function requestFindRefs(session, document, offset) {
if (!document || !session) {
return;
}
let path = document.file.fullPath,
fileInfo = {
type: MessageIds.TERN_FILE_INFO_TYPE_FULL,
name: path,
offsetLines: 0,
text: ScopeManager.filterText(session.getJavascriptText())
};
let ternPromise = getRefs(fileInfo, offset);
return {promise: ternPromise};
}
// This is the highlight references under cursor feature. We should ideally move this to
// features/findReferencesManager
const HIGHLIGHT_REFS_MARKER = "JS_REFS";
function _handleHighLightRefs(editor, refsResp) {
if (!refsResp || !refsResp.references || !refsResp.references.refs) {
return;
}
editor.operation(function () {
for(let ref of refsResp.references.refs){
if(editor.document.file.fullPath.endsWith(ref.file)){
editor.markText(HIGHLIGHT_REFS_MARKER, ref.start, ref.end, Editor.MARK_OPTION_MATCHING_REFS);
}
}
});
}
function _hasASingleCursor(editor) {
let selections = editor.getSelections();
if(selections.length > 1){
// multi cursor, no highlight
return false;
}
let start = selections[0].start,
end = selections[0].end;
if(start.line !== end.line || start.ch !== end.ch){
// has a range selection
return false;
}
return true;
}
let allowedHighlightTypes = ["def", "variable", "variable-2", "variable-3", "property"];
let lastHighlightToken = {};
function _cursorActivity(_evt, editor) {
// Only provide a JavaScript editor when cursor is in JavaScript content
if (editor.getModeForSelection() !== "javascript") {
return;
}
if(!_hasASingleCursor(editor)){
editor.clearAllMarks(HIGHLIGHT_REFS_MARKER);
return;
}
let token = editor.getToken();
if(lastHighlightToken === token) {
return;
}
editor.clearAllMarks(HIGHLIGHT_REFS_MARKER);
lastHighlightToken = token;
if(!allowedHighlightTypes.includes(token.type)){
return;
}
let offset = session.getOffset();
// only do this request if token under cursor is a variable type
requestFindRefs(session, session.editor.document, offset).promise
.done(response =>{
_handleHighLightRefs(editor, response);
})
.fail(function (err) {
console.error("find references failed with: ", err);
});
}
function _activeEditorChanged(_evt, current, previous) {
if(previous){
previous.off("cursorActivity.highlightRefs");
}
if(current){
current.off("cursorActivity.highlightRefs");
current.on("cursorActivity.highlightRefs", _cursorActivity);
initializeSession(current);
_cursorActivity(_evt, current);
}
}
EditorManager.on("activeEditorChange", _activeEditorChanged);
exports.HIGHLIGHT_REFS_MARKER = HIGHLIGHT_REFS_MARKER;
});