Rename in Javadoc fixed

This commit is contained in:
Roman Shevchenko
2010-08-05 22:24:13 +04:00
parent 352482241b
commit cb56ea4905
8 changed files with 111 additions and 26 deletions
@@ -15,9 +15,12 @@
*/
package com.intellij.lang.java;
import com.intellij.lang.CodeDocumentationAwareCommenter;
import com.intellij.lang.ASTNode;
import com.intellij.lang.CodeDocumentationAwareCommenterEx;
import com.intellij.psi.JavaDocTokenType;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.source.tree.JavaDocElementType;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.tree.IElementType;
@@ -26,7 +29,7 @@ import org.jetbrains.annotations.Nullable;
/**
* @author max
*/
public class JavaCommenter implements CodeDocumentationAwareCommenter {
public class JavaCommenter implements CodeDocumentationAwareCommenterEx {
public String getLineCommentPrefix() {
return "//";
@@ -78,4 +81,10 @@ public class JavaCommenter implements CodeDocumentationAwareCommenter {
public boolean isDocumentationComment(final PsiComment element) {
return element instanceof PsiDocComment;
}
public boolean isDocumentationCommentText(final PsiElement element) {
if (element == null) return false;
final ASTNode node = element.getNode();
return node != null && node.getElementType() == JavaDocTokenType.DOC_COMMENT_DATA;
}
}
@@ -0,0 +1,9 @@
public class ConflictWithJavadocTag {
/**
* Receives some param.
* @param param some param
*/
public void my(int <caret>param) {
System.out.println("param=" + param); // print param
}
}
@@ -0,0 +1,9 @@
public class ConflictWithJavadocTag {
/**
* Receives some i.
* @param i some i
*/
public void my(int i) {
System.out.println("i=" + i); // print i
}
}
@@ -38,6 +38,10 @@ public class RenameLocalTest extends LightCodeInsightTestCase {
doTest("f");
}
public void testConflictWithJavadocTag() throws Exception {
doTest("i");
}
private void doTest(final String newName) throws Exception {
configureByFile(BASE_PATH + getTestName(false) + ".java");
PsiElement element = TargetElementUtilBase
@@ -0,0 +1,33 @@
/*
* Copyright 2000-2010 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.intellij.lang;
import com.intellij.psi.PsiElement;
/**
* Support for extended code documentation handling.
*/
public interface CodeDocumentationAwareCommenterEx extends CodeDocumentationAwareCommenter {
/**
* Documentation comments may consist of various nested elements: e.g. javadoc tags, start/end markers,
* and comment text elements. This method verifies is given element represents the latter.
*
* @param element element to check
* @return true if the element is a documentation comment part with text
*/
boolean isDocumentationCommentText(PsiElement element);
}
@@ -16,16 +16,20 @@
package com.intellij.codeInsight;
import com.intellij.psi.codeStyle.Indent;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.openapi.project.Project;
import com.intellij.lang.*;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.Indent;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.text.CharArrayUtil;
import org.jetbrains.annotations.Nullable;
public class CommentUtil {
private CommentUtil() {
}
private CommentUtil() { }
public static Indent getMinLineIndent(Project project, Document document, int line1, int line2, FileType fileType) {
CharSequence chars = document.getCharsSequence();
@@ -53,4 +57,26 @@ public class CommentUtil {
//}
return minIndent;
}
public static boolean isComment(@Nullable final PsiElement element) {
return element != null && isComment(element.getNode());
}
public static boolean isComment(@Nullable final ASTNode node) {
if (node == null) return false;
final IElementType type = node.getElementType();
final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(type.getLanguage());
return parserDefinition != null && parserDefinition.getCommentTokens().contains(type);
}
public static boolean isCommentTextElement(final PsiElement element) {
final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(element.getLanguage());
if (commenter instanceof CodeDocumentationAwareCommenterEx) {
final CodeDocumentationAwareCommenterEx commenterEx = (CodeDocumentationAwareCommenterEx)commenter;
if (commenterEx.isDocumentationCommentText(element)) return true;
if (element instanceof PsiComment && commenterEx.isDocumentationComment((PsiComment)element)) return false;
}
return isComment(element);
}
}
@@ -16,11 +16,10 @@
package com.intellij.psi.impl.search;
import com.intellij.codeInsight.CommentUtil;
import com.intellij.concurrency.JobUtil;
import com.intellij.ide.todo.TodoConfiguration;
import com.intellij.ide.todo.TodoIndexPatternProvider;
import com.intellij.lang.LanguageParserDefinitions;
import com.intellij.lang.ParserDefinition;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.application.Result;
@@ -162,19 +161,17 @@ public class PsiSearchHelperImpl implements PsiSearchHelper {
public boolean processCommentsContainingIdentifier(@NotNull String identifier,
@NotNull SearchScope searchScope,
@NotNull final Processor<PsiElement> processor) {
TextOccurenceProcessor occurenceProcessor = new TextOccurenceProcessor() {
TextOccurenceProcessor occurrenceProcessor = new TextOccurenceProcessor() {
public boolean execute(PsiElement element, int offsetInElement) {
final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(element.getLanguage());
if (parserDefinition == null) return true;
if (element.getNode() != null && !parserDefinition.getCommentTokens().contains(element.getNode().getElementType())) return true;
if (element.findReferenceAt(offsetInElement) == null) {
return processor.process(element);
if (CommentUtil.isCommentTextElement(element)) {
if (element.findReferenceAt(offsetInElement) == null) {
return processor.process(element);
}
}
return true;
}
};
return processElementsWithWord(occurenceProcessor, searchScope, identifier, UsageSearchContext.IN_COMMENTS, true);
return processElementsWithWord(occurrenceProcessor, searchScope, identifier, UsageSearchContext.IN_COMMENTS, true);
}
public boolean processElementsWithWord(@NotNull final TextOccurenceProcessor processor,
@@ -56,7 +56,7 @@ public class RenameUtil {
public static UsageInfo[] findUsages(final PsiElement element,
String newName,
boolean searchInStringsAndComments,
boolean searchForTextOccurences,
boolean searchForTextOccurrences,
Map<? extends PsiElement, String> allRenames) {
final List<UsageInfo> result = Collections.synchronizedList(new ArrayList<UsageInfo>());
@@ -75,6 +75,7 @@ public class RenameUtil {
processor.findCollisions(element, newName, allRenames, result);
final PsiElement searchForInComments = processor.getElementToSearchInStringsAndComments(element);
if (searchInStringsAndComments && searchForInComments != null) {
String stringToSearch = ElementDescriptionUtil.getElementDescription(searchForInComments, NonCodeSearchDescriptionLocation.STRINGS_AND_COMMENTS);
if (stringToSearch.length() > 0) {
@@ -84,27 +85,24 @@ public class RenameUtil {
}
}
if (searchForTextOccurences && searchForInComments != null) {
if (searchForTextOccurrences && searchForInComments != null) {
String stringToSearch = ElementDescriptionUtil.getElementDescription(searchForInComments, NonCodeSearchDescriptionLocation.NON_JAVA);
if (stringToSearch.length() > 0) {
final String stringToReplace = getStringToReplace(element, newName, true, processor);
addTextOccurence(searchForInComments, result, projectScope, stringToSearch, stringToReplace);
addTextOccurrence(searchForInComments, result, projectScope, stringToSearch, stringToReplace);
}
final Pair<String, String> additionalStringToSearch = processor.getTextOccurrenceSearchStrings(searchForInComments, newName);
if (additionalStringToSearch != null && additionalStringToSearch.first.length() > 0) {
addTextOccurence(searchForInComments, result, projectScope, additionalStringToSearch.first, additionalStringToSearch.second);
addTextOccurrence(searchForInComments, result, projectScope, additionalStringToSearch.first, additionalStringToSearch.second);
}
}
return result.toArray(new UsageInfo[result.size()]);
}
private static void addTextOccurence(final PsiElement element, final List<UsageInfo> result, final GlobalSearchScope projectScope,
final String stringToSearch,
final String stringToReplace) {
private static void addTextOccurrence(final PsiElement element, final List<UsageInfo> result, final GlobalSearchScope projectScope,
final String stringToSearch, final String stringToReplace) {
TextOccurrencesUtil.UsageInfoFactory factory = new TextOccurrencesUtil.UsageInfoFactory() {
public UsageInfo createUsageInfo(@NotNull PsiElement usage, int startOffset, int endOffset) {
TextRange textRange = usage.getTextRange();