microsoft / vscode

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

Code snippet control indentation #20112

Open thetrompf opened 7 years ago

thetrompf commented 7 years ago

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.

jrieken commented 7 years ago

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 commented 7 years ago

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

jrieken commented 7 years ago

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

thetrompf commented 7 years ago

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

dakotak commented 6 years ago

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 commented 6 years ago

In https://github.com/Microsoft/vscode/issues/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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 5 years ago

Is there any update for current case?

Hezkore commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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

Hezkore commented 5 years ago

My issue is still very much relevant. And I'm still not sure why my issue was closed if it's not similar to this one. @jrieken any update on my issue (#76139) or why the merge happened? It's driving me insane trying to code with this bug happening.

kripper commented 1 year ago

Maybe related to https://github.com/microsoft/vscode/issues/171439 ?

thetrompf commented 1 year ago

Maybe related to https://github.com/microsoft/vscode/issues/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 commented 1 year ago

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.