vscode-opa icon indicating copy to clipboard operation
vscode-opa copied to clipboard

Easy workspace-wide Regal toggle

Open johanfylling opened this issue 11 months ago • 8 comments

Sometimes, you're working in a workspace where you, for various reasons, don't want to change the source code to comply with Regal, or disable it through inline comments or introducing a regal config file. For these situations, it'd be great to have a boolean extension config param to turn off Regal, which could be applied on the workspace. The workspace's .vscode directory, where the config is stored, is likely to be captured by any existing.gitignore.

johanfylling avatar Mar 14 '24 12:03 johanfylling

Agreed, this would be great when working with code you can't modify and fix.

I would love to have a little Regal viking next to the GitHub co-pilot whateverthatis to bring up a context menu with some options related to Regal 😍 But just being able to set it via the usual context menu is fine, and I guess less intrusive for people not as obsessed by Regal (if such people exist). Disabling Regal for the current workspace would be a good first option to add.

Screenshot 2024-03-14 at 13 40 40

anderseknert avatar Mar 14 '24 12:03 anderseknert

As more features are added to the Regal language server, it's likely just the diagnostics feature (i.e. linting) that one might want to disable. There should be little need to disable something like tooltips for built-in functions even when working with code you can't modify. I know that's what you meant @johanfylling, just noting it down for when we get to this.

anderseknert avatar Apr 02 '24 21:04 anderseknert

If you generally want a language server (such as regal), but then don't want it in one workspace you can create a file at .vscode/settings.json with the contents:

{
    "opa.languageServers": [],
}

https://github.com/open-policy-agent/vscode-opa/assets/1774239/0b638d56-05a4-46fa-bfd3-2e75619f8975

Clearly there are more high tech options here too. But this might be good enough for someone searching and finding this issue before we do anything nicer.

charlieegan3 avatar Apr 04 '24 09:04 charlieegan3

I think the right way to look at this now is, how can I disable Regal's linting for a workspace since it's unlikely that you'd want to disable the other functions of the language server in the same way (i.e. they'd be managed in the client settings rather than Regal).

Anders has suggested a command or similar that splats out a disable regal linting config file like this:

rules:
  default:
    level: ignore

I think this is the right end goal, but I'm not sure how we can offer such a command. One idea is to have a StatusBarItem which allows the user to run some commands.

e.g.

https://github.com/open-policy-agent/vscode-opa/assets/1774239/9dc0ffd5-ebec-440c-b84d-5765b728192c

charlieegan3 avatar Apr 22 '24 17:04 charlieegan3

Love the status bar! :) Adding a .regal/config.yaml file is the right choice for projects you own, but as @johanfylling pointed out, it'd be good if there was an option to disable linting without touching any files in a project, as it might not be yours to begin with. I haven't looked into how it works, but it seems there are "dynamic registration" properties for most things in the LSP configuration, suggesting that it's possible for the client to register/deregister support for features after initialization. But it could be just me misinterpreting the spec.

anderseknert avatar Apr 22 '24 17:04 anderseknert

without touching any files in a project

One issue that I see with this is that I don't think we have a good way to 'save' the setting for the next start up in that workspace.

"dynamic registration"

Yeah, I don't know how to disable diagnostics only. That see to me to be worth looking into. It seems we set this in the Regal client settings:

        diagnosticPullOptions: {
            onChange: true,
            onSave: true
        }

This means that the client will request diagnostics when the file is changed and saved. If we set these to false, then this doesn't happen. However, our server will also send diagnostics when the contents of the file change anyway. I think this is something that we'd need to change in the behaviour of the server to address.

I've not been able to see how diagnosticPullOptions could be set by a user at runtime though, so we might need to expose a setting for that the user can change, and that'd be a settings.json file... 🤷‍♂️

charlieegan3 avatar Apr 23 '24 08:04 charlieegan3

Solid research! Yeah, could be that we'll need to add a button or menu item for that somewhere in the VS Code UI ourselves. But let's start by supporting it properly in Regal.

anderseknert avatar Apr 23 '24 08:04 anderseknert

Interestingly, it doesn't seem to be possible to stop the client from sending textDocument/diagnostic requests 😞. It sends fewer with the above settings on false, but it still sends them and so the server responds with the results.

It also doesn't seem to be featured in the client initialisation options.

I think the way to do this would be to have a setting, much like this one: https://github.com/bmewburn/vscode-intelephense/blob/master/package.json#L508-L513

and have a client-side middleware to drop diagnostics if the feature was off.

const clientOptions: LanguageClientOptions = {
    ...
    middleware: {
        // Optionally ignore diagnostics messages based on config
        handleDiagnostics: (uri, diagnostics, next) => {
            // next(uri, []); // Send empty diagnostics array to suppress them
        }
    }
};

charlieegan3 avatar Apr 23 '24 09:04 charlieegan3