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

Code snippet control indentation #20112

Open
thetrompf opened this issue Feb 7, 2017 · 18 comments
Open

Code snippet control indentation #20112

thetrompf opened this issue Feb 7, 2017 · 18 comments
Assignees
Labels
feature-request Request for new features or functionality snippets
Milestone

Comments

@thetrompf
Copy link

thetrompf commented Feb 7, 2017

How can I control unindentation in code snippets?

{
    "Create migration helper execute statement": {
        "prefix": "mhe",
        "body": [
            "\\$helper->execute(<<<SQL",
            "\t$0",
            "\rSQL",
            ");"
        ]
    }
}

When invoking it in indented context:

class Migration {
→   public function up(Helper $helper) {
→   →   mhe<tab complete snippet>
→   }
}

Expected behavior:

class Migration {
→   public function up(Helper $helper) {
→   →   $helper->execute(<<<SQL
→   →   →   |<cursor>
SQL
→   →   );
→   }
}

Actual behavior:

class Migration {
→   public function up(Helper $helper) {
→   →   $helper->execute(<<<SQL
→   →   →   |<cursor>
→   →   
→   →   SQL
→   →   );
→   }
}

This is invalid behavior when using heredoc.

I tried to insert \r to try removing all indentation,
but it is just translated to a newline and indented according to snippet context.

  • VSCode Version: 1.9.0
  • OS Version: macOS Sierra
@rebornix rebornix added the feature-request Request for new features or functionality label Apr 19, 2017
@rebornix rebornix added the editor-autoindent Editor auto indentation issues label Jun 7, 2017
@jrieken jrieken assigned jrieken and unassigned rebornix Jul 19, 2017
@jrieken jrieken removed the editor-autoindent Editor auto indentation issues label Jul 19, 2017
@jrieken
Copy link
Member

jrieken commented Jul 19, 2017

Tricky, today snippets are always adjusted to the indent of the line it is inserted at. Think of the normal for-loop. We might need some special character to signal this behaviour.

@thetrompf
Copy link
Author

I thought that was what I was doing with \r?

@jrieken
Copy link
Member

jrieken commented Jul 25, 2017

That's just interpreted as newline, like the elements in the body array...

@thetrompf
Copy link
Author

Yes I'm aware :) what I meant was, that \r could be that "special" character?

@dakotak
Copy link

dakotak commented Jun 12, 2018

To me it seems more intuitive to be able to use \b in snippets (handle ascii backspaces in the json string).

It would also be handy to have a special sequence to remove n levels of indentation, clear line, and have a reference to the starting indent level the cursor was on when the snippet was inserted.

@jrieken
Copy link
Member

jrieken commented Aug 24, 2018

In #57093 we are discussion a flag that tells the snippet controller to not adjust indentation. With that unwanted "extra" indentation can be controlled, but un-indentation isn't part of that. We can then still discuss the \b idea

@jrieken
Copy link
Member

jrieken commented Aug 24, 2018

The question with the unindent is how far to go? Should \b unindent by one indentation-unit or to the start of the line? Or should another character be use for start of like, like the start of text ascii control character?

@thetrompf
Copy link
Author

I think, if you want to go that way, we need two special characters, one for start of line, and one for unindent one. Both are useful in different contexts. In my particular example I need, start of line in order to support heredoc.

@jlcvp
Copy link

jlcvp commented Sep 6, 2018

I've came to an issue where I need to unindent the snippet by 1 indentation-unit. e.g.: imagine a lot of elseif or python's elif statements where the indentation is PART OF THE LANGUAGE.

\b support for unindent by 1 unit would be really great

@thetrompf
Copy link
Author

I think support for unindent a single indentation unit will be great too. Sadly it does not solve this particular issue, because I don't how I need to unindent, I simply need to unindent to the start of the line, which \r represents. To be honest I don't really care what the token should be.

@Monk3yDev
Copy link

Is there any update for current case?

@Hezkore
Copy link

Hezkore commented Jun 28, 2019

I've put else in both increaseIndentPattern and decreaseIndentPattern.
When I type else; the indentation works perfectly.
But when using a snippet or auto-complete; it fails.

So I'd be happy if the snippet/auto-complete issue is at least fixed.

Example:
2019-06-25_22-52-38

@thetrompf
Copy link
Author

@Hezkore I see your issue, but I don't think it's related to this. This is an issue about "calling" formatters when a snippet is "completed" all placeholders has been replaced or the snippet is "tabbed away from".

@Hezkore
Copy link

Hezkore commented Jun 28, 2019

@thetrompf Well... my bug report was closed in reference to this issue.
See #76139

@Hezkore

This comment has been minimized.

@jrieken jrieken added this to the Backlog milestone Oct 31, 2019
@kripper
Copy link

kripper commented Jan 16, 2023

Maybe related to #171439 ?

@thetrompf
Copy link
Author

Maybe related to #171439 ?

Maybe. If the issue you're referring to is solved in the way you suggest, it probably/maybe fixes this too.

But I don't agree that there is an issue in 171439. IMO if you select some code and instruct the editor to indent it, the editor shouldn't suddenly not "obey" - it is easy enough to only select the code you want to indent.

In this issue, at least how I imagined the solution, I just want snippets to adhere to the \r (carriage return) character.

@kripper
Copy link

kripper commented Jan 18, 2023

But I don't agree that there is an issue in 171439. IMO if you select some code and instruct the editor to indent it, the editor shouldn't suddenly not "obey" - it is easy enough to only select the code you want to indent.

This should be better discussed on that issue, but the thing is, it's very common having to move and indent big chunks of code and it's really annoying having to select and indent the code per blocks manually or to accidentally introduce syntax errors because you oversaw some HEREDOC tags.
You will never want to indent a HEREDOC tag because it simply breaks the code.

Indenting code is a formatting feature, and a formatting feature shouldn't break your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality snippets
Projects
None yet
Development

No branches or pull requests

10 participants