Skip to content

Commit

Permalink
[JavaScript] fix tagged template string key bindings regression (#4022)
Browse files Browse the repository at this point in the history
* [JavaScript] Adjust tagged template string related key bindings

This commit...

1. scopes tagged template strings `meta.string.template` to give all of them
   a common scope without regards of used syntax highlighting
2. uses that scope in key bindings' selectors to enable them in syntax
   highlighted tagged templates.
3. restrict ` to JavaScript strings by enforcing scope order in selectors
   in binding 1 and 2
4. merge "selector" keys in binding 3 and 4

* [JavaScript] Add binding to auto-indent content whithin backquotes

This commit adds a `enter` key binding to achieve the following, when hitting
enter within two backquotes.

before:

    foo = `|`

after:

    foo = `
        |
    `

* [JavaScript] Adjust context key order

This commit moves `selector` after `settings` and `selection_empty`
as it seems to be the commonly used order in ST's default key bindings.

* [JavaScript] Add missing `match_all` attribute to selector context checks

This commit aligns selector checks with ST's default key bindings by
adding `"match_all": true` to all selector context checks.

* [JavaScrit] Simplify keybindings' context checks

This commit removes default entries.

-  operator: equal
-  operand: true

* [JavaScript] Fix typo

* [JavaScript] Tweak enter key binding

This commit changes `enter` key binding to "Add Line in Braces" macro,
as it indents line between backticks using indentation rules instead of
forcing it to be indented via `\t`.

This works also, if indentation rules decide not to indent backticked contents.
  • Loading branch information
deathaxe committed Aug 26, 2024
1 parent 80f1a61 commit 89155fe
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 86 deletions.
62 changes: 40 additions & 22 deletions JavaScript/Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,71 @@
// Auto-pair backticks
{ "keys": ["`"], "command": "insert_snippet", "args": {"contents": "`$0`"}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "(source.js, source.ts) - string" },
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "setting.auto_match_enabled" },
{ "key": "selection_empty", "match_all": true },
{ "key": "selector", "operand": "source.js - source.js meta.string, source.jsx - source.jsx meta.string, source.ts - source.ts meta.string, source.tsx - source.tsx meta.string", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|>|$)", "match_all": true },
]
},
{ "keys": ["`"], "command": "insert_snippet", "args": {"contents": "`${0:$SELECTION}`"}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "(source.js, source.ts) - string" },
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
{ "key": "setting.auto_match_enabled" },
{ "key": "selection_empty", "operand": false, "match_all": true },
{ "key": "selector", "operand": "source.js - source.js meta.string, source.jsx - source.jsx meta.string, source.ts - source.ts meta.string, source.tsx - source.tsx meta.string", "match_all": true }
]
},
{ "keys": ["`"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.js, source.ts" },
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^`", "match_all": true },
{ "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
{ "key": "setting.auto_match_enabled" },
{ "key": "selection_empty", "match_all": true },
{ "key": "selector", "operand": "meta.string.template.js - meta.string.template.js punctuation.definition.string.begin", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^`", "match_all": true }
]
},
{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Left Right.sublime-macro"}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "string.quoted.other.js" },
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "setting.auto_match_enabled" },
{ "key": "selection_empty", "match_all": true },
{ "key": "selector", "operand": "meta.string.template.js - meta.string.template.js punctuation.definition.string.begin", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "`$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^`", "match_all": true },
{ "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^`", "match_all": true }
]
},

// Add indented line in backticks
{ "keys": ["enter"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}, "context":
[
{ "key": "setting.auto_indent" },
{ "key": "selection_empty", "match_all": true },
{ "key": "selector", "operand": "meta.string.template.js", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "`$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^`", "match_all": true }
]
},
{ "keys": ["keypad_enter"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}, "context":
[
{ "key": "setting.auto_indent" },
{ "key": "selection_empty", "match_all": true },
{ "key": "selector", "operand": "meta.string.template.js", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "`$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^`", "match_all": true }
]
},

