This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.js
171 lines (154 loc) · 5.32 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
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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
/*global define, brackets, $, window */
define(function(require, exports, module) {
"use strict";
var AppInit = brackets.getModule("utils/AppInit"),
CodeHintManager = brackets.getModule("editor/CodeHintManager"),
WPfuncHint = require("text!./result.txt");
var lastLine,
lastFileName,
cachedMatches,
cachedWordList,
tokenDefinition,
currentTokenDefinition;
/**
* @constructor
*/
function WPhints() {
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.
*/
WPhints.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 = WPfuncHint.match(this.tokenDefinition);
this.cachedWordList = [];
rawWordList.forEach(word => {
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;
};
/**
* @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.
*/
WPhints.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 = this.cachedWordList.filter(
word =>
typeof word === "string" &&
word.indexOf(symbolBeforeCursorArray[0]) == 0
);
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.
*/
WPhints.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);
if (hint.slice(-1) === ")") {
cursor = this.editor.getCursorPos();
cursor.ch--;
this.editor.setCursorPos(cursor);
}
console.log(
"hint: " +
hint +
" | lineBeginning: " +
lineBeginning.line +
", " +
lineBeginning.ch +
" | textBeforeCursor: " +
textBeforeCursor +
" | indexOfTheSymbol: " +
indexOfTheSymbol +
" | replaceStart: " +
replaceStart.line +
", " +
replaceStart.ch
);
return false;
};
AppInit.appReady(function() {
var wpHints = new WPhints();
CodeHintManager.registerHintProvider(wpHints, ["php"], 10);
});
});