Skip to content

Commit 8593710

Browse files
committed
整理代码
1 parent 867826b commit 8593710

File tree

11 files changed

+75
-66
lines changed

11 files changed

+75
-66
lines changed

client/package-lock.json

Lines changed: 23 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"url": "https://github.com/Microsoft/vscode-extension-samples"
1515
},
1616
"engines": {
17-
"vscode": "^1.70.0"
17+
"vscode": "^1.85.0"
1818
},
1919
"dependencies": {
2020
"axios": "^1.2.1",
@@ -25,8 +25,8 @@
2525
"winston": "^3.8.2"
2626
},
2727
"devDependencies": {
28-
"@types/node": "^17.0.35",
29-
"@types/vscode": "^1.70.0",
30-
"typescript": "^5.4.5"
28+
"@types/node": "^18.19.18",
29+
"@types/vscode": "1.85.0",
30+
"typescript": "^5.5.0"
3131
}
3232
}

client/src/addon_manager/commands/disable.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export default async (context: vscode.ExtensionContext, message: Message) => {
1515
const addon = addonManager.addons.get(message.data.name);
1616
const workspaceFolders = vscode.workspace.workspaceFolders;
1717

18+
if (!addon || !workspaceFolders) {
19+
return;
20+
}
21+
1822
let selectedFolders: vscode.WorkspaceFolder[];
1923

2024
if (workspaceFolders && workspaceFolders.length === 1) {
@@ -35,7 +39,7 @@ export default async (context: vscode.ExtensionContext, message: Message) => {
3539
return workspaceFolders.find(
3640
(folder) => folder.name === selection.label
3741
);
38-
});
42+
}).filter((folder) => !!folder);
3943
}
4044

4145
for (const folder of selectedFolders) {

client/src/addon_manager/services/settings.service.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from "vscode";
2-
import { getConfig, setConfig } from "../../languageserver";
2+
import { ConfigChange, getConfig, setConfig } from "../../languageserver";
33
import { createChildLogger } from "./logging.service";
44
import { LIBRARY_SETTING } from "../config";
55

@@ -8,8 +8,6 @@ const localLogger = createChildLogger("Settings");
88
/** An error with the user's configuration `.vscode/settings.json` or an
99
* addon's `config.json`. */
1010
class ConfigError extends Error {
11-
message: string;
12-
1311
constructor(message: string) {
1412
super(message);
1513
localLogger.error(message);
@@ -37,7 +35,7 @@ export const applyAddonSettings = async (
3735
) => {
3836
if (!folder) throw new ConfigError(`Workspace is not open!`);
3937

40-
const changes = [];
38+
const changes: ConfigChange[] = [];
4139
for (const [newKey, newValue] of Object.entries(config)) {
4240
if (Array.isArray(newValue)) {
4341
newValue.forEach((val) => {
@@ -48,9 +46,9 @@ export const applyAddonSettings = async (
4846
uri: folder.uri,
4947
});
5048
});
51-
} else if (typeof newValue === "object") {
49+
} else if (typeof newValue === "object" && newValue !== null) {
5250
changes.push(
53-
...Object.entries(newValue).map(([key, value]) => {
51+
...Object.entries(newValue).map(([key, value]): ConfigChange => {
5452
return {
5553
action: "prop",
5654
key: newKey,
@@ -79,22 +77,22 @@ export const revokeAddonSettings = async (
7977
) => {
8078
if (!folder) throw new ConfigError(`Workspace is not open!`);
8179

82-
const changes = [];
80+
const changes: ConfigChange[] = [];
8381
for (const [newKey, newValue] of Object.entries(config)) {
8482
const currentValue = await getConfig(newKey, folder.uri);
8583

8684
if (Array.isArray(newValue)) {
8785
// Only keep values that the addon settings does not contain
8886
const notAddon = currentValue.filter(
89-
(oldValue) => !newValue.includes(oldValue)
87+
(oldValue: any) => !newValue.includes(oldValue)
9088
);
9189
changes.push({
9290
action: "set",
9391
key: newKey,
9492
value: notAddon,
9593
uri: folder.uri,
9694
});
97-
} else if (typeof newValue === "object") {
95+
} else if (typeof newValue === "object" && newValue !== null) {
9896
for (const objectKey of Object.keys(newValue)) {
9997
delete currentValue[objectKey];
10098
}

client/src/extension.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,11 @@ export function activate(context: vscode.ExtensionContext) {
99
languageserver.activate(context);
1010

1111
const luaDocContext = {
12-
ViewType: undefined,
13-
OpenCommand: undefined,
14-
extensionPath: undefined,
15-
};
16-
17-
for (const k in context) {
18-
try {
19-
luaDocContext[k] = context[k];
20-
} catch (error) {
21-
}
12+
...context,
13+
extensionPath: context.extensionPath + '/client/3rd/vscode-lua-doc',
14+
ViewType: 'lua-doc',
15+
OpenCommand: 'extension.lua.doc',
2216
}
23-
luaDocContext.ViewType = 'lua-doc';
24-
luaDocContext.OpenCommand = 'extension.lua.doc';
25-
luaDocContext.extensionPath = context.extensionPath + '/client/3rd/vscode-lua-doc';
2617

2718
luadoc.activate(luaDocContext);
2819
psi.activate(context);

client/src/languageserver.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export let defaultClient: LuaClient | null;
2525

2626
function registerCustomCommands(context: ExtensionContext) {
2727
context.subscriptions.push(Commands.registerCommand('lua.config', (changes) => {
28-
const propMap: Map<string, Map<string, unknown>> = new Map();
28+
const propMap: Record<string, Record<string, unknown>> = {};
2929

3030
for (const data of changes) {
3131
const config = Workspace.getConfiguration(undefined, Uri.parse(data.uri));
@@ -43,7 +43,10 @@ function registerCustomCommands(context: ExtensionContext) {
4343
}
4444
if (data.action === 'prop') {
4545
if (!propMap[data.key]) {
46-
propMap[data.key] = config.get(data.key);
46+
let prop = config.get(data.key);
47+
if (typeof prop === 'object' && prop !== null) {
48+
propMap[data.key] = prop as Record<string, unknown>;
49+
}
4750
}
4851
propMap[data.key][data.prop] = data.value;
4952
config.update(data.key, propMap[data.key], data.global);
@@ -71,14 +74,14 @@ function registerCustomCommands(context: ExtensionContext) {
7174
if (!output) {
7275
return;
7376
}
74-
defaultClient.client.sendRequest(ExecuteCommandRequest.type, {
77+
defaultClient.client?.sendRequest(ExecuteCommandRequest.type, {
7578
command: 'lua.exportDocument',
7679
arguments: [output.toString()],
7780
});
7881
}));
7982

8083
context.subscriptions.push(Commands.registerCommand('lua.reloadFFIMeta', async () => {
81-
defaultClient.client.sendRequest(ExecuteCommandRequest.type, {
84+
defaultClient?.client?.sendRequest(ExecuteCommandRequest.type, {
8285
command: 'lua.reloadFFIMeta',
8386
})
8487
}))
@@ -100,7 +103,7 @@ export const createClient = (context: ExtensionContext) => {
100103
}
101104

102105
class LuaClient extends Disposable {
103-
public client: LanguageClient;
106+
public client: LanguageClient | undefined;
104107
private disposables = new Array<Disposable>();
105108
constructor(
106109
private context: ExtensionContext,
@@ -241,14 +244,14 @@ class LuaClient extends Disposable {
241244
}
242245

243246
async stop() {
244-
this.client.stop();
247+
this.client?.stop();
245248
for (const disposable of this.disposables) {
246249
disposable.dispose();
247250
}
248251
}
249252

250253
statusBar() {
251-
const client = this.client;
254+
const client = this.client!;
252255
const bar = window.createStatusBarItem(vscode.StatusBarAlignment.Right);
253256
bar.text = "Lua";
254257
bar.command = "Lua.statusBar";
@@ -278,6 +281,9 @@ class LuaClient extends Disposable {
278281
}
279282

280283
onCommand() {
284+
if (!this.client) {
285+
return;
286+
}
281287
this.disposables.push(
282288
this.client.onNotification("$/command", (params) => {
283289
Commands.executeCommand(params.command, params.data);
@@ -318,10 +324,10 @@ export async function reportAPIDoc(params: unknown) {
318324
if (!defaultClient) {
319325
return;
320326
}
321-
defaultClient.client.sendNotification('$/api/report', params);
327+
defaultClient.client?.sendNotification('$/api/report', params);
322328
}
323329

324-
type ConfigChange = {
330+
export type ConfigChange = {
325331
action: "set",
326332
key: string,
327333
value: LSPAny,
@@ -357,7 +363,7 @@ export async function setConfig(changes: ConfigChange[]): Promise<boolean> {
357363
global: change.global,
358364
});
359365
}
360-
await defaultClient.client.sendRequest(ExecuteCommandRequest.type, {
366+
await defaultClient.client?.sendRequest(ExecuteCommandRequest.type, {
361367
command: 'lua.setConfig',
362368
arguments: params,
363369
});
@@ -368,7 +374,7 @@ export async function getConfig(key: string, uri: vscode.Uri): Promise<LSPAny> {
368374
if (!defaultClient) {
369375
return undefined;
370376
}
371-
return await defaultClient.client.sendRequest(ExecuteCommandRequest.type, {
377+
return await defaultClient.client?.sendRequest(ExecuteCommandRequest.type, {
372378
command: 'lua.getConfig',
373379
arguments: [{
374380
uri: uri.toString(),

client/src/psi/psiViewer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class PsiViewer {
2121
private readonly extensionPath: string;
2222
private readonly builtAppFolder: string;
2323
private disposables: vscode.Disposable[] = [];
24-
private timeoutToReqAnn?: NodeJS.Timer;
24+
private timeoutToReqAnn?: NodeJS.Timeout;
2525

2626
public static createOrShow(context: vscode.ExtensionContext) {
2727
// const column = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined;
@@ -115,7 +115,7 @@ class PsiViewer {
115115
}
116116

117117
private requestPsiImpl(editor: vscode.TextEditor) {
118-
const client = defaultClient.client;
118+
const client = defaultClient?.client;
119119
const params = { uri: editor.document.uri.toString() };
120120
client?.sendRequest<{ data: unknown }>("$/psi/view", params).then(result => {
121121
if (result) {
@@ -128,7 +128,7 @@ class PsiViewer {
128128
}
129129

130130
private requestPsiSelect(position: vscode.Position, uri: vscode.Uri) {
131-
const client = defaultClient.client;
131+
const client = defaultClient?.client;
132132
const params = { uri: uri.toString(), position };
133133
client?.sendRequest<{ data: unknown }>("$/psi/select", params).then(result => {
134134
if (result) {
@@ -146,15 +146,15 @@ class PsiViewer {
146146
if (activeEditor
147147
&& activeEditor.document === event.document
148148
&& activeEditor.document.languageId === LANGUAGE_ID) {
149-
viewer.requestPsi(activeEditor);
149+
viewer?.requestPsi(activeEditor);
150150
}
151151
}
152152

153153
private static onDidChangeActiveTextEditor(editor: vscode.TextEditor | undefined) {
154154
const viewer = PsiViewer.currentPanel;
155155
if (editor
156156
&& editor.document.languageId === LANGUAGE_ID) {
157-
viewer.requestPsi(editor);
157+
viewer?.requestPsi(editor);
158158
}
159159
}
160160

client/tsconfig.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"compilerOptions": {
3-
"module": "commonjs",
4-
"target": "es6",
3+
"module": "Node16",
4+
"target": "ES2022",
55
"outDir": "out",
6-
"rootDir": "src",
7-
"lib": ["es6"],
6+
"rootDir": ".",
7+
"lib": ["ES2022"],
88
"sourceMap": true,
9-
"esModuleInterop": true
9+
"strict": true,
10+
"allowJs": true
1011
},
11-
"include": ["src"],
12+
"include": ["src", "3rd"],
1213
"exclude": ["node_modules", ".vscode-test"]
1314
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3358,7 +3358,7 @@
33583358
"EmmyLua"
33593359
],
33603360
"license": "MIT",
3361-
"main": "./client/out/extension",
3361+
"main": "./client/out/src/extension",
33623362
"name": "lua",
33633363
"publisher": "sumneko",
33643364
"repository": {

package/package.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ return {
3131
"onWebviewPanel:lua-doc",
3232
"onCommand:extension.lua.doc",
3333
},
34-
main = "./client/out/extension",
34+
main = "./client/out/src/extension",
3535
contributes = {
3636
commands = {
3737
{

0 commit comments

Comments
 (0)