forked from codemirror/codemirror5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CodeMirror.defineOption function for defining new options. Add findMatchingBracket to the methods exported by this extension. Closes codemirror#746
- Loading branch information
Showing
40 changed files
with
113 additions
and
85 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
(function() { | ||
var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"}; | ||
function findMatchingBracket(cm) { | ||
var cur = cm.getCursor(), line = cm.getLineHandle(cur.line), pos = cur.ch - 1; | ||
var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)]; | ||
if (!match) return null; | ||
var ch = match.charAt(0), forward = match.charAt(1) == ">", d = forward ? 1 : -1; | ||
var style = cm.getTokenAt({line: cur.line, ch: pos + 1}).className; | ||
|
||
var stack = [line.text.charAt(pos)], re = /[(){}[\]]/; | ||
function scan(line, lineNo, start) { | ||
if (!line.text) return; | ||
var pos = forward ? 0 : line.text.length - 1, end = forward ? line.text.length : -1; | ||
if (start != null) pos = start + d; | ||
for (; pos != end; pos += d) { | ||
var ch = line.text.charAt(pos); | ||
if (re.test(ch) && cm.getTokenAt({line: lineNo, ch: pos + 1}).className == style) { | ||
var match = matching[ch]; | ||
if (match.charAt(1) == ">" == forward) stack.push(ch); | ||
else if (stack.pop() != match.charAt(0)) return {pos: pos, match: false}; | ||
else if (!stack.length) return {pos: pos, match: true}; | ||
} | ||
} | ||
} | ||
for (var i = cur.line, found, e = forward ? Math.min(i + 100, cm.lineCount()) : Math.max(-1, i - 100); i != e; i+=d) { | ||
if (i == cur.line) found = scan(line, i, pos); | ||
else found = scan(cm.getLineHandle(i), i); | ||
if (found) break; | ||
} | ||
return {from: {line: cur.line, ch: pos}, to: found && {line: i, ch: found.pos}, match: found && found.match}; | ||
} | ||
|
||
function matchBrackets(cm, autoclear) { | ||
var found = findMatchingBracket(cm); | ||
if (!found) return; | ||
var style = found.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket"; | ||
var one = cm.markText(found.from, {line: found.from.line, ch: found.from.ch + 1}, style); | ||
var two = found.to && cm.markText(found.to, {line: found.to.line, ch: found.to.ch + 1}, style); | ||
var clear = function() { | ||
cm.operation(function() { one.clear(); two && two.clear(); }); | ||
}; | ||
if (autoclear) setTimeout(clear, 800); | ||
else return clear; | ||
} | ||
|
||
var currentlyHighlighted = null; | ||
function doMatchBrackets(cm) { | ||
setTimeout(cm.operation(function() { | ||
if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;} | ||
if (!cm.somethingSelected()) currentlyHighlighted = matchBrackets(cm, false); | ||
}), 20); | ||
} | ||
|
||
CodeMirror.defineOption("matchBrackets", false, function(cm, val) { | ||
if (val) cm.connect("cursorActivity", doMatchBrackets); | ||
else cm.disconnect("cursorActivity", doMatchBrackets); | ||
}); | ||
|
||
CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);}); | ||
CodeMirror.defineExtension("findMatchingBracket", function(){return findMatchingBracket(this);}); | ||
})(); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.