|
1 | | -import * as vscode from 'vscode'; |
2 | | -import * as path from 'path'; |
| 1 | +import * as vscode from "vscode"; |
| 2 | +import * as path from "path"; |
3 | 3 | 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"; |
10 | 11 | /** |
11 | 12 | * Singleton class to manage the Python LSP client for Code-Aster. |
12 | 13 | * Handles client creation, start, restart, notifications, and editor listeners. |
13 | 14 | */ |
14 | 15 | export class LspServer { |
| 16 | + private static _instance: LspServer; |
| 17 | + private _client?: LanguageClient; |
15 | 18 |
|
16 | | - private static _instance: LspServer; |
17 | | - private _client?: LanguageClient; |
| 19 | + private constructor() {} |
18 | 20 |
|
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(); |
26 | 24 | } |
| 25 | + return LspServer._instance; |
| 26 | + } |
27 | 27 |
|
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."); |
33 | 31 | } |
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); |
62 | 41 | } |
63 | 42 |
|
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, |
96 | 52 | ); |
| 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(); |
97 | 153 | } |
98 | 154 |
|
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; |
137 | 175 | } |
| 176 | + return this._client.stop(); |
| 177 | + } |
138 | 178 | } |
0 commit comments