Sublime Forum

Use regex capture group from context in insert_snippet keymap command

#1

Is it possible to use a capture group from the context of a keymap command in the argument of the keymap command?

When I am typing a single-line comment I would like to be able to press the return key and have all of the characters (tabs, spaces, comment characters) automatically inserted before the insertion point on the new line.

Current behavior on return (pipe used to indicate location of insertion point):

# This is a comment
|

Desired behavior:

# This is a comment
# |

Here is what I think this keymap could possibly look like (for now I have simply replaced \1 on line 3 with # but this ignores indentation which the capture group would include, as well as allowing for other comment characters:)

{ "keys": ["enter"], 
  "command": "insert_snippet",
  "args": {"contents": "\n\1"}, // \1 == first capture group.
  "context":
    [
        { "key": "preceding_text", 
          "operator": "regex_match",
          "operand": "^([ \t]*(#|//)[ \t]*).*$",
          "match_all": true },
    ]
},

Thanks!
Lowell

1 Like

#2

No. This needs to be implemented in a plugin.

By the way, https://packagecontrol.io/packages/DocBlockr does this.

2 Likes

#3

DocBlockr’s comment extension feature was exactly what I was looking for! Thanks for pointing me that way!

0 Likes