forked from MiguelCastillo/Brackets-Ternific
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTernDemo.js
158 lines (135 loc) · 5.47 KB
/
TernDemo.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
/**
* Ternific Copyright (c) 2014 Miguel Castillo.
* Fork by David Sánchez i Gregori
* Licensed under MIT
*/
define(function(require, exports, module) {
"use strict";
var spromise = require("libs/js/spromise");
var Timer = require("Timer");
var CodeMirror = brackets.getModule("thirdparty/CodeMirror2/lib/codemirror");
var Pos = CodeMirror.Pos, bigDoc = 250, curDoc = null, docs = [], server = null;
function trackChange(doc, change) {
for (var i = 0; i < docs.length; ++i) {var data = docs[i]; if (data.doc == doc) break;}
var changed = data.changed;
if (changed == null)
data.changed = changed = {from: change.from.line, to: change.from.line};
var end = change.from.line + (change.text.length - 1);
if (change.from.line < changed.to) changed.to = changed.to - (change.to.line - end);
if (end >= changed.to) changed.to = end + 1;
if (changed.from > change.from.line) changed.from = change.from.line;
if (doc.lineCount() > bigDoc && change.to - changed.from > 100) setTimeout(function() {
if (data.changed && data.changed.to - data.changed.from > 100) sendDoc(data);
}, 100);
}
function getFragmentAround(cm, start, end) {
var minIndent = null, minLine = null, endLine, tabSize = cm.getOption("tabSize");
for (var p = start.line - 1, min = Math.max(0, p - 50); p >= min; --p) {
var line = cm.getLine(p), fn = line.search(/\bfunction\b/);
if (fn < 0) continue;
var indent = CodeMirror.countColumn(line, null, tabSize);
if (minIndent != null && minIndent <= indent) continue;
if (cm.getTokenAt(Pos(p, fn + 1)).type != "keyword") continue;
minIndent = indent;
minLine = p;
}
if (minLine == null) minLine = min;
var max = Math.min(cm.lastLine(), start.line + 20);
if (minIndent == null || minIndent == CodeMirror.countColumn(cm.getLine(start.line), null, tabSize))
endLine = max;
else for (endLine = start.line + 1; endLine < max; ++endLine) {
var indent = CodeMirror.countColumn(cm.getLine(endLine), null, tabSize);
if (indent <= minIndent) break;
}
var from = Pos(minLine, 0);
return {type: "part",
name: curDoc.name,
offsetLines: from.line,
text: cm.getRange(from, Pos(endLine, 0))};
}
function displayError(err) {
}
function incLine(off, pos) { return Pos(pos.line + off, pos.ch); }
function buildRequest(cm, query, allowFragments) {
var files = [], offsetLines = 0;
if (typeof query == "string") query = {type: query};
query.lineCharPositions = true;
if (query.end == null) {
query.end = cm.getCursor("end");
if (cm.somethingSelected())
query.start = cm.getCursor("start");
}
var startPos = query.start || query.end;
if (curDoc.changed) {
if (cm.lineCount() > bigDoc && allowFragments !== false &&
curDoc.changed.to - curDoc.changed.from < 100 &&
curDoc.changed.from <= startPos.line && curDoc.changed.to > query.end.line) {
files.push(getFragmentAround(cm, startPos, query.end));
query.file = "#0";
var offsetLines = files[0].offsetLines;
if (query.start != null) query.start = incLine(-offsetLines, query.start);
query.end = incLine(-offsetLines, query.end);
} else {
files.push({type: "full",
name: curDoc.name,
text: cm.getValue()});
query.file = curDoc.name;
curDoc.changed = null;
}
} else {
query.file = curDoc.name;
}
for (var i = 0; i < docs.length; ++i) {
var doc = docs[i];
if (doc.changed && doc != curDoc) {
files.push({type: "full", name: doc.name, text: doc.doc.getValue()});
doc.changed = null;
}
}
return {query: query, files: files};
}
function sendDoc(doc) {
server.request({files: [{type: "full", name: doc.name, text: doc.doc.getValue()}]}, function(error) {
if (error) return displayError(error);
else doc.changed = null;
});
}
function ternApi(_server) {
server = _server;
return {
server: _server,
trackChange: trackChange,
buildRequest: buildRequest,
query: function(cm, query, allowFragments){
if(!curDoc) {
return "";
}
var ternRequest = buildRequest.apply(ternApi, arguments);
ternRequest.query = $.extend({
filter: true, // Results will be pretty large if we don't filter stuff out
sort: true,
depths: true,
guess: true,
origins: false,
docs: false,
expandWordForward: false
}, ternRequest.query);
return spromise(function(resolve) {
server.request(ternRequest).done(function(result) {
resolve(result, ternRequest);
});
}).promise;
},
setCurrentDocument: function(_curDoc){
curDoc = _curDoc;
},
setServer: function(_server){
//server = _server;
},
setDocs: function(_docs){
docs = _docs;
}
};
}
return ternApi;
});