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

Emit holes as diagnostics #1653

Merged
merged 17 commits into from
Apr 9, 2021
Merged

Emit holes as diagnostics #1653

merged 17 commits into from
Apr 9, 2021

Conversation

isovector
Copy link
Collaborator

@isovector isovector commented Apr 2, 2021

This PR adds new diagnostics on every hole. The use case is to support workflows where Wingman users can quickly jump to the next area they need to work on when refining holes.

The severity of these diagnostics is configurable, since many clients do not support jumping to diagnostics by code, and VSCode doesn't even allow jumping to severity=hint diagnostics.

@isovector isovector changed the title [wip] Jump to next hole after running wingman Emit holes as diagnostics Apr 2, 2021
@isovector isovector marked this pull request as ready for review April 2, 2021 18:43

action $ do
files <- getFilesOfInterest
void $ uses WriteDiagnostics $ Map.keys files
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this really necessary? It will be executed on every single keystroke, so please be very sure about it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Happy to get suggestions here; this is just cargo culted from hlint.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the desired purpose? Do you want to refresh the code action diagnostics on every keystroke, or only when code actions are invoked?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I must admit I'm in over my head here. I'd like a workflow that allows for refining holes by hand and via Wingman, that makes it easy to get to the next hole.

My original thought was to just automatically move to the next hole after running a code action, but LSP doesn't support that. And so if users need to do the movement themselves, it'd be nice if it worked under all circumstances.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Have you seen this: haskell/ghcide#889

The idea is that you can hook into GHC to record all the holes in the program while its being typechecked, no need to do a potentially slow SYB style traversal afterwards.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's embarrassing. I had them disabled in my test project for some reason. Thanks. How can I get the existing diagnostics?

Copy link
Collaborator

Choose a reason for hiding this comment

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

You might want this:

getDiagnostics :: IdeState -> IO [FileDiagnostic]
getDiagnostics IdeState{shakeExtras = ShakeExtras{diagnostics}} = do
val <- readVar diagnostics
return $ getAllDiagnostics val

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm confused about the flow here, and worried about races.

getDiagnostics above requires an IdeState, which AFAICT isn't available inside of the shake Action where the new diagnostics need to be generated. Easy enough to work around this by mucking with the internals, but then getDiagnostics just reads a mutable variable. Without a proper rule that only fires when the diagnostics have changed, to ensure the other diagnostics get generated before I look at them, it seems like I'm just inviting race conditions.

But since diagnostics are generated (exclusively?) by rules, and that this new one will produce new diagnostics, it feels like this approach won't work. Unless I'm missing something obvious?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, you are right. The diagnostic store is mutable, and getDiagnostics will only read whatever is in there.

But that said, I think you could add a dependency on the Typecheck rule, and then I believe getDiagnostics will find the Typecheck diagnostics in there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That makes sense, thanks!

@isovector
Copy link
Collaborator Author

I'm going to make this opt-in and continue using SYB for the time being. I have a follow up feature that is going to need to interact with this (saving intermediate hypotheses from Wingman inside of the generated holes), and it seems like reusing the diagnostics approach won't work there. Rather than do something smart that might end up being unhelpful next week, I'm just going to do the stupid thing to get unblocked and re-evaluate in the followup work.

@wz1000
Copy link
Collaborator

wz1000 commented Apr 9, 2021

I guess I don't really understand what the point of this PR is supposed to be. We already get diagnostics for holes, why duplicate them?

@isovector
Copy link
Collaborator Author

LSP doesn't support "move cursor", but does support "go to next diagnostic". Wingman needs the ability to jump to the next hole, not just any diagnostic, so it's necessary to differentiate the hole diagnostics from others. To make matters worse, most clients only support filtering diagnostics by severity, despite all the information that can be attached to diagnostics. It's a wildly frustrating state of affairs, but this is the best I can come up with.

@wz1000
Copy link
Collaborator

wz1000 commented Apr 9, 2021

I guess if you simply want to add more metadata to hole diagnostics, a better way would be extending ghcide to support a hook which can modify diagnostics, and making use of that.

@isovector isovector added the merge me Label to trigger pull request merge label Apr 9, 2021
@mergify mergify bot merged commit 0c5a317 into haskell:master Apr 9, 2021
@isovector isovector deleted the jump-to-hole branch May 21, 2021 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge me Label to trigger pull request merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants