[java] "go to duplicate" quick fix for module highlighting

This commit is contained in:
Roman Shevchenko
2016-07-14 12:19:48 +02:00
parent 6db40d73ed
commit e91fc68156
3 changed files with 82 additions and 4 deletions
@@ -19,6 +19,7 @@ import com.intellij.codeInsight.daemon.JavaErrorMessages;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
import com.intellij.codeInsight.daemon.impl.quickfix.GoToSymbolFix;
import com.intellij.codeInsight.daemon.impl.quickfix.MoveFileFix;
import com.intellij.codeInsight.daemon.impl.quickfix.QuickFixAction;
import com.intellij.codeInsight.intention.QuickFixFactory;
@@ -30,6 +31,7 @@ import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaModule;
import com.intellij.psi.PsiManager;
import com.intellij.psi.impl.java.stubs.index.JavaModuleNameIndex;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.ProjectScope;
@@ -61,8 +63,11 @@ public class ModuleHighlightUtil {
Collection<PsiJavaModule> others = JavaModuleNameIndex.getInstance().get(name, project, ProjectScope.getAllScope(project));
if (others.size() > 1) {
String message = JavaErrorMessages.message("module.name.duplicate", name);
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element.getNameElement()).description(message).create();
//todo show duplicates quick fix
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(element.getNameElement()).description(message).create();
others.stream().filter(m -> m != element).findFirst().ifPresent(
duplicate -> QuickFixAction.registerQuickFixAction(info, new GoToSymbolFix(duplicate, JavaErrorMessages.message("module.open.duplicate.text")))
);
return info;
}
return null;
@@ -79,8 +84,11 @@ public class ModuleHighlightUtil {
FilenameIndex.getVirtualFilesByName(project, MODULE_INFO_FILE, new ModulesScope(Collections.singleton(module), project));
if (others.size() > 1) {
String message = JavaErrorMessages.message("module.file.duplicate");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range(element)).description(message).create();
//todo show duplicates quick fix
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range(element)).description(message).create();
others.stream().map(f -> PsiManager.getInstance(project).findFile(f)).filter(f -> f != file).findFirst().ifPresent(
duplicate -> QuickFixAction.registerQuickFixAction(info, new GoToSymbolFix(duplicate, JavaErrorMessages.message("module.open.duplicate.text")))
);
return info;
}
}
}
@@ -0,0 +1,69 @@
/*
* Copyright 2000-2016 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.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.NavigatablePsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.SmartPointerManager;
import com.intellij.psi.SmartPsiElementPointer;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
public class GoToSymbolFix implements IntentionAction {
private final SmartPsiElementPointer<NavigatablePsiElement> myPointer;
private final String myMessage;
public GoToSymbolFix(@NotNull NavigatablePsiElement symbol, @NotNull @Nls String message) {
myPointer = SmartPointerManager.getInstance(symbol.getProject()).createSmartPsiElementPointer(symbol);
myMessage = message;
}
@Nls
@NotNull
@Override
public String getText() {
return myMessage;
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return getText();
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return myPointer.getElement() != null;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
NavigatablePsiElement e = myPointer.getElement();
if (e != null && e.isValid()) {
e.navigate(true);
}
}
@Override
public boolean startInWriteAction() {
return false;
}
}
@@ -395,6 +395,7 @@ module.file.wrong.name=Module declaration should be in a file named 'module-info
module.name.duplicate=Module ''{0}'' already exists in the project
module.file.duplicate='module-info.java' already exists in the module
module.file.wrong.location=Module declaration should be located in a module's source root
module.open.duplicate.text=Go to duplicate
feature.generics=Generics
feature.annotations=Annotations