-
Notifications
You must be signed in to change notification settings - Fork 32
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
prepend decl tests #116
base: master
Are you sure you want to change the base?
prepend decl tests #116
Conversation
Hi @santiweight I am glad to see you jumping in on this stuff. Firstly, adding a decl and managing the comments around it is intended to be something that "just works", and probably has the most, terribly hairy code devoted to it. As such, you should be modelling your tests on the In essence,
I generally run If you look at it you will see the commented out debris from prior tests. The short form
All of these generate a file next to the original source file with a suffix of
I am currently writing a lot of rust code, and autoformatting. It is a liberating experience, and I feel frustrated not being able to do it on the haskell code. So in principle I am not against it. We just need to be careful about timing, as I currently have the
As per my other email, in principle fine, so long as we sync with GHC check-exact too. |
I'm excited to be involved :) There's a lot of potential here for HLS and I'm excited to be a guinea pig!
I think I have some confusion here. For example, in my current case, I am attempting to prepend an I tried a few things, and I came up with something that works-ish (this MR). But it's helpful for me to understand the things that don't work. If you could help me understand, for example, why this doesn't work: addLocaLDecl7 :: Changer
addLocaLDecl7 libdir top = do
Right (L ld (ValD _ decl)) <- withDynFlags libdir (\df -> parseDecl df "decl" "nn = 2")
let decl' = setEntryDP (L ld decl) (DifferentLine 1 5)
doAddLocal = do
let lp = makeDeltaAst top
ds <- balanceCommentsList =<< hsDecls lp
(unzip -> (ds', _)) <- flip mapM ds $ \d -> do
modifyValD (getLocA d) d $ \ _m ds -> pure (wrapDecl decl': ds, Just ())
replaceDecls lp $ ds'
let (lp',_,w) = runTransform doAddLocal
debugM $ "addLocaLDecl7:" ++ intercalate "\n" w
return lp'
==============
d1 = 1
where -- c1
w1 = 1
------>
d1 = 1
where
nn = 2 -- c1
w1 = 1
d2 = 1
where w2 = 1
------>
d2 = 1
where
nn = 2 w2 = 1 I'm being a little obtuse, since in this example I've put no effort into fixing the anchors. But if we start here, what exactly is the right approach to make it work? From my perspective, I feel like I have to implement what I did in this MR ( At face value, it appears that Is the right approach to alter the anchors prior to returning the new list of decls in
Thank you for explaining the repo wrt tests and formatting etc. I'll let you tell me when the right time is to add formatting. It has been helpful to understand better what your current approach is, and I feel much more ready to contribute. |
@santiweight I took a quick look at this. The heart of the ghc-exactprint/src/Language/Haskell/GHC/ExactPrint/Transform.hs Lines 1199 to 1208 in 3b36f5d
and in turn the key working parts of that is
So the work done in your ghc-exactprint/src/Language/Haskell/GHC/ExactPrint/Transform.hs Lines 932 to 948 in 3b36f5d
And in fact probably somewhere in the implementation of Some other hints
|
Thanks - that's exactly what my impression was, but I didn't want to put the time in before you could confirm my approach. I will put the time in now :) |
I've been thinking about this for a bit, but I'm worried about adding the logic to replaceDecls as you suggest. When we receive the new list of decls, it makes a lot of sense to automatically handle the anchor for adding a new where if needed, and to be faithful to the indentation provided by the new decls, which is what the name But let's look at the follow two functions:
I don't see how both of these functions could be supported by a reasonable implementation of
For my The only way to do this is to do would be to perform RealSrcSpan comparisons and check which are the old and new decl, but that is no different from exposing a collection of new functions Finally, what if I want to implement My current thinking is to keep replaceDecls as it is right now: a helper function that removes the need to manually insert Obviously, I'm quite new to GHC's exactprint, so this is only my newbie thoughts. Perhaps it'd be good to hop on a call? I feel like I would gain understanding quicker that way! |
86864e8
to
98f3fb3
Compare
Hey @alanz :)
I tried out adding something that I would want (not need) for HLS plugins: prepending a declaration onto a list of declarations. I also wanted to try this out to see if I'm being sane in general when using the API.
Let me know if this feature seems valuable, in which case I will add it to the high-level API in the Transform module. The goal is simply to allow adding a declaration at the start of a where clause, which is a nice middle-ground of simple but non-trivial.
Hopefully, my using the API can be helpful to you as feedback - so please do ask lots of questions :)
Some things I bumped into: