-
Notifications
You must be signed in to change notification settings - Fork 206
Avoid runtime dependency involving brittany #1653
Comments
The new items in the brittany API are so far only on a feature branch, as I was hoping on confirmation that the new API works for this. With some more comments, the new exposed function is: pPrintModule
:: Config -- ^ brittany config, same as before.
-- The previous brittany plugin code should know how to
-- obtain this.
-> ExactPrint.Anns
-> GHC.ParsedSource -- ^ these two basically come out of exactprint.
-- see https://hackage.haskell.org/package/ghc-exactprint-0.6.2/docs/Language-Haskell-GHC-ExactPrint-Parsers.html
-- brittany has some glue code around this, but HIE can and should rely on
-- its own methods to make this work.
-> ([BrittanyError], TextL.Text)
-- ^ This again is the same as for the "old" API. The existing brittany plugin
-- knows what to do with it. I have not looked at the HIE sources in a while, so I have no idea how things are set up. Judging from
you should have a (The additional glue code in brittany is mostly about setting up extensions to even allow parsing. If HIE already has parsing, you should be good to go.) I currently won't try building HIE, but if you run into specific errors in exactprint/ghc/brittany API usage I might be able to help. |
@lspitzner thank you for this function! Moreover, I was struggling to see how a different feature can be implemented with this: format only a source code selection. |
Ah yeah, I feared that would be the case. I'd have to start digging into HIE source too to figure this out. Maybe @alanz can give a hint? |
The simple answer is: Yeah, you could just do that. In my current editor I can select one declaration and format it, which just treats a selection as an (anonymous, implicit) module. However this breaks in two fashions: 1) per-module extensions are not respected 2) inline brittany config is not respected. The first item is not a problem here, because you select parts from an already-parsed syntax tree. The other item is a bit more complex. An example: -- brittany-next-binding --columns 120
foo :: Int -> IO ()
foo x = do
lots of code here now if the user wants to format just the So the proper solution definitely requires one more function in the brittany API. Something like "here is the full module AST, all the ANNs, but please only return the formatted version of this node in the syntax tree/this binding/this declaration". Should make it a bit less fiddly for you to implement, too. This will still be tricky around empty lines and comments before and after the part-to-be-formatted, but that should be manageable. |
For the last point, consider abc = 13
-- some comment
def = print True
-- final comment if you'd give brittany the whole module, but "please only format the def binding", what should it return? It could be
or
or
or even
because the final comment is attached to the |
And in the end, HIE would need to figure out what part of the textual buffer to replace, too. Feels a bit like the proper return value is a diff/patch. Then the whitespace/comment question is easy to resolve. Or at least, HIE would not need to care, because it just applies the patch, and brittany could implement it however it wanted, presuming it produced a consistent patch. |
@fendor One small follow-up question: Would it make sense to implement the "format just this function" functionality in the following manner: pass the whole module, together with a restriction "only format This depends on the API/protocol to the editor - not sure if "replace the whole file buffer with this" has downsides. |
Replace whole buffer does indeed have downsides (it breaks recorded type info and vim undo chains). We should really be generating granular changes by diffing the output of all formatters. I have not had time to look in to doing this. |
@lspitzner I think this sounds like a very reasonable API! |
I would have to check, but I think the change gets turned into a diff for the applyedit. So if it only changes certain parts, only those will show up as the edit. |
hie
binaries #1068)brittany
did the necessary changes (thanks to @lspitzner) to let us avoid that dependencyhie
binaries #1068 (comment))As a temporary alternative we could provide the binaries with a warning about brittany (we have other two formatters providers) or, if it exists, provide a manual workaround to make it work
The text was updated successfully, but these errors were encountered: