Last active
May 8, 2018 13:41
-
-
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.
This file contains hidden or 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'; | |
| 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