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

Add option to enable/disable clangd #636

Merged
merged 2 commits into from
Nov 13, 2024

Conversation

ryanplusplus
Copy link
Contributor

I know that this could be done by disabling the extension for a given workspace, but that requires everyone working in the repository to do that via the VS Code GUI. This allows the extension to be disabled in the workspace settings which can be committed to the repo.

In our organization we're looking to transition from using cpptools to clangd and having an easy way to have both extensions installed but only one enabled in a given workspace will be extremely helpful for us. We can already have a workspace that has been migrated to clangd disable Intellisense via cpptools, but there's no equivalent on the clangd side that I was able to find. For workspaces that haven't been migrated, clangd and cpptools will conflict until the clangd extension is disabled manually.

@ryanplusplus
Copy link
Contributor Author

@sam-mccall @hokein sorry to bother you, but could you or one of your associates take a quick look at this? It's pretty short :)

@edvard-pettersen-elas-ai

I support the addition of this feature. In our organization we also use a mix between cpptools and clangd between different code bases, and it would be nice to have this setting available to lessen the overhead of going back and forth between different workspace setups.

Copy link
Contributor

@HighCommander4 HighCommander4 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 the patch, this looks like a reasonable change to me.

Needs a rebase.

@@ -66,6 +66,10 @@ export class ClangdContext implements vscode.Disposable {
if (!clangdPath)
return;

if (!config.get<boolean>('enable')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason not to move the check even earlier (in particular before the install.activate call, but maybe even at the top of the activate function in extension.ts, thereby avoiding the need for the added check there)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No reason except my lack of familiarity with the code. Thanks for the feedback! I'll give this a go.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried this out and it's a bit more complicated than I expected. In extension.ts I can't return early because activate needs to return a ClangdExtension so at least some of that code needs to run to create and configure the context used to create the ClangdExtensionImpl. I think the safest simplification I can make that doesn't result in a half-initialized context is to do this in extension.ts:

  let shouldCheck = false;

  if (!vscode.workspace.getConfiguration('clangd').get<boolean>('enable')) {
    await clangdContext.activate(context.globalStoragePath, outputChannel);

    shouldCheck = vscode.workspace.getConfiguration('clangd').get<boolean>(
      'detectExtensionConflicts') ?? false;
  }

  if (shouldCheck) {
    const interval = setInterval(function() {
      ...

Would you like me to make this change?

Copy link
Contributor

Choose a reason for hiding this comment

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

This actually raises an interesting issue. The reason activate() returns anything is that we expose an API for use by other extensions. So the question is: how should the API behave if clangd.enable is set to false.

With the current patch, it looks like we pass a null client to ClangdExtension. This is technically not permitted by the API, since the client object it exposes has type BaseLanguageClient, not BaseLanguageClient | null. However, it type-checks because we fudge the check on the nullness of ClangdContext.client itself.

I guess we already have a codepath that can leave ClangdContext.client as null, so adding another one is fine for now. But we should revisit this in a follow-up issue and consider changing the API to explicitly make the exposed client object nullable (or maybe allow a null ClangdExtension return value for activate itself).

TL;DR: Yes, please go ahead and make the described simplification.

Copy link
Contributor

Choose a reason for hiding this comment

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

we should revisit this in a follow-up issue and consider changing the API to explicitly make the exposed client object nullable (or maybe allow a null ClangdExtension return value for activate itself).

I filed #721 about this.

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 again for your help. I rebased, applied the simplified change, and re-tested this.

@ryanplusplus ryanplusplus marked this pull request as ready for review November 13, 2024 01:39
Copy link
Contributor

@HighCommander4 HighCommander4 left a comment

Choose a reason for hiding this comment

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

LGTM

@HighCommander4 HighCommander4 merged commit 91022b2 into clangd:master Nov 13, 2024
1 check passed
@HighCommander4
Copy link
Contributor

Released in 0.1.30.

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.

3 participants