2013-12-04 17:24:04 +00:00
|
|
|
define([
|
|
|
|
"command",
|
|
|
|
"editor",
|
|
|
|
"ui/statusbar",
|
2013-12-16 16:51:56 +00:00
|
|
|
"settings!user,ace"
|
2013-12-04 17:24:04 +00:00
|
|
|
], function(command, editor, status, Settings) {
|
2013-10-25 02:13:07 +00:00
|
|
|
|
|
|
|
var userConfig = Settings.get("user");
|
|
|
|
command.on("init:restart", function() {
|
|
|
|
userConfig = Settings.get("user");
|
|
|
|
});
|
2014-03-05 17:36:19 +00:00
|
|
|
|
2013-12-16 16:51:56 +00:00
|
|
|
//load the syntax commands and set them up in the command listings
|
|
|
|
var aceConfig = Settings.get("ace");
|
|
|
|
for (var i = 0; i < aceConfig.modes.length; i++) {
|
|
|
|
var mode = aceConfig.modes[i];
|
|
|
|
command.list.push({
|
|
|
|
command: "session:syntax",
|
|
|
|
argument: mode.name,
|
|
|
|
label: "Set Syntax: " + mode.label
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for (var i = 0; i < aceConfig.themes.length; i++) {
|
|
|
|
var theme = aceConfig.themes[i];
|
|
|
|
command.list.push({
|
|
|
|
command: "editor:theme",
|
|
|
|
argument: theme.name,
|
|
|
|
label: "Set Theme: " + theme.label
|
|
|
|
});
|
|
|
|
}
|
2013-09-04 19:45:06 +00:00
|
|
|
|
2013-09-06 02:44:42 +00:00
|
|
|
//this is a place to put bindings that don't have direct equivalents in Ace, but are required for Sublime compatibility
|
2013-12-24 00:15:03 +00:00
|
|
|
command.on("sublime:expand-to-line", function(c) {
|
2013-09-06 02:44:42 +00:00
|
|
|
editor.execCommand("gotolinestart");
|
|
|
|
editor.execCommand("selecttolineend");
|
2013-12-24 00:15:03 +00:00
|
|
|
if (c) c();
|
2013-09-06 02:44:42 +00:00
|
|
|
});
|
2014-03-05 17:36:19 +00:00
|
|
|
|
2013-12-24 00:15:03 +00:00
|
|
|
command.on("sublime:expand-to-paragraph", function(c) {
|
2013-10-08 01:09:35 +00:00
|
|
|
var session = editor.getSession();
|
|
|
|
var selection = editor.getSelection();
|
|
|
|
var currentLine = editor.getCursorPosition().row;
|
|
|
|
var startLine = currentLine;
|
|
|
|
var endLine = currentLine;
|
|
|
|
while (startLine > 0) {
|
|
|
|
startLine--;
|
|
|
|
var line = session.getLine(startLine);
|
|
|
|
if (line == "") {
|
|
|
|
//we'll skip the preceding space
|
|
|
|
startLine += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var length = session.getLength();
|
|
|
|
while (endLine < length) {
|
|
|
|
endLine++;
|
|
|
|
var line = session.getLine(endLine);
|
|
|
|
if (line == "") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
editor.clearSelection();
|
|
|
|
editor.moveCursorTo(startLine);
|
|
|
|
selection.selectTo(endLine);
|
2013-12-24 00:15:03 +00:00
|
|
|
if (c) c();
|
2013-10-08 01:09:35 +00:00
|
|
|
});
|
2014-03-05 17:36:19 +00:00
|
|
|
|
2013-12-24 00:15:03 +00:00
|
|
|
command.on("sublime:expand-to-matching", function(c) {
|
2013-10-08 17:21:10 +00:00
|
|
|
var Range = ace.require("ace/range").Range;
|
|
|
|
var position = editor.getCursorPosition();
|
|
|
|
var line = editor.getSession().getLine(position.row);
|
|
|
|
var depth = 0;
|
|
|
|
var openers = {
|
|
|
|
"(": ")",
|
|
|
|
"{": "}"
|
|
|
|
};
|
|
|
|
var closers = {
|
|
|
|
")": "(",
|
|
|
|
"}": "{"
|
|
|
|
};
|
|
|
|
//look for tokens inside the line first
|
|
|
|
var matchable = /(['"({])/;
|
|
|
|
for (var i = position.column; i >= 0; i--) {
|
|
|
|
if (matchable.test(line[i])) {
|
|
|
|
var match = line[i];
|
|
|
|
if (match in openers) {
|
|
|
|
match = openers[match];
|
|
|
|
}
|
2013-10-08 18:06:47 +00:00
|
|
|
for (var j = position.column + 1; j < line.length; j++) {
|
2013-10-08 17:21:10 +00:00
|
|
|
if (line[j] == match && depth == 0) {
|
|
|
|
var selection = editor.getSession().getSelection();
|
|
|
|
selection.setRange(new Range(position.row, i + 1, position.row, j));
|
|
|
|
return;
|
|
|
|
} else if (line[j] == match) {
|
|
|
|
depth--;
|
|
|
|
} else if (line[j] == closers[match]) {
|
|
|
|
depth++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//if we couldn't find any matching pairs, we'll just use the default multiline bracket selection
|
|
|
|
//this is a little wonky, but it's better than nothing.
|
2013-10-08 01:09:35 +00:00
|
|
|
editor.execCommand("jumptomatching");
|
|
|
|
editor.execCommand("selecttomatching");
|
2013-12-24 00:15:03 +00:00
|
|
|
if (c) c();
|
2013-10-08 17:21:10 +00:00
|
|
|
});
|
2014-03-05 17:36:19 +00:00
|
|
|
|
2013-12-24 00:15:03 +00:00
|
|
|
command.on("sublime:tabs-to-spaces", function(c) {
|
2013-10-25 02:13:07 +00:00
|
|
|
var session = editor.getSession();
|
|
|
|
var text = session.getValue();
|
|
|
|
var spaces = new Array(userConfig.indentation + 1).join(" ");
|
|
|
|
text = text.replace(/\t/g, spaces);
|
|
|
|
session.setValue(text);
|
2013-12-24 00:15:03 +00:00
|
|
|
if (c) c();
|
2013-10-25 02:13:07 +00:00
|
|
|
});
|
2014-03-05 17:36:19 +00:00
|
|
|
|
2013-12-24 00:15:03 +00:00
|
|
|
command.on("sublime:spaces-to-tabs", function(c) {
|
2013-10-25 02:13:07 +00:00
|
|
|
var session = editor.getSession();
|
|
|
|
var text = session.getValue();
|
|
|
|
var replace = new RegExp(new Array(userConfig.indentation + 1).join(" "), "g");
|
|
|
|
text = text.replace(replace, "\t");
|
|
|
|
session.setValue(text);
|
2013-12-24 00:15:03 +00:00
|
|
|
if (c) c();
|
2013-10-25 02:13:07 +00:00
|
|
|
});
|
2014-05-03 22:34:17 +00:00
|
|
|
|
|
|
|
command.on("ace:set-newline-mode", function(type, c) {
|
|
|
|
editor.session.doc.setNewLineMode(type);
|
|
|
|
if (c) c();
|
|
|
|
});
|
2014-03-05 17:36:19 +00:00
|
|
|
|
2014-03-05 16:52:48 +00:00
|
|
|
command.on("ace:trim-whitespace", function(c) {
|
|
|
|
var session = editor.getSession();
|
|
|
|
var doc = session.doc;
|
|
|
|
var selection = editor.getSelection();
|
2014-03-05 17:36:19 +00:00
|
|
|
var lines = doc.getAllLines();
|
|
|
|
lines.forEach(function(line, i) {
|
2014-03-05 16:52:48 +00:00
|
|
|
var range = selection.getLineRange(i);
|
2014-03-05 17:36:19 +00:00
|
|
|
range.end.row = range.start.row;
|
|
|
|
range.end.column = line.length;
|
2014-04-08 19:07:08 +00:00
|
|
|
if (userConfig.trimEmptyLines) {
|
|
|
|
line = line.replace(/\s+$/, "");
|
|
|
|
} else {
|
|
|
|
line = line.replace(/(\S)\s+$/, "$1");
|
|
|
|
}
|
2014-03-05 17:36:19 +00:00
|
|
|
doc.replace(range, line);
|
|
|
|
});
|
2014-03-05 16:52:48 +00:00
|
|
|
if (c) c();
|
|
|
|
});
|
2013-09-04 19:45:06 +00:00
|
|
|
|
2013-09-06 03:01:24 +00:00
|
|
|
//we also add a command redirect for firing Ace commands via regular command attributes
|
2013-10-08 01:18:41 +00:00
|
|
|
command.on("ace:command", editor.execCommand.bind(editor));
|
2014-03-05 17:36:19 +00:00
|
|
|
|
2013-09-11 06:15:04 +00:00
|
|
|
//unbind the keys for the palette, whatever it does.
|
2013-09-10 16:19:14 +00:00
|
|
|
editor.commands.bindKey("Ctrl-P", null);
|
2013-09-11 06:15:04 +00:00
|
|
|
editor.commands.bindKey("Ctrl-Shift-P", null);
|
2014-03-05 17:36:19 +00:00
|
|
|
|
2013-09-20 15:31:04 +00:00
|
|
|
//filter some Ace commands for UI purposes
|
|
|
|
var isRecording = false;
|
2013-12-24 00:15:03 +00:00
|
|
|
command.on("ace:togglemacro", function(c) {
|
2013-09-20 15:31:04 +00:00
|
|
|
isRecording = !isRecording;
|
|
|
|
editor.execCommand("togglerecording");
|
|
|
|
editor.focus();
|
|
|
|
if (isRecording) {
|
|
|
|
status.setMessage("Recording macro...");
|
|
|
|
} else {
|
|
|
|
status.clearMessage();
|
|
|
|
}
|
2013-12-24 00:15:03 +00:00
|
|
|
if (c) c();
|
2013-12-02 23:17:46 +00:00
|
|
|
});
|
2014-03-05 17:36:19 +00:00
|
|
|
|
2013-12-13 16:25:10 +00:00
|
|
|
//API bindings
|
2013-12-24 00:15:03 +00:00
|
|
|
command.on("editor:insert", function(text, c) {
|
2013-12-02 23:17:46 +00:00
|
|
|
editor.insert(text);
|
2013-12-24 00:15:03 +00:00
|
|
|
if (c) c();
|
2013-12-02 23:17:46 +00:00
|
|
|
});
|
2013-09-06 03:01:24 +00:00
|
|
|
|
2014-03-13 15:42:39 +00:00
|
|
|
});
|