Skip to content

Instantly share code, notes, and snippets.

@Beefster09
Last active May 1, 2018 17:57
Show Gist options
  • Save Beefster09/accf596069c68f20fee2c033ba5951ec to your computer and use it in GitHub Desktop.
Save Beefster09/accf596069c68f20fee2c033ba5951ec to your computer and use it in GitHub Desktop.
Useful Atom Commands
atom.commands.add('atom-text-editor', 'custom:indent-line', function() {
let editor = atom.workspace.getActiveTextEditor();
if (editor.hasMultipleCursors() || editor.getSelectedText() != '') {
atom.commands.dispatch(this, 'editor:indent-selected-rows');
}
else {
let line = editor.lineTextForBufferRow(editor.getCursorBufferPosition().row).trim();
if (line == '') {
atom.commands.dispatch(this, 'editor:indent');
}
else {
atom.commands.dispatch(this, 'editor:indent-selected-rows');
}
}
});
let TOKEN_REGEX = /\w+|'''|"""|\S/g;
let OPENERS = ['[', '{', '('];
let CLOSERS = [']', '}', ')'];
let QUOTES = ['"', "'", '"""', "'''"];
function stringType(str, index) {
let type = null;
str.replace(/'[^']*'|"[^"]*"|""".*"""|'''.*'''/g, function(match, offset) {
if (index > offset && index < offset + match.length) {
type = match[0];
if (match[1] == type && match[2] == type) {
type = match.substr(3);
}
}
return match
});
return type;
}
atom.commands.add('atom-text-editor', 'custom:next-element', function() {
let editor = atom.workspace.getActiveTextEditor();
let buffer = editor.getBuffer();
let context = [];
let commaFound = false;
let pos = editor.getCursorBufferPosition();
let line = editor.lineTextForBufferRow(pos.row);
let inString = stringType(line, pos.column);
if (inString) {context.push(inString);}
editor.scanInBufferRange(
TOKEN_REGEX,
[pos, buffer.getEndPosition()],
function(item) {
if (commaFound) {
editor.setCursorBufferPosition(item.range.start);
item.stop();
return;
}
let token = item.matchText;
let contextTop = context[context.length - 1];
if (QUOTES.includes(contextTop)) {
if (contextTop == token) {
context.pop();
}
}
else {
if (token == ',' && !contextTop) {
commaFound = true;
}
else if (QUOTES.includes(token) || OPENERS.includes(token)) {
context.push(token);
}
else if (CLOSERS.includes(token)) {
if (contextTop) {
if (token == CLOSERS[OPENERS.indexOf(contextTop)]) {
context.pop();
}
}
else {
editor.setCursorBufferPosition(item.range.start);
item.stop();
}
}
}
}
);
});
atom.commands.add('atom-text-editor', 'custom:previous-element', function() {
let editor = atom.workspace.getActiveTextEditor();
let buffer = editor.getBuffer();
let context = [];
let notFirst = false;
let lastTokenPos = undefined;
let pos = editor.getCursorBufferPosition();
let line = editor.lineTextForBufferRow(pos.row);
let inString = stringType(line, pos.column);
if (inString) {context.push(inString);}
editor.backwardsScanInBufferRange(
TOKEN_REGEX,
[buffer.getFirstPosition(), pos],
function(item) {
let token = item.matchText;
let contextTop = context[context.length - 1];
if (QUOTES.includes(contextTop)) {
if (contextTop == token) {
context.pop();
}
}
else {
if (token == ',' && !contextTop) {
if (notFirst) {
editor.setCursorBufferPosition(lastTokenPos);
item.stop();
}
}
else if (QUOTES.includes(token) || CLOSERS.includes(token)) {
context.push(token);
}
else if (OPENERS.includes(token)) {
if (contextTop) {
if (token == OPENERS[CLOSERS.indexOf(contextTop)]) {
context.pop();
}
}
else {
if (lastTokenPos) {
editor.setCursorBufferPosition(lastTokenPos);
}
item.stop();
}
}
}
notFirst = true;
lastTokenPos = item.range.start;
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment