-
Notifications
You must be signed in to change notification settings - Fork 10
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
Initial work on document formatting support for PICO-8 Lua files #27
Conversation
It works in general, but there are some bugs to be fixed and features missing. See https://github.com/beetrootpaul/pico8-ls/blob/17e3ce399720b1bbb696ab4b9434468dc850bfcb/server/src/server.ts#L684-L695 for more info |
… file types introduced in a future
…nitializer, and handle 'not' operator
(answering #26 (comment) and the next one there)
I also have no idea how to collaborate effectively across 2 repos, so yes, this makes sense 😄
great, I will take a look soon! 🙂
will do 👍 (although in WebStorm, but yes, can attach ESLint as well)
great! 🎉
thx for info, I will take a look as well 👍
thx for info 🙂 So many platforms, so many different logic regarding notifying 😄
Up to you OFC and in general I think same, but let me share my 2 cents 🙂 If it is possible to not use formatter by default (I think it is, I didn't have it run automatically on save by default or something like that), then I would opt for a release which has a minimal set of features to give any added value for some people. Then improve later to make it better for more people. In other words: let's force always spaces around operators or always no spaces around them, then improve to give a choice. I think I might be less inclined for smart features, because I got used to Prettier in JS world. Before Prettier I was 100% for customisation, then Prettier got hyped and included in projects I was working on, then I finally got used to the fact that I have little control over style and the main value is in having it consistent. On the other hand… PICO-8 has limit of characters. If not that, I would for example opt for multiline table syntax as implemented first (and only?), but I know from my own experience such code style hits char limits. In the end I use Lua minified in my own PICO-8 games which allows me to write code which takes a lot of space, both vertically and horizontally. But it would be not nice to expect other users of OK, that was me sharing thoughts about level of strictness vs flexibility for code formatting 😄 In the end I think I agree with you, but also am aware that the lower the scope of something releasable, the easier it is for me to maintain motivation 😅 |
… easier start for contributors
I just treated my entire project with current formatting, it's available on a commit beetrootpaul/dart-07@cb810b0 . Some examples of errors I found:
ℹ️ I want to try fix some of those calculations before you do @japhib , since it will give me opportunity to learn codebase better 😄 Other than that there were some issues with comments' placing we are aware of. Some examples on screenshots below: One annoying (but expected) thing was that when I had a function called with 4 args, each one in a separate line because of how long it was, it got joined into a single super long line. The more interesting variant of it was when callback function got preserved multiline, but args got joined after them 😄 See screenshots below: |
and here is a commit which fixes my game's codebase for me: beetrootpaul/dart-07@af490aa (game still does not work, since wrong calculations are there due to missing parentheses, but this set of changes makes |
I added some failing tests for errors I found |
and fixed formatting of local functions |
FYI I am implementing a fix for applying parentheses in expressions like |
What is your approach for applying parentheses? Are you just going to add logic so the parser remembers which expressions have parentheses around them? That's probably the most straightforward way to do it. |
I am implementing it based on what I am used to from other formatters, which is: formatter knows better where parentheses are required and where are not. Therefore my work-in-progress fix so far bases on operator precedence: Let me know if you see this path as not the best one 😄 BTW I pushed the wip commit to GitHub right now, but please be aware I will most likely amend it later (in other words: do not rely on referring to it by its commit SHA) |
BTW I thought that it might be a good idea to have one extra test which operates on an assumption that AST of formatted code should be same as before formatting. I know it would duplicate a lot of other unit tests, so my motivation is only to have it just expressed in form of just a single test. From a very high level integration test level point of view. Something like:
And, if introducing at some point a separation of AST (Abstract Syntax Tree) from a FST (Full Syntax Tree), assertions would differ in a way that:
Even one more step further: have a lot of test Lua files for that test (added over time) and have this test in form of "for each file from dir". So it would be kind of a final safety net to catch that "something is wrong, it's not as useful as unit tests since assertion is super broad, but we have to dive deeper now and understand what is happening and write that potentially missing one more unit test to cover the edge case". WDYT @japhib ? |
And for a moment back to parentheses: do I understand correctly that what you are suggesting is to for example pass an extra "isSurroundedWithParentheses = true" param to pico8-ls/server/src/parser/parser.ts Lines 1243 to 1245 in 3aae15c
|
So my main question to you @japhib is whether you prefer us to follow a path where:
|
That's great, definitely a smarter approach. However, rather than hard-coding all the operators inside Right now, I prefer this method rather than doing the other thing I said with adding an extra "isSurroundedWithParentheses = true" param.
That seems great! I doubt we'll implement an FST ever (I'm not sure exactly what that would entail anyways), but just testing it on the AST's for now seems great. You can probably use the |
will do exactly that! 🙂
If I understand correctly, FST would include a lot of stylistic info (is there a new line? Is there an extra parenthesis pair? Is there a space after an operator? Is there a comment?) while AST is meant to represent bare bone functionality. I suppose we can put everything we need to AST and it won't hurt much? I imagine it might just make some operations unnecessary complex in the future, like "hey, I want to know if both parsed trees are equivalent functionality-wise, but I need to know how to distinguish between what is important and what is not" (like my example of a test assertion which should ignore everything that is not pure functionality). But I believe this is not something necessary for now nor for a long time 🙂 |
cc3dd39
to
057e9cf
Compare
057e9cf
to
de21ec4
Compare
Looking good. I'm working on adding comments inside various statement types, as well as preserving multiline table constructors/function calls. |
ceb6187
to
910a2de
Compare
Oh, good to know! Please take a look at my last 2 commits where I already fixed some comment insertions @japhib ! (and added more tests over last several commits). Would be good to avoid difficult merge conflicts 😄 |
…ut the right place in the AST for comments
Nice! FYI I pushed a couple more commits for adding comments within expressions like return expressions and some other things. There's a merge commit in there so it's probably going to be hard to cherry-pick the commits. Instead, I'd recommend adding this original repo as another remote, and then you can pull directly from my branch: # with your `formatter` branch checked out
git remote add upstream [email protected]:japhib/pico8-ls.git
git pull upstream brp-formatter The only failing unit test now is the one with comments inside a table constructor. Note: inside several files in this repo, there are some switch statements that look like this: Also, I found this comment of yours: // TODO: move formatter (in a separate PR) to its own folder, parallel to parser,
// then move shared statements and expressions outside parser as well.
// Remember to update test files' locations as well. Perhaps I don't understand what you mean, but I'm not sure this is a good idea. For the most part, I like the file structure how it is now. But, maybe you can elaborate on what you mean? |
eeae018
to
d5470b8
Compare
d5470b8
to
75d291f
Compare
In the end I just reset my branch on yours @japhib and added some tests from my non-merged commits. There was no reason to merge formatter changes from my commits, since I see your implementation already solves comments preservation for most of tests 🙂 FYI I was not analysing your changes nor compared whether I like my fixes for comments better or not, settling on just choosing your way as the preferred one 🙂
No problem! In fact, I wanted to keep it as they are to minimise my changes, but I haven't found an IDE's formatter config for this thing in particular. And since I keep formatting files without thinking (a so called muscle memory), it was tedious to revert that change again and again. Sorry!
Thanks for asking about that TODO marker! 👍 I left it there to bring it later to discussion, which is what happens right now 👍 So, my reasoning is:
So, you see my reasoning, but I have no strong opinion here, especially that it is your project 😄 Just sharing my thoughts 🙂 Since the topic was touched, I removed that comment for now 🙂 |
…arenthesesIfOnTheRightSide in order to avoid confusion about associativity in terms of math/programming
(there is my previous comment above, answering some of your questions, and now in a more general manner) @japhib , how do you want to proceed with this PR in order to make it good enough to be merged? Some of my thoughts:
|
RE: the formatting thing - no worries, I understand! Since it's being done automatically, you can feel free to leave it however your IDE is formatting it, and I'll fox the formatting as I like it at some later point when I merge this stuff in.
That makes sense - I had actually forgotten that all this stuff was under the
You can step out any time you like! I appreciate you giving me some motivation to move this feature forwards, and we've made a lot of good progress. I'll keep (probably pretty slowly) moving the branch forward and developing on top of your stuff, and then once all the issues & TODOs we've discussed are resolved, I'll merge it into master. I'll be sure to give you credit in the readme/changelog when the formatter is released!
This would be awesome. I'd also check that all the comments are preserved, via |
I have to admit I really enjoyed the no-pressure async approach we had here @japhib 🙂 👍
💯 👍
In commit 4328064 :
BTW In a commit d8bf459 I also added tests for how I imagine empty line preservation could work. It's just my suggestion of how formatter should work, but feel free to remove them 🙂 👍 |
…hile not exposing incomplete formatter yet
One more thing, apart from the message I wrote above: What do you think about merging the PR with formatting capability disabled @japhib ? I modified name of this PR to not indicate release ready implementation and I added commit 57e8e88 in which I turned formatting off, which means VS Code won't use formatter which is not ready for release yet (I tested if locally, that one changed line result with VS Code complaining there is no extension which can format I see 2 benefits of it:
WDYT? |
5a47d7e
to
d7c324c
Compare
Hm, I don't love the idea of merging it "just because" when it's still in an unfinished state. There's not work happening on any other branches, including the main branch, so you don't have to worry about having merge conflicts. I'll see if I can get this into a good spot soon and then merge it. |
Actually I think you're right, if I'm going to be the one continuing to work on this feature, it's easier if it's contained in the repo itself. I'll go ahead and merge it. |
OK, thx! 🙂 |
Based on top of cherry-picked 6195b55
In context of #26