Auto indent method chaining in Sublime Text 3
Asked Answered
L

2

14

As I type the following javascript code (exact keystrokes):

myObject
.doSomething()
.doSomethingElse();

I'd expect it to automatically become the following...

myObject
    .doSomething()
    .doSomethingElse();

... as would any other programmer, right?

How do I enable this, and why isn't it enabled by default?

Lough answered 24/11, 2014 at 3:46 Comment(1)
I am searching for this feature as well. Have you found anything?Use
N
13

You can use Sublime Code formatter with support JavaScript/JSON Beautifier, this link will show you how to usage this plugin.

Or another way is to insert your code inside Key Bindings - User file:

{"keys": ["alt+shift+f"], "command": "reindent", "args": {"single_line": false}}
Nostradamus answered 1/8, 2015 at 21:29 Comment(2)
@Use I don't really use sublime text anymore. If this works for you, I'll accept this answer.Lough
Built in reindent won't format code as expected. Contrary it will become 'as typed', because it's the way st3 thinks it should be.Gunwale
G
0

This happens because sublime's smart_indent is not smart enough.

You can fix indentation post factum using code formatter plugin like JsFomat.

Or preventively indent first chained method call. Following lines will have same level of indentation. This solution can be slightly improved by creating new key binding:

    {
        "keys": ["alt+enter"],
        "command": "insert",
        "args": {"characters": "\n\t"}
    }

Press alt+enter instead of enter, tab for newline to have extra indentation.

Gunwale answered 13/2, 2016 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.