-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
146 lines (131 loc) · 4.74 KB
/
main.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
define(function (require, exports, module) {
"use strict";
var AppInit = brackets.getModule("utils/AppInit"),
CodeHintManager = brackets.getModule("editor/CodeHintManager"),
LanguageManager = brackets.getModule("language/LanguageManager"),
ONfuncHint = require('text!on.txt');
var lastLine,
lastFileName,
cachedMatches,
cachedWordList,
tokenDefinition,
currentTokenDefinition;
/**
* @constructor
*/
function ONhints() {
this.lastLine = 0;
this.lastFileName = "";
this.cachedMatches = [];
this.cachedWordList = [];
this.tokenDefinition = /[a-zA-Z][(_a-zA-Z0-9$,.';_ )].{2,}/g;
this.currentTokenDefinition = /[a-zA-Z][a-zA-Z0-9_]+$/g;
}
/**
*
* @param {Editor} editor
* A non-null editor object for the active window.
*
* @param {String} implicitChar
* Either null, if the hinting request was explicit, or a single character
* that represents the last insertion and that indicates an implicit
* hinting request.
*
* @return {Boolean}
* Determines whether the current provider is able to provide hints for
* the given editor context and, in case implicitChar is non- null,
* whether it is appropriate to do so.
*/
ONhints.prototype.hasHints = function (editor, implicitChar) {
this.editor = editor;
var cursor = this.editor.getCursorPos();
// if it is not the same line as the last input - rebuild word list
if(cursor.line != this.lastLine){
var rawWordList = ONfuncHint.match(this.tokenDefinition);
this.cachedWordList = [];
for(var i in rawWordList){
var word = rawWordList[i]; if(this.cachedWordList.indexOf(word)==-1){
this.cachedWordList.push(word);
}
}
}
this.lastLine = cursor.line;
// if has entered more than 2 characters - start completion
var lineBeginning = {line:cursor.line,ch:0};
var textBeforeCursor = this.editor.document.getRange(lineBeginning, cursor);
var symbolBeforeCursorArray = textBeforeCursor.match(this.currentTokenDefinition);
if(symbolBeforeCursorArray){
// find if the half-word inputed is in the list
for(var i in this.cachedWordList){
if(this.cachedWordList[i].indexOf(symbolBeforeCursorArray[0])==0){
return true;
}
}
}
return false;
};
/**
* Returns a list of availble CSS propertyname or -value hints if possible for the current editor context.
*
* @param {Editor} implicitChar
* Either null, if the hinting request was explicit, or a single character
* that represents the last insertion and that indicates an implicit
* hinting request.
*
* @return {jQuery.Deferred|{
* hints: Array.<string|jQueryObject>,
* match: string,
* selectInitial: boolean,
* handleWideResults: boolean}}
* Null if the provider wishes to end the hinting session. Otherwise, a
* response object that provides:
* 1. a sorted array hints that consists of strings
* 2. a string match that is used by the manager to emphasize matching
* substrings when rendering the hint list
* 3. a boolean that indicates whether the first result, if one exists,
* should be selected by default in the hint list window.
* 4. handleWideResults, a boolean (or undefined) that indicates whether
* to allow result string to stretch width of display.
*/
ONhints.prototype.getHints = function (implicitChar) {
var cursor = this.editor.getCursorPos();
var lineBeginning = {line:cursor.line,ch:0};
var textBeforeCursor = this.editor.document.getRange(lineBeginning, cursor);
var symbolBeforeCursorArray = textBeforeCursor.match(this.currentTokenDefinition);
var hintList = [];
for(var i in this.cachedWordList){
if(this.cachedWordList[i].indexOf(symbolBeforeCursorArray[0])==0){
hintList.push(this.cachedWordList[i]);
}
}
return {
hints: hintList,
match: symbolBeforeCursorArray[0],
selectInitial: true,
handleWideResults: false
};
};
/**
* Complete the word
*
* @param {String} hint
* The hint to be inserted into the editor context.
*
* @return {Boolean}
* Indicates whether the manager should follow hint insertion with an
* additional explicit hint request.
*/
ONhints.prototype.insertHint = function (hint) {
var cursor = this.editor.getCursorPos();
var lineBeginning = {line:cursor.line,ch:0};
var textBeforeCursor = this.editor.document.getRange(lineBeginning, cursor);
var indexOfTheSymbol = textBeforeCursor.search(this.currentTokenDefinition);
var replaceStart = {line:cursor.line,ch:indexOfTheSymbol};
this.editor.document.replaceRange(hint, replaceStart, cursor);
return false;
};
AppInit.appReady(function () {
var ONHints = new ONhints();
CodeHintManager.registerHintProvider(ONHints, ["php"], 10);
});
});