Skip to content

Instantly share code, notes, and snippets.

@robrix
Last active May 8, 2018 13:41
Show Gist options
  • Select an option

  • Save robrix/fb3deddbb30b09643907 to your computer and use it in GitHub Desktop.

Select an option

Save robrix/fb3deddbb30b09643907 to your computer and use it in GitHub Desktop.
select-outside-brackets and select-scope commands, built using Bracket Matcher’s select-inside-brackets.
'use babel';
import {Range} from 'atom';
atom.commands.add('atom-text-editor', 'editor:select-outside-brackets', function () {
const editor = atom.workspace.getActiveTextEditor();
const initial = editor && editor.getSelectedBufferRange().freeze();
atom.commands.dispatch(editor.element, 'bracket-matcher:select-inside-brackets');
const range = editor && editor.getSelectedBufferRange();
if (!range.isEqual(initial)) {
const newRange = new Range(range.start.traverse([0, -1]), range.end.traverse([0, 1]));
editor.setSelectedBufferRange(newRange);
}
});
atom.commands.add('atom-text-editor', 'editor:select-scope', function () {
const editor = atom.workspace.getActiveTextEditor();
const cursor = editor && editor.getLastCursor();
const selectionRange = editor && editor.getSelectedBufferRange().freeze();
const position = cursor && cursor.getBufferPosition();
for (const scope of cursor ? cursor.getScopeDescriptor().getScopesArray().slice().reverse() : []) {
const scopeRange = editor.bufferRangeForScopeAtPosition(scope, position);
if (scopeRange && scopeRange.containsRange(selectionRange) && !scopeRange.isEqual(selectionRange)) {
editor.setSelectedBufferRange(scopeRange);
return;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment