sublimehq / sublime_text

Issue tracker for Sublime Text
https://www.sublimetext.com
811 stars 39 forks source link

Auto-indent tag breaks HTML specific indention rules #3018

Open deathaxe opened 5 years ago

deathaxe commented 5 years ago

Description

The issue described at https://forum.sublimetext.com/t/indentation-when-pressing-enter-html-erb/47006 is caused by the auto_indent_tag text command which is provided by the Default.sublime-package/auto_indent_tag.py module.

The command is bound to the enter key as follows:

    { "keys": ["enter"], "command": "auto_indent_tag", "context":
        [
            { "key": "setting.auto_indent", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "selector", "operator": "equal", "operand": "punctuation.definition.tag.begin", "match_all": true },
            { "key": "preceding_text", "operator": "regex_contains", "operand": ">$", "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^</", "match_all": true },
        ]
    },
    { "keys": ["shift+enter"], "command": "auto_indent_tag", "context":
        [
            { "key": "setting.auto_indent", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "selector", "operator": "equal", "operand": "punctuation.definition.tag.begin", "match_all": true },
            { "key": "preceding_text", "operator": "regex_contains", "operand": ">$", "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^</", "match_all": true },
        ]
    },

Steps to reproduce

  1. Open Sublime Text 3 Vanille Install
  2. Create a new HTML file with the content of (1).
  3. Move the caret to in front of the </h2> (where the pipe is located)
  4. Hit enter

(1)

<html>
<body>
    <h2>
        Text of a <% heading %>|</h2>
</body>
</html>

Expected behavior

The indention level of a closing html tag should be decreased by one if it is moved to the next line, even if there are inline tags/sections like <% ... %> or <? ... ?> in the old line.

after

<html>
<body>
    <h2>
        Text of a <% heading %>
    |</h2>
</body>
</html>

Actual behavior

If inline tags/sections like <% ... %> or <? ... ?> are found in the line, the auto_indent_tag function increases the indention level if a closing tag is moved to the next line.

after

<html>
<body>
    <h2>
        Text of a <% heading %>
            |
        </h2>
</body>
</html>

Suggestions

Remove the module and let the indention rules of the HTML/XML syntax packages handle indention of tags.

If it provides more logic than can be handled via Indention Rules, it should be moved to the related syntax package as it provides syntax specific functionality.

Workaround

To disable the auto_indent_tag until a fix is shipped with ST, a user could overwrite its key binding in order to make the indention rules of the syntax definition apply.

    { "keys": ["enter"], "command": "insert", "args": {"characters": "\n"}, "context":
        [
            { "key": "setting.auto_indent", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "selector", "operator": "equal", "operand": "punctuation.definition.tag.begin", "match_all": true },
            { "key": "preceding_text", "operator": "regex_contains", "operand": ">$", "match_all": true },
            { "key": "following_text", "operator": "regex_contains", "operand": "^</", "match_all": true },
        ]
    },

Environment

gregblass commented 5 years ago

Thank you @deathaxe!!

gregblass commented 4 years ago

Any updates on this or plans to address this? It is the bane of my existence.

I'll have to look at the workaround in the mean time.

gregblass commented 4 years ago

Bump :(