Skip to content

Commit 751680e

Browse files
committed
Add continuecomment.js utility add-on
1 parent 8ea33dc commit 751680e

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

doc/manual.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ <h2 id="keymaps">Keymaps</h2>
355355
also contains the <code>"default"</code> keymap holding the
356356
default bindings.</p>
357357

358-
<p>The values of properties in keymaps can be either functions of
358+
<p id="commands">The values of properties in keymaps can be either functions of
359359
a single argument (the CodeMirror instance), strings, or
360360
<code>false</code>. Such strings refer to properties of the
361361
<code>CodeMirror.commands</code> object, which defines a number of
@@ -993,6 +993,11 @@ <h2 id="addons">Add-ons</h2>
993993
which will ensure the given mode is loaded and cause the given
994994
editor instance to refresh its mode when the loading
995995
succeeded. See the <a href="../demo/loadmode.html">demo</a>.</dd>
996+
<dt id="util_continuecomment"><a href="../lib/util/continuecomment.js"><code>continuecomment.js</code></a></dt>
997+
<dd>Adds a <a href="#commands">command</a>
998+
called <code>newlineAndIndentContinueComment</code> that you can
999+
bind <code>Enter</code> to in order to have the editor prefix
1000+
new lines inside C-like block comments with an asterisk.</dd>
9961001
</dl>
9971002

9981003
<h2 id="modeapi">Writing CodeMirror Modes</h2>

lib/util/continuecomment.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(function() {
2+
var modes = ["clike", "css", "javascript"];
3+
for (var i = 0; i < modes.length; ++i)
4+
CodeMirror.extendMode(modes[i], {blockCommentStart: "/*",
5+
blockCommentEnd: "*/",
6+
blockCommentContinue: " * "});
7+
8+
CodeMirror.commands.newlineAndIndentContinueComment = function(cm) {
9+
var pos = cm.getCursor(), token = cm.getTokenAt(pos);
10+
var mode = CodeMirror.innerMode(cm.getMode(), token.state).mode;
11+
var space;
12+
13+
if (token.className == "comment" && mode.blockCommentStart) {
14+
var end = token.string.indexOf(mode.blockCommentEnd);
15+
var full = cm.getRange({line: pos.line, ch: 0}, {line: pos.line, ch: token.end}), found;
16+
if (end != -1 && end == token.string.length - mode.blockCommentEnd.length) {
17+
// Comment ended, don't continue it
18+
} else if (token.string.indexOf(mode.blockCommentStart) == 0) {
19+
space = full.slice(0, token.start);
20+
if (!/^\s*$/.test(space)) {
21+
space = "";
22+
for (var i = 0; i < token.start; ++i) space += " ";
23+
}
24+
} else if ((found = full.indexOf(mode.blockCommentContinue)) != -1 &&
25+
found + mode.blockCommentContinue.length > token.start &&
26+
/^\s*$/.test(full.slice(0, found))) {
27+
space = full.slice(0, found);
28+
}
29+
}
30+
31+
if (space != null)
32+
cm.replaceSelection("\n" + space + mode.blockCommentContinue, "end");
33+
else
34+
cm.execCommand("newlineAndIndent");
35+
};
36+
})();

mode/javascript/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<title>CodeMirror: JavaScript mode</title>
66
<link rel="stylesheet" href="../../lib/codemirror.css">
77
<script src="../../lib/codemirror.js"></script>
8+
<script src="../../lib/util/continuecomment.js"></script>
89
<script src="javascript.js"></script>
910
<link rel="stylesheet" href="../../doc/docs.css">
1011
<style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
@@ -65,7 +66,8 @@ <h1>CodeMirror: JavaScript mode</h1>
6566
<script>
6667
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
6768
lineNumbers: true,
68-
matchBrackets: true
69+
matchBrackets: true,
70+
extraKeys: {"Enter": "newlineAndIndentContinueComment"}
6971
});
7072
</script>
7173

0 commit comments

Comments
 (0)