From f11978387aa936538f1357dbd46e8a257af53de8 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke <marijn@haverbeke.nl> Date: Wed, 21 Jun 2017 08:51:48 +0200 Subject: [PATCH] [matchbrackets addon] Simplify interfact to findMatchingBracket --- addon/edit/matchbrackets.js | 19 ++++++++++++++----- keymap/emacs.js | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/addon/edit/matchbrackets.js b/addon/edit/matchbrackets.js index 43473c1b1..4d7a23085 100644 --- a/addon/edit/matchbrackets.js +++ b/addon/edit/matchbrackets.js @@ -16,7 +16,7 @@ var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"}; - function findMatchingBracket(cm, where, strict, config) { + function findMatchingBracket(cm, where, config) { var line = cm.getLineHandle(where.line), pos = where.ch - 1; var afterCursor = config && config.afterCursor if (afterCursor == null) @@ -30,7 +30,7 @@ matching[line.text.charAt(++pos)]; if (!match) return null; var dir = match.charAt(1) == ">" ? 1 : -1; - if (strict && (dir > 0) != (pos == where.ch)) return null; + if (config && config.strict && (dir > 0) != (pos == where.ch)) return null; var style = cm.getTokenTypeAt(Pos(where.line, pos + 1)); var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir, style || null, config); @@ -78,7 +78,7 @@ var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000; var marks = [], ranges = cm.listSelections(); for (var i = 0; i < ranges.length; i++) { - var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, false, config); + var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, config); if (match && cm.getLine(match.from.line).length <= maxHighlightLen) { var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket"; marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style})); @@ -122,8 +122,17 @@ }); CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);}); - CodeMirror.defineExtension("findMatchingBracket", function(pos, strict, config){ - return findMatchingBracket(this, pos, strict, config); + CodeMirror.defineExtension("findMatchingBracket", function(pos, config, oldConfig){ + // Backwards-compatibility kludge + if (oldConfig || typeof config == "boolean") { + if (!oldConfig) { + config = config ? {strict: true} : null + } else { + oldConfig.strict = config + config = oldConfig + } + } + return findMatchingBracket(this, pos, config) }); CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config){ return scanForBracket(this, pos, dir, style, config); diff --git a/keymap/emacs.js b/keymap/emacs.js index 2d5fe1b81..63da52fa3 100644 --- a/keymap/emacs.js +++ b/keymap/emacs.js @@ -97,7 +97,7 @@ function byExpr(cm, pos, dir) { var wrap; - if (cm.findMatchingBracket && (wrap = cm.findMatchingBracket(pos, true)) + if (cm.findMatchingBracket && (wrap = cm.findMatchingBracket(pos, {strict: true})) && wrap.match && (wrap.forward ? 1 : -1) == dir) return dir > 0 ? Pos(wrap.to.line, wrap.to.ch + 1) : wrap.to; -- GitLab