Sublime Forum

Quest for the smart newline

#1

I have this dream of a single “smart newline” command that would be a bit like a “soft newline” on steroids. In my lexicon, a “soft newline” is a keyboard shortcut that takes the caret to the end of the current line, then inserts an ordinary newline.

A “smart newline” would do the same, only be clever enough to insert necessary characters and the end of the current line in a context-aware way, before inserting the newline.

For example:

– in python, insert “:” at the end of a line containing a correctly formed if / def / for / while statements, if said ‘:’ is missing
– in javascript, insert ‘;’ at the end of correctly formed statements, if said semicolon is missing
– in javascript, start a new scope {} at the end of a line containing a correctly formed if statement (or function delcaration, while / for statement etc): e.g., go from

if (my_shoe === true)

to

if (my_shoe === true) {
    // <- caret is right here, now!
}

when the caret is anywhere inside of if (my_shoe === true) when the smart newline command is issued

– in JSON: automatically insert commas after list elements (are we inside square brackets?) and after valid key-value pairs (are we inside curly braces?), if said comma is missing

…and so on. The general point being: the smart newline inserts whatever stuff one can automatically deduce should be there given the language syntax, to spare the programmer the trouble of doing this grunt work.

OK, long story short, I’m wondering if such a plugin exists? And if not, would it be feasible? Would the plugin have to do all the parsing itself, or could it leverage the parsing that is already being done for syntax coloring?

0 Likes

#2

I’d say this is probably possible, though adapting to a broad number of languages might be the hardest part. Something that is perhaps somewhat similar, I have enter bound in such a way that if the line is a comment and if the character prior to the point is a space, upon executing the key, it will automatically insert continuation comment characters and a space. The idea being that you can write comments in paragraph flow rather than worrying about the prefixed characters.

Given that modestly basic behavior, what you’re describing isn’t out of the realm of possibility.

0 Likes