Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixes restart command being unavailable if initial start failed #63

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Change Log

## [Unreleased]

### Added

- The `Restart Lexical's language server` command now starts the language server
if it isn't already running. Useful to quickly restart Lexical if initial
start failed due to environment reasons, like an incompatible version of
Erlang or Elixir.

## [0.0.11]

### Added
Expand Down
7 changes: 4 additions & 3 deletions src/commands/restart-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import Commands from ".";

interface Context {
client: LanguageClient;
showWarning: (message: string) => void;
}

const restartServer: Commands.T<Context> = {
id: "lexical.server.restart",
createHandler: ({ client, showWarning }) => {
createHandler: ({ client }) => {
function handle() {
if (client.isRunning()) {
console.log("Lexical client is already running. Restarting.");
client.restart();
} else {
showWarning("Server is not running, cannot restart.");
console.log("Lexical client is not running. Starting.");
client.start();
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ export async function activate(context: ExtensionContext): Promise<void> {
context.subscriptions.push(commands.registerCommand(id, handler));
});

if (client !== undefined) {
registerCommand(restartServer, {
client,
showWarning: window.showWarningMessage,
});
}
registerCommand(restartServer, {
client,
});
}
}

Expand Down Expand Up @@ -73,7 +70,7 @@ function isExecutableFile(path: fs.PathLike): boolean {

async function start(
startScriptOrReleaseFolderPath: string
): Promise<LanguageClient | undefined> {
): Promise<LanguageClient> {
const outputChannel = window.createOutputChannel("Lexical");
const startScriptPath = isExecutableFile(startScriptOrReleaseFolderPath)
? startScriptOrReleaseFolderPath
Expand Down Expand Up @@ -114,7 +111,6 @@ async function start(
await client.start();
} catch (reason) {
window.showWarningMessage(`Failed to start Lexical: ${reason}`);
return undefined;
}

return client;
Expand Down
28 changes: 16 additions & 12 deletions src/test/commands/restart-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,43 @@ import restartServer from "../../commands/restart-server";
import { LanguageClient } from "vscode-languageclient/node";

describe("restartServer", () => {
test("restarts the language client", () => {
test("given it is running, restarts it", () => {
const restart = jest.fn<LanguageClient["restart"]>();
const handler = restartServer.createHandler({
client: getClientStub(true, restart),
showWarning: jest.fn(),
client: getClientStub({ isRunning: true, restart }),
});

handler();

expect(restart).toHaveBeenCalled();
});

test("given the client is not running, shows a warning", () => {
const showWarning = jest.fn();
test("given the client is not running, starts it", () => {
const start = jest.fn<LanguageClient["restart"]>();
const handler = restartServer.createHandler({
client: getClientStub(false, jest.fn<LanguageClient["restart"]>()),
showWarning: showWarning,
client: getClientStub({ isRunning: false, start }),
});

handler();

expect(showWarning).toHaveBeenCalled();
expect(start).toHaveBeenCalled();
});
});

function getClientStub(
isRunning: boolean,
restart: LanguageClient["restart"]
): LanguageClient {
function getClientStub({
isRunning,
restart = jest.fn<LanguageClient["restart"]>(),
start = jest.fn<LanguageClient["start"]>(),
}: {
isRunning: boolean;
restart?: LanguageClient["restart"];
start?: LanguageClient["start"];
}): LanguageClient {
return {
isRunning() {
return isRunning;
},
restart,
start,
} as unknown as LanguageClient;
}
Loading