-
Notifications
You must be signed in to change notification settings - Fork 20
/
TernReferences.js
104 lines (86 loc) · 3.29 KB
/
TernReferences.js
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
/**
* Ternific Copyright (c) 2014 Miguel Castillo.
*
* Licensed under MIT
*/
define(function (require /*, exports, module*/) {
"use strict";
var DocumentManager = brackets.getModule("document/DocumentManager");
var MainViewManager = brackets.getModule("view/MainViewManager");
var ProjectManager = brackets.getModule("project/ProjectManager");
var EventDispatcher = brackets.getModule("utils/EventDispatcher");
var Promise = require("node_modules/spromise/dist/spromise.min");
/**
* Provider to tind references of a variable/property
*
* @param {TernProvider} ternProvider instance of the tern provider
*/
function TernReferences(ternProvider) {
var _self = this;
this.ternProvider = ternProvider;
this._cm = null;
this._token = null;
this._matches = null;
this.events = {};
EventDispatcher.makeEventDispatcher(this.events);
MainViewManager
.on("currentDocumentChange", function(evt, currentDocument) {
_self.events.trigger("documentChange", currentDocument);
});
DocumentManager
.on("pathDeleted", function(/*evt*/) {
_self.events.trigger("documentChange");
})
.on("documentRefreshed", function(/*evt*/) {
_self.events.trigger("documentChange");
})
.on("dirtyFlagChange", function(evt, doc) {
if (doc.isDirty) {
_self.events.trigger("documentChange");
}
});
ProjectManager
.on("beforeProjectClose", function () {
_self.events.trigger("documentChange");
});
}
/**
* Gets all references of whatever the editor cursor is located
*
* @param {CodeMirror} cm CodeMirror instance to initiate the processing from. This is where the
* editor cursor is extracted from
* @returns {Promise} Promise that will resolved with the references sorted by file
*/
TernReferences.prototype.getReferences = function(cm) {
cm = cm || this.cm;
this._cm = cm;
this._token = null;
if (!cm) {
return Promise.reject("Invalid CodeMirror instance");
}
this._token = cm.getTokenAt(cm.getCursor());
if (!this._token.string) {
this.events.trigger("references", this.ternProvider, {}, this._token.string);
return Promise.resolve();
}
// Set fragments processing to false because references can only be
// searched in full documents.
return this.ternProvider.query(cm, "refs", false)
.then(function(data) {
var perFile = {}, i, ref;
if (data) {
for (i = 0; i < data.refs.length; ++i) {
ref = data.refs[i];
(perFile[ref.file] || (perFile[ref.file] = [])).push(ref);
}
}
this._matches = perFile;
this.events.trigger("references", this.ternProvider, perFile, this._token.string);
return perFile;
}.bind(this),
function(error) {
return error;
});
};
return TernReferences;
});