mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-132440 (more straightforward element lookup)
This commit is contained in:
+14
-22
@@ -21,11 +21,14 @@ import com.intellij.ide.fileTemplates.FileTemplate;
|
||||
import com.intellij.ide.fileTemplates.FileTemplateManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.JavaPsiFacade;
|
||||
import com.intellij.psi.PsiComment;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.javadoc.PsiDocComment;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import gnu.trove.TIntObjectHashMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -48,27 +51,16 @@ public class FileHeaderChecker {
|
||||
FileTemplate defaultTemplate = FileTemplateManager.getInstance().getDefaultTemplate(FileTemplateManager.FILE_HEADER_TEMPLATE_NAME);
|
||||
Pattern pattern = getTemplatePattern(defaultTemplate, file.getProject(), offsetToProperty);
|
||||
Matcher matcher = pattern.matcher(file.getViewProvider().getContents());
|
||||
if (!matcher.matches()) return null;
|
||||
if (!matcher.matches()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final int startOffset = matcher.start(1);
|
||||
final int endOffset = matcher.end(1);
|
||||
final Ref<PsiDocComment> docComment = new Ref<PsiDocComment>();
|
||||
file.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitElement(PsiElement element) {
|
||||
if (docComment.get() != null) return;
|
||||
TextRange range = element.getTextRange();
|
||||
if (!range.contains(startOffset) && !range.contains(endOffset)) return;
|
||||
super.visitElement(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitDocComment(PsiDocComment comment) {
|
||||
docComment.set(comment);
|
||||
}
|
||||
});
|
||||
PsiDocComment element = docComment.get();
|
||||
if (element == null) return null;
|
||||
int startOffset = matcher.start(1);
|
||||
int endOffset = matcher.end(1);
|
||||
PsiComment element = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), PsiComment.class);
|
||||
if (element == null || !element.getTextRange().equals(new TextRange(startOffset, endOffset))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
LocalQuickFix[] fixes = createQuickFix(matcher, offsetToProperty);
|
||||
String description = InspectionsBundle.message("default.file.template.description");
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package mylibs.aspectjlibs.lib2;
|
||||
|
||||
<warning descr="Default File template">/**
|
||||
* Created by irina on 11/6/2014.
|
||||
*/</warning>
|
||||
class Range {
|
||||
/**
|
||||
* for all public methods with name == foo* and String type
|
||||
*/
|
||||
}
|
||||
+1
@@ -28,6 +28,7 @@ public class DefaultFileTemplateUsageInspectionTest extends LightCodeInsightFixt
|
||||
public void testX() { doTest(); }
|
||||
public void testX2() { doTest(); }
|
||||
public void testX3() { doTest(); }
|
||||
public void testRange() { doTest(); }
|
||||
|
||||
public void doTest() {
|
||||
myFixture.enableInspections(new DefaultFileTemplateUsageInspection());
|
||||
|
||||
Reference in New Issue
Block a user