From 9a1746bc12ae01695f7b079e0526770c2f8a4a63 Mon Sep 17 00:00:00 2001 From: deathaxe Date: Sun, 27 Oct 2024 12:21:02 +0100 Subject: [PATCH] Add some helper key bindings Inspired by Jinja2 and Twig. --- Default.sublime-commands | 11 +++ Default.sublime-keymap | 183 +++++++++++++++++++++++++++++++++++++++ Main.sublime-menu | 28 ++++++ 3 files changed, 222 insertions(+) create mode 100644 Default.sublime-commands create mode 100644 Default.sublime-keymap create mode 100644 Main.sublime-menu diff --git a/Default.sublime-commands b/Default.sublime-commands new file mode 100644 index 0000000..e569601 --- /dev/null +++ b/Default.sublime-commands @@ -0,0 +1,11 @@ +[ + { + "caption": "Preferences: Liquid Key Bindings", + "command": "edit_settings", + "args": { + "base_file": "${packages}/Liquid/Default.sublime-keymap", + "user_file": "${packages}/User/Default ($platform).sublime-keymap", + "default": "[\n\t${0}\n]\n" + } + } +] diff --git a/Default.sublime-keymap b/Default.sublime-keymap new file mode 100644 index 0000000..38ab8f0 --- /dev/null +++ b/Default.sublime-keymap @@ -0,0 +1,183 @@ +[ + // Auto-pair braces also in front of , : ; so starting embeddings in CSS properties and values works well. + + { + "keys": ["{"], + "command": "insert_snippet", + "args": { + "contents": "{$0}" + }, + "context": [ + { "key": "selector", "operand": "meta.template.liquid - meta.embedded.liquid - meta.interpolation.liquid - meta.template.liquid string" }, + { "key": "setting.auto_match_enabled" }, + { "key": "selection_empty", "match_all": true }, + { "key": "following_text", "operator": "regex_contains", "operand": "^(?:[ \t\\)\\]\\},:;]|$)", "match_all": true } + ] + }, + + // Auto-pair braces also in front of closing ' and " in strings + + { + "keys": ["{"], + "command": "insert_snippet", + "args": { + "contents": "{$0}" + }, + "context": [ + { "key": "selector", "operand": "meta.template.liquid string" }, + { "key": "setting.auto_match_enabled" }, + { "key": "selection_empty", "match_all": true }, + { "key": "following_text", "operator": "regex_contains", "operand": "^(?:[ \t\\)\\]\\}\"',:;]|$)", "match_all": true } + ] + }, + + // Bindings for inserting and removing statement block. + + // Expand {|} to {% | %} when % is pressed + { + "keys": ["%"], + "command": "insert_snippet", + "args": { + "contents": "% ${0} %" + }, + "context": [ + { "key": "selector", "operand": "meta.template.liquid - meta.embedded.liquid - meta.interpolation.liquid" }, + { "key": "setting.auto_match_enabled" }, + { "key": "preceding_text", "operator": "regex_contains", "operand": "{$", "match_all": true }, + { "key": "following_text", "operator": "regex_contains", "operand": "^}", "match_all": true } + ] + }, + // Reduce {%|%} to | when backspace is pressed + { + "keys": ["backspace"], + "command": "chain", + "args": { + "commands": [ + {"command": "left_delete" }, + {"command": "right_delete" }, + {"command": "left_delete" }, + {"command": "right_delete" } + ] + }, + "context": [ + { "key": "selector", "operand": "meta.embedded.liquid" }, + { "key": "setting.auto_match_enabled" }, + { "key": "selection_empty", "match_all": true }, + { "key": "preceding_text", "operator": "regex_contains", "operand": "{%$", "match_all": true }, + { "key": "following_text", "operator": "regex_contains", "operand": "^%}", "match_all": true } + ] + }, + // Expand {%|%} to {% | %} when space is pressed + { + "keys": [" "], + "command": "insert_snippet", + "args": { + "contents": " ${0} " + }, + "context": [ + { "key": "selector", "operand": "meta.embedded.liquid" }, + { "key": "setting.auto_match_enabled" }, + { "key": "preceding_text", "operator": "regex_contains", "operand": "{%$", "match_all": true }, + { "key": "following_text", "operator": "regex_contains", "operand": "^%}", "match_all": true } + ] + }, + // Reduce {% | %} to {%|%} when backspace is pressed + { + "keys": ["backspace"], + "command": "chain", + "args": { + "commands": [ + {"command": "left_delete" }, + {"command": "right_delete" } + ] + }, + "context": [ + { "key": "selector", "operand": "meta.embedded.liquid" }, + { "key": "setting.auto_match_enabled" }, + { "key": "selection_empty", "match_all": true }, + { "key": "preceding_text", "operator": "regex_contains", "operand": "{% $", "match_all": true }, + { "key": "following_text", "operator": "regex_contains", "operand": "^ %}", "match_all": true } + ] + }, + + // Bindings for inserting and removing expression block. + + // Expand {|} to {{ | }} when { is pressed + { + "keys": ["{"], + "command": "insert_snippet", + "args": { + "contents": "{ ${0} }" + }, + "context": [ + { "key": "selector", "operand": "meta.template.liquid - meta.embedded.liquid - meta.interpolation.liquid" }, + { "key": "setting.auto_match_enabled" }, + { "key": "preceding_text", "operator": "regex_contains", "operand": "{$", "match_all": true }, + { "key": "following_text", "operator": "regex_contains", "operand": "^}", "match_all": true } + ] + }, + // Reduce {{|}} to | when backspace is pressed + { + "keys": ["backspace"], + "command": "chain", + "args": { + "commands": [ + {"command": "left_delete" }, + {"command": "right_delete" }, + {"command": "left_delete" }, + {"command": "right_delete" } + ] + }, + "context": [ + { "key": "selector", "operand": "meta.interpolation.liquid" }, + { "key": "setting.auto_match_enabled" }, + { "key": "selection_empty", "match_all": true }, + { "key": "preceding_text", "operator": "regex_contains", "operand": "{{$", "match_all": true }, + { "key": "following_text", "operator": "regex_contains", "operand": "^}}", "match_all": true } + ] + }, + // Expand {{|}} to {{ | }} when space is pressed + { + "keys": [" "], + "command": "insert_snippet", + "args": { + "contents": " ${0} " + }, + "context": [ + { "key": "selector", "operand": "meta.interpolation.liquid" }, + { "key": "setting.auto_match_enabled" }, + { "key": "preceding_text", "operator": "regex_contains", "operand": "{{$", "match_all": true }, + { "key": "following_text", "operator": "regex_contains", "operand": "^}}", "match_all": true } + ] + }, + // Reduce {{ | }} to {{|}} when backspace is pressed + { + "keys": ["backspace"], + "command": "chain", + "args": { + "commands": [ + {"command": "left_delete" }, + {"command": "right_delete" } + ] + }, + "context": [ + { "key": "selector", "operand": "meta.interpolation.liquid" }, + { "key": "setting.auto_match_enabled" }, + { "key": "selection_empty", "match_all": true }, + { "key": "preceding_text", "operator": "regex_contains", "operand": "{{ $", "match_all": true }, + { "key": "following_text", "operator": "regex_contains", "operand": "^ }}", "match_all": true } + ] + }, + { + "keys": ["enter"], + "command": "run_macro_file", + "args": {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}, + "context": [ + { "key": "selector", "operand": "meta.template.liquid - source.liquid, meta.embedded.liquid punctuation.section.embedded.begin" }, + { "key": "setting.auto_indent", "operator": "equal", "operand": true }, + { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }, + { "key": "preceding_text", "operator": "regex_contains", "operand": "%\\}\\s*$", "match_all": true }, + { "key": "following_text", "operator": "regex_contains", "operand": "^\\{%", "match_all": true } + ] + } +] diff --git a/Main.sublime-menu b/Main.sublime-menu new file mode 100644 index 0000000..4af7ceb --- /dev/null +++ b/Main.sublime-menu @@ -0,0 +1,28 @@ +[ + { + "id": "preferences", + "children": [ + { + "caption": "Package Settings", + "mnemonic": "P", + "id": "package-settings", + "children": [ + { + "caption": "Liquid", + "children": [ + { + "caption": "Key Bindings", + "command": "edit_settings", + "args": { + "base_file": "${packages}/Liquid/Default.sublime-keymap", + "user_file": "${packages}/User/Default ($platform).sublime-keymap", + "default": "[\n\t${0}\n]\n" + } + } + ] + } + ] + } + ] + } +]