Skip to content

Instantly share code, notes, and snippets.

@darklight721
Last active February 7, 2017 16:05
Show Gist options
  • Save darklight721/1eb9badf1f4d0425918a to your computer and use it in GitHub Desktop.
Save darklight721/1eb9badf1f4d0425918a to your computer and use it in GitHub Desktop.
An atom command and keymap to quickly end lines with semicolons. Only applies for javascript files. Code is based from turbo-javascript package.
# Open up your atom's init script file and add the following lines, then restart atom.
path = require 'path'
endLine = (insertNewLine) ->
editor = atom.workspace.activePaneItem
if path.extname(editor.getPath()) is '.js'
editor.getCursors().forEach((cursor) ->
editor.moveCursorToEndOfLine()
line = cursor.getCurrentBufferLine().trim()
if line && ';({,=?:'.indexOf(line[line.length - 1]) is -1
editor.insertText(';')
)
if insertNewLine
editor.insertNewlineBelow()
atom.workspaceView.command 'easy-semicolon:end-line', -> endLine(false)
atom.workspaceView.command 'easy-semicolon:end-new-line', -> endLine(true)
# Open up your atom's keymap file and add the following lines.
# This will overwrite the default cmd-enter, but should still work the same for file types other than js.
'.workspace .editor:not(.mini)':
'cmd-enter': 'easy-semicolon:end-new-line'
'cmd-;': 'easy-semicolon:end-line'
@darklight721
Copy link
Author

[cmd] + [enter] to terminate current line with semicolon and insert and go to new line.
[cmd] + [;] to terminate current line with semicolon

@KevinBatdorf
Copy link

Updated version for the newest API (as of this posting)

# init.coffee

path = require 'path'

endLine = (insertNewLine) ->
  editor = atom.workspace.getActiveTextEditor()

  if path.extname(editor.getPath()) is '.js'
    editor.getCursors().forEach((cursor) ->
      editor.moveToEndOfLine()
      line = cursor.getCurrentBufferLine().trim()
      if line && ';({,=?:'.indexOf(line[line.length - 1]) is -1
        editor.insertText(';')
    )
  if insertNewLine
    editor.insertNewlineBelow()

atom.commands.add 'atom-text-editor', 'easy-semicolon:end-line', ->  endLine(false)
atom.commands.add 'atom-text-editor', 'easy-semicolon:end-new-line', ->  endLine(true)

@SamGerber-zz
Copy link

Further updated version using atom's scoping, instead of file extension, thereby extending support to embedded JS. It also excludes comments.

endLine = (insertNewLine) ->
  editor = atom.workspace.getActiveTextEditor()

  editor.getCursors().forEach((cursor) ->
    editor.moveToEndOfLine()
    line = cursor.getCurrentBufferLine().trim()
    console.log cursor.getScopeDescriptor()
    if line &&
        ';({,=?:'.indexOf(line[line.length - 1]) is -1 &&
        cursor.getScopeDescriptor().scopes.some((scopeDescriptor) ->
          return scopeDescriptor.match(/.*source\.js.*/)
        ) &&
        cursor.getScopeDescriptor().scopes.every((scopeDescriptor) ->
          return !scopeDescriptor.match(/.*comment.*/)
        )

      editor.insertText(';')
  )
  if insertNewLine
    editor.insertNewlineBelow()

atom.commands.add 'atom-text-editor', 'easy-semicolon:end-line', ->  endLine(false)
atom.commands.add 'atom-text-editor', 'easy-semicolon:end-new-line', ->  endLine(true)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment