Skip to content

Instantly share code, notes, and snippets.

@mads-hartmann
Created November 23, 2015 13:54
Show Gist options
  • Save mads-hartmann/49437a98e553da9b542b to your computer and use it in GitHub Desktop.
Save mads-hartmann/49437a98e553da9b542b to your computer and use it in GitHub Desktop.
Just-one-space (from Emacs) implementation for Atom
"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