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

Re-read config when restarting server #78

Merged

Conversation

stefanhaller
Copy link
Contributor

When restarting the clangd server, don't just call stop and activate on the same client, but instead tear down the client and instantiate a whole new one. This way we get to see any changed settings (e.g. clangd.path or clangd.arguments).

I'm new to VS Code development and to Typescript, so please be gentle if I'm doing something stupid here!

Addresses #77.

Copy link
Collaborator

@hokein hokein left a comment

Choose a reason for hiding this comment

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

Thanks for implementing this, I think it is a useful feature!

src/extension.ts Outdated
vscode.commands.registerCommand('clangd.activate', async () => {}));
context.subscriptions.push(
clientContext.subscriptions.push(
vscode.commands.registerCommand('clangd.restart', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the restart command registration needs to move to activate -- if any flags passing to clangd are invalid, this code will be not executed, then restart command would not work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, thanks; this is addressed by the rewritten version, see below.

src/extension.ts Outdated
await client.stop();
client.activate();
await clientContext.dispose();
clientContext = new LocalContext;
Copy link
Collaborator

Choose a reason for hiding this comment

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

looks like we don't need this initialization as the startClient already does this (Line 56) for us.

* A class that lets us keep track of disposables, independently from the
* extension's global context.
*/
export class LocalContext implements vscode.Disposable {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thinking more about this, I feel like we should probably move clangdClient into the Context,

What do you think the proposed API below?

export class ClangdContext implements vscode.Disposable {
   subscriptions: vscode.Disposable[] = [];
   client: ClangdLanguageClients
   globalStoragePath: string

   activate() {
      // do whatever `startClient` does, get client & server options, init client, call client.start etc.
   }

   dispose() {
      ...
   }
}

This seems to simplify the codebase:

  • the client parameter in activate is not needed anymore, it can be fetched via clangdContext object (the same for the globalStoragePath);
  • the startDisposable in clangdLanuageClient can be removed;
  • the restart command implement logic seems more natural;

But it would require more refactoring changes, I think it is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that's much nicer.

I pushed a new version of the branch (it's totally rewritten, this couldn't be done with fixup commits in any useful way, sorry for that). I decided to leave globalStoragePath the way it is, since it's only used in constructors, so I thought it's not useful to store it in the class.

The first commit is a bit big and the diff is hard to read because of the large amount of moved code. Let me know if you have any suggestions for improving that.

@stefanhaller stefanhaller force-pushed the re-read-config-when-restarting-server branch from 885e3fc to 3ec282f Compare October 1, 2020 13:18
We introduce a new class ClangdContext that holds the language client and tracks
any resources that were allocated for it; this allows us to tear down the client
and instanciate a whole new one for restarting the server, instead of just
calling stop() and activate() on one and the same instance. This makes it
possible to change config options (e.g. the clangd path, or clangd arguments),
and have that change take effect when restarting the server.
It's now a member of the context, so let everybody get it from there.
Now that we have a context with the same lifetime as the client, we can use it
in the normal way for this, too.

This reverts part of 6f2cef1.
@stefanhaller stefanhaller force-pushed the re-read-config-when-restarting-server branch from 3ec282f to 591a929 Compare October 1, 2020 13:21
Copy link
Collaborator

@hokein hokein left a comment

Choose a reason for hiding this comment

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

Thanks, it looks better.

src/extension.ts Outdated
}));

startClient(context.globalStoragePath, outputChannel);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can get rid of startClient, stopClient.

const ClangdContext = new ....
context.subscriptions.push(
   vscode.commands.registerCommand('clangd.restart', async () => {
      await clangdContext.dispose();
      clangdContext.activate(...);          
}));

clangdContext.activate(...);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yes! I didn't realize that we can reuse the existing clangdContext instance. This also allows us to get rid of the ugly global variable and the deactivate() function. See fixup commit 405c9de.

Copy link
Collaborator

@hokein hokein left a comment

Choose a reason for hiding this comment

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

thanks, looks good.

@hokein hokein merged commit 8b16f9f into clangd:master Oct 5, 2020
@stefanhaller stefanhaller deleted the re-read-config-when-restarting-server branch October 5, 2020 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants