Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Ormolu range format support #1602

Merged
merged 7 commits into from
Jan 29, 2020
Merged

Conversation

Avi-D-coder
Copy link
Collaborator

This is a bit of a hack but it works pretty great.
We format lines of equal or increasing indention, not precise ranges, and there are a few documented edge cases.

Range (Position sl 0) $ Position el $ T.length $ last txt
-- Pragmas will not be picked up in a non standard location,
-- or when range starts on a Pragma
extPragmas = takeWhile ("{-#" `T.isPrefixOf`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Given we are working in the context of a module that is likely to already be loaded by GHC, in principle we should be able to query these directly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

And if we put this behaviour into a utility, we can either get the pragmas from the module DynFlags, or do this parsing as a fallback. And the GHC API already has a function to do that.

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 didn't see that I could show extensions from the EnumSet in DynFlags, I mistakenly thought I was going to have to test if each extension was set, to construct the list.

src/Haskell/Ide/Engine/Plugin/Ormolu.hs Outdated Show resolved Hide resolved
(IdeError
PluginError
(T.pack
"You must format a whole block of code. Ormolu does not support arbitrary ranges."
Copy link
Collaborator

Choose a reason for hiding this comment

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

What user experience does this result in? A pop-up window? This is likely to be a fairly common experience, so should be non-threatening, just a notice.

Another possible approach is to find the enclosing block that can be formatted, and process that. What does the brittany plugin do in this case? I wonder if that logic should be captured in a library, for use in all RangeFormatting requests, so we have a standard behaviour across hie.

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 use nvim and coc so I don't know how it looks to most users. Brittany will often produce an error, in similar situations. The point of this line is just to provide a more helpful error than what we would usually get unable to parse.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I am happy with the message, and its contents. I just want to make sure the end user experience makes sense. But it can be tweaked easily enough, so its not a blocker.

@Avi-D-coder
Copy link
Collaborator Author

If there are no more objections I'm going to merge this.

Copy link
Collaborator

@fendor fendor left a comment

Choose a reason for hiding this comment

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

Too complicated for me to review, I trust that @Avi-D-coder tested it. So, no explicit review from me, but I am happy with merging! Thank you for this nice feature addition!

@Avi-D-coder
Copy link
Collaborator Author

Avi-D-coder commented Jan 28, 2020

There is one more edge case, reintroduced in the switch to DynFlags case I found today.

@Avi-D-coder
Copy link
Collaborator Author

I've been using this version today, no issues so far, so I'm going to merge.

@Avi-D-coder Avi-D-coder merged commit 6dffe13 into haskell:master Jan 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants