Add semi-colon at end of line and return?

@michaelaye Try this:

atom.commands.add atom.views.getView(atom.workspace), 'custom:semicolonize', ->
  editor = atom.workspace.getActiveTextEditor()

Makes me think of a similar scenario for the autocomplete service:
If autocompleting a method name, it could/should add an opening and closing bracket immediately afterwards; and if the language of the file is one that requires semicolons, add that at the end as well.
After that it should return inside the brackets.

Doesn’t autocomplete already support snippets? If so then this would seem doable.

1 Like

Ok, so I got the command implemented. Actually, I implemented the colonized version of it:

atom.commands.add 'atom-workspace', 'custom:colonize', ->
  editor = atom.workspace.getActiveTextEditor()

because I want this ability for Python. I got 2 probs with it.

  1. The keyboard command does not activate within a Python grammar, am I missing something? I got simply this in keymap.cson:

    ‘shift-enter’: ‘custom:colonize’

  2. If I activate the command via the command palette, it does not respect the current Python indent. How could I achieve that?

Thanks for helping an Atom noob. This editor is great fun!

Have you tried using the Keybinding Resolver (Cmd+. on OS X and Ctrl+. on other platforms)? It will show you what key Atom saw you press and the command that was triggered because of it.

I’m not sure what you mean by this. Can you give some repro steps?

Used the Keybinding Resolver. I used Shift-Enter as described above. It shows me that first (in green font) ‘editor:newline’ from atom-workspace atom-texteditor:not([mini]) has been used and in the 2nd line (in black font) my command is listed, with the 2nd column stating atom-text-editor. Do I have to control their order somehow?

With not respecting the current Python indent I simply mean that if my current line starts in column 5 and I execute my command via the command palette, the cursor ends up being in column 1 instead of column 5.

In essence, yes. Key bindings follow CSS specificity rules:

So what you have to do is make your key binding at least as specific as the built-in one. The easy way is to copy the selector from the winning one and use that for your key binding:

'atom-workspace atom-texteditor:not([mini])':
  'shift-enter': 'custom:colonize'

For this, you could extend the command to detect the indentation level of the current line and duplicate that when inserting the text.

Does not work, now only the previous winning one appears and mine is gone.

Oops … I copy-pasted your text for the selector without proofreading, it should look like this:

'atom-workspace atom-text-editor:not([mini])':
  'shift-enter': 'custom:colonize'

Sorry, my bad. Works now. For the indentation, all that was required was adding this to the end of the function:


and it seems to work now, cool!

1 Like

But the enter/return key already knows how to do that, so wouldn’t it be better to execute that command instead of inserting a literal newline character?

In Python, if I’m not mistaken, then after a colon you get an additional indent.

1 Like

and how do I press programmatically the Enter/Return key? :wink:

Good point. So I created a Python file and I entered some Python code and I turned on the keybinding resolver and then I hit the enter/return key, and the resolver told me it’s the command “editor:newline”. Now the next question is how do we run a command programmatically?

I would now search the Atom source code for that command name to see what it does – maybe it’s just a single function that you can call, too.

well, my problem is solved with this command I mentioned above. What’s wrong with using that?

You can execute a command programmatically using atom.commands.dispatch or (a quick search led to) TextEditor.insertNewline.

I apologize. I was overshooting the target. Of course, the command you mention is very good. There is nothing wrong with it. I was just being stubborn and following my own train of thoughts. Very silly.

There’s nothing wrong with an academic train of thought. I was just wondering if you had a problem in mind that wasn’t solved yet. :wink:

If I’m not mistaken, TextEditor.insertNewLine places a “\n” so won’t resolve the auto indent issue. So using:

atom.commands.dispatch(atom.views.getView(editor), 'editor:newline')

would be best out of those two alternatives. l actually think the newline, rather than the autoIndent, command is also (ever so slightly) more elegant an approach here as then you’re just using the ‘all in one’ core command. Ridiculously small difference between the two in this case though, so that’s just me being picky :stuck_out_tongue:

Necropost, but for posterity…

Add the following to

atom.commands.add 'atom-workspace', 'custom:semicolonize', ->
  editor = atom.workspace.getActiveTextEditor()

…and the following to keymap.cson: (Replace ‘cmd’ with ‘ctrl’ or some other binding in the following if you’re using Windows/Linux)

'atom-workspace atom-text-editor:not([mini])':
  'shift-cmd-enter': 'custom:semicolonize'

…and restart Atom (or cmd-shift-P and type ‘reload’). The above will respect autoindentation and links shift-cmd-enter to your semicolonize function, following the IntelliJ IDEA statement completion keymapping, albeit (obviously) not context aware.

I created a package that pretty much just implements the code by @aromn

1 Like