// Auto-pair interpolation
{ "keys": ["{"], "command": "insert_snippet", "args": {"contents": "{$0}"}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "string.quoted.other.js", "match_all": true },
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "setting.auto_match_enabled" },
{ "key": "selection_empty", "match_all": true },
{ "key": "selector", "operand": "meta.string.template.js", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\$$", "match_all": true }
]
},
{ "keys": ["$"], "command": "insert_snippet", "args": {"contents": "\\${${0:$SELECTION}}"}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "string.quoted.other.js", "match_all": true },
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
{ "key": "setting.auto_match_enabled" },
{ "key": "selection_empty", "operand": false, "match_all": true },
{ "key": "selector", "operand": "meta.string.template.js", "match_all": true }
]
},
]
30 changes: 15 additions & 15 deletions JavaScript/JavaScript.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -1245,62 +1245,62 @@ contexts:
- match: (css)\s*((\`){{trailing_wspace}}?)
captures:
1: variable.function.tagged-template.js
2: meta.string.js string.quoted.other.js
2: meta.string.template.js string.quoted.other.js
3: punctuation.definition.string.begin.js
embed: scope:source.css.js-template
embed_scope: meta.string.js source.css.embedded.js
embed_scope: meta.string.template.js source.css.embedded.js
escape: '{{leading_wspace}}?(\`)'
escape_captures:
0: meta.string.js string.quoted.other.js
0: meta.string.template.js string.quoted.other.js
1: punctuation.definition.string.end.js
pop: 1
- match: (html)\s*((\`){{trailing_wspace}}?)
captures:
1: variable.function.tagged-template.js
2: meta.string.js string.quoted.other.js
2: meta.string.template.js string.quoted.other.js
3: punctuation.definition.string.begin.js
embed: scope:text.html.js-template
embed_scope: meta.string.js text.html.embedded.js
embed_scope: meta.string.template.js text.html.embedded.js
escape: '{{leading_wspace}}?(\`)'
escape_captures:
0: meta.string.js string.quoted.other.js
0: meta.string.template.js string.quoted.other.js
1: punctuation.definition.string.end.js
pop: 1
- match: (js)\s*((\`){{trailing_wspace}}?)
captures:
1: variable.function.tagged-template.js
2: meta.string.js string.quoted.other.js
2: meta.string.template.js string.quoted.other.js
3: punctuation.definition.string.begin.js
embed: scope:source.js.js-template
embed_scope: meta.string.js source.js.embedded.js
embed_scope: meta.string.template.js source.js.embedded.js
escape: '{{leading_wspace}}?(\`)'
escape_captures:
0: meta.string.js string.quoted.other.js
0: meta.string.template.js string.quoted.other.js
1: punctuation.definition.string.end.js
pop: 1
- match: (json)\s*((\`){{trailing_wspace}}?)
captures:
1: variable.function.tagged-template.js
2: meta.string.js string.quoted.other.js
2: meta.string.template.js string.quoted.other.js
3: punctuation.definition.string.begin.js
embed: scope:source.json.js-template
embed_scope: meta.string.js source.json.embedded.js
embed_scope: meta.string.template.js source.json.embedded.js
escape: '{{leading_wspace}}?(\`)'
escape_captures:
0: meta.string.js string.quoted.other.js
0: meta.string.template.js string.quoted.other.js
1: punctuation.definition.string.end.js
pop: 1
- match: (?:({{identifier_name}})\s*)?(\`)
captures:
1: variable.function.tagged-template.js
2: meta.string.js string.quoted.other.js punctuation.definition.string.begin.js
2: meta.string.template.js string.quoted.other.js punctuation.definition.string.begin.js
set: literal-string-template-content

literal-string-template-content:
- meta_include_prototype: false
- meta_content_scope: meta.string.js string.quoted.other.js
- meta_content_scope: meta.string.template.js string.quoted.other.js
- match: \`
scope: meta.string.js string.quoted.other.js punctuation.definition.string.end.js
scope: meta.string.template.js string.quoted.other.js punctuation.definition.string.end.js
pop: 1
- include: string-interpolations
- include: string-content
Expand Down
Loading

0 comments on commit 89155fe

Please sign in to comment.