Newline indention on insert


#1

For example I have code like this

class X {
    func foo() {
        $
    }
}

and I want to insert this code instead of $

guard let x = x else {
    return
}

with newline after closing bracket.

What I’m expecting is

class X {
    func foo() {
        guard let x = x else {
            return
        }
        
    }
}

But I’m getting

class X {
    func foo() {
        guard let x = x else {
            return
        }

    }
}

Without indent on newline.

Im inserting text with this command

editor = atom.workspace.getActiveTextEditor()
editor.insertText(text, {autoIndent: true, autoIndentNewline: true})

Is it a bug or am I doing something wrong?
What should I do to have indent on newline?

System: macOS High Sierra 10.13.5
Atom: 1.2.4


#2

The whitespace package will eat white space in blank lines. Pressing tab will jump in line with where it should be.


#3

I want this programmatically. Can I somehow make here indent?


#4

You haven’t shared the part of your code where you tell Atom what text should be.


#5

It was just for example, but if you want a text variable it will look like this and I want indent on the last newline.

text = “guard let x = x else {\m return\n}\n”


#6

It might work to do this at the end:

atom.commands.dispatch editor.element, "editor:indent"

That command should indent the most recent cursor (which will be at the end of your inserted text).


#7

Thanks. You give me a clue. Indent does not work for me, but auto-indent was exactly what I need.

atom.commands.dispatch atom.views.getView(editor), 'editor:auto-indent'