diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/comment/CommentOnLineWithSourcePredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/comment/CommentOnLineWithSourcePredicate.java index a76a86fedbbe..462b19fd0df1 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/comment/CommentOnLineWithSourcePredicate.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/comment/CommentOnLineWithSourcePredicate.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2005 Dave Griffith + * Copyright 2003-2017 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,33 +25,32 @@ import com.siyeh.ipp.base.PsiElementPredicate; class CommentOnLineWithSourcePredicate implements PsiElementPredicate { public boolean satisfiedBy(PsiElement element) { - //final PsiFile file = element.getContainingFile(); - //if (file instanceof JspFile){ - // return false; - //} if (!(element instanceof PsiComment)) { return false; } if (element instanceof PsiDocComment) { return false; } - + if (element.getTextOffset() == 0) { + return false; + } final PsiComment comment = (PsiComment)element; if (comment instanceof PsiLanguageInjectionHost && InjectedLanguageUtil.hasInjections((PsiLanguageInjectionHost)comment)) { return false; } final IElementType type = comment.getTokenType(); - if (!JavaTokenType.C_STYLE_COMMENT.equals(type) && - !JavaTokenType.END_OF_LINE_COMMENT.equals(type)) { + if (!JavaTokenType.C_STYLE_COMMENT.equals(type) && !JavaTokenType.END_OF_LINE_COMMENT.equals(type)) { return false; // can't move JSP comments } final PsiElement prevSibling = PsiTreeUtil.prevLeaf(element); + if (prevSibling == null || prevSibling.getTextLength() == 0) { + return false; + } if (!(prevSibling instanceof PsiWhiteSpace)) { return true; } final String prevSiblingText = prevSibling.getText(); - if (prevSiblingText.indexOf((int)'\n') < 0 && - prevSiblingText.indexOf((int)'\r') < 0) { + if (prevSiblingText.indexOf('\n') < 0 && prevSiblingText.indexOf('\r') < 0) { return true; } final PsiElement nextSibling = PsiTreeUtil.nextLeaf(element); @@ -59,7 +58,6 @@ class CommentOnLineWithSourcePredicate implements PsiElementPredicate { return true; } final String nextSiblingText = nextSibling.getText(); - return nextSiblingText.indexOf((int)'\n') < 0 && - nextSiblingText.indexOf((int)'\r') < 0; + return nextSiblingText.indexOf('\n') < 0 && nextSiblingText.indexOf('\r') < 0; } } diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/comment/separate_line/Simple.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/comment/separate_line/Simple.java new file mode 100644 index 000000000000..6544eb737e08 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/comment/separate_line/Simple.java @@ -0,0 +1,6 @@ +class X { + + void m() { + System.out.println(); // covfafa + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/comment/separate_line/Simple_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/comment/separate_line/Simple_after.java new file mode 100644 index 000000000000..5f4a766a6494 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/comment/separate_line/Simple_after.java @@ -0,0 +1,7 @@ +class X { + + void m() { + // covfafa + System.out.println(); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/comment/separate_line/StartOfFile.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/comment/separate_line/StartOfFile.java new file mode 100644 index 000000000000..29a661f782fd --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/comment/separate_line/StartOfFile.java @@ -0,0 +1,4 @@ +/* + covfefe + */ +class X {} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/comment/MoveCommentToSeparateLineIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/comment/MoveCommentToSeparateLineIntentionTest.java new file mode 100644 index 000000000000..f8b4e02f0bb9 --- /dev/null +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/comment/MoveCommentToSeparateLineIntentionTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.siyeh.ipp.comment; + +import com.siyeh.ipp.IPPTestCase; + +/** + * @author Bas Leijdekkers + */ +public class MoveCommentToSeparateLineIntentionTest extends IPPTestCase { + + public void testSimple() { + doTest(); + } + + public void testStartOfFile() { + assertIntentionNotAvailable(); + } + + @Override + protected String getIntentionName() { + return "Move comment to separate line"; + } + + @Override + protected String getRelativePath() { + return "comment/separate_line"; + } +} \ No newline at end of file