Skip to content
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

[folding] Move line up/down should skip over folded regions or folded sections #63972

Open
ali80 opened this issue Nov 29, 2018 · 13 comments
Open
Assignees
Labels
editor-commands Editor text manipulation commands editor-folding Editor code folding issues feature-request Request for new features or functionality help wanted Issues identified as good community contribution opportunities
Milestone

Comments

@ali80
Copy link

ali80 commented Nov 29, 2018

  • VSCode Version:1.30.0-insider
  • OS Version: windows 10

I think when code is in a folded region or section it should never unfold unless explicitly commanded, so if you are moving a line using move command, the line should skip over both folded regions and folded sections
Steps to Reproduce:

  1. create a folded region using #region #endregion
  2. fold some code section
  3. move a line using alt+up/down over the folded region/section
    vscodemovelinebug

Does this issue occur when all extensions are disabled?: Yes

@vscodebot vscodebot bot added the insiders label Nov 29, 2018
@vscodebot
Copy link

vscodebot bot commented Nov 29, 2018

@rebornix rebornix added the feature-request Request for new features or functionality label Nov 29, 2018
@rebornix rebornix self-assigned this Nov 29, 2018
@rebornix rebornix added editor-folding Editor code folding issues editor-commands Editor text manipulation commands labels Nov 29, 2018
@vscodebot vscodebot bot removed the insiders label Dec 13, 2018
@aeschli aeschli changed the title Move line up/down should skip over folded regions or folded sections [folding] Move line up/down should skip over folded regions or folded sections Jan 8, 2019
@aeschli aeschli added this to the Backlog milestone Jan 8, 2019
@spott
Copy link

spott commented Oct 17, 2019

I want to add my support for this problem. It is particularly annoying when using the vim keybindings, as right after the fold is closed (using zc), the mouse is above the just folded code, so if I want to move down below the folded region, it automatically unfolds as I do so.

@rebornix rebornix removed their assignment Oct 21, 2019
@Frug
Copy link

Frug commented Jan 13, 2020

As someone who uses the keyboard to navigate (vim plugin is a godsend) it's very frustrating that collapsed regions auto expand when I try to move past them. If the collapsed region is big I totally lose my place on the page.

@beaugunderson
Copy link

beaugunderson commented Jan 13, 2020

@Frug you can set the vim.foldfix setting to true if you're using the vim plugin and it will fix this annoyance for you 😄️ (this works with simple movement but I'm not sure if it works with moving lines up and down, which is what this issue is about)

@minig0d
Copy link

minig0d commented Dec 20, 2020

Any solution to the issue without an extension? I frequently use the alt up/down (editor.action.moveLinesUpAction / editor.action.moveLinesDownAction) shortcuts and this may be one of the most annoying counter-intuitive behaviors of VSC... Honestly, if you're moving a line of text (or a block), would anyone really want folded section to unfold? I mean, isn't the reason we are folding the section because we are not actively dealing with it?

@ssmooncoder
Copy link

Every time I try to use folding, I'm reminded how buggy and unintuitive it is and why I'd disabled folding altogether.

The feature needs a rework but I think it may have been abandoned, since this issue has now been open for +2 years.

@BettyJJ
Copy link

BettyJJ commented Feb 27, 2021

I also need this feature.

BTW: I don't think it's possible to do this in a extension, since VSC doesn't seem to provide API to access the folded ranges. Unless you implement the folding feature all over again by yourself.

@hediet hediet added the help wanted Issues identified as good community contribution opportunities label Oct 27, 2021
@unikcc
Copy link

unikcc commented Mar 14, 2022

@Frug you can set the vim.foldfix setting to true if you're using the vim plugin and it will fix this annoyance for you 😄️ (this works with simple movement but I'm not sure if it works with moving lines up and down, which is what this issue is about)

Yes this is what I'm looking for. Thank you!

@fontseca
Copy link

@Frug you can set the vim.foldfix setting to true if you're using the vim plugin and it will fix this annoyance for you 😄️ (this works with simple movement but I'm not sure if it works with moving lines up and down, which is what this issue is about)

I used vim.foldfix but then when I try 10j or 100k I have a cursor delay due to the movement. I don´t know how to solve it.

@filmerjarred
Copy link

I also need this feature.

BTW: I don't think it's possible to do this in a extension, since VSC doesn't seem to provide API to access the folded ranges. Unless you implement the folding feature all over again by yourself.

Through experimentation it seems that vscode.window.activeTextEditor.visibleRanges represents currently visible folded ranges, which one could use to know what to skip.

If someone with more knowledge can confirm this is accurate and there's no reason an extension couldn't use this info to know which ranges to skip when moving lines up and down I'd be happy to write it.

@paulmsmith
Copy link

paulmsmith commented Jun 19, 2022

I'd love this to be resolved. It's not major but it is mighty frustrating, makes the feature of 'move line' much less powerful.

Did this functionality ever existing VSCode or am I remembering Sublime/Textmate?

@jzyrobert
Copy link
Contributor

I've had a look at the code starting from

public getEditOperations(model: ITextModel, builder: IEditOperationBuilder): void {

and implementing this would require some major logic changes (the logic hasn't changed in 5-7 years) as currently it assumes only 1 line will have to move:
const otherLineNumber = (this._isMovingDown ? lineNumber + 1 : lineNumber - 1);

with the indentation checking/edits also making those assumptions
const movingLineMatchResult = this.matchEnterRule(model, indentConverter, tabSize, movingLineNumber, s.startLineNumber - 1);
// if s.startLineNumber - 1 matches onEnter rule, we still honor that.

* @param futureAboveLineNumber the line which will be at the `line` position
* @param futureAboveLineText
*/
private matchEnterRuleMovingDown(model: ITextModel, indentConverter: IIndentConverter, tabSize: number, line: number, futureAboveLineNumber: number, futureAboveLineText: string) {

An implementation question would also be how to add checking whether a line belongs to a fold.

  • You could check if a decoration on that line matches inline-folded but its not the proper way and folds could be 1 line long (region) or two lines (brackets)
  • You could pass the ServicesAccessor and use it to get the ICodeEditor , then use FoldingController to get the FoldingModel and call getRegionAtLine but getFoldingModel is a Promise and then you'd have to wrap the rest of the function in a .then() block
    private foldingModelPromise: Promise<FoldingModel | null> | null;
    • Add a function to FoldingController to return FoldingModel without updating it? Could cause bugs I assume.
    • Is there a quicker way to do this?

Once you have the folding ranges you could then update the logic from "Move 1 line" to "Move n lines".

@aeschli I also noticed that moving down and up don't actually perform the same logic.

  • Moving down checks and corrects indentation for both the selection and the line below it
    // check if the line being moved before matches onEnter rules, if so let's adjust the indentation by onEnter rules.
  • But moving up only checks it for the selection, so the line that was above the selection and is now moved below it does not get indentation corrected.
    Quick video demo:
Code_81taQdj4r6.mp4

So moving the statement down and moving the bracket up should theoretically be the same but results in different edits. Is this a bug or intended behaviour?

@starball5
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editor-commands Editor text manipulation commands editor-folding Editor code folding issues feature-request Request for new features or functionality help wanted Issues identified as good community contribution opportunities
Projects
None yet
Development

No branches or pull requests