Created
November 23, 2015 13:54
-
-
Save mads-hartmann/49437a98e553da9b542b to your computer and use it in GitHub Desktop.
Just-one-space (from Emacs) implementation for Atom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use babel"; | |
atom.commands.add('atom-text-editor', 'hartmann:just-one-space', () => { | |
const editor = atom.workspace.getActiveTextEditor(); | |
if (!editor) { | |
return; | |
} | |
const changes = editor.getCursorBufferPositions().map((point) => { | |
const line = editor.lineTextForBufferRow(point.row) | |
const range = editor.getBuffer().rangeForRow(point.row, false) | |
const leftOfCursor = line.substring(0, point.column) | |
const rightOfCursor = line.substring(point.column) | |
const leftOfCursorNoWhitespace = leftOfCursor.trimRight() | |
const rightOfCursorNoWhitespace = rightOfCursor.trimLeft() | |
const newPosition = [point.row, leftOfCursorNoWhitespace.length + 1] | |
const newLine = `${leftOfCursorNoWhitespace} ${rightOfCursorNoWhitespace}`; | |
return { | |
range: range, | |
line: newLine, | |
position: newPosition, | |
} | |
}); | |
// This is a bit hacky, I use setCursorBufferPosition to reset all cursors | |
// and then add the remaining cursors using addCursorAtBufferPosition. | |
const [first, ...rest] = changes; | |
editor.getBuffer().setTextInRange(first.range, first.line); | |
editor.setCursorBufferPosition(first.position); | |
rest.forEach( ({ range, line, position}) => { | |
editor.getBuffer().setTextInRange(range, line); | |
editor.addCursorAtBufferPosition(position); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment