IPP: intention should not be available when comment is already on a line of its own (IDEA-173813)

This commit is contained in:
Bas Leijdekkers
2017-06-04 21:36:09 +02:00
parent e37cee317b
commit 7c03d01e5f
5 changed files with 69 additions and 12 deletions
@@ -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;
}
}
@@ -0,0 +1,6 @@
class X {
void m() {
System.out.println(); // covfafa<caret>
}
}
@@ -0,0 +1,7 @@
class X {
void m() {
// covfafa
System.out.println();
}
}
@@ -0,0 +1,4 @@
/*
<caret>covfefe
*/
class X {}
@@ -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";
}
}