forked from Wikunia/brackets-FuncDocr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhints.js
232 lines (203 loc) · 8.42 KB
/
hints.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
228
229
230
231
232
// =========================================================================
// Hint Manager
// =========================================================================
define(['text!definitions/default.json',
'text!definitions/js.json',
'text!definitions/php.json'],
function (defaultDef, javascriptDef, phpDef) {
/**
* Parse Documentation Definitions, used to populate tag suggestions
*/
var DOC_DEFINITIONS = {
default: JSON.parse(defaultDef),
javascript: JSON.parse(javascriptDef),
php: JSON.parse(phpDef)
};
DOC_DEFINITIONS.coffeescript = DOC_DEFINITIONS.javascript;
var DOCBLOCK_STATIC_HINTS = /(\[\[[^\]]+\<[^\]]+\>\]\])/;
var DOCBLOCK_FIELD = /(\[\[[^\]]+\]\])/;
/**
* DocrHint constructor
* @param {Object} importFuncs functions that will be used in this class and must be imported
*/
function DocrHint(importFuncs) {
var docrHintThis = this;
$.each(importFuncs, function (key, func) {
docrHintThis[key] = func;
});
docrHintThis.docDefinitions = DOC_DEFINITIONS;
}
/**
* Checks, if it is possible to give hints inside the current docblock.
* @param {editor} editor current brackets editor
* @param {String} implicitChar implicit character
* @returns {Boolean} true for has hints otherwise false
*/
DocrHint.prototype.hasHints = function (editor, implicitChar) {
this.editor = editor;
this.selection = editor.getSelectedText();
this.pos = editor.getCursorPos();
this.insideDocPos = this.insideDocBlock(this.pos);
if (this.insideDocPos) {
this.pos = editor.getCursorPos();
switch (this.selection) {
case "[[Type]]":
this.implicitChar = "[[Type]]";
return true;
case "[[Link]]":
this.implicitChar = "[[Link]]";
return true;
default:
if (implicitChar == '@') {
this.removeSelection = true;
this.implicitChar = "@";
return true;
} else if (DOCBLOCK_STATIC_HINTS.exec(this.selection)) {
this.removeSelection = true;
this.implicitChar = this.selection;
return true;
}
}
}
return false;
};
/**
* Get the hints for a selection
* uses {@link removeWrongHints removeWrongHints}
* @param {String} implicitChar implicit character
* @returns {Object} hintManager object
*/
DocrHint.prototype.getHints = function () {
var hints;
this.match = this.editor.document.getRange(this.pos, this.editor.getCursorPos());
this.hintText = '';
this.removeSelection = true;
this.boolSetSelection = false;
this.deleteFirstNChars = 0;
switch (this.implicitChar) {
case "[[Type]]":
hints = [
"Number",
"String",
"Boolean",
"Array",
"Object",
"RegExp",
"Function"
];
break;
case "[[Link]]":
var functionList = this.createFunctionList();
var functionSignature = this.getFunctionCodeTypes(this.editor, {
line: this.insideDocPos.end,
ch: 0
}, []);
var bestFuncs = [];
var otherFuncs = [];
for (var i = 0; i < functionList.length; i++) {
var regexFuncTest = new RegExp('\[^a-zA-Z0-9\]' + functionList[i].name + '\\(');
// remove the function name
functionSignature.code = functionSignature.code.substr(functionSignature.code.indexOf('{'));
if (regexFuncTest.test(functionSignature.code)) {
bestFuncs.push(functionList[i].name);
} else {
otherFuncs.push(functionList[i].name);
}
}
hints = bestFuncs.concat(otherFuncs);
break;
case "@":
var line = this.editor.document.getRange({
line: this.pos.line,
ch: 0
}, this.pos);
hints = [];
var match = /{\s*@$/.exec(line);
if (match) {
this.deleteFirstNChars = 2;
} else {
this.deleteFirstNChars = 1;
}
var language = this.editor.getLanguageForSelection().getId();
var docs = this.docDefinitions;
var definition;
if (docs[language] === undefined) {
definition = docs.default;
} else {
definition = docs[language];
}
for (var tag in definition.tags) {
hints.push(definition.tags[tag]);
}
this.boolSetSelection = true;
break;
default:
// Here we have static suggestions included in the tag (i.e. [[Access<protected|public|private>]])
var hintsStartIndex = this.implicitChar.indexOf('<');
var hintsStopIndex = this.implicitChar.indexOf('>');
if (hintsStartIndex !== -1 && hintsStopIndex !== -1 && hintsStopIndex > hintsStartIndex) {
var hintsRaw = this.implicitChar.substring(hintsStartIndex+1, hintsStopIndex);
hints = hintsRaw.split('|');
} else {
hints = [];
}
break;
}
this.removeSelection = (this.removeSelection && this.match !== '') ? false : this.removeSelection;
hints = this.removeWrongHints(hints);
return {
hints: hints,
match: this.match,
selectInitial: true,
handleWideResults: false
};
};
/**
* Remove hints that doesn't match the current this.match
* @param {Array} hints hints that are availabe
* @returns {Array} hints that matches this.match
*/
DocrHint.prototype.removeWrongHints = function (hints) {
var result = [];
for (var i = 0; i < hints.length; i++) {
if (hints[i].indexOf(this.match) >= 0) {
result.push(hints[i]);
}
}
return result;
};
/**
* Inserts the hint
*/
DocrHint.prototype.insertHint = function (hint) {
// Document objects represent file contents
var currentDoc = this.editor.document;
// Where the range end that should be replaced
var start = {
line: this.pos.line,
ch: this.pos.ch - this.deleteFirstNChars
};
var end = {
line: this.pos.line,
ch: this.pos.ch + ((this.removeSelection) ? this.selection.length : this.match.length)
};
// Add some text in our document
currentDoc.replaceRange(hint, start, end);
var hintContainsField = DOCBLOCK_FIELD.exec(hint);
if (this.boolSetSelection && hintContainsField) {
var match = /\[\[[a-z]+\]\]/i.exec(hint);
if (match) {
var selectionEnd = this.pos.ch - this.deleteFirstNChars + match.index;
var selectionStart = selectionEnd + match[0].length;
this.setSelection(this.editor, {
line: this.pos.line,
ch: selectionStart
}, {
line: this.pos.line,
ch: selectionEnd
});
}
}
};
return DocrHint;
});