Skip to content

Commit

Permalink
fix: wrong doc comment * insert behaviour (#5571)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkslanc committed Jun 13, 2024
1 parent 06fd974 commit 16c95b3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 17 deletions.
31 changes: 31 additions & 0 deletions src/mode/behaviour/behaviour_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,37 @@ module.exports = {
editor.setValue(" /**", 1);
exec("insertstring", 1, "\n");
assert.equal(editor.getValue(), " /**\n * \n */");

// Test case 4: Cursor between closing */ and opening /** on the same line
editor.setValue("/**\n * Some comment\n *//**", 1);
editor.gotoLine(3, 3);
exec("insertstring", 1, "\n");
assert.equal(editor.getValue(), "/**\n * Some comment\n */\n /**");

// Test case 5: Cursor at start of the line with doc comment
editor.setValue("/**\n * Some comment\n */", 1);
editor.gotoLine(1, 0);
exec("insertstring", 1, "\n");
assert.equal(editor.getValue(), "\n/**\n * Some comment\n */");

// Test case 6: Cursor at the end of the first comment
editor.setValue("/** comment */identifier/**", 1);
editor.gotoLine(1, 14);
exec("insertstring", 1, "\n");
assert.equal(editor.getValue(), "/** comment */\nidentifier/**");

// Test case 7: Cursor at the start of the second comment
editor.setValue("/** comment */identifier/**", 1);
editor.gotoLine(1, 24);
exec("insertstring", 1, "\n");
assert.equal(editor.getValue(), "/** comment */identifier\n/**");

// Test case 8: Cursor between '/' and '*' in a comment
editor.setValue("/** comment */", 1);
editor.gotoLine(1, 1);
exec("insertstring", 1, "\n");
assert.equal(editor.getValue(), "/\n** comment */");

},
"test: fragment auto-closing": function () {
editor.setWrapBehavioursEnabled(true);
Expand Down
38 changes: 37 additions & 1 deletion src/mode/behaviour/cstyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,50 @@ CstyleBehaviour = function(options) {
this.add("doc comment end", "insertion", function (state, action, editor, session, text) {
if (state === "doc-start" && (text === "\n" || text === "\r\n") && editor.selection.isEmpty()) {
var cursor = editor.getCursorPosition();
if (cursor.column === 0) {
return;
}
var line = session.doc.getLine(cursor.row);
var nextLine = session.doc.getLine(cursor.row + 1);
var tokens = session.getTokens(cursor.row);
var index = 0;
for (var i = 0; i < tokens.length; i++) {
index += tokens[i].value.length;
var currentToken = tokens[i];
if (index >= cursor.column) {
if (index === cursor.column) {
if (!/\.doc/.test(currentToken.type)) {
return;
}
if (/\*\//.test(currentToken.value)) {
var nextToken = tokens[i + 1];
if (!nextToken || !/\.doc/.test(nextToken.type)) {
return;
}
}
}
var cursorPosInToken = cursor.column - (index - currentToken.value.length);

// Check for the pattern `*/` followed by `/**` within the token
var closeDocPos = currentToken.value.indexOf("*/");
var openDocPos = currentToken.value.indexOf("/**", closeDocPos > - 1 ? closeDocPos + 2 : 0);

if (openDocPos !== -1 && cursorPosInToken > openDocPos && cursorPosInToken < openDocPos + 3) {
return;
}
if (closeDocPos !== -1 && openDocPos !== -1 && cursorPosInToken >= closeDocPos
&& cursorPosInToken <= openDocPos || !/\.doc/.test(currentToken.type)) {
return;
}
break;
}
}
var indent = this.$getIndent(line);
if (/\s*\*/.test(nextLine)) {
if (/^\s*\*/.test(line)) {
return {
text: text + indent + "* ",
selection: [1, 3 + indent.length, 1, 3 + indent.length]
selection: [1, 2 + indent.length, 1, 2 + indent.length]
};
}
else {
Expand Down
7 changes: 0 additions & 7 deletions src/mode/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ oop.inherits(Mode, TextMode);
if (endState == "start" || endState == "no_regex") {
return "";
}
var match = line.match(/^\s*(\/?)\*/);
if (match) {
if (match[1]) {
indent += " ";
}
indent += "* ";
}
}

return indent;
Expand Down
10 changes: 1 addition & 9 deletions src/mode/javascript_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,7 @@ module.exports = {
assert.equal(" ", this.mode.getNextLineIndent("start", " cde", " "));
assert.equal(" ", this.mode.getNextLineIndent("start", "function foo(items) {", " "));
},

"test: special indent in doc comments" : function() {
assert.equal(" * ", this.mode.getNextLineIndent("doc-start", "/**", " "));
assert.equal(" * ", this.mode.getNextLineIndent("doc-start", " /**", " "));
assert.equal(" * ", this.mode.getNextLineIndent("doc-start", " *", " "));
assert.equal(" * ", this.mode.getNextLineIndent("doc-start", " *", " "));
assert.equal(" ", this.mode.getNextLineIndent("doc-start", " abc", " "));
},


"test: no indent after doc comments" : function() {
assert.equal("", this.mode.getNextLineIndent("doc-start", " */", " "));
},
Expand Down

0 comments on commit 16c95b3

Please sign in to comment.