From b318a99efe2b909b2c003eab4f4d226e2dde138d Mon Sep 17 00:00:00 2001 From: Ekaterina Shliakhovetskaja Date: Thu, 1 Jul 2010 02:13:34 +0400 Subject: [PATCH] Spellchecker: fix generics support in type tokenizer (IDEA-54464, IDEA-54431), add test cases --- .../spellchecker/DocCommentTokenizer.java | 14 +++++-- .../JavaSpellcheckingStrategy.java | 2 +- .../spellchecker/PsiTypeTokenizer.java | 13 ++++++- .../spellchecker/tokenizer/Token.java | 32 +++++++++++++--- .../testData/inspection/java/GenericTest.java | 37 ++++++++++++++++++ .../inspection/java/GenericTestt.java | 35 +++++++++++++++++ .../inspection/JavaSpellcheckingTest.java | 38 +++++++++++++++++++ 7 files changed, 160 insertions(+), 11 deletions(-) create mode 100644 plugins/spellchecker/testData/inspection/java/GenericTest.java create mode 100644 plugins/spellchecker/testData/inspection/java/GenericTestt.java create mode 100644 plugins/spellchecker/testSrc/com/intellij/spellchecker/inspection/JavaSpellcheckingTest.java diff --git a/plugins/java-i18n/src/com/intellij/spellchecker/DocCommentTokenizer.java b/plugins/java-i18n/src/com/intellij/spellchecker/DocCommentTokenizer.java index 5cd12f478bec..a661ec6957a4 100644 --- a/plugins/java-i18n/src/com/intellij/spellchecker/DocCommentTokenizer.java +++ b/plugins/java-i18n/src/com/intellij/spellchecker/DocCommentTokenizer.java @@ -22,12 +22,13 @@ import com.intellij.spellchecker.inspections.CommentSplitter; import com.intellij.spellchecker.inspections.SplitterFactory; import com.intellij.spellchecker.tokenizer.Token; import com.intellij.spellchecker.tokenizer.Tokenizer; +import com.intellij.util.containers.hash.HashSet; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; +import java.util.Set; /** * Created by IntelliJ IDEA. @@ -36,8 +37,13 @@ import java.util.List; */ public class DocCommentTokenizer extends Tokenizer { - - private final String[] excludedTags = new String[]{"author", "link"}; + private static final Set excludedTags = new HashSet(); + { + excludedTags.add("author"); + excludedTags.add("see"); + excludedTags.add("by"); + excludedTags.add("link"); + } @Nullable @Override @@ -48,7 +54,7 @@ public class DocCommentTokenizer extends Tokenizer { for (PsiElement el : comment.getChildren()) { if (el instanceof PsiDocTag) { PsiDocTag tag = (PsiDocTag)el; - if (!Arrays.asList(excludedTags).contains(tag.getName())) { + if (!excludedTags.contains(tag.getName())) { for (PsiElement data : tag.getDataElements()) { result.add(new Token(data, splitter)); } diff --git a/plugins/java-i18n/src/com/intellij/spellchecker/JavaSpellcheckingStrategy.java b/plugins/java-i18n/src/com/intellij/spellchecker/JavaSpellcheckingStrategy.java index 43e35f2bbbeb..31b09c1a58b5 100644 --- a/plugins/java-i18n/src/com/intellij/spellchecker/JavaSpellcheckingStrategy.java +++ b/plugins/java-i18n/src/com/intellij/spellchecker/JavaSpellcheckingStrategy.java @@ -22,8 +22,8 @@ import com.intellij.psi.PsiLiteralExpression; import com.intellij.psi.PsiMethod; import com.intellij.psi.PsiNamedElement; import com.intellij.psi.javadoc.PsiDocComment; -import com.intellij.spellchecker.tokenizer.Tokenizer; import com.intellij.spellchecker.tokenizer.SpellcheckingStrategy; +import com.intellij.spellchecker.tokenizer.Tokenizer; import org.jetbrains.annotations.NotNull; /** diff --git a/plugins/java-i18n/src/com/intellij/spellchecker/PsiTypeTokenizer.java b/plugins/java-i18n/src/com/intellij/spellchecker/PsiTypeTokenizer.java index c34df9034d4b..c7a7eeaea5a4 100644 --- a/plugins/java-i18n/src/com/intellij/spellchecker/PsiTypeTokenizer.java +++ b/plugins/java-i18n/src/com/intellij/spellchecker/PsiTypeTokenizer.java @@ -17,6 +17,7 @@ package com.intellij.spellchecker; import com.intellij.openapi.roots.ProjectFileIndex; import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.util.TextRange; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiTypeElement; @@ -49,6 +50,16 @@ public class PsiTypeTokenizer extends Tokenizer { final boolean isInSource = (virtualFile != null) && fileIndex.isInContent(virtualFile); - return !isInSource ? null : new Token[]{new Token(element, element.getText(), true, SplitterFactory.getInstance().getIdentifierSplitter())}; + return !isInSource + ? null + : new Token[]{ + new Token(element, element.getText(), true, 0, getRangeToCheck(element.getText(), psiClass.getName()), + SplitterFactory.getInstance().getIdentifierSplitter())}; + } + + @NotNull + private TextRange getRangeToCheck(@NotNull String text, @NotNull String name) { + final int i = text.indexOf(name); + return new TextRange(i, i + name.length()); } } \ No newline at end of file diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/tokenizer/Token.java b/plugins/spellchecker/src/com/intellij/spellchecker/tokenizer/Token.java index 50e36fde70e8..ad3b8f454dac 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/tokenizer/Token.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/tokenizer/Token.java @@ -15,9 +15,11 @@ */ package com.intellij.spellchecker.tokenizer; +import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; import com.intellij.spellchecker.inspections.CheckArea; import com.intellij.spellchecker.inspections.Splitter; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; @@ -25,18 +27,24 @@ import java.util.List; public class Token { - private String text; - private String description; - private T element; - private boolean useRename; + private final String text; + private final String description; + private final boolean useRename; private int offset; + + + private final T element; private final Splitter splitter; + private TextRange range; + public Token(T element, String text, boolean useRename, Splitter splitter) { this.element = element; this.text = text; this.useRename = useRename; this.splitter = splitter; + this.offset = 0; + this.description = null; } public Token(T element, String text, boolean useRename, int offset, Splitter splitter) { @@ -52,6 +60,11 @@ public class Token { this(element, element.getText(), useRename, splitter); } + public Token(T element, String text, boolean useRename, int offset, TextRange textRange, Splitter splitter) { + this(element, text, useRename, offset, splitter); + this.range = textRange; + } + public String getText() { return text; } @@ -72,12 +85,21 @@ public class Token { return offset; } + @NotNull + public TextRange getRange() { + if (range==null){ + range = new TextRange(0,(text!=null?text.length():0)); + } + return range; + } @Nullable public List getAreas() { if (splitter == null || text == null) { return null; } - return splitter.split(text); + return splitter.split(text, getRange()); } + + } diff --git a/plugins/spellchecker/testData/inspection/java/GenericTest.java b/plugins/spellchecker/testData/inspection/java/GenericTest.java new file mode 100644 index 000000000000..537cd4295823 --- /dev/null +++ b/plugins/spellchecker/testData/inspection/java/GenericTest.java @@ -0,0 +1,37 @@ +public class GenericTest { + /** + @see com.intellij.openapi + */ + public void a( GenTest obj ) { + + } + + public void b( AnotherGenTest obj ) { + + } + + public void c( Predicate obj ) { + + } + + public void d( AnotherPredicate obj ) { + + } + + public class GenTest { + + } + + public class AnotherGenTest { + + } + + public interface Predicate { + + } + + public interface AnotherPredicate { + + } + +} diff --git a/plugins/spellchecker/testData/inspection/java/GenericTestt.java b/plugins/spellchecker/testData/inspection/java/GenericTestt.java new file mode 100644 index 000000000000..cd3981716509 --- /dev/null +++ b/plugins/spellchecker/testData/inspection/java/GenericTestt.java @@ -0,0 +1,35 @@ +public class GenericTestt { + + public void a( GenTestt obj ) { + + } + + public void b( AnotherGenTestt obj ) { + + } + + public void c( Predicatte obj ) { + + } + + public void d( AnotherPredicatte obj ) { + + } + + public class GenTestt { + + } + + public class AnotherGenTestt { + + } + + public interface Predicatte { + + } + + public interface AnotherPredicatte { + + } + +} diff --git a/plugins/spellchecker/testSrc/com/intellij/spellchecker/inspection/JavaSpellcheckingTest.java b/plugins/spellchecker/testSrc/com/intellij/spellchecker/inspection/JavaSpellcheckingTest.java new file mode 100644 index 000000000000..3d6ce4142f05 --- /dev/null +++ b/plugins/spellchecker/testSrc/com/intellij/spellchecker/inspection/JavaSpellcheckingTest.java @@ -0,0 +1,38 @@ +/* + * Copyright 2000-2009 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.spellchecker.inspection; + +import com.intellij.spellchecker.inspections.SpellCheckerInspectionToolProvider; + +/** + * Created by IntelliJ IDEA. + * User: Ekaterina Shliakhovetskaja + */ +public class JavaSpellcheckingTest extends SpellcheckerInspectionTestCase { + + + protected String getBasePath() { + return getSpellcheckerTestDataPath() + "/inspection/java"; + } + + public void testCorrectJava() throws Throwable { + doTest("GenericTest.java", SpellCheckerInspectionToolProvider.getInspectionTools()); + } + + public void testTypoInJava() throws Throwable { + doTest("GenericTestt.java", SpellCheckerInspectionToolProvider.getInspectionTools()); + } +}