Skip to content

Commit 824046d

Browse files
committed
change annotator
1 parent c0990ba commit 824046d

File tree

6 files changed

+70
-57
lines changed

6 files changed

+70
-57
lines changed

package.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,22 @@
272272
"type": "string",
273273
"default": "#FF6699"
274274
},
275-
"emmylua.colors.doc_type": {
276-
"type": "string",
277-
"default": "#66CCFF"
278-
},
279275
"emmylua.colors.upvalue": {
280276
"type": "string",
281277
"default": "#a8c023"
278+
},
279+
"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+
]
282291
}
283292
}
284293
},

src/annotator.ts

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import * as notifications from "./lspExt";
77

88
let D_PARAM: vscode.TextEditorDecorationType;
99
let D_GLOBAL: vscode.TextEditorDecorationType;
10-
let D_DOC_TYPE: vscode.TextEditorDecorationType;
10+
let D_LOCALS: vscode.TextEditorDecorationType[];
1111
let D_UPVALUE: vscode.TextEditorDecorationType;
12-
let D_NOTUSE: vscode.TextEditorDecorationType;
1312

1413
function createDecoration(key: string | undefined, config: vscode.DecorationRenderOptions = {}): vscode.TextEditorDecorationType {
1514
if (key == undefined) {
@@ -23,19 +22,29 @@ function createDecoration(key: string | undefined, config: vscode.DecorationRend
2322
return vscode.window.createTextEditorDecorationType(config);
2423
}
2524

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+
}));
32+
}
33+
return [];
34+
}
35+
2636
function updateDecorations() {
2737
// 各种方式更新时之前的decoration没有dispose导致重复渲染
2838
if (D_PARAM) {
2939
D_PARAM.dispose();
3040
D_GLOBAL.dispose();
31-
D_DOC_TYPE.dispose();
41+
D_LOCALS.forEach(d => d.dispose());
3242
D_UPVALUE.dispose();
33-
D_NOTUSE.dispose();
3443
}
3544

3645
D_PARAM = createDecoration("colors.parameter");
3746
D_GLOBAL = createDecoration("colors.global");
38-
D_DOC_TYPE = createDecoration("colors.doc_type");
47+
D_LOCALS = createDecorations("colors.local");
3948

4049
let upvalueColor = vscode.workspace.getConfiguration("emmylua").get("colors.upvalue");
4150
if (upvalueColor && upvalueColor != "") {
@@ -46,8 +55,6 @@ function updateDecorations() {
4655
else {
4756
D_UPVALUE = createDecoration(undefined);
4857
}
49-
50-
D_NOTUSE = createDecoration("colors.not_use", {});
5158
}
5259

5360
export function onDidChangeConfiguration(client: LanguageClient) {
@@ -70,60 +77,63 @@ function requestAnnotatorsImpl(editor: vscode.TextEditor, client: LanguageClient
7077

7178
let params: notifications.AnnotatorParams = { uri: editor.document.uri.toString() };
7279
client.sendRequest<notifications.IAnnotator[]>("emmy/annotator", params).then(list => {
73-
let map: Map<AnnotatorType, notifications.RenderRange[]> = new Map();
74-
map.set(AnnotatorType.DocType, []);
80+
let map: Map<AnnotatorType, vscode.Range[]> = new Map();
7581
map.set(AnnotatorType.Param, []);
7682
map.set(AnnotatorType.Global, []);
7783
map.set(AnnotatorType.Upvalue, []);
78-
map.set(AnnotatorType.NotUse, []);
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+
}
89+
7990
if (!list) {
8091
return;
8192
}
8293

83-
list.forEach(data => {
84-
let uri = vscode.Uri.parse(data.uri);
85-
let uriSet = new Set<string>();
86-
// 而vscode 在diff,分屏以及其他一些情况下可以获得多个相同的uri
87-
vscode.window.visibleTextEditors.forEach((editor) => {
88-
let docUri = editor.document.uri;
89-
if (uriSet.has(docUri.path)) {
90-
return;
94+
let local_index = 0;
95+
96+
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);
91107
}
92-
uriSet.add(docUri.path)
93-
94-
if (uri.path.toLowerCase() === docUri.path.toLowerCase()) {
95-
let list = map.get(data.type);
96-
if (list === undefined) {
97-
list = data.ranges;
98-
} else {
99-
list = list.concat(data.ranges);
100-
}
101-
map.set(data.type, list);
108+
ranges.push(...annotation.ranges);
109+
local_index++;
110+
if (local_index >= D_LOCALS.length) {
111+
local_index = 0;
102112
}
103-
});
113+
}
104114
});
105115
map.forEach((v, k) => {
106116
updateAnnotators(editor, k, v);
107117
});
118+
119+
local_maps.forEach((v, i) => {
120+
if (i < D_LOCALS.length) {
121+
editor.setDecorations(D_LOCALS[i], v);
122+
}
123+
});
108124
});
109125
}
110126

111-
function updateAnnotators(editor: vscode.TextEditor, type: AnnotatorType, renderRanges: notifications.RenderRange[]) {
127+
function updateAnnotators(editor: vscode.TextEditor, type: AnnotatorType, ranges: vscode.Range[]) {
112128
switch (type) {
113129
case AnnotatorType.Param:
114-
editor.setDecorations(D_PARAM, renderRanges.map(e => e.range));
130+
editor.setDecorations(D_PARAM, ranges);
115131
break;
116132
case AnnotatorType.Global:
117-
editor.setDecorations(D_GLOBAL, renderRanges.map(e => e.range));
118-
break;
119-
case AnnotatorType.DocType:
120-
editor.setDecorations(D_DOC_TYPE, renderRanges.map(e => e.range));
133+
editor.setDecorations(D_GLOBAL, ranges);
121134
break;
122135
case AnnotatorType.Upvalue:
123-
editor.setDecorations(D_UPVALUE, renderRanges.map(e => e.range));
124-
break;
125-
case AnnotatorType.NotUse:
126-
editor.setDecorations(D_NOTUSE, renderRanges.map(e => e.range));
136+
editor.setDecorations(D_UPVALUE, ranges);
127137
break;
128138
}
129139
}

src/debugger/base/DebuggerProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export abstract class DebuggerProvider implements vscode.DebugConfigurationProvi
4141
for (const key in associations) {
4242
if (associations.hasOwnProperty(key)) {
4343
const element = associations[key];
44-
if (element === 'lua' && key.substr(0, 2) === '*.') {
45-
ext.push(key.substr(1));
44+
if (element === 'lua' && key.startsWith('*.')) {
45+
ext.push(key.substring(1));
4646
}
4747
}
4848
}

src/debugger/base/EmmyDebugData.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as proto from "./EmmyDebugProto";
22
import { DebugProtocol } from "@vscode/debugprotocol";
33
import { Handles } from "@vscode/debugadapter";
4+
import iconv = require('iconv-lite');
45

56
export interface IEmmyStackContext {
67
handles: Handles<IEmmyStackNode>;
@@ -89,7 +90,9 @@ export class EmmyVariable implements IEmmyStackNode {
8990
// else {
9091
// presentationHint.attributes?.push('public');
9192
// }
92-
93+
// if (!/^[\x00-\x7F]*$/.test(name)) {
94+
95+
// }
9396
break;
9497
case proto.ValueType.TNUMBER:
9598
name = `[${name}]`;

src/extension.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ export function activate(context: vscode.ExtensionContext) {
2929

3030
context.subscriptions.push(vscode.commands.registerCommand("emmy.stopServer", stopServer));
3131
context.subscriptions.push(vscode.commands.registerCommand("emmy.restartServer", restartServer));
32-
// context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(onDidChangeConfiguration, null, context.subscriptions));
33-
3432
context.subscriptions.push(vscode.commands.registerCommand("emmy.showReferences", showReferences));
3533
context.subscriptions.push(vscode.workspace.onDidChangeTextDocument(onDidChangeTextDocument, null, context.subscriptions));
3634
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(onDidChangeActiveTextEditor, null, context.subscriptions));

src/lspExt.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,12 @@ export interface AnnotatorParams {
77
export enum AnnotatorType {
88
Param,
99
Global,
10-
DocType,
10+
Local,
1111
Upvalue,
12-
NotUse
13-
}
14-
15-
export interface RenderRange{
16-
range: vscode.Range;
17-
hint: string;
1812
}
1913

2014
export interface IAnnotator {
21-
uri: string;
22-
ranges: RenderRange[];
15+
ranges: vscode.Range[];
2316
type: AnnotatorType;
2417
}
2518

0 commit comments

Comments
 (0)