JoinLinesHandler: Only skip conversion to block comment if next line starts with line comment

GitOrigin-RevId: 895b8631127d321f7db8f5e25871a2dca5777760
This commit is contained in:
Nikolaj Schumacher
2022-02-17 15:49:06 +01:00
committed by intellij-monorepo-bot
parent a01edaba49
commit 818325442d
6 changed files with 25 additions and 1 deletions

View File

@@ -0,0 +1,4 @@
class A{
// comment <caret>1
/* comment 2 */ /* comment 3 */
}

View File

@@ -0,0 +1,4 @@
class A{
// comment <caret>1
/* comment 2 */ /* comment 3 */void test() {}
}

View File

@@ -0,0 +1,3 @@
class A{
/* comment 1 */<caret>/* comment 2 */ /* comment 3 */void test() {}
}

View File

@@ -0,0 +1,3 @@
class A{
/* comment 1 */<caret>/* comment 2 */ /* comment 3 */
}

View File

@@ -154,6 +154,8 @@ public class JoinLinesTest extends LightJavaCodeInsightTestCase {
public void testLocalVarImplicit() { doTest(); }
public void testSlashComment() { doTest(); }
public void testSlashCommentFollowedByBlockComment() { doTest(); }
public void testSlashCommentFollowedByBlockCommentAndCode() { doTest(); }
public void testDocComment() { doTest(); }
public void testOnEmptyLine() { doTest(); }

View File

@@ -39,6 +39,7 @@ import java.util.Collections;
import java.util.List;
import static com.intellij.codeInsight.editorActions.JoinLinesHandlerDelegate.CANNOT_JOIN;
import static com.intellij.psi.util.PsiUtilCore.getElementType;
public class JoinLinesHandler extends EditorActionHandler.ForEachCaret {
private static final Logger LOG = Logger.getInstance(JoinLinesHandler.class);
@@ -149,7 +150,7 @@ public class JoinLinesHandler extends EditorActionHandler.ForEachCaret {
int nextStart = CharArrayUtil.shiftForward(text, myDoc.getLineStartOffset(line + 1), " \t\n");
if (nextStart < text.length() &&
myDoc.getLineNumber(nextStart) <= myLine + lineCount &&
getCommentElement(myFile.findElementAt(nextStart)) == null) {
!isLineComment(getCommentElement(myFile.findElementAt(nextStart)))) {
endComments.add(comment);
}
}
@@ -167,6 +168,13 @@ public class JoinLinesHandler extends EditorActionHandler.ForEachCaret {
}
}
private static boolean isLineComment(@Nullable PsiComment element) {
if (element == null) return false;
Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(element.getLanguage());
return commenter instanceof CodeDocumentationAwareCommenter &&
((CodeDocumentationAwareCommenter)commenter).getLineCommentTokenType() == getElementType(element);
}
/**
* @param lineCount number of lines to process
* @return number of unprocessed lines