diff options
author | Aleksey Kladov <[email protected]> | 2018-08-10 23:04:09 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-10 23:04:09 +0100 |
commit | f99551f46b1583ed43751e4d26fe76a0b913eb5f (patch) | |
tree | f0ab26d58c5bfd7237eb2f0117fcc141a1c3a5f4 /code/src/main.ts | |
parent | 9863b9161d8d702848516be70c5c8161b7f382e8 (diff) |
reorganize
Diffstat (limited to 'code/src/main.ts')
-rw-r--r-- | code/src/main.ts | 208 |
1 files changed, 0 insertions, 208 deletions
diff --git a/code/src/main.ts b/code/src/main.ts deleted file mode 100644 index 72bef7061..000000000 --- a/code/src/main.ts +++ /dev/null | |||
@@ -1,208 +0,0 @@ | |||
1 | 'use strict' | ||
2 | import * as vscode from 'vscode' | ||
3 | |||
4 | const backend = require("../../native") | ||
5 | |||
6 | let docToSyntax; | ||
7 | |||
8 | let uris = { | ||
9 | syntaxTree: vscode.Uri.parse('libsyntax-rust://syntaxtree') | ||
10 | } | ||
11 | |||
12 | |||
13 | export function activate(context: vscode.ExtensionContext) { | ||
14 | let textDocumentContentProvider = new TextDocumentContentProvider() | ||
15 | let dispose = (disposable) => { | ||
16 | context.subscriptions.push(disposable); | ||
17 | } | ||
18 | |||
19 | let registerCommand = (name, f) => { | ||
20 | dispose(vscode.commands.registerCommand(name, f)) | ||
21 | } | ||
22 | |||
23 | docToSyntax = documentToFile(context.subscriptions, () => { | ||
24 | let emitter = textDocumentContentProvider.eventEmitter | ||
25 | emitter.fire(uris.syntaxTree) | ||
26 | let syntax = activeSyntax() | ||
27 | setHighlights(vscode.window.activeTextEditor, syntax.highlight()) | ||
28 | }) | ||
29 | |||
30 | |||
31 | dispose(vscode.workspace.registerTextDocumentContentProvider( | ||
32 | 'libsyntax-rust', | ||
33 | textDocumentContentProvider | ||
34 | )) | ||
35 | |||
36 | registerCommand('libsyntax-rust.syntaxTree', () => openDoc(uris.syntaxTree)) | ||
37 | registerCommand('libsyntax-rust.extendSelection', () => { | ||
38 | let editor = vscode.window.activeTextEditor | ||
39 | let file = activeSyntax() | ||
40 | if (editor == null || file == null) return | ||
41 | editor.selections = editor.selections.map((s) => { | ||
42 | let range = file.extendSelection(s) | ||
43 | if (range == null) return null | ||
44 | return new vscode.Selection(range.start, range.end) | ||
45 | }) | ||
46 | }) | ||
47 | } | ||
48 | |||
49 | export function deactivate() { } | ||
50 | |||
51 | export class Syntax { | ||
52 | imp; | ||
53 | doc: vscode.TextDocument; | ||
54 | |||
55 | constructor(imp, doc: vscode.TextDocument) { | ||
56 | this.imp = imp | ||
57 | this.doc = doc | ||
58 | } | ||
59 | |||
60 | syntaxTree(): string { return this.imp.syntaxTree() } | ||
61 | highlight(): Array<[number, number, string]> { return this.imp.highlight() } | ||
62 | extendSelection(range: vscode.Range): vscode.Range { | ||
63 | let range_ = fromVsRange(this.doc, range); | ||
64 | let extRange = this.imp.extendSelection(range_[0], range_[1]); | ||
65 | return toVsRange(this.doc, extRange); | ||
66 | } | ||
67 | } | ||
68 | |||
69 | |||
70 | function activeDoc() { | ||
71 | return vscode.window.activeTextEditor.document | ||
72 | } | ||
73 | |||
74 | function activeSyntax(): Syntax { | ||
75 | let doc = activeDoc() | ||
76 | if (doc == null) return null | ||
77 | return docToSyntax(doc) | ||
78 | } | ||
79 | |||
80 | async function openDoc(uri: vscode.Uri) { | ||
81 | let document = await vscode.workspace.openTextDocument(uri) | ||
82 | return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true) | ||
83 | } | ||
84 | |||
85 | function documentToFile(disposables: vscode.Disposable[], onChange) { | ||
86 | let docs = {} | ||
87 | function update(doc: vscode.TextDocument, file) { | ||
88 | let key = doc.uri.toString() | ||
89 | if (file == null) { | ||
90 | delete docs[key] | ||
91 | } else { | ||
92 | docs[key] = file | ||
93 | } | ||
94 | onChange(doc) | ||
95 | } | ||
96 | function get(doc: vscode.TextDocument) { | ||
97 | return docs[doc.uri.toString()] | ||
98 | } | ||
99 | |||
100 | function isKnownDoc(doc: vscode.TextDocument) { | ||
101 | return doc.fileName.endsWith('.rs') | ||
102 | } | ||
103 | |||
104 | function createFile(text: String) { | ||
105 | console.time("parsing") | ||
106 | let res = new backend.RustFile(text); | ||
107 | console.timeEnd("parsing") | ||
108 | return res | ||
109 | } | ||
110 | |||
111 | vscode.workspace.onDidChangeTextDocument((event: vscode.TextDocumentChangeEvent) => { | ||
112 | let doc = event.document | ||
113 | if (!isKnownDoc(event.document)) return | ||
114 | update(doc, null) | ||
115 | }, null, disposables) | ||
116 | |||
117 | vscode.workspace.onDidOpenTextDocument((doc: vscode.TextDocument) => { | ||
118 | if (!isKnownDoc(doc)) return | ||
119 | update(doc, createFile(doc.getText())) | ||
120 | }, null, disposables) | ||
121 | |||
122 | vscode.workspace.onDidCloseTextDocument((doc: vscode.TextDocument) => { | ||
123 | update(doc, null) | ||
124 | }, null, disposables) | ||
125 | |||
126 | return (doc: vscode.TextDocument) => { | ||
127 | if (!isKnownDoc(doc)) return null | ||
128 | |||
129 | if (!get(doc)) { | ||
130 | update(doc, createFile(doc.getText())) | ||
131 | } | ||
132 | let imp = get(doc) | ||
133 | return new Syntax(imp, doc) | ||
134 | } | ||
135 | } | ||
136 | |||
137 | export class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { | ||
138 | public eventEmitter = new vscode.EventEmitter<vscode.Uri>() | ||
139 | public syntaxTree: string = "Not available" | ||
140 | |||
141 | public provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult<string> { | ||
142 | let syntax = activeSyntax() | ||
143 | if (syntax == null) return | ||
144 | if (uri.toString() == uris.syntaxTree.toString()) { | ||
145 | return syntax.syntaxTree() | ||
146 | } | ||
147 | } | ||
148 | |||
149 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
150 | return this.eventEmitter.event | ||
151 | } | ||
152 | } | ||
153 | |||
154 | const decorations = (() => { | ||
155 | const decor = (obj) => vscode.window.createTextEditorDecorationType({ color: obj }) | ||
156 | return { | ||
157 | background: decor("#3F3F3F"), | ||
158 | error: vscode.window.createTextEditorDecorationType({ | ||
159 | borderColor: "red", | ||
160 | borderStyle: "none none dashed none", | ||
161 | }), | ||
162 | comment: decor("#7F9F7F"), | ||
163 | string: decor("#CC9393"), | ||
164 | keyword: decor("#F0DFAF"), | ||
165 | function: decor("#93E0E3"), | ||
166 | parameter: decor("#94BFF3"), | ||
167 | builtin: decor("#DD6718"), | ||
168 | text: decor("#DCDCCC"), | ||
169 | attribute: decor("#BFEBBF"), | ||
170 | literal: decor("#DFAF8F"), | ||
171 | } | ||
172 | })() | ||
173 | |||
174 | function setHighlights( | ||
175 | editor: vscode.TextEditor, | ||
176 | highlihgs: Array<[number, number, string]> | ||
177 | ) { | ||
178 | let byTag = {} | ||
179 | for (let tag in decorations) { | ||
180 | byTag[tag] = [] | ||
181 | } | ||
182 | |||
183 | for (let [start, end, tag] of highlihgs) { | ||
184 | if (!byTag[tag]) { | ||
185 | console.log(`unknown tag ${tag}`) | ||
186 | continue | ||
187 | } | ||
188 | let range = toVsRange(editor.document, [start, end]) | ||
189 | byTag[tag].push(range) | ||
190 | } | ||
191 | |||
192 | for (let tag in byTag) { | ||
193 | let dec = decorations[tag] | ||
194 | let ranges = byTag[tag] | ||
195 | editor.setDecorations(dec, ranges) | ||
196 | } | ||
197 | } | ||
198 | |||
199 | export function toVsRange(doc: vscode.TextDocument, range: [number, number]): vscode.Range { | ||
200 | return new vscode.Range( | ||
201 | doc.positionAt(range[0]), | ||
202 | doc.positionAt(range[1]), | ||
203 | ) | ||
204 | } | ||
205 | |||
206 | function fromVsRange(doc: vscode.TextDocument, range: vscode.Range): [number, number] { | ||
207 | return [doc.offsetAt(range.start), doc.offsetAt(range.end)] | ||
208 | } | ||