Skip to content

JavaScript: Improve indentation on insert with trailing newline #85781

Description

@ensko

Let's say I have the following JS snippet:

() => {
  a
}

() => {
  b
}

Now select the entire 'a' part, from the start of line 1 to the start of line 4, i.e. including a trailing newline, and insert it at the start of the line now containing 'b'. The result is this:

() => {
  a
}

() => {
  () => {
    a
  }
    b
}

The 'b' is now over-indented because that line received additional indentation at insert. Expected behavior: the 'b' should remain at its original indentation level. I can confirm this behavior at least for JS and JSON files.


The reason I care is that selecting whole lines with the keyboard is the easiest from start to start, so this kind of pasting is very common in my workflow. I often do Ctrl+C, Ctrl+V, Ctrl+V to duplicate lines; with this bug, this will add two additional indentations to the next line, which is potentially unrelated to the copied lines.

Activity

  1. added
    editor-autoindent
    help wanted
    javascript
    on Dec 2, 2019
  2. jzyrobert commented on Dec 4, 2019

    @jzyrobert
    Contributor

    Would something like this be acceptable?

    On the last paste line, if

    • range.endColumn is 1 (Just a newline pasted)
    • lineLength of that line is > 0 (So content existed before paste)

    Then don't add extra indent?

  3. ensko commented on Dec 4, 2019

    @ensko
    Author

    I think that's consistent with my mental model, that pasting should behave like the following:

    1. do the paste without any auto-indentation
    2. select exactly the text that was pasted
    3. use tab to indent to the level of the insertion position

    So in the case that the insertion range ended at the start of the line (your first condition), the selection from 2. would have an empty last line which would not be indented during 3. I don't care about the second condition too much, as it's trailing whitespace that can be auto-removed. So yes, that sounds good!

  4. added
    under-discussion
    on Nov 3, 2020
  5. added this to the Backlog milestone on Dec 7, 2022
  6. added 3 commits that reference this issue on May 19, 2023
    dd67538
    3b8024e
    b733589
  7. added a commit that references this issue on Jun 9, 2023
    3ae10ad
  8. added a commit that references this issue on Jul 18, 2023
    835deb4
  9. added 5 commits that reference this issue on Sep 30, 2023
    38fa637
    886e568
    6ed6108
    1e2c3d3
    8b96807
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

editor-autoindenthelp wantedjavascripttree-sitter-autoindent-candidatetypescriptunder-discussion

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions