Skip to content

Commit e76dc35

Browse files
committed
use rust for langauge server
1 parent b481f2f commit e76dc35

File tree

8 files changed

+579
-508
lines changed

8 files changed

+579
-508
lines changed

.github/workflows/build.yml

+7-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ jobs:
1212
strategy:
1313
matrix:
1414
include:
15-
- {target: win32-x64, file: EmmyLua.LanguageServer-win32-x64.zip}
16-
- {target: linux-x64, file: EmmyLua.LanguageServer-linux-x64.tar.gz}
17-
- {target: darwin-x64, file: EmmyLua.LanguageServer-darwin-x64.zip}
18-
- {target: darwin-arm64, file: EmmyLua.LanguageServer-darwin-arm64.zip}
15+
- {target: win32-x64, file: emmylua_ls-win32-x64.zip}
16+
- {target: win32-ia32, file: emmylua_ls-win32-ia32.zip}
17+
- {target: win32-arm64, file: emmylua_ls-win32-arm64.zip}
18+
- {target: linux-x64, file: emmylua_ls-linux-x64-glibc.2.17.tar.gz}
19+
- {target: linux-arm64, file: emmylua_ls-linux-aarch64-glibc.2.17.tar.gz}
20+
- {target: darwin-x64, file: emmylua_ls-darwin-x64.zip}
21+
- {target: darwin-arm64, file: emmylua_ls-darwin-arm64.zip}
1922

2023
runs-on: ubuntu-latest
2124

build/config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
exports.default = {
22
emmyDebuggerVersion: '1.8.2',
33
emmyDebuggerUrl: 'https://github.com/EmmyLua/EmmyLuaDebugger/releases/download',
4-
newLanguageServerVersion: "0.7.1",
5-
newLanguageServerUrl: "https://github.com/CppCXY/EmmyLuaAnalyzer/releases/download"
4+
newLanguageServerVersion: "0.1.5",
5+
newLanguageServerUrl: "https://github.com/CppCXY/emmylua-analyzer-rust/releases/download"
66
}

package.json

+2-15
Original file line numberDiff line numberDiff line change
@@ -272,22 +272,9 @@
272272
"type": "string",
273273
"default": "#FF6699"
274274
},
275-
"emmylua.colors.upvalue": {
276-
"type": "string",
277-
"default": "#a8c023"
278-
},
279275
"emmylua.colors.local": {
280-
"type": "array",
281-
"items": {
282-
"type": "string"
283-
},
284-
"description": "Local variable colors",
285-
"default": [
286-
"#66CCFF",
287-
"#ADD8E6",
288-
"#FFA07A",
289-
"#90EE90"
290-
]
276+
"type": "string",
277+
"default": "#66CCFF"
291278
}
292279
}
293280
},

src/annotator.ts

+51-67
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import * as vscode from 'vscode';
32
import { AnnotatorType } from './lspExt';
43
import { LanguageClient } from 'vscode-languageclient/node';
@@ -7,57 +6,60 @@ import * as notifications from "./lspExt";
76

87
let D_PARAM: vscode.TextEditorDecorationType;
98
let D_GLOBAL: vscode.TextEditorDecorationType;
10-
let D_LOCALS: vscode.TextEditorDecorationType[];
11-
let D_UPVALUE: vscode.TextEditorDecorationType;
9+
let D_LOCAL: vscode.TextEditorDecorationType;
10+
let D_MUT_LOCAL: vscode.TextEditorDecorationType;
11+
let D_MUT_PARAM: vscode.TextEditorDecorationType;
1212

13-
function createDecoration(key: string | undefined, config: vscode.DecorationRenderOptions = {}): vscode.TextEditorDecorationType {
14-
if (key == undefined) {
15-
return vscode.window.createTextEditorDecorationType(config);
16-
}
13+
function createDecoration(key: string): vscode.TextEditorDecorationType {
14+
let config: vscode.DecorationRenderOptions = {}
1715
let color = vscode.workspace.getConfiguration("emmylua").get(key);
1816
if (typeof (color) === 'string') {
19-
config.light = { color: color };
20-
config.dark = { color: color };
17+
config.light = { color };
18+
config.dark = { color };
2119
}
2220
return vscode.window.createTextEditorDecorationType(config);
2321
}
2422

25-
function createDecorations(key: string, config: vscode.DecorationRenderOptions = {}): vscode.TextEditorDecorationType[] {
26-
let colors = vscode.workspace.getConfiguration("emmylua").get(key);
27-
if (colors instanceof Array) {
28-
return colors.map(color => vscode.window.createTextEditorDecorationType({
29-
light: { color: color },
30-
dark: { color: color }
31-
}));
23+
function createDecorationUnderline(key: string): vscode.TextEditorDecorationType {
24+
let config: vscode.DecorationRenderOptions = {}
25+
let color = vscode.workspace.getConfiguration("emmylua").get(key);
26+
if (typeof (color) === 'string') {
27+
config.light = {
28+
color,
29+
textDecoration: `underline;text-decoration-color:${color};`
30+
};
31+
config.dark = {
32+
color,
33+
textDecoration: `underline;text-decoration-color:${color};`
34+
};
35+
} else {
36+
config.light = {
37+
textDecoration: `underline;`
38+
};
39+
config.dark = {
40+
textDecoration: `underline;`
41+
};
3242
}
33-
return [];
43+
return vscode.window.createTextEditorDecorationType(config);
44+
}
45+
46+
function disposeDecorations(...decorations: (vscode.TextEditorDecorationType | undefined)[]) {
47+
decorations.forEach(d => d && d.dispose());
3448
}
3549

3650
function updateDecorations() {
37-
// 各种方式更新时之前的decoration没有dispose导致重复渲染
3851
if (D_PARAM) {
39-
D_PARAM.dispose();
40-
D_GLOBAL.dispose();
41-
D_LOCALS.forEach(d => d.dispose());
42-
D_UPVALUE.dispose();
52+
disposeDecorations(D_PARAM, D_GLOBAL, D_LOCAL, D_MUT_LOCAL, D_MUT_PARAM);
4353
}
4454

4555
D_PARAM = createDecoration("colors.parameter");
4656
D_GLOBAL = createDecoration("colors.global");
47-
D_LOCALS = createDecorations("colors.local");
48-
49-
let upvalueColor = vscode.workspace.getConfiguration("emmylua").get("colors.upvalue");
50-
if (upvalueColor && upvalueColor != "") {
51-
D_UPVALUE = createDecoration(undefined, {
52-
textDecoration: `underline;text-decoration-color:${upvalueColor};`
53-
});
54-
}
55-
else {
56-
D_UPVALUE = createDecoration(undefined);
57-
}
57+
D_LOCAL = createDecoration("colors.local");
58+
D_MUT_LOCAL = createDecorationUnderline("colors.local");
59+
D_MUT_PARAM = createDecorationUnderline("colors.parameter");
5860
}
5961

60-
export function onDidChangeConfiguration(client: LanguageClient) {
62+
export function onDidChangeConfiguration() {
6163
updateDecorations();
6264
}
6365

@@ -78,62 +80,44 @@ function requestAnnotatorsImpl(editor: vscode.TextEditor, client: LanguageClient
7880
let params: notifications.AnnotatorParams = { uri: editor.document.uri.toString() };
7981
client.sendRequest<notifications.IAnnotator[]>("emmy/annotator", params).then(list => {
8082
let map: Map<AnnotatorType, vscode.Range[]> = new Map();
81-
map.set(AnnotatorType.Param, []);
83+
map.set(AnnotatorType.ReadOnlyParam, []);
8284
map.set(AnnotatorType.Global, []);
83-
map.set(AnnotatorType.Upvalue, []);
84-
85-
let local_maps = new Map<number, vscode.Range[]>();
86-
for (let i = 0; i < D_LOCALS.length; i++) {
87-
local_maps.set(i, []);
88-
}
85+
map.set(AnnotatorType.ReadOnlyLocal, []);
86+
map.set(AnnotatorType.MutLocal, []);
87+
map.set(AnnotatorType.MutParam, []);
8988

9089
if (!list) {
9190
return;
9291
}
9392

94-
let local_index = 0;
95-
9693
list.forEach(annotation => {
97-
if (annotation.type !== AnnotatorType.Local) {
98-
let ranges = map.get(annotation.type);
99-
if (ranges) {
100-
ranges.push(...annotation.ranges);
101-
}
102-
} else if (D_LOCALS.length > 0) {
103-
let ranges = local_maps.get(local_index);
104-
if (!ranges) {
105-
ranges = [];
106-
local_maps.set(local_index, ranges);
107-
}
94+
let ranges = map.get(annotation.type);
95+
if (ranges) {
10896
ranges.push(...annotation.ranges);
109-
local_index++;
110-
if (local_index >= D_LOCALS.length) {
111-
local_index = 0;
112-
}
11397
}
11498
});
11599
map.forEach((v, k) => {
116100
updateAnnotators(editor, k, v);
117101
});
118-
119-
local_maps.forEach((v, i) => {
120-
if (i < D_LOCALS.length) {
121-
editor.setDecorations(D_LOCALS[i], v);
122-
}
123-
});
124102
});
125103
}
126104

127105
function updateAnnotators(editor: vscode.TextEditor, type: AnnotatorType, ranges: vscode.Range[]) {
128106
switch (type) {
129-
case AnnotatorType.Param:
107+
case AnnotatorType.ReadOnlyParam:
130108
editor.setDecorations(D_PARAM, ranges);
131109
break;
132110
case AnnotatorType.Global:
133111
editor.setDecorations(D_GLOBAL, ranges);
134112
break;
135-
case AnnotatorType.Upvalue:
136-
editor.setDecorations(D_UPVALUE, ranges);
113+
case AnnotatorType.ReadOnlyLocal:
114+
editor.setDecorations(D_LOCAL, ranges);
115+
break;
116+
case AnnotatorType.MutLocal:
117+
editor.setDecorations(D_MUT_LOCAL, ranges);
118+
break;
119+
case AnnotatorType.MutParam:
120+
editor.setDecorations(D_MUT_PARAM, ranges);
137121
break;
138122
}
139123
}

src/extension.ts

+37-54
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { InlineDebugAdapterFactory } from './debugger/DebugFactory'
1616
import * as os from 'os';
1717
import * as fs from 'fs';
1818
import { IServerLocation, IServerPosition } from './lspExt';
19+
import { onDidChangeConfiguration } from './annotator';
1920

2021
export let ctx: EmmyContext;
2122
let activeEditor: vscode.TextEditor;
@@ -34,6 +35,13 @@ export function activate(context: vscode.ExtensionContext) {
3435
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(onDidChangeActiveTextEditor, null, context.subscriptions));
3536
context.subscriptions.push(vscode.commands.registerCommand("emmy.insertEmmyDebugCode", insertEmmyDebugCode));
3637
context.subscriptions.push(vscode.languages.setLanguageConfiguration("lua", new LuaLanguageConfiguration()));
38+
context.subscriptions.push(
39+
vscode.workspace.onDidChangeConfiguration(e => {
40+
if (e.affectsConfiguration("emmylua")) {
41+
onDidChangeConfiguration()
42+
}
43+
})
44+
);
3745
startServer();
3846
registerDebuggers();
3947
return {
@@ -97,17 +105,26 @@ async function startServer() {
97105
});
98106
}
99107

108+
const serverPaths: { [key: string]: { [key: string]: string } } = {
109+
win32: {
110+
arm64: 'emmylua_ls-win32-arm64',
111+
x64: 'emmylua_ls-win32-x64',
112+
ia32: 'emmylua_ls-win32-ia32'
113+
},
114+
linux: {
115+
arm64: 'emmylua_ls-linux-aarch64-glibc.2.17',
116+
x64: 'emmylua_ls-linux-x64-glibc.2.17'
117+
},
118+
darwin: {
119+
arm64: 'emmylua_ls-darwin-arm64',
120+
x64: 'emmylua_ls-darwin-x64'
121+
}
122+
};
123+
100124
async function doStartServer() {
101125
const context = ctx.extensionContext;
102126
const clientOptions: LanguageClientOptions = {
103127
documentSelector: [{ scheme: 'file', language: ctx.LANGUAGE_ID }],
104-
// synchronize: {
105-
// configurationSection: ["emmylua", "files.associations"],
106-
// fileEvents: [
107-
// vscode.workspace.createFileSystemWatcher("**/*.lua"),
108-
// vscode.workspace.createFileSystemWatcher("**/.editorconfig"),
109-
// ]
110-
// },
111128
initializationOptions: {}
112129
};
113130

@@ -130,54 +147,20 @@ async function doStartServer() {
130147
return Promise.resolve(result);
131148
};
132149
} else {
133-
let platform: string = os.platform();
150+
let platform = os.platform();
151+
let arch = os.arch();
152+
let executableName = platform === 'win32' ? 'emmylua_ls.exe' : 'emmylua_ls';
153+
154+
const serverDir = serverPaths[platform]?.[arch];
155+
if (!serverDir) {
156+
vscode.window.showErrorMessage(`Unsupported platform: ${platform} ${arch}`);
157+
return;
158+
}
159+
160+
const command = path.join(context.extensionPath, 'server', serverDir, executableName);
134161

135-
let command: string = "";
136-
switch (platform) {
137-
case "win32":
138-
command = path.join(
139-
context.extensionPath,
140-
'server',
141-
'emmylua_ls-win32-x64',
142-
'emmylua_ls.exe'
143-
)
144-
break;
145-
case "linux":
146-
if (os.arch() === "arm64") {
147-
command = path.join(
148-
context.extensionPath,
149-
'server',
150-
'emmylua_ls-linux-arm64',
151-
'emmylua_ls'
152-
);
153-
} else {
154-
command = path.join(
155-
context.extensionPath,
156-
'server',
157-
'emmylua_ls-linux-x64',
158-
'emmylua_ls'
159-
);
160-
}
161-
fs.chmodSync(command, '777');
162-
break;
163-
case "darwin":
164-
if (os.arch() === "arm64") {
165-
command = path.join(
166-
context.extensionPath,
167-
'server',
168-
'emmylua_ls-darwin-arm64',
169-
'emmylua_ls'
170-
);
171-
} else {
172-
command = path.join(
173-
context.extensionPath,
174-
'server',
175-
'emmylua_ls-darwin-x64',
176-
'emmylua_ls'
177-
);
178-
}
179-
fs.chmodSync(command, '777');
180-
break;
162+
if (platform !== 'win32') {
163+
fs.chmodSync(command, '777');
181164
}
182165
serverOptions = {
183166
command: command,

src/lspExt.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ export interface AnnotatorParams {
55
}
66

77
export enum AnnotatorType {
8-
Param,
8+
ReadOnlyParam,
99
Global,
10-
Local,
11-
Upvalue,
10+
ReadOnlyLocal,
11+
MutLocal,
12+
MutParam,
1213
}
1314

1415
export interface IAnnotator {

0 commit comments

Comments
 (0)