Sublime Forum

JavaScript automatic go to next line and indent

#1

I know this is hard, but is it possible to detect if the line is a const or let, when I type the comma at the end of the line, it will go to the next line and indent.

For example, assume below is the first line:
const test = document.querySelector('#id')

Now as I type a comma at the end like this:
const test = document.querySelector('#id'),

The expected result is:

const test = document.querySelector('#id'),

   ///Cursor should be at here, note now it's at new line and also indent.
0 Likes

#2

A naive implementation via keybindings:

    {
        "keys": [","],
        "command": "insert_snippet",
        "args": {"contents": ",\n"},
        "context": [
            {"key":"following_text", "operator": "regex_match", "operand": "$"},
            {"key": "selector", "operand": "(source.js | source.jsx | source.ts | source.tsx) - comment - string"},
        ],
    },
    {
        "keys": [","],
        "command": "insert_snippet",
        "args": {"contents": ",\n\t"},
        "context": [
            {"key":"preceding_text", "operator": "regex_match", "operand": "^\\s*(const|let|var)\\s.*"},
            {"key":"following_text", "operator": "regex_match", "operand": "$"},
            {"key": "selector", "operand": "source.js | source.jsx | source.ts | source.tsx"},
        ],
    },
1 Like

#3

Hi, is it possible for this key binding to be working for PHP too?

0 Likes

#4

just add another source.php to those source.xxx part

0 Likes