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

Fix error in code range #3229

Merged
merged 7 commits into from
Sep 29, 2022
Merged

Fix error in code range #3229

merged 7 commits into from
Sep 29, 2022

Conversation

kokobd
Copy link
Collaborator

@kokobd kokobd commented Sep 27, 2022

I'm seeing this popup every time an empty source file is opened, like below:
image

module Empty where

HieASTs doesn't have an AST for empty files (by empty files, I mean those haskell source files without anything other than the module header)

I'm not sure if it's the problem of GetHieAST, but let's just decrease the error level in the code range plugin, and handle the case when CodeRange rule fails in a more tolerant way.

Copy link
Collaborator

@michaelpj michaelpj left a comment

Choose a reason for hiding this comment

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

What about keeping the structure of the current code, but using ExceptT over a custom error type. Then we can fail saying "couldn't get a dependency" or whatever, and then decide just below pluginResponse what to do about that, e.g. log or return an error or what.

(Generally, I think it would be nice if pluginResponse encouraged people to do this instead of just using a String as the error type and turning them all into response errors...)

@kokobd
Copy link
Collaborator Author

kokobd commented Sep 28, 2022

What about keeping the structure of the current code, but using ExceptT over a custom error type. Then we can fail saying "couldn't get a dependency" or whatever, and then decide just below pluginResponse what to do about that, e.g. log or return an error or what.

(Generally, I think it would be nice if pluginResponse encouraged people to do this instead of just using a String as the error type and turning them all into response errors...)

Good idea!

Comment on lines 110 to 120
showError :: SelectionRangeError -> Maybe String
-- This might happen if the HieAst is not ready, so we give it a default value instead of throwing an error
showError SelectionRangeBadDependency = Nothing
showError SelectionRangeInputPositionMappingFailure = Just "failed to apply position mapping to input positions"
showError SelectionRangeOutputPositionMappingFailure = Just "failed to apply position mapping to output positions"

data SelectionRangeError = SelectionRangeBadDependency
| SelectionRangeInputPositionMappingFailure
| SelectionRangeOutputPositionMappingFailure

getSelectionRanges :: NormalizedFilePath -> [Position] -> ExceptT SelectionRangeError IdeAction [SelectionRange]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@michaelpj Did I get it correctly?

Copy link
Collaborator

@michaelpj michaelpj left a comment

Choose a reason for hiding this comment

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

Nice! Makes me want to refactor the other plugins :D

Right list -> Right list

showError :: SelectionRangeError -> Maybe String
-- This might happen if the HieAst is not ready, so we give it a default value instead of throwing an error
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd probably just inline this function, then it would look less weird that you're kind of doing different things in the different branches?

getSelectionRanges file positions = do
(codeRange, positionMapping) <- maybeToExceptT "fail to get code range" $ useE GetCodeRange file
(codeRange, positionMapping) <- maybeToExceptT SelectionRangeBadDependency . MaybeT $
useWithStaleFast GetCodeRange file
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe we should include the dependency that failed in the error so we can log it later? I guess useWithStaleFast doesn't log if it fails?

Copy link
Collaborator Author

@kokobd kokobd Sep 29, 2022

Choose a reason for hiding this comment

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

Makes sense. At first, I thought it was too annoying to see a lot of error logs when creating a new Haskell source file, so I wanted to get rid of them completely.
Maybe we should just log it in warning level (I remember at least one other plugin does this), and take a look at why HieAst is missing for files with only a line of module header.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right, I realised when thinking about this that a plugin returning a ResponseError can be "normal" and not necessarily something that HLS needs to log as an error. I still think it's good to log them so you know what's happening, but maybe it doesn't need to be an error log.

@kokobd kokobd added the merge me Label to trigger pull request merge label Sep 29, 2022
@mergify mergify bot merged commit 011d110 into master Sep 29, 2022
@kokobd kokobd deleted the kokobd/fix-coderange-error branch September 29, 2022 16:29
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.

2 participants