forked from MiguelCastillo/Brackets-Ternific
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTernHints.js
111 lines (88 loc) · 3.08 KB
/
TernHints.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
/**
* 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 HintHelper = require("HintHelper");
function TernHints(ternProvider) {
var _self = this;
_self.ternProvider = ternProvider;
}
TernHints.prototype.query = function( ) {
var cm = this._cm;
return this.ternProvider.query(cm, {
caseInsensitive: true,
type: "completions",
types: true,
docs: false,
filter: this.newSession !== true
})
.then(function(result, query) {
return {
text: query.doc.cm.getDoc().getRange(result.start, result.end),
result: result,
query: query,
cm: cm
};
},
function(error) {
return error;
});
};
/**
* Utility function that helps determine if the the parameter _char
* is one that we can start or continue hinting on. There are some
* characters that are not hintable.
*/
TernHints.prototype.canHint = function (_char, cm /*, file*/) {
var _self = this;
_self.newSession = cm !== undefined;
// Whenever cm is not not undefined, we are in a hinting session.
// Every time cm is not undefined, the HintManager is trying to
// start a session.
if (_self.newSession) {
_self._cm = cm;
}
cm = _self._cm;
if (!_char || HintHelper.maybeIdentifier(_char)) {
var cursor = cm.getCursor();
var token = cm.getTokenAt(cursor);
return HintHelper.hintable(token);
}
delete _self._cm;
return false;
};
/**
* Inserts a hint from the hints object.
* The idea here is that getHints is called first to build a valid list of hints.
* Then select one of the hint items from hints returned by getHints. The selected
* hint and the hints object are the parameters you supply in insertHint.
*
* 1. call getHints.
* 2. select a hint from the hints returned by getHints
* 3. feed the selected hint and the list of hints back in insertHint
*/
TernHints.prototype.insertHint = function( hint, hints ) {
if ( !hint || !hints ) {
throw new TypeError("Must provide valid hint and hints object as they are returned by calling getHints");
}
hints.cm.getDoc().replaceRange(hint.name, hints.result.start, hints.result.end);
};
/**
* Interface to ask tern for hints. You can pass in any particular code
* mirror instance you want to operate on or let ternManager pick the last
* code mirror instance that was registered via register.
*/
TernHints.prototype.getHints = function( ) {
var _self = this;
var cm = _self._cm;
if ( !cm ){
return spromise.rejected();
}
return this.query();
};
return TernHints;
});