Sublime Forum

Disabling bracket wrapping for certain syntaxes

#1

I have been searching for a solution to the following problem with no luck. If anyone know’s how to fix this or what the following feature is called so I could research more myself it would be appreciated.

Issue:

In certain syntaxes Sublime will wrap a new bracket around the line below it if it contains a bracket. For example in a go text template:

<div class="somediv">
  {{ $var }}
</div>

Then I put my cursor in a new line above the brackets (like handle bars).

<div class="somediv">
  | <-- Cursor here
  {{ $var }}
</div>

Then when I start typing another set of double brackets I get.

<div class="somediv">
  { | <-- Cursor is now here
  {{ $var }}
  }
</div>

What I wanted to do was this

<div class="somediv">
  {{ $helloWorld }} | <-- Cursor is now here
  {{ $var }}
</div>

A possible solution would be to create a plugin paired with keyboard shortcut to disable this feature. Or, maybe it can be disabled within certain syntaxes?

Thanks

0 Likes

#2

This behavior is caused by the wrap_block text command which is defined in the Default.sublime-package/block.py. It is bound to the { key in certain contexts. Look for wrap_block in the default keybindings. There are a couple of bindings for { so I can’t provide a quick solution.

Unfortunately the command just checks the indention levels but doesn’t provide any syntax-limitations.

Maybe you can copy the key binding definition and add another context line to disable it quickly.

1 Like

#3

Thank you for help!

I am trying your suggestion to use contexts but am having trouble getting it to work. I am unfamiliar with keybinding contexts but I read the documentation and below is my attempt to remove it when scope is “text.html.gohtml”. The scope was copied from the console when I am within a go text template file. For some reason it’s not working.

Console command used to get scope:

$: view.scope_name(view.sel()[0].begin())

My current keybinding for wrap_block based on the default:

{ "keys": ["{"], "command": "wrap_block", "args": {"begin": "{", "end": "}"}, "context":
  [
    { "key": "indented_block", "match_all": true },
    { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
    { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
    { "key": "following_text", "operator": "regex_match", "operand": "^$", "match_all": true },
    { "key": "selector", "operator": "not_equal", "operand": "text.html.gohtml" }
 ]
},
0 Likes

#4

Just missing the "match_all": true

{ "keys": ["{"], "command": "wrap_block", "args": {"begin": "{", "end": "}"}, "context":
  [
    { "key": "indented_block", "match_all": true },
    { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
    { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
    { "key": "following_text", "operator": "regex_match", "operand": "^$", "match_all": true },
    { "key": "selector", "operator": "not_equal", "operand": "text.html.gohtml", "match_all": true }
 ]
},
0 Likes

#5

I think you would be better of triggering when the scope selector does match, and inserting just a { character (and the closing } , I guess its desired as auto match is enabled) instead of executing wrap_block, because the default keybinding will still apply otherwise and you won’t get the behavior you want

0 Likes

#6

Hi Keith,

Thanks for your advice, but I am unsure of how to approach the method you described. Would you be able to go into more detail or point me towards a similar example/documentation.

Thanks

0 Likes