Skip to content

Commit f6c9e05

Browse files
Add support for more comm file extensions
1 parent 417603f commit f6c9e05

3 files changed

Lines changed: 263 additions & 149 deletions

File tree

package.json

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,26 @@
6161
{
6262
"id": "comm",
6363
"aliases": [
64-
"Comm"
64+
"Aster Commands"
6565
],
6666
"extensions": [
67-
".comm"
67+
".comm",
68+
".com",
69+
".com0",
70+
".com1",
71+
".com2",
72+
".com3",
73+
".com4",
74+
".com5",
75+
".com6",
76+
".com7",
77+
".com8",
78+
".com9"
6879
],
80+
"icon": {
81+
"light": "./resources/images/icone-code-aster.png",
82+
"dark": "./resources/images/icone-code-aster.png"
83+
},
6984
"configuration": "./language-configuration.json"
7085
}
7186
],
@@ -90,7 +105,7 @@
90105
},
91106
{
92107
"command": "vs-code-aster.meshViewer",
93-
"when": "resourceFilename =~ /\\.comm$/",
108+
"when": "resourceFilename =~ /\\.(comm|com[0-9])$/",
94109
"group": "navigation"
95110
}
96111
]
@@ -99,14 +114,36 @@
99114
"title": "VS Code Aster",
100115
"type": "object",
101116
"properties": {
102-
"vs-code-aster.aliasForRun": {
117+
"vs-code-aster.commFileExtensions": {
103118
"order": 0,
119+
"type": "array",
120+
"items": {
121+
"type": "string"
122+
},
123+
"default": [
124+
".comm",
125+
".com",
126+
".com0",
127+
".com1",
128+
".com2",
129+
".com3",
130+
".com4",
131+
".com5",
132+
".com6",
133+
".com7",
134+
".com8",
135+
".com9"
136+
],
137+
"markdownDescription": "Specifies the file extensions to be recognized as Code Aster command files. Default includes `.comm`, `.com`, and `.com0`-`.com9`. You can customize this list as needed."
138+
},
139+
"vs-code-aster.aliasForRun": {
140+
"order": 1,
104141
"type": "string",
105142
"default": "cave run",
106143
"markdownDescription": "Specifies the alias used to run code-aster. For example, with alias `cave run` and export file `test.export`, the command will be `cave run test.export`."
107144
},
108145
"vs-code-aster.pythonExecutablePath": {
109-
"order": 1,
146+
"order": 2,
110147
"type": "string",
111148
"default": "python3",
112149
"markdownDescription": "Specifies the python executable used to run the language server."

src/LspServer.ts

Lines changed: 160 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,178 @@
1-
import * as vscode from 'vscode';
2-
import * as path from 'path';
1+
import * as vscode from "vscode";
2+
import * as path from "path";
33
import {
4-
LanguageClient,
5-
LanguageClientOptions,
6-
ServerOptions,
7-
TransportKind,
8-
} from 'vscode-languageclient/node';
9-
import { StatusBar } from './StatusBar';
4+
LanguageClient,
5+
LanguageClientOptions,
6+
ServerOptions,
7+
TransportKind,
8+
} from "vscode-languageclient/node";
9+
import { StatusBar } from "./StatusBar";
10+
import { SUPPORTED_COMM_EXTENSIONS } from "./VisuManager";
1011
/**
1112
* Singleton class to manage the Python LSP client for Code-Aster.
1213
* Handles client creation, start, restart, notifications, and editor listeners.
1314
*/
1415
export class LspServer {
16+
private static _instance: LspServer;
17+
private _client?: LanguageClient;
1518

16-
private static _instance: LspServer;
17-
private _client?: LanguageClient;
19+
private constructor() {}
1820

19-
private constructor() { }
20-
21-
public static get instance(): LspServer {
22-
if (!LspServer._instance) {
23-
LspServer._instance = new LspServer();
24-
}
25-
return LspServer._instance;
21+
public static get instance(): LspServer {
22+
if (!LspServer._instance) {
23+
LspServer._instance = new LspServer();
2624
}
25+
return LspServer._instance;
26+
}
2727

28-
public get client(): LanguageClient {
29-
if (!this._client) {
30-
throw new Error('LSP client has not been initialized yet.');
31-
}
32-
return this._client;
28+
public get client(): LanguageClient {
29+
if (!this._client) {
30+
throw new Error("LSP client has not been initialized yet.");
3331
}
34-
35-
/**
36-
* Starts the LSP client
37-
*/
38-
public async start(context: vscode.ExtensionContext) {
39-
if (!this._client) {
40-
this._client = await this.createClient(context);
41-
}
42-
43-
this._client.start()
44-
.then(() => {
45-
vscode.window.showInformationMessage('LSP Python Code-Aster ready!');
46-
this.attachEditorListeners();
47-
})
48-
.catch((err: any) => {
49-
vscode.window.showErrorMessage('Error starting LSP Python: ' + err.message);
50-
});
51-
52-
this._client.onDidChangeState(e => console.log('LSP client state changed:', e));
53-
54-
this._client.onNotification('logParser', (params: { text: string }) => {
55-
console.log(`${params.text}`);
56-
});
57-
58-
//TO DO : reload the bar
59-
this._client.onNotification('reloadStatusBar', (params) => {
60-
StatusBar.instance.onEditorChange(vscode.window.activeTextEditor);
61-
});
32+
return this._client;
33+
}
34+
35+
/**
36+
* Starts the LSP client
37+
*/
38+
public async start(context: vscode.ExtensionContext) {
39+
if (!this._client) {
40+
this._client = await this.createClient(context);
6241
}
6342

64-
/**
65-
* Creates the LanguageClient for Python LSP
66-
*/
67-
private async createClient(context: vscode.ExtensionContext): Promise<LanguageClient> {
68-
const serverModule = context.asAbsolutePath(path.join('python', 'lsp', 'server.py'));
69-
70-
const config = vscode.workspace.getConfiguration('vs-code-aster');
71-
const pythonExecutablePath = config.get<string>('pythonExecutablePath', 'python3');
72-
const serverOptions: ServerOptions = {
73-
command: pythonExecutablePath,
74-
args: [serverModule],
75-
transport: TransportKind.stdio,
76-
options: {
77-
env: {
78-
...process.env,
79-
PYTHONPATH: context.asAbsolutePath('python'),
80-
},
81-
},
82-
};
83-
84-
const clientOptions: LanguageClientOptions = {
85-
documentSelector: [{ scheme: 'file', language: 'comm' }],
86-
synchronize: {
87-
fileEvents: vscode.workspace.createFileSystemWatcher('**/*.comm'),
88-
},
89-
};
90-
91-
return new LanguageClient(
92-
'pythonLanguageServer',
93-
'Python Language Server',
94-
serverOptions,
95-
clientOptions
43+
this._client
44+
.start()
45+
.then(() => {
46+
vscode.window.showInformationMessage("LSP Python Code-Aster ready!");
47+
this.attachEditorListeners();
48+
})
49+
.catch((err: any) => {
50+
vscode.window.showErrorMessage(
51+
"Error starting LSP Python: " + err.message,
9652
);
53+
});
54+
55+
this._client.onDidChangeState((e) =>
56+
console.log("LSP client state changed:", e),
57+
);
58+
59+
this._client.onNotification("logParser", (params: { text: string }) => {
60+
console.log(`${params.text}`);
61+
});
62+
63+
//TO DO : reload the bar
64+
this._client.onNotification("reloadStatusBar", (params) => {
65+
StatusBar.instance.onEditorChange(vscode.window.activeTextEditor);
66+
});
67+
}
68+
69+
/**
70+
* Creates the LanguageClient for Python LSP
71+
*/
72+
private async createClient(
73+
context: vscode.ExtensionContext,
74+
): Promise<LanguageClient> {
75+
const serverModule = context.asAbsolutePath(
76+
path.join("python", "lsp", "server.py"),
77+
);
78+
79+
const config = vscode.workspace.getConfiguration("vs-code-aster");
80+
const pythonExecutablePath = config.get<string>(
81+
"pythonExecutablePath",
82+
"python3",
83+
);
84+
const commFileExtensions = config.get<string[]>(
85+
"commFileExtensions",
86+
SUPPORTED_COMM_EXTENSIONS,
87+
);
88+
89+
// Build file system watcher patterns
90+
const watchPatterns = commFileExtensions.map((ext) => `**/*${ext}`);
91+
92+
const serverOptions: ServerOptions = {
93+
command: pythonExecutablePath,
94+
args: [serverModule],
95+
transport: TransportKind.stdio,
96+
options: {
97+
env: {
98+
...process.env,
99+
PYTHONPATH: context.asAbsolutePath("python"),
100+
},
101+
},
102+
};
103+
104+
const clientOptions: LanguageClientOptions = {
105+
documentSelector: [{ scheme: "file", language: "comm" }],
106+
synchronize: {
107+
fileEvents: vscode.workspace.createFileSystemWatcher(
108+
`{${watchPatterns.join(",")}}`,
109+
),
110+
},
111+
};
112+
113+
return new LanguageClient(
114+
"pythonLanguageServer",
115+
"Python Language Server",
116+
serverOptions,
117+
clientOptions,
118+
);
119+
}
120+
121+
/**
122+
* Adds editor listeners for parameter hints
123+
*/
124+
private async attachEditorListeners() {
125+
vscode.workspace.onDidChangeTextDocument((event) => {
126+
const editor = vscode.window.activeTextEditor;
127+
if (!editor || event.document !== editor.document) {
128+
return;
129+
}
130+
131+
const changes = event.contentChanges;
132+
if (changes.length === 0) {
133+
return;
134+
}
135+
136+
//activate signature and completion triggers
137+
const lastChange = changes[changes.length - 1];
138+
if (
139+
["(", ","].includes(lastChange.text) &&
140+
editor.document.languageId === "comm"
141+
) {
142+
vscode.commands.executeCommand("editor.action.triggerParameterHints");
143+
}
144+
});
145+
}
146+
147+
/**
148+
* Restarts the LSP server
149+
*/
150+
public async restart() {
151+
if (this._client && this._client.isRunning()) {
152+
await this._client.stop();
97153
}
98154

99-
/**
100-
* Adds editor listeners for parameter hints
101-
*/
102-
private async attachEditorListeners() {
103-
vscode.workspace.onDidChangeTextDocument(event => {
104-
const editor = vscode.window.activeTextEditor;
105-
if (!editor || event.document !== editor.document) { return; }
106-
107-
const changes = event.contentChanges;
108-
if (changes.length === 0) { return; }
109-
110-
//activate signature and completion triggers
111-
const lastChange = changes[changes.length - 1];
112-
if (['(',','].includes(lastChange.text) && editor.document.languageId === 'comm') {
113-
vscode.commands.executeCommand('editor.action.triggerParameterHints');
114-
}
115-
});
116-
}
117-
118-
/**
119-
* Restarts the LSP server
120-
*/
121-
public async restart() {
122-
if (this._client && this._client.isRunning()) {
123-
await this._client.stop();
124-
}
125-
126-
this._client?.start()
127-
.then(() => vscode.window.showInformationMessage('LSP Python Code-Aster restarted!'))
128-
.catch((err: any) => vscode.window.showErrorMessage('Error restarting LSP Python: ' + err.message));
129-
}
130-
131-
/**
132-
* Deactivates the LSP server
133-
*/
134-
public deactivate(): Thenable<void> | undefined {
135-
if (!this._client) { return undefined; }
136-
return this._client.stop();
155+
this._client
156+
?.start()
157+
.then(() =>
158+
vscode.window.showInformationMessage(
159+
"LSP Python Code-Aster restarted!",
160+
),
161+
)
162+
.catch((err: any) =>
163+
vscode.window.showErrorMessage(
164+
"Error restarting LSP Python: " + err.message,
165+
),
166+
);
167+
}
168+
169+
/**
170+
* Deactivates the LSP server
171+
*/
172+
public deactivate(): Thenable<void> | undefined {
173+
if (!this._client) {
174+
return undefined;
137175
}
176+
return this._client.stop();
177+
}
138178
}

0 commit comments

Comments
 (0)