-
Notifications
You must be signed in to change notification settings - Fork 20
/
TernProvider.js
227 lines (173 loc) · 6.08 KB
/
TernProvider.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* Ternific Copyright (c) 2014 Miguel Castillo.
*
* Licensed under MIT
*/
define(function (require /*, exports, module*/) {
"use strict";
var CodeMirror = brackets.getModule("thirdparty/CodeMirror2/lib/codemirror");
var fileUtils = brackets.getModule("file/FileUtils");
var logger = require("Logger").factory("TernProvider");
var Promise = require("node_modules/spromise/dist/spromise.min");
var reportError = require("reportError");
var fileReader = require("fileReader");
var localServer = require("LocalServer");
var Repository = require("Repository");
/**
* @constructor
*/
function TernProvider() {
this.documentRepository = new Repository();
this.currentDocument = null;
}
TernProvider.prototype.query = function(cm, settings, allowFragments) {
return this.tern.query(cm, settings, allowFragments);
};
TernProvider.prototype.registerDocument = function(cm, file) {
var _self = this;
var docMeta = documentMetaFactory(file.name, cm.getDoc()).create({file: file});
this.tern.clear();
this.currentDocument = docMeta;
providerExtensions(this).addDocument(docMeta);
serverExtensions(this).addDocument(docMeta);
this.tern.setDocuments(this.documentRepository.items());
this.tern.setCurrentDocument(docMeta);
this.tern.loadSettings(file.parentPath);
docMeta._trackChange = function(cm, change) {
_self.tern.trackChange(docMeta.doc, change);
};
CodeMirror.on(docMeta.doc, "change", docMeta._trackChange);
};
TernProvider.prototype.unregisterDocument = function(cm) {
var docMeta = this.documentRepository.find({"doc": cm.getDoc()});
if (docMeta) {
if (docMeta.doc && docMeta._trackChange) {
CodeMirror.off(docMeta.doc, "change", docMeta._trackChange);
docMeta._trackChange = null;
}
this.documentRepository.remove(docMeta);
}
//
// TODO: Provide a way to clear all loaded documents. Useful when
// switching projects.
//
};
/**
* Will read file from storage and then pushes the content to the tern server.
*/
TernProvider.prototype.loadDocument = function(filePath) {
logger.log("loadDocument", filePath);
// Get the extension and make sure we have an extension before loading the file.
var extension = fileUtils.getFileExtension(filePath);
if (!extension) {
filePath += ".js";
}
//
// TODO: Figure out a way to pass in full paths. This would work
// well only if name resolution of modules was properly done with
// something like browser-resolve or amd-resolve.
//
// For now we will just do a simple heuristic based on whether the
// file is relative or absolute.
//
filePath = fileReader.isAbsolute(filePath) ? filePath : (this.currentDocument.file.parentPath + filePath);
return fileReader
.fromDirectory(filePath)
.then(_read, reportError)
.then(documentMetaFactory(filePath).create, reportError)
.then(providerExtensions(this).addDocument, reportError);
//.then(serverExtensions(this).addDocument, reportError);
};
/**
* Get a document. It will first look in the repository, otherwise it loads it
* from storage.
*/
TernProvider.prototype.getDocument = function(name) {
var docMeta = this.documentRepository.getByName(name);
if (docMeta) {
return Promise.resolve(docMeta);
}
return this.loadDocument(name);
};
function _read(stream) {
return stream.read();
}
function documentMetaFactory(name, doc) {
function create(data) {
return {
file: data.file,
name: name, //data.fullPath,
doc: doc || (new CodeMirror.Doc(data.content, "javascript")),
changed: null
};
}
return {
create: create
};
}
function providerExtensions(provider) {
/**
* Function that adds a document to the document repository
*
* @param {object} docMeta - Document meta object with information about
* the document.
*/
function addDocument(docMeta) {
if (!provider.documentRepository.getByName(docMeta.name)) {
provider.documentRepository.add(docMeta);
}
return docMeta;
}
/**
* Function that reads a document from storage
*/
function getDocument(name) {
return provider.getDocument(name);
}
return {
addDocument: addDocument,
getDocument: getDocument
};
}
function serverExtensions(provider) {
/**
* Function that adds a document to the tern server.
*
* @param {object} docMeta - Document meta object with information about
* the document.
*/
function addDocument(docMeta) {
provider.tern.addFile(docMeta.name, docMeta.doc.getValue());
return docMeta;
}
return {
addDocument: addDocument
};
}
/**
* Interface to operate against a local instance of tern
*/
function LocalProvider() {
TernProvider.apply(this, arguments);
var _self = this;
function setServer(tern) {
_self.tern = tern;
return _self;
}
var deferred = localServer
.create(providerExtensions(this))
.then(setServer);
this.onReady = deferred.promise.done.bind(deferred);
}
LocalProvider.prototype = new TernProvider();
LocalProvider.prototype.constructor = LocalProvider;
/**
* Factory method to create providers
*/
function createProvider() {
return (new LocalProvider());
}
return {
create: createProvider
};
});