From 16c95b32197c0ec952e52be85d21269005875940 Mon Sep 17 00:00:00 2001 From: Azat Alimov <32402726+mkslanc@users.noreply.github.com> Date: Thu, 13 Jun 2024 16:19:17 +0400 Subject: [PATCH] fix: wrong doc comment * insert behaviour (#5571) --- src/mode/behaviour/behaviour_test.js | 31 +++++++++++++++++++++++ src/mode/behaviour/cstyle.js | 38 +++++++++++++++++++++++++++- src/mode/javascript.js | 7 ----- src/mode/javascript_test.js | 10 +------- 4 files changed, 69 insertions(+), 17 deletions(-) diff --git a/src/mode/behaviour/behaviour_test.js b/src/mode/behaviour/behaviour_test.js index 740d1e5f3dd..98ff41e6691 100644 --- a/src/mode/behaviour/behaviour_test.js +++ b/src/mode/behaviour/behaviour_test.js @@ -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); diff --git a/src/mode/behaviour/cstyle.js b/src/mode/behaviour/cstyle.js index 37dce23ea46..707fc663879 100644 --- a/src/mode/behaviour/cstyle.js +++ b/src/mode/behaviour/cstyle.js @@ -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 { diff --git a/src/mode/javascript.js b/src/mode/javascript.js index 080dc1ecea1..76889461d74 100644 --- a/src/mode/javascript.js +++ b/src/mode/javascript.js @@ -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; diff --git a/src/mode/javascript_test.js b/src/mode/javascript_test.js index ae17854d69e..57e90b3c89c 100644 --- a/src/mode/javascript_test.js +++ b/src/mode/javascript_test.js @@ -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", " */", " ")); },