Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: wrong doc comment * insert behaviour #5571

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/mode/behaviour/behaviour_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

require("../../multi_select");
var assert = require("../../test/assertions");
var Range = require("../../range").Range;

Check warning on line 10 in src/mode/behaviour/behaviour_test.js

View workflow job for this annotation

GitHub Actions / build (16.x)

'Range' is assigned a value but never used
var Editor = require("../../editor").Editor;
var UndoManager = require("../../undomanager").UndoManager;
var EditSession = require("../../edit_session").EditSession;
Expand All @@ -25,7 +25,7 @@
editor.commands.exec(name, editor, args);
} while(times --> 1);
};
var testRanges = function(str) {

Check warning on line 28 in src/mode/behaviour/behaviour_test.js

View workflow job for this annotation

GitHub Actions / build (16.x)

'testRanges' is assigned a value but never used
assert.equal(editor.selection.getAllRanges() + "", str + "");
};

Expand Down Expand Up @@ -460,6 +460,37 @@
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 @@
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;

Check warning on line 354 in src/mode/behaviour/cstyle.js

View check run for this annotation

Codecov / codecov/patch

src/mode/behaviour/cstyle.js#L354

Added line #L354 was not covered by tests
}
if (closeDocPos !== -1 && openDocPos !== -1 && cursorPosInToken >= closeDocPos
&& cursorPosInToken <= openDocPos || !/\.doc/.test(currentToken.type)) {
return;

Check warning on line 358 in src/mode/behaviour/cstyle.js

View check run for this annotation

Codecov / codecov/patch

src/mode/behaviour/cstyle.js#L358

Added line #L358 was not covered by tests
}
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
Loading