-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathAceSnippets.hx
133 lines (129 loc) · 4.25 KB
/
AceSnippets.hx
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
package ace;
import ace.AceWrapCompleter;
import ace.extern.AceAutoCompleteCb;
import ace.extern.AceAutoCompleteItems;
import ace.extern.AcePos;
import ace.extern.AceSession;
import ace.extern.AceToken;
import ace.extern.AceTokenType;
import electron.FileSystem;
import electron.FileWrap;
import haxe.DynamicAccess;
import haxe.extern.EitherType;
import tools.Dictionary;
import ace.AceWrap;
import ace.extern.AceAutoCompleter;
import tools.JsTools;
import js.html.Console;
using tools.NativeString;
/**
* ...
* @author YellowAfterlife
*/
class AceSnippets {
static var langTools:AceLanguageTools;
static var manager:AceSnippetManager;
static var map:Dictionary<AceSnippetFile> = new Dictionary();
public static var completer:AceAutoCompleter;
static inline function getPath(mode:String):String {
return "ace/snippets/" + mode;
}
public static function getText(mode:String) {
var r:String = null;
if (FileSystem.canSync) try {
return FileSystem.readTextFileSync(FileWrap.userPath + "/snippets/" + mode + ".snippets");
} catch (_:Dynamic) return "";
var r = Main.window.localStorage.getItem(getPath(mode));
return r != null ? r : "";
}
public static function setText(mode:String, text:String):Void {
if (FileSystem.canSync) {
FileSystem.writeFileSync(FileWrap.userPath + "/snippets/" + mode + ".snippets", text);
} else {
Main.window.localStorage.setItem(getPath(mode), text);
}
reload(mode, text);
}
public static function reload(mode:String, ?text:String) {
var file = map[mode];
if (file != null) try {
if (text == null) text = getText(mode);
file.snippetText = text;
file.snippets = manager.parseSnippetFile(text);
manager.snippetMap.remove(mode);
manager.snippetNameMap.remove(mode);
manager.register(file.snippets, file.scope);
return true;
} catch (x:Dynamic) {
Console.error("Couldn't refresh snippets:", x);
}
return false;
}
public static function init() {
langTools = AceWrap.require("ace/ext/language_tools");
manager = AceWrap.require("ace/snippets").snippetManager;
for (mode in ["text", "gml", "gml_search", "shader"]) {
AceWrap.define("ace/snippets/" + mode, [
"require", "exports", "module",
], function(require:AceRequire, exports:AceExports, module:AceModule) {
var file:AceSnippetFile = exports;
map.set(mode, file);
file.snippetText = getText(mode);
file.scope = mode;
});
}
completer = langTools.snippetCompleter;
}
}
class AceSnippetCompleterProxy extends AceWrapCompleter {
public var completer:AceAutoCompleter;
public function new(
completer:AceAutoCompleter,
tokenFilterDictOrArray:EitherType<Dictionary<Bool>, Array<AceTokenType>>, not:Bool,
modeFilter:AceSession->Bool
) {
this.completer = completer;
super([], tokenFilterDictOrArray, not, modeFilter);
}
override public function getCompletions(
editor:AceEditor, session:AceSession, pos:AcePos, prefix:String, callback:AceAutoCompleteCb
):Void {
if (!ui.Preferences.current.compFilterSnippets || !modeFilter(session)) { // non-GML
completer.getCompletions(editor, session, pos, prefix, callback);
return;
}
//
var ml = procMinLength();
if (prefix.length < ml || !modeFilter(session)) {
callback(null, AceWrapCompleter.noItems);
return;
}
//
var tk:AceToken = session.getTokenAtPos(pos);
var tkf:Bool = tokenFilter.exists(JsTools.nca(tk, tk.type));
if (!tkf && tokenFilterComment && tk.type.startsWith("comment")) tkf = true;
if (tkf != tokenFilterNot) {
completer.getCompletions(editor, session, pos, prefix, callback);
} else callback(null, AceWrapCompleter.noItems);
}
}
extern class AceLanguageTools {
public var textCompleter:Dynamic;
public var keyWordCompleter:Dynamic;
public var snippetCompleter:Dynamic;
public function addCompleter(comp:Dynamic):Void;
}
extern class AceSnippetManager {
public var snippetMap:DynamicAccess<Array<AceSnippetItem>>;
public var snippetNameMap:DynamicAccess<DynamicAccess<AceSnippetItem>>;
public var files:DynamicAccess<Dynamic>;
public function parseSnippetFile(text:String):Array<AceSnippetItem>;
public function register(snippets:Array<AceSnippetItem>, scope:String):Void;
}
extern class AceSnippetFile {
public var snippetText:String;
public var snippets:Array<AceSnippetItem>;
public var scope:String;
}
extern class AceSnippetItem {
}