Provide "go to matching brace" command with shortcut Ctrl+M. #3320

Closed
wants to merge 1 commit into
from
@@ -201,6 +201,9 @@
"navigate.gotoDefinition": [
"Ctrl-T"
],
+ "navigate.gotoMatchingBrace": [
+ "Ctrl-M"
+ ],
"navigate.nextDoc": [
{
"key": "Ctrl-Tab"
View
@@ -96,6 +96,7 @@ define(function (require, exports, module) {
exports.NAVIGATE_QUICK_OPEN = "navigate.quickOpen";
exports.NAVIGATE_GOTO_DEFINITION = "navigate.gotoDefinition";
exports.NAVIGATE_GOTO_LINE = "navigate.gotoLine";
+ exports.GOTO_MATCHING_BRACE = "navigate.gotoMatchingBrace";
exports.TOGGLE_QUICK_EDIT = "navigate.toggleQuickEdit";
exports.QUICK_EDIT_NEXT_MATCH = "navigate.nextMatch";
exports.QUICK_EDIT_PREV_MATCH = "navigate.previousMatch";
@@ -121,6 +121,7 @@ define(function (require, exports, module) {
menu.addMenuItem(Commands.NAVIGATE_QUICK_OPEN);
menu.addMenuItem(Commands.NAVIGATE_GOTO_LINE);
menu.addMenuItem(Commands.NAVIGATE_GOTO_DEFINITION);
+ menu.addMenuItem(Commands.GOTO_MATCHING_BRACE);
menu.addMenuDivider();
menu.addMenuItem(Commands.NAVIGATE_NEXT_DOC);
menu.addMenuItem(Commands.NAVIGATE_PREV_DOC);
@@ -785,21 +785,39 @@ define(function (require, exports, module) {
return result.promise();
}
+ /**
+ * Move the cursor to the matching brace if the character before/after is
+ * one of these characters -- ()[]{} --- and a match is found.
+ */
+ function findMatchingBrace() {
+ var editor = EditorManager.getFocusedEditor();
+ if (editor) {
+ var maxLinesToScan = 1500,
@TomMalbran

TomMalbran Apr 20, 2013

Contributor

This should be defined as a constant.

+ found = editor._codeMirror.findMatchingBracket(maxLinesToScan);
+
+ if (found && found.to) {
+ editor.setCursorPos(found.to.line, found.to.ch);
+ }
+ }
+ }
+
// Register commands
- CommandManager.register(Strings.CMD_INDENT, Commands.EDIT_INDENT, indentText);
- CommandManager.register(Strings.CMD_UNINDENT, Commands.EDIT_UNINDENT, unidentText);
- CommandManager.register(Strings.CMD_COMMENT, Commands.EDIT_LINE_COMMENT, lineComment);
- CommandManager.register(Strings.CMD_BLOCK_COMMENT, Commands.EDIT_BLOCK_COMMENT, blockComment);
- CommandManager.register(Strings.CMD_DUPLICATE, Commands.EDIT_DUPLICATE, duplicateText);
- CommandManager.register(Strings.CMD_DELETE_LINES, Commands.EDIT_DELETE_LINES, deleteCurrentLines);
- CommandManager.register(Strings.CMD_LINE_UP, Commands.EDIT_LINE_UP, moveLineUp);
- CommandManager.register(Strings.CMD_LINE_DOWN, Commands.EDIT_LINE_DOWN, moveLineDown);
- CommandManager.register(Strings.CMD_SELECT_LINE, Commands.EDIT_SELECT_LINE, selectLine);
+ CommandManager.register(Strings.CMD_INDENT, Commands.EDIT_INDENT, indentText);
+ CommandManager.register(Strings.CMD_UNINDENT, Commands.EDIT_UNINDENT, unidentText);
+ CommandManager.register(Strings.CMD_COMMENT, Commands.EDIT_LINE_COMMENT, lineComment);
+ CommandManager.register(Strings.CMD_BLOCK_COMMENT, Commands.EDIT_BLOCK_COMMENT, blockComment);
+ CommandManager.register(Strings.CMD_DUPLICATE, Commands.EDIT_DUPLICATE, duplicateText);
+ CommandManager.register(Strings.CMD_DELETE_LINES, Commands.EDIT_DELETE_LINES, deleteCurrentLines);
+ CommandManager.register(Strings.CMD_LINE_UP, Commands.EDIT_LINE_UP, moveLineUp);
+ CommandManager.register(Strings.CMD_LINE_DOWN, Commands.EDIT_LINE_DOWN, moveLineDown);
+ CommandManager.register(Strings.CMD_SELECT_LINE, Commands.EDIT_SELECT_LINE, selectLine);
+
+ CommandManager.register(Strings.CMD_UNDO, Commands.EDIT_UNDO, handleUndo);
+ CommandManager.register(Strings.CMD_REDO, Commands.EDIT_REDO, handleRedo);
+ CommandManager.register(Strings.CMD_CUT, Commands.EDIT_CUT, ignoreCommand);
+ CommandManager.register(Strings.CMD_COPY, Commands.EDIT_COPY, ignoreCommand);
+ CommandManager.register(Strings.CMD_PASTE, Commands.EDIT_PASTE, ignoreCommand);
+ CommandManager.register(Strings.CMD_SELECT_ALL, Commands.EDIT_SELECT_ALL, _handleSelectAll);
- CommandManager.register(Strings.CMD_UNDO, Commands.EDIT_UNDO, handleUndo);
- CommandManager.register(Strings.CMD_REDO, Commands.EDIT_REDO, handleRedo);
- CommandManager.register(Strings.CMD_CUT, Commands.EDIT_CUT, ignoreCommand);
- CommandManager.register(Strings.CMD_COPY, Commands.EDIT_COPY, ignoreCommand);
- CommandManager.register(Strings.CMD_PASTE, Commands.EDIT_PASTE, ignoreCommand);
- CommandManager.register(Strings.CMD_SELECT_ALL, Commands.EDIT_SELECT_ALL, _handleSelectAll);
+ CommandManager.register(Strings.CMD_GOTO_MATCHING_BRACE, Commands.GOTO_MATCHING_BRACE, findMatchingBrace);
});
View
@@ -229,6 +229,7 @@ define({
"CMD_QUICK_OPEN" : "Quick Open",
"CMD_GOTO_LINE" : "Go to Line",
"CMD_GOTO_DEFINITION" : "Go to Definition",
+ "CMD_GOTO_MATCHING_BRACE" : "Go to Matching Brace",
@TomMalbran

TomMalbran Apr 20, 2013

Contributor

Maybe Jump to Matching Brace might be more appropriate here, since it jumps and doesn't open a dialog like the first 2, and might go better with the new Jump to Definition command, although is also an even longer name. If changed, maybe the key could just be "JUMPTO_BRACE".

"CMD_JSLINT_FIRST_ERROR" : "Go to First JSLint Error",
"CMD_TOGGLE_QUICK_EDIT" : "Quick Edit",
"CMD_QUICK_EDIT_PREV_MATCH" : "Previous Match",