Skip to content

Commit 16c95b3

Browse files
authored
fix: wrong doc comment * insert behaviour (#5571)
1 parent 06fd974 commit 16c95b3

File tree

4 files changed

+69
-17
lines changed

4 files changed

+69
-17
lines changed

src/mode/behaviour/behaviour_test.js

+31
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,37 @@ module.exports = {
460460
editor.setValue(" /**", 1);
461461
exec("insertstring", 1, "\n");
462462
assert.equal(editor.getValue(), " /**\n * \n */");
463+
464+
// Test case 4: Cursor between closing */ and opening /** on the same line
465+
editor.setValue("/**\n * Some comment\n *//**", 1);
466+
editor.gotoLine(3, 3);
467+
exec("insertstring", 1, "\n");
468+
assert.equal(editor.getValue(), "/**\n * Some comment\n */\n /**");
469+
470+
// Test case 5: Cursor at start of the line with doc comment
471+
editor.setValue("/**\n * Some comment\n */", 1);
472+
editor.gotoLine(1, 0);
473+
exec("insertstring", 1, "\n");
474+
assert.equal(editor.getValue(), "\n/**\n * Some comment\n */");
475+
476+
// Test case 6: Cursor at the end of the first comment
477+
editor.setValue("/** comment */identifier/**", 1);
478+
editor.gotoLine(1, 14);
479+
exec("insertstring", 1, "\n");
480+
assert.equal(editor.getValue(), "/** comment */\nidentifier/**");
481+
482+
// Test case 7: Cursor at the start of the second comment
483+
editor.setValue("/** comment */identifier/**", 1);
484+
editor.gotoLine(1, 24);
485+
exec("insertstring", 1, "\n");
486+
assert.equal(editor.getValue(), "/** comment */identifier\n/**");
487+
488+
// Test case 8: Cursor between '/' and '*' in a comment
489+
editor.setValue("/** comment */", 1);
490+
editor.gotoLine(1, 1);
491+
exec("insertstring", 1, "\n");
492+
assert.equal(editor.getValue(), "/\n** comment */");
493+
463494
},
464495
"test: fragment auto-closing": function () {
465496
editor.setWrapBehavioursEnabled(true);

src/mode/behaviour/cstyle.js

+37-1
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,50 @@ CstyleBehaviour = function(options) {
322322
this.add("doc comment end", "insertion", function (state, action, editor, session, text) {
323323
if (state === "doc-start" && (text === "\n" || text === "\r\n") && editor.selection.isEmpty()) {
324324
var cursor = editor.getCursorPosition();
325+
if (cursor.column === 0) {
326+
return;
327+
}
325328
var line = session.doc.getLine(cursor.row);
326329
var nextLine = session.doc.getLine(cursor.row + 1);
330+
var tokens = session.getTokens(cursor.row);
331+
var index = 0;
332+
for (var i = 0; i < tokens.length; i++) {
333+
index += tokens[i].value.length;
334+
var currentToken = tokens[i];
335+
if (index >= cursor.column) {
336+
if (index === cursor.column) {
337+
if (!/\.doc/.test(currentToken.type)) {
338+
return;
339+
}
340+
if (/\*\//.test(currentToken.value)) {
341+
var nextToken = tokens[i + 1];
342+
if (!nextToken || !/\.doc/.test(nextToken.type)) {
343+
return;
344+
}
345+
}
346+
}
347+
var cursorPosInToken = cursor.column - (index - currentToken.value.length);
348+
349+
// Check for the pattern `*/` followed by `/**` within the token
350+
var closeDocPos = currentToken.value.indexOf("*/");
351+
var openDocPos = currentToken.value.indexOf("/**", closeDocPos > - 1 ? closeDocPos + 2 : 0);
352+
353+
if (openDocPos !== -1 && cursorPosInToken > openDocPos && cursorPosInToken < openDocPos + 3) {
354+
return;
355+
}
356+
if (closeDocPos !== -1 && openDocPos !== -1 && cursorPosInToken >= closeDocPos
357+
&& cursorPosInToken <= openDocPos || !/\.doc/.test(currentToken.type)) {
358+
return;
359+
}
360+
break;
361+
}
362+
}
327363
var indent = this.$getIndent(line);
328364
if (/\s*\*/.test(nextLine)) {
329365
if (/^\s*\*/.test(line)) {
330366
return {
331367
text: text + indent + "* ",
332-
selection: [1, 3 + indent.length, 1, 3 + indent.length]
368+
selection: [1, 2 + indent.length, 1, 2 + indent.length]
333369
};
334370
}
335371
else {

src/mode/javascript.js

-7
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,6 @@ oop.inherits(Mode, TextMode);
4646
if (endState == "start" || endState == "no_regex") {
4747
return "";
4848
}
49-
var match = line.match(/^\s*(\/?)\*/);
50-
if (match) {
51-
if (match[1]) {
52-
indent += " ";
53-
}
54-
indent += "* ";
55-
}
5649
}
5750

5851
return indent;

src/mode/javascript_test.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,7 @@ module.exports = {
141141
assert.equal(" ", this.mode.getNextLineIndent("start", " cde", " "));
142142
assert.equal(" ", this.mode.getNextLineIndent("start", "function foo(items) {", " "));
143143
},
144-
145-
"test: special indent in doc comments" : function() {
146-
assert.equal(" * ", this.mode.getNextLineIndent("doc-start", "/**", " "));
147-
assert.equal(" * ", this.mode.getNextLineIndent("doc-start", " /**", " "));
148-
assert.equal(" * ", this.mode.getNextLineIndent("doc-start", " *", " "));
149-
assert.equal(" * ", this.mode.getNextLineIndent("doc-start", " *", " "));
150-
assert.equal(" ", this.mode.getNextLineIndent("doc-start", " abc", " "));
151-
},
152-
144+
153145
"test: no indent after doc comments" : function() {
154146
assert.equal("", this.mode.getNextLineIndent("doc-start", " */", " "));
155147
},

0 commit comments

Comments
 (0)