From 630adaf69243569b495872521aedfa7914e9f22f Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke <marijnh@gmail.com> Date: Fri, 9 Nov 2012 17:11:51 +0100 Subject: [PATCH] Refine indentLine behavior to leave multi-line comments alone --- doc/manual.html | 19 ++++++++++++++----- lib/codemirror.js | 13 ++++++++----- mode/clike/clike.js | 3 +-- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/doc/manual.html b/doc/manual.html index f47065f0..91c1bb60 100644 --- a/doc/manual.html +++ b/doc/manual.html @@ -713,11 +713,20 @@ called.</dd> <dt id="indentLine"><code>indentLine(line, dir)</code></dt> - <dd>Reset the given line's indentation to the indentation - prescribed by the mode. If the second argument is given, - indentation will be increased (if <code>dir</code> is true) or - decreased (if false) by an <a href="#option_indentUnit">indent - unit</a> instead.</dd> + <dd>Adjust the indentation of the given line. The second + argument (which defaults to <code>"smart"</code>) may be one of: + <dl> + <dt><code>"prev"</code></dt> + <dd>Base indentation on the indentation of the previous line.</dd> + <dt><code>"smart"</code></dt> + <dd>Use the mode's smart indentation if available, behave + like <code>"prev"</code> otherwise.</dd> + <dt><code>"add"</code></dt> + <dd>Increase the indentation of the line by + one <a href="#option_indentUnit">indent unit</a>.</dd> + <dt><code>"subtract"</code></dt> + <dd>Reduce the indentation of the line.</dd> + </dl></dd> <dt id="getTokenAt"><code>getTokenAt(pos) → object</code></dt> <dd>Retrieves information about the token the current mode found diff --git a/lib/codemirror.js b/lib/codemirror.js index 6bba16a9..7eebda14 100644 --- a/lib/codemirror.js +++ b/lib/codemirror.js @@ -2112,7 +2112,7 @@ window.CodeMirror = (function() { // API UTILITIES - function indentLine(cm, n, how) { + function indentLine(cm, n, how, aggressive) { var doc = cm.view.doc; if (!how) how = "add"; if (how == "smart") { @@ -2125,7 +2125,10 @@ window.CodeMirror = (function() { var curSpaceString = line.text.match(/^\s*/)[0], indentation; if (how == "smart") { indentation = cm.view.mode.indent(state, line.text.slice(curSpaceString.length), line.text); - if (indentation == Pass) how = "prev"; + if (indentation == Pass) { + if (!aggressive) return; + how = "prev"; + } } if (how == "prev") { if (n) indentation = countColumn(getLine(doc, n-1).text, null, tabSize); @@ -2247,12 +2250,12 @@ window.CodeMirror = (function() { undo: operation(null, function() {unredoHelper(this, "undo");}), redo: operation(null, function() {unredoHelper(this, "redo");}), - indentLine: operation(null, function(n, dir) { + indentLine: operation(null, function(n, dir, aggressive) { if (typeof dir != "string") { if (dir == null) dir = this.options.smartIndent ? "smart" : "prev"; else dir = dir ? "add" : "subtract"; } - if (isLine(this.view.doc, n)) indentLine(this, n, dir); + if (isLine(this.view.doc, n)) indentLine(this, n, dir, aggressive); }), indentSelection: operation(null, function(how) { @@ -2862,7 +2865,7 @@ window.CodeMirror = (function() { newlineAndIndent: function(cm) { operation(cm, function() { cm.replaceSelection("\n", "end"); - cm.indentLine(cm.getCursor().line); + cm.indentLine(cm.getCursor().line, null, true); })(); }, toggleOverwrite: function(cm) {cm.toggleOverwrite();} diff --git a/mode/clike/clike.js b/mode/clike/clike.js index 687fee3c..715c522c 100644 --- a/mode/clike/clike.js +++ b/mode/clike/clike.js @@ -140,8 +140,7 @@ CodeMirror.defineMode("clike", function(config, parserConfig) { }, indent: function(state, textAfter) { - if (state.tokenize == tokenComment) return CodeMirror.Pass; - if (state.tokenize != tokenBase && state.tokenize != null) return 0; + if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass; var ctx = state.context, firstChar = textAfter && textAfter.charAt(0); if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev; var closing = firstChar == ctx.type; -- GitLab