-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.ts
221 lines (198 loc) · 6.88 KB
/
extension.ts
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* --------------------------------------------------------------------------------------------
* TEASPN license:
* Copyright (c) TEASPN developers. All rights reserved.
* Licensed under the MIT License.
* Original license:
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
import * as net from 'net';
import * as vscode from 'vscode';
import { workspace, window } from 'vscode';
import { join } from 'path';
import { LanguageClient,
LanguageClientOptions,
ServerOptions,
Trace,
RequestType,
TextDocumentIdentifier,
Range
} from 'vscode-languageclient';
import { PassThrough } from 'stream';
interface SyntaxHighlightParams {
textDocument: TextDocumentIdentifier
}
interface SyntaxHighlight {
range: Range
type: string
hoverMessage?: string
}
namespace SyntaxHighlightRequest {
export const type = new RequestType<SyntaxHighlightParams, SyntaxHighlight[] | null, void, void>('textDocument/syntaxHighlight');
}
const decorationTypes = {
'blue': vscode.window.createTextEditorDecorationType({
overviewRulerColor: 'blue',
overviewRulerLane: vscode.OverviewRulerLane.Right,
light: { color: 'darkblue' },
dark: { color: 'lightblue' }
}),
'red': vscode.window.createTextEditorDecorationType({
overviewRulerColor: 'red',
overviewRulerLane: vscode.OverviewRulerLane.Right,
light: { color: 'darkred' },
dark: { color: 'lightpink' }
}),
'coral': vscode.window.createTextEditorDecorationType({
overviewRulerColor: 'coral',
overviewRulerLane: vscode.OverviewRulerLane.Right,
light: { color: 'darkcoral' },
dark: { color: 'lightcoral' }
}),
'cyan': vscode.window.createTextEditorDecorationType({
overviewRulerColor: 'cyan',
overviewRulerLane: vscode.OverviewRulerLane.Right,
light: { color: 'darkcyan' },
dark: { color: 'lightcyan' }
}),
'green': vscode.window.createTextEditorDecorationType({
overviewRulerColor: 'green',
overviewRulerLane: vscode.OverviewRulerLane.Right,
light: { color: 'darkgreen' },
dark: { color: 'lightgreen' }
}),
'salmon': vscode.window.createTextEditorDecorationType({
overviewRulerColor: 'salmon',
overviewRulerLane: vscode.OverviewRulerLane.Right,
light: { color: 'darksalmon' },
dark: { color: 'lightsalmon' }
}),
'seagreen': vscode.window.createTextEditorDecorationType({
overviewRulerColor: 'seagreen',
overviewRulerLane: vscode.OverviewRulerLane.Right,
light: { color: 'darkseagreen' },
dark: { color: 'lightseagreen' }
}),
'yellow': vscode.window.createTextEditorDecorationType({
overviewRulerColor: 'yellow',
overviewRulerLane: vscode.OverviewRulerLane.Right,
light: { color: 'darkyellow' },
dark: { color: 'lightyellow' }
}),
'orange': vscode.window.createTextEditorDecorationType({
overviewRulerColor: 'orange',
overviewRulerLane: vscode.OverviewRulerLane.Right,
light: { color: 'darkorange' },
dark: { color: 'orange' }
}),
'gold': vscode.window.createTextEditorDecorationType({
overviewRulerColor: 'gold',
overviewRulerLane: vscode.OverviewRulerLane.Right,
light: { color: 'goldenrod' },
dark: { color: 'gold' }
}),
'lime': vscode.window.createTextEditorDecorationType({
overviewRulerColor: 'lime',
overviewRulerLane: vscode.OverviewRulerLane.Right,
light: { color: 'limegreen' },
dark: { color: 'mediumspringgreen' }
}),
'skyblue': vscode.window.createTextEditorDecorationType({
overviewRulerColor: 'skyblue',
overviewRulerLane: vscode.OverviewRulerLane.Right,
light: { color: 'deepskyblue' },
dark: { color: 'lightskyblue' }
})
// TODO: Add more decoration types
};
interface WorkspaceSearchExampleParams {
query: string;
}
interface ExampleInformation {
label: string;
description: string;
}
namespace WorkspaceSearchExampleRequest {
export const type = new RequestType<WorkspaceSearchExampleParams, ExampleInformation[] | null, void, void>('workspace/searchExample');
}
function startLangServer(command: string, args: string[], documentSelector: string[]): vscode.Disposable {
const serverOptions: ServerOptions = {
command,
args,
};
const clientOptions: LanguageClientOptions = {
documentSelector: documentSelector,
synchronize: {
configurationSection: "teaspn"
}
}
const client = new LanguageClient('teaspn', 'TEASPN client', serverOptions, clientOptions, true);
client.trace = Trace.Verbose;
// Add syntax highlighting
const callback = (event: vscode.TextDocumentChangeEvent): void => {
const document = event.document;
if (document.uri.scheme == "output")
return;
const params = { textDocument: { uri: document.uri.toString() }};
client.sendRequest(SyntaxHighlightRequest.type, params).then(
(response) => {
const typeToOptions: { [type: string]: any[] } = {};
for (let highlight of response) {
if (typeToOptions[highlight.type] == undefined)
typeToOptions[highlight.type] = [];
typeToOptions[highlight.type].push({range: highlight.range, hoverMessage: highlight.hoverMessage});
}
for (let type in typeToOptions) {
window.activeTextEditor.setDecorations(decorationTypes[type], typeToOptions[type]);
}
}
);
};
workspace.onDidChangeTextDocument(callback);
// Add example search command
vscode.commands.registerCommand('teaspn.searchExample', () => {
window.showInputBox().then((query) => {
if (query == undefined) return;
client.sendRequest(WorkspaceSearchExampleRequest.type, { query }).then(
(response) => {
window.showQuickPick(response,
{ matchOnDescription: true,
placeHolder: 'further filter results by typing in English or the language used for desription.',
canPickMany: true }).then(
(values) => {
console.log(values);
for (var value of values) {
vscode.window.showInformationMessage(value.label + ' ' + value.description);
}
});
});
});
});
return client.start();
}
function startLangServerTCP(port: number, documentSelector: string[]): vscode.Disposable {
const serverOptions: ServerOptions = function() {
return new Promise((resolve, reject) => {
var client = new net.Socket();
client.connect(port, "127.0.0.1", function() {
resolve({
reader: client,
writer: client
});
});
});
}
const clientOptions: LanguageClientOptions = {
documentSelector: documentSelector,
}
const client = new LanguageClient('teaspn', serverOptions, clientOptions);
client.trace = Trace.Verbose;
return client.start();
}
export function activate(context: vscode.ExtensionContext) {
const executable = workspace.getConfiguration("teaspn").get<string>("executable");
context.subscriptions.push(startLangServer(join(context.extensionPath, executable), [], ["plaintext"]));
// For TCP server needs to be started seperately
// context.subscriptions.push(startLangServerTCP(3000, ["plaintext"]));
}