microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
164.17k stars 29.28k forks source link

Trailing comma-aware move line up/down #86467

Open wojtekmaj opened 4 years ago

wojtekmaj commented 4 years ago

Some files like JSON or pre-ES6 JavaScript files don't allow trailing commas, so moving last line in a block up or moving other line to the bottom causes these files to be corrupted and in a need of manual editing.

{
  "someProperty": 1,
  "otherProperty": 2,
  "anotherProperty": 3 /* <-- My cursor is in this line */
}

Now, I press option+ and...

Current behavior

{
  "someProperty": 1,
  "anotherProperty": 3
  "otherProperty": 2,
}

...oh no! two errors!

Expected behavior

{
  "someProperty": 1,
  "anotherProperty": 3,
  "otherProperty": 2
}

Everything good!

rdhar commented 3 years ago

Would be really handy for tidying up trivial linting errors.

zardoy commented 1 year ago

Hi all, as I understand builtin Move Line Down / Move Line Up have only language configuration understanding for indentation, but overall its just a text operation. They don't communicate with extensions and don't have any deep knowledge of the language. Of course, builtin JSON extension can detect line moves, but adding additional edits after line moves will pollute the undo stack.

Much easier solution for me was to make an extension: Move Statement that can manage commas in JSON & JS after moving statements (yes, it moves whole statements instead of lines, but now I can't imagine a case where I need to move lines specifically in JSON).

Builtin Solution

We already have API to help vscode do selection: SelectionRangeProvider. I can imagine of similar API like MoveLinesProvider to help vscode adjust the text contents after moving lines action but before inserting it into the editor. Cases are handling indentation by extension and things like commas.