Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Change hard coded restrictions to enable more correct indentions - #6808

Merged
maxbrunsfeld merged 1 commit into
atom:masterfrom
svanharmelen:f-improve-indenting
Jul 8, 2015
Merged

maxbrunsfeld merged 1 commit into
atom:masterfrom
svanharmelen:f-improve-indenting

Conversation

@svanharmelen

Copy link
Copy Markdown
Contributor

Currently the hard coded restrictions prevent some very valid use cases (will add a before and after screencasts at the bottom) and limit the possibilities to implement correct indentation.

Before:
before
As you can see here the bracket isn't indented correctly when moving it to the next line. The decreaseIndentPattern is correct as it does increase when removing and typing in the bracket again on the same line.

After:
after
After removing the hard coded restrictions, the bracket is indented as expected.

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@thomasjo did some more thinking and came to this solution. All existing tests now run successfully. I wouldn't mind adding more/specific tests for this, but I could use some help/pointers as to where to place those tests (in text-editor-spec.coffee, selection-spec.coffee or language-mode-spec.coffee) and what you think should be in the test exactly?

I looked around in the existing tests and there are already lots of tests around the indentation logic, so the only tests I can think of that would make sense are a test that verifies the behaviour as shown in the screencasts for both a language using brackets and a language using white spaces.

@svanharmelen

Copy link
Copy Markdown
Contributor Author

This last commit adds another improvement which I will describe in a little more detail here...

Currently there are only two regexps that can influence the indents. Those are the increaseIndentPattern (which tells Atom the indent of next line should be increased) and decreaseIndentPattern (which tells Atom the indent of the current line should be decreased).

But I found that a couple of languages would need a 3rd regexp in order to support their use cases. This 3rd regexp should be a mixture of the existing two so it tells Atom that the indent of the next line should decrease.

I’ll add a screencast to show a use case for this 3rd regexp which I would like to call decreaseNextIndentPattern. And so to keep things in sync and so they more accurate describe what they do, I would opt for renaming the current increaseIndentPattern to
increaseNextIndentPattern.

I did this already in the PR, but in a fully backwards compatible way so this can be changed over time.

As for an example, see this before screencast:
before
As you can see the new line indents to the same level as the previous one. But in this case (in Go) it should decrease the indent level.

And after:
after
Here you can see the (expected) result after these commits.

Again all existing tests are successful, but I fully understand we would need some tests before merging something like this in. But any help/pointers on those tests would be great to make sure they are what you expect and what would be needed to get this merged.

Thanks...

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@thomasjo @kevinsawicki just added some tests (and squashed the commits) for the first change (removing the hard coded restriction for decreasing indents) for both a curly-bracket language and a off-side rule language. Does these tests make sense and are they enough to cover the first use case?

If so I will move on and add some tests for the second use case (the one needing the new/additional decreaseNextIndentPattern) hoping to get this PR up-to-spec so it can be merged.

So please let me know any comments and/or concerns. Thanks!

/CC @nathansobo as it looks like you've written (at least part) of the current code that's extended in this PR.

@thomasjo

Copy link
Copy Markdown
Contributor

I'll try to find time to play around with this PR tomorrow. Just wanted to let you know it's not being ignored. Maybe someone from @atom/feedback has time to give it a go?

@svanharmelen

Copy link
Copy Markdown
Contributor Author

Cool... And thanks for the ping 😃

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@thomasjo any comments and/or feedback you can share already?

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@thomasjo @kevinsawicki @mnquintana guess you guys are pretty busy as is, but would be great to get some feedback on this one.

I myself and a few colleagues are using this for about 2 weeks now and it really improved our user experience. So would be great to get this reviewed and (if no issues/concerns) merged into Atom.

Thanks!

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

Wow thanks so much for diving in to fix this fairly gnarly issue @svanharmelen! Looking at the code now...

Comment thread spec/text-editor-spec.coffee Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this test verify the new feature that you added? I would think that in order to test the new behavior, you would need to change the indent-pattern settings in the language-coffee-script or language-javascript packages.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh, I think I see. I didn't realize that there was more to this PR than the addition of decreaseNextIndentPattern. So these tests must be about other fixes that you made to the indentation logic.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are absolutely right! Please see the first comment I made to this PR, as the existing tests are to verify that behaviour. And indeed they do not require any changes to the existing regexps as they seem to fix something that wasn't working as expected before.

As for the behaviour as described in the 3rd comment, for that indeed a regexp should be added to a language that needs/uses such behaviour. Go does, so I created a PR there which was merged just a few hours ago (atom/language-go#44) whit an additional regexp. So now that this one is merged I can add some more tests to verify the additional behaviour as well. Will do that shortly...

Comment thread src/language-mode.coffee Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't fully understand what this new code does, and I'm a little bit concerned about the performance implications of calling testSync() in doubly-nested loop. Just to help me understand: could you explain in a little more detail your intent behind the while loop and the inner for loop?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Of course... To determine the correct level of indentation it will search for the most recent line that was indented and then calculate the current indent level based on the rows between the last indented row and the current row.

So the first part of the while loop searches for a previous line that matches the indent pattern. As soon as 1 is found, it will enter the for loop and process every row between the two. After entering the for loop, it will never get back to the while loop but will return the function there.

Does this help or do you want/need more insights/reasoning?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@maxbrunsfeld additionally please note that it will only ever get to the while and for loops when you insert a newline (press return) and the trailing chars that will then move to the newline, match a decrease or decreaseNext pattern.

So this code path itself will be triggered by every newline (return), but in most cases the loops will never be reached.

Still it should be efficient of course! But I do believe it is. I myself (and some colleagues) are using this for some time now and I can tell any delays or something alike. Don't know if there are benchmark tests for something like this to show the impact?

@mnquintana

Copy link
Copy Markdown
Contributor

@svanharmelen Would you mind merging in / rebasing master and fixing the merge conflicts?

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@mnquintana done...

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@maxbrunsfeld just updated the PR so Atom now uses version 0.27.0 of the language-go package as that version is specifically updated to contain a decreaseNextIndentPattern. So with this new version I could add a test for the last part of this PR.

I believe all changes are now fully covered with tests so I would like to know what I can do next to get this merged? It would be really awesome to get this in Atom v1.0, but even if that's not an option we should really try to get this in as it improves the auto indentation a lot.

So please let me know your thoughts and how to continue.

/CC @thomasjo @kevinsawicki

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@maxbrunsfeld @mnquintana just rebased again to have a clean/merge-able PR. Any update on how to proceed?

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@thomasjo @maxbrunsfeld @kevinsawicki I really appreciate you guys a very busy, but this PR is open for about a month now and even through I did receive some comments, after following up on them it got pretty silent again.

I really believe this is a good fix that improves the overall indenting capabilities of Atom and does not have any general performance drawbacks. If you know of a way how to benchmark that (if that is a concern) then please let me know how to do that so I can take care of that.

Otherwise I don't know what else I can/should do to get this merged, so please if any of you can spare a minute? Thanks!

/CC @atom/feedback @anyone?

@thomasjo

Copy link
Copy Markdown
Contributor

Sorry! Been super busy, but will try to play around with it over the weekend.

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@thomasjo I understand... And that would be really great! Will be awaiting your comments...

Thx!

@svanharmelen

Copy link
Copy Markdown
Contributor Author

Hi @thomasjo... Hope you had a good weekend and had some time to play with this? Any feedback would be great in order to get this one going. Thx...

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

Ok, really sorry for the delay in giving you further feedback on this @svanharmelen. We've been super busy and this change is pretty involved. Thanks again for taking it on. Here is my take on it:

  1. It looks like you must have squashed the commits that removed the hard-coded indentation restrictions in selection.coffee and the ones that added the decreaseNextIndentPattern setting. I'd really like to see this work un-squashed because my feeling is that we should definitely remove the hard-coded restrictions as you described, but I don't think that we should add the new indentation regexp (or rename the existing indentation regexps) right now.
  2. In LanguageMode::suggestedIndentForTokenizedLineAtBufferRow, I don't think it should be necessary to search all the way up the previous line that matches the increaseIndentPattern. Maybe this was only needed to support the decreaseNextIndentPattern feature? I could be mistaken, but I think that because of the way these patterns work, it should always be sufficient to find the previous non-blank line.

Do you still have the un-squashed version of your work from this PR? Could you submit a second PR that references this one, and just addresses the problems that originally described at the beginning of the PR?

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@thomasjo @maxbrunsfeld thanks for the update!

I must admit I think it's a bad idea to only fix the hard-coded indentation restriction and not add the decreaseNextIndentPattern feature as I believe with these two fixes together we are really fixing and improving the overall indentation capabilities of Atom to a level that will allow 99% (maybe even a 100%, but there probably some more corner cases out there I don't know about) of all use cases to be handled properly. Personally I feel that with this PR the indentation support and capabilities are now better in Atom than I'm used to have in other editors like Sublime (which already does a pretty good job for most languages).

As for your reasoning why you would like to hold of on the decreaseNextIndentPattern feature, I think your having the wrong impression of the code needed to fix the hard-coded indentation restriction properly. Looking all the way up for the last line matching the increaseNextIndentPattern is not needed for the decreaseNextIndentPattern feature at all. It's needed for fixing the hard-coded indentation restriction for languages like coffee-script (so any off-side rule languages that is).

But I just had another look (spent a couple of hours actually) to see if there really wasn't another way to get the same result without the additional while and for loops and I think I might have found a much, much better solution (sometimes looking at it again after a little while helps giving new insights 😉).

So let me run some tests and work with the new code for a day or two to see if everything still works as intended after which I will update the PR and give you a ping again for another review.

As for renaming increaseIndentPattern to increaseNextIndentPattern, of course there is no real need for this one. Still I do believe the updated name describes the purpose and use of the regexp much better and with that it improves the readability of the code. Especially taken into account that increaseIndentPattern and decreaseIndentPattern seem to imply the same behaviour, but actually they behave very different (increaseIndentPattern only has effect on the next line where increaseIndentPattern has effect on the current line).

The code in this PR is currently allowing both pattern names so it doesn't require any immediate changes in existing (language) packages. So we can safely merge/update this now and then start updating all the existing (language) packages in the upcoming time (I will personally make a PR for that in every language package I can find 😉). And then later on we can make the increaseIndentPattern deprecated for a while and again a bit later really remove support for the old pattern name.

But as said let me test and work with the updated solution first and if everything looks good, I'll update the PR and we can have another look. I think the current solution looks very clean and so I would say adding the decreaseNextIndentPattern feature should almost be a no-brainer (it requires only 2 additional lines in the suggestedIndentForTokenizedLineAtBufferRow function and if decreaseNextIndentPattern doesn't exist in your language package it will just skip the related code anyway).

I'll ping you again in a day or two. Thx!

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

But as said let me test and work with the updated solution first and if everything looks good

@svanharmelen That sounds great. Thanks so much for sticking with this.

I must admit I think it's a bad idea to only fix the hard-coded indentation restriction and not add the decreaseNextIndentPattern feature

I'm not dead-set opposed to adding this feature, but because it constitutes a new API that we would need to support indefinitely, we need to be more cautious about adding it than the other change that you had originally addressed with this PR. I'm also considering the fact that currently, we support a subset of TextMate's indentation system (see section 24.2.2 of the texmate docs). TextMate does not have a separate regex for decreaseNextIndent, and generally I find its indentation suggestions to be good. That said, there could still be room for improvement.

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@maxbrunsfeld could you give Travis a little push to build/test the PR again (as the reason it currently failed wasn't due to code itself)?

Also please take a minute to review the PR again. I changed/updated the solution so it doesn't need/use any loops anymore, so I think the current solution is much better and cleaner.

As for renaming increaseIndentPatternto increaseNextIndentPattern and for adding the decreaseNextIndentPattern feature, I would really like to hold on to these changes. I really believe they improve the capabilities a lot and with these changes it enabled Atom to handle indentations better then other existing text editors currently can.

You referenced the TextMate docs in which I noticed they also acknowledge the need for a increaseNextIndentPattern like feature, but they called it indentNextLinePattern. Reading their docs, it seems the logic/reasoning behind the indentNextLinePattern is actually the logic currently implemented in Atom but then Atom uses the increaseIndentPattern name for it. So if you like to stay close to the TextMate API then of course I'm fine with renaming increaseIndentPattern to indentNextLinePattern instead of increaseNextIndentPattern.

As for the decreaseNextIndentPattern (which could then become the dedentNextLinePattern), I see they currently don't offer a solution like this in TextMate, but I think they should. How else can a text editor dedent a line after it came across a continuation indent? The only why to automatically dedent after that, is by being able to dedent based on the content of the previous line.

Thanks!

@thomasjo

Copy link
Copy Markdown
Contributor

Bumped Mr. Travis CI. Hopefully it goes 💚

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@thomasjo unfortunately it seems I got the same error again: Request for package information failed: atom.io is temporarily unavailable, please try again later. Can you maybe check/confirm what is going on and let me know if I can/must do anything to get past this? Thanks...

@thomasjo

Copy link
Copy Markdown
Contributor

@svanharmelen Not your fault. The problem is caused by a license change in jschardet. Either we need to fix the version of the dependency to v1.1.1, or we need to replace the dependency.

/cc @atom/feedback

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@thomasjo check... Then I'll just wait for the feedback from @maxbrunsfeld 😃

@svanharmelen

Copy link
Copy Markdown
Contributor Author

Please note that the Travis failure has noting to do with the code itself. It seems atom.io was unavailable... Travis was triggered by rebasing my PR on v1.0.0.

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@maxbrunsfeld it's been a pretty silent week... Any change you could pick this one up again?

Thanks!

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

Sorry @svanharmelen. I forgot to let you know that I was on vacation last week. Testing now...

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

Ok, looks good. I like the way that you cleaned up ::suggestedIndentForTokenizedLineAtBufferRow. One thing though: I don't want to rename increaseIndentPattern to increaseNextIndentPattern right now; I'd prefer to leave it as increaseIndentPattern. We can always rename it later, separately from this PR. Would you mind removing the code for dealing w/ the new config setting name?

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@maxbrunsfeld sure thing, I'll update the PR shortly so we keep using increaseIndentPattern just like TextMate does.

And to be better inline with the naming TextMate uses, I will also rename decreaseNextIndentPattern to dedentNextLinePattern (matching it's existing counter part indentNextLinePattern).

Will update as soon as atom/language-go#47 is merged so the tests can use the update name to confirm all is still good and we can get this one merged in 😃

@joefitzgerald

Copy link
Copy Markdown
Contributor

Kudos to @svanharmelen for the persistence and patience on this!

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@maxbrunsfeld just updated the PR like discussed yesterday...

@maxbrunsfeld

Copy link
Copy Markdown
Contributor

And to be better inline with the naming TextMate uses, I will also rename decreaseNextIndentPattern to dedentNextLinePattern (matching it's existing counter part indentNextLinePattern).

I'm not sure if makes sense to adopt that naming convention. TextMate's indentNextLine is specifically for the case where you only want to indent one line, not all subsequent lines. I actually thought decreaseNextIndentLinePattern was more consistent with our other setting names.

/cc @nathansobo what do you think about naming here?

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@maxbrunsfeld you could have saved me some time and effort if you would have told me this yesterday after I suggested the change, instead of now after I updated language-go and this PR.

But I'm fine either way so please let me know how you want it and I'll make it so...

@nathansobo

Copy link
Copy Markdown
Contributor

Yeah, please keep in mind that we're dealing with hundreds of existing language bundles. The value of changes, even if the names are more optimal, needs to be weighed against the cost of forcing everyone to change or the potential confusion caused by supporting two options that do the same exact thing. We need to be conservative with changes and find ways to blend in with what exists the best we can. I wish that weren't the case, and sometimes it will be worth it to make a breaking change. In the case of these regex names I don't think it is.

@nathansobo

Copy link
Copy Markdown
Contributor

So, to clarify, I think decreaseNextIndentPattern fits most cleanly into our existing naming scheme. I agree that increaseIndentPattern would be better if it were called increaseNextIndentPattern, but I don't think it's worth the cost of changing it on an ecosystem-wide level.

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@nathansobo I'm fully aware of the impact and agree it's important to get the naming right... So I'm perfectly fine with having this discussion 😃

Will update and rebase the PR in a few...

…g a 3rd regexp

Currently there are only two regexps that can influence the indents.
Those are `increaseIndentPattern` (which tells Atom the indent of next
line should be increased) and `decreaseIndentPattern` (which tells Atom
the indent of the current line should be decreased).

But I found that a couple of languages would need a 3rd regexp in order
to support their use cases. This 3rd regexp should be a mixture of the
existing two so it tells Atom that the indent of the next line should
decrease.

I’ll add a screencast to show a use case for this 3rd regexp which I
would like to call `decreaseNextIndentPattern`.
@maxbrunsfeld

Copy link
Copy Markdown
Contributor

Alright. This thing's done. 🍴

Thanks for sticking with it @svanharmelen.

maxbrunsfeld pushed a commit that referenced this pull request Jul 8, 2015
Change hard coded restrictions to enable more correct indentions
@maxbrunsfeld
maxbrunsfeld merged commit 009a65c into atom:master Jul 8, 2015
@thomasjo

thomasjo commented Jul 8, 2015

Copy link
Copy Markdown
Contributor

Great job @svanharmelen!

@svanharmelen

Copy link
Copy Markdown
Contributor Author

@maxbrunsfeld thank you too for sticking with me as well 😉

@svanharmelen
svanharmelen deleted the f-improve-indenting branch July 8, 2015 18:54
@kevinsawicki

Copy link
Copy Markdown
Contributor

This was just released in Atom 1.0.2, thanks @svanharmelen 👍

@svanharmelen

Copy link
Copy Markdown
Contributor Author

Cool 😃

One thing left I assume... Documenting the new decreaseNextIndentPattern that can be used from now on in the language bundles. Any pointers on how and where to do that @maxbrunsfeld @kevinsawicki ?

@thomasjo

thomasjo commented Jul 9, 2015

Copy link
Copy Markdown
Contributor

@svanharmelen Ideally in the Flight Manual at least, specifically in https://github.com/atom/docs/blob/master/book/03-hacking-atom/sections/xx-language.asc, but unfortunately that section was never finished it seems like.

Maybe you feel like taking on the challenge of beginning to document the language grammar support in Atom? That'd be amazing ✨

@svanharmelen

Copy link
Copy Markdown
Contributor Author

Uuh... Hmm... Think I'll have to think about that one 😉

But thanks for the info!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants