Skip to content

Commit

Permalink
A few external server/docker improvements (#255)
Browse files Browse the repository at this point in the history
* Fix lockup when external server not present on startup

* Clean up restart command

Disposing checks if the server actually needs stopping.
Using dispose prevents trying to stop the connection when the underlying
socket is already gone. VSCode sends a shutdown command and marks the
client as `Stopping`. But Aquiring the connection never resolves or errors
resulting in the language server never actually restarting

* Offer users to try connecting again for external clients

Co-authored-by: Fred Snyder <[email protected]>
  • Loading branch information
Earlopain and castwide authored Sep 29, 2022
1 parent 88f5e6d commit 2ffd03c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
23 changes: 7 additions & 16 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,11 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(disposableClient);
};

var restartLanguageServer = function (): Promise<void> {
return new Promise((resolve) => {
if (languageClient) {
languageClient.stop().then(() => {
disposableClient.dispose();
startLanguageServer();
resolve();
});
} else {
startLanguageServer();
resolve();
}
});
var restartLanguageServer = function (): void {
if (disposableClient) {
disposableClient.dispose();
}
startLanguageServer();
};

// Open URL command (used internally for browsing documentation pages)
Expand All @@ -115,9 +107,8 @@ export function activate(context: vscode.ExtensionContext) {

// Restart command
var disposableRestart = vscode.commands.registerCommand('solargraph.restart', () => {
restartLanguageServer().then(() => {
vscode.window.showInformationMessage('Solargraph server restarted.');
});
restartLanguageServer();
vscode.window.showInformationMessage('Solargraph server restarted.');
});
context.subscriptions.push(disposableRestart);

Expand Down
41 changes: 36 additions & 5 deletions src/language-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,43 @@ export function makeLanguageClient(configuration: solargraph.Configuration): Lan
});
};
} else {
return () => {
return new Promise((resolve) => {
var getSocket = function(): Promise<net.Socket> {
return new Promise((resolve, reject) => {
let socket: net.Socket = net.createConnection({ host: vscode.workspace.getConfiguration('solargraph').externalServer.host, port: parseInt(vscode.workspace.getConfiguration('solargraph').externalServer.port) });
resolve({
reader: socket,
writer: socket
let errorHandler = function(err: Error) {
reject(err);
};
socket.addListener('connect', () => {
socket.removeListener('error', errorHandler);
resolve(socket);
});
socket.addListener('error', errorHandler);
});
};
var getSocketOrNotifyUser = function(): Promise<net.Socket> {
return new Promise((resolve, reject) => {
getSocket().then((socket) => {
resolve(socket);
}).catch((err) => {
vscode.window.showWarningMessage('Failed to connect to the external language client: ' + err.message, 'Try again').then((item) => {
if(item === 'Try again') {
resolve(getSocketOrNotifyUser());
} else {
reject(err);
}
});
});
});
};
return () => {
return new Promise((resolve, reject) => {
getSocketOrNotifyUser().then((socket) => {
resolve({
reader: socket,
writer: socket
});
}).catch((err) => {
reject(err);
});
});
};
Expand Down

1 comment on commit 2ffd03c

@a0n
Copy link
Contributor

@a0n a0n commented on 2ffd03c Sep 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is awesome!

Please sign in to comment.