Repository navigation
Reintroduce hls-retrie-plugin - #5147
simonhorlick wants to merge 112 commits into
Conversation
aab48b0 to
ac474c9
Compare
Aster89
left a comment
There was a problem hiding this comment.
Just minor suggestions or requests of clarification.
| withCommas [] = [] | ||
| withCommas [x] = [x] | ||
| withCommas (L l e : xs) = L (ensureTrailingComma l) e : withCommas xs |
There was a problem hiding this comment.
If I understand correctly, you can do this with lenses:
| withCommas [] = [] | |
| withCommas [x] = [x] | |
| withCommas (L l e : xs) = L (ensureTrailingComma l) e : withCommas xs | |
| withCommas = over _init $ map ensureTrailingComma' | |
| where ensureTrailingComma' (L l e) = L (ensureTrailingComma l) e |
I'm still new to them, so I'm just spotting opportunities to practice with it
λ> over _init (map (*10)) ([] :: [Int])
[]
λ> over _init (map (*10)) ([1] :: [Int])
[1]
λ> over _init (map (*10)) ([1..3] :: [Int])
[10,20,3]There was a problem hiding this comment.
I need to spend some time to learn how to use lenses. It reads quite clearly.
|
|
||
| import ConPatDef (R (R), e) | ||
|
|
||
| -- without DisambiguateRecordFields a pattern label resolves by |
There was a problem hiding this comment.
Do we have a test for when DisambiguateRecordFields is enabled?
febaafc to
efdb147
Compare
08f4bd0 to
c4f6b01
Compare
| -- the name is imported into the defining module | ||
| case map greImportModule (gre_imp gre) of | ||
| m : _ -> Just $ Importable m (memberForm gre) | ||
| [] -> Nothing -- is this possible? |
There was a problem hiding this comment.
If we think it's not, shall we log it, somehow, so it's easier to tell from a --debug run that this corner case was hit?
I think this is what that logging stuff you were removing might come into play. But I've not used it myself 😆
fendor
left a comment
There was a problem hiding this comment.
Partial review, will continue tomorrow
| runWithRetrie :: Session a -> IO a | ||
| runWithRetrie = runSessionWithTestConfig def | ||
| { testDirLocation = Left testDataDir | ||
| , testConfigCaps = codeActionResolveCaps | ||
| , testPluginDescriptor = testPlugins | ||
| } . const |
There was a problem hiding this comment.
Nitpick, this does not affect whether we merge the PR as is.
Using Left testDataDir copies all test data into a temporary directory and then runs HLS on all of them. This can make tests trickier to write correctly (i.e., without race conditions) as more files are involved that are stand alone.
Generally, I think it is good practice to only copy the files you actually need for a test. This makes the test faster and also more reliable.
To do this, you need to construct a VirtualFileTree and supply it here as the Right constructor.
Let me know, whether you would like to change this and need more pointers.
fendor
left a comment
There was a problem hiding this comment.
Looks pretty good to me, some nitpicks, some suggestions. The only thing that needs more dicussion is getCPPmodule, this can be very expensive.
| -- warn and don't make any edits | ||
| pluginSendNotification SMethod_WindowShowMessage $ | ||
| ShowMessageParams MessageType_Warning msg | ||
| return (Right ca) |
There was a problem hiding this comment.
If there are warnings, why are we still offering the code actions?
There was a problem hiding this comment.
Should this be a PluginInvalidUserState or a PluginInternalError instead? It is an error in the sense that we tried to do the inline, failed with a message and didn't make any changes. This path is to indicate to the user that if they fix their code they can still retry the action.
There was a problem hiding this comment.
If the user fixed their code, won't the client request new code actions? Or does the editor cache this so aggressively that you can reuse the same code action after a change?
There was a problem hiding this comment.
Yes, it would be a new action. What did you mean in your original point, I think I’m missing something?
There was a problem hiding this comment.
We are sending a warning, but we are still producing Right ca, which reads to me as we are returning possible code actions, even though we shouldn't, according to the comment?
Does that mean, we offer code actions, even though we say we couldn't compute something?
There was a problem hiding this comment.
Yes, it’s just an empty, successful action with no edits. Is there a better way to do this?
There was a problem hiding this comment.
Returning Right []? I don't feel like we should be offering any code actions if we can't compute the text edit, what would happen if the user requests it? Not doing anything?
There was a problem hiding this comment.
The intention in the original plugin was to allow a subset of modules to error out so if, say, one module failed because there was some unsupported syntax it would warn the user that one file was not rewritten but still apply the edits to all of the other files. Within one module it's all-or-nothing so you wouldn't be left with a half edited file. Let me know which behaviour is better, I'm fine either way.
| case outcome of | ||
| Left msg -> do | ||
| -- warn and don't make any edits | ||
| pluginSendNotification SMethod_WindowShowMessage $ |
There was a problem hiding this comment.
Should this be logged, or does our infrastructure log MessageType_Warning by default?
| -> CodeAction | ||
| -> RunRetrieInlineThisParams | ||
| -> ExceptT PluginError (HandlerM Config) CodeAction | ||
| resolveInlineThis recorder state ca RunRetrieInlineThisParams{..} = ExceptT $ do |
There was a problem hiding this comment.
This function feels tricky to read, perhaps it would make sense to split this up into multiple functions with descriptive names?
Usually, I try to structure such functions like this:
- Should produce something for this call? (E.g., we can offer rewrite code actions)
- Prepare and validate the work (use custom data types here)
- Transform the validated work to the actual change
- Transform the actual change to whatever format we need (in this case, CodeAction)
Perhaps something tlike that would be useful here as well?
| editParams = | ||
| WorkspaceEdit (Just $ asEditMap $ concat replacements) Nothing Nothing | ||
|
|
||
| return (errors, editParams) |
There was a problem hiding this comment.
Do we want to return anything when there are errors?
| getCPPmodule | ||
| :: Recorder (WithPriority Log) | ||
| -> IdeState | ||
| -> HscEnv | ||
| -> FixityEnv | ||
| -> FilePath | ||
| -> IO (CPP AnnotatedModule) | ||
| getCPPmodule recorder state session fixities t = do |
There was a problem hiding this comment.
Should this be a rule of its own?
Probably would require us to also create rule for FixityEnv 🤔
| let parseWith contents = do | ||
| modSummary <- msrModSummary <$> | ||
| useOrFail state "Retrie.GetModSummary" (CallRetrieInternalError "file not found") GetModSummary nt | ||
| let ms' = | ||
| modSummary | ||
| { ms_hspp_buf = | ||
| Just (stringToStringBuffer contents) | ||
| -- keep comments, as GetParsedModuleWithComments does | ||
| , ms_hspp_opts = | ||
| ms_hspp_opts modSummary | ||
| `gopt_set` Opt_KeepRawTokenStream | ||
| `gopt_unset` Opt_Haddock | ||
| } | ||
| logWith recorder Info $ LogParsingModule t | ||
| parsed <- evalGhcEnv session (parseModule ms') | ||
| `catch` \e -> throwIO (GHCParseError nt (show @SomeException e)) | ||
| transformA (fixAnns parsed) (fix fixities) | ||
|
|
There was a problem hiding this comment.
This is quite expensive, when is this executed? I think this should be a rule, and I don't know why we are unsetting Opt_Haddock?
There was a problem hiding this comment.
It is and initially I thought to exclude this because of the performance implications, but it is also a really nice feature to have (especially for us working on HLS).
The getCPPmodule function is called for every target file (so for inlining/unfolding, every file that contains a call to the function being inlined).
On line 914 we have the following CPP check:
if LangExt.Cpp `EnumSet.member` extensions
then parseCPP parseWith contents
else NoCPP <$> transformA (fixAnns pm) (fix fixities)If CPP is not used then we enter the fast path and re-use the parse from the GetParsedModuleWithComments rule. If CPP is enabled, there is a somewhat questionable algorithm that forks the file into every possible CPP branch and parses all of them (see https://github.com/xich/retrie/blob/b118ec69f172f25ae3dd15baeb18801259aad69f/Retrie/CPP.hs#L179) - that's what the parseWith is for. This allows us to apply rewrites across all of the CPP branches, which is really nice, but it can be horribly slow if there are too many branches (on that point there is also an improvement to retrie that would drastically reduce the number of forks, but I haven't had time to look into this).
As for unsetting haddock, it is copied from getParsedModuleWithCommentsRule (see
GetParsedModuleWithComments does it should probably do the same.
|
@fendor Is there a specific recommendation regarding the ghc version branching? I ask because in retrie the style is to use |
This reverts commit b6f2b74.
Replace the two plugin commands with a resolve provider registered via mkCodeActionWithResolveAndCommand. This allows for better integration with editors.
stylish-haskell blanks the #if lines and parses the whole module, with the body in the previous position that caused a parse error from stylish haskell. This seems like the simplest fix.
Make traverseMaybeWithKey more obvious by explicitly stating the sequence and "drop Nothings" steps. This seems like it would be slower, but sequenceA inlines and specialises to the applicative, giving a speedup and lower memory usage.
c4f6b01 to
76c802a
Compare
Co-authored-by: fendor <fendor@users.noreply.github.com>
|
@simonhorlick We use We have this guideline in HLS for imports: Since we usually support many GHc versions, the goal is to write code from the perspective of the latest GHC version, s.t. when we drop an older GHC version, we simply delete the code we don't care about any more and don't need to refactor anything to make use of the latest API improvements. I would generally recommend the same guidelines for non import statements. |
This PR resurrects the hls-retrie-plugin.
The previous retrie plugin had a few issues that I wanted to ensure were solved by the new plugin:
This is the result of many attempts and failures, each leading to a hopefully better implementation. I have tried to document below the known shortcomings, many of these I intend to address with follow up PRs.
AI disclaimer: Some of the test cases were suggested by Claude. The plugin code itself is almost completely hand written.
What's supported
What's missing?
Testing
This PR has undergone the following tests:
Fixes #279
Fixes #307
Fixes #1758
Partially fixes #4374 (by rejecting defs with guards, for now)
Fixes #4408 (except for the ghc10 support as noted in the comments)