-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.js
80 lines (65 loc) · 2.4 KB
/
editor.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
var path = require('path');
var fs = require('fs');
// add our own builtins
require('./firedoc-helper.js').generateBuiltin();
var langTools = ace.require('ace/ext/language_tools');
var helper = require('./helper.js');
var UNKNOWN_FILE_NAME = path.join(__dirname, 'unknown.js');
var currentEdittingFile = UNKNOWN_FILE_NAME;
// whether current editting file content is changed
// true: when the file content is changed without saving
// false: when it is saved and there is not change after saving
var currentFileIsChanged = false;
// use esprima to parse the file and return completions
function computeCompletions(editor, session, pos, prefix) {
var offset = session.getDocument().positionToIndex(pos);
var EsprimaHelper = require('./esprima-helper.js');
var esprimaHelper = new EsprimaHelper(currentEdittingFile);
var proposals = esprimaHelper.computeCompletions(session.getValue(), offset, prefix);
return proposals;
}
var myCompleter = {
getCompletions: function(editor, session, pos, prefix, callback) {
// if the pop up window is triggered by `.`, such as foo., then should disable other keyWordCompleters,
// because we know it wants to access attributes now
if (prefix === '')
helper.disableSystemCompleters();
else
helper.enableSystemCompleters();
var completions = [];
var proposals = computeCompletions(editor, session, pos, prefix);
for (var i in proposals) {
var proposal = proposals[i];
completions.push({
value: prefix + proposal.proposal,
description: proposal.description,
});
}
callback(null, completions);
},
getDocTooltip: function(selected) {
return selected.description;
},
// add this line to make foo. can trigger the pop-up window
identifierRegexps: [ /[a-zA-Z_0-9\$\-\u00A2-\uFFFF.]/ ]
};
// get a file path that is currently editted
function getEdittingFilePath() {
return currentEdittingFile;
}
function setEdittingFilePath(filePath) {
currentEdittingFile = filePath;
}
// create and initialize ace editor
var editor = helper.editor;
editor.setOptions({
enableLiveAutocompletion: true,
enableSnippets: true,
});
editor.setTheme('ace/theme/monokai');
editor.getSession().setMode('ace/mode/javascript');
editor.$blockScrolling = Infinity;
editor.completers = [myCompleter, langTools.keyWordCompleter, langTools.snippetCompleter];
editor.on('change', function(e){
currentFileIsChanged = true;
});