-
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9ad29c
commit cccee6f
Showing
7 changed files
with
129 additions
and
6 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
resources/inspectionDescriptions/ErlangDuplicateBehaviour.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
Checks that every behaviour is mentioned once. | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/org/intellij/erlang/inspection/ErlangDuplicateBehaviourInspection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2012-2015 Sergey Ignatov | ||
* | ||
* 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 org.intellij.erlang.inspection; | ||
|
||
import com.intellij.codeInspection.LocalQuickFixBase; | ||
import com.intellij.codeInspection.ProblemDescriptor; | ||
import com.intellij.codeInspection.ProblemsHolder; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import com.intellij.util.containers.ContainerUtil; | ||
import org.intellij.erlang.psi.ErlangAttribute; | ||
import org.intellij.erlang.psi.ErlangBehaviour; | ||
import org.intellij.erlang.psi.ErlangFile; | ||
import org.intellij.erlang.psi.ErlangModule; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Set; | ||
|
||
public class ErlangDuplicateBehaviourInspection extends ErlangInspectionBase { | ||
public static final String FIX_MESSAGE = "Remove duplicate behaviours"; | ||
|
||
@Override | ||
protected void checkFile(@NotNull ErlangFile file, @NotNull ProblemsHolder holder) { | ||
ErlangModule module = file.getModule(); | ||
if (module == null) return; | ||
|
||
Set<String> distinctBehaviourNames = ContainerUtil.newHashSet(); | ||
Set<String> behaviourDuplicates = ContainerUtil.newHashSet(); | ||
for (ErlangBehaviour behaviour : file.getBehaviours()) { | ||
String name = behaviour.getName(); | ||
if (!distinctBehaviourNames.add(name)) { | ||
behaviourDuplicates.add(name); | ||
} | ||
} | ||
if (behaviourDuplicates.isEmpty()) return; | ||
|
||
StringBuilder builder = new StringBuilder("Duplicate behaviours - "); | ||
String separator = ", "; | ||
for (String name : behaviourDuplicates) { | ||
builder.append("'").append(name).append("'").append(separator); | ||
} | ||
String message = builder.substring(0, builder.length() - separator.length()); | ||
registerProblem(holder, module, message, new RemoveDuplicateBehaviourFix()); | ||
} | ||
|
||
private static class RemoveDuplicateBehaviourFix extends LocalQuickFixBase { | ||
protected RemoveDuplicateBehaviourFix() { | ||
super(FIX_MESSAGE); | ||
} | ||
|
||
@Override | ||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor problemDescriptor) { | ||
ErlangFile file = PsiTreeUtil.getParentOfType(problemDescriptor.getPsiElement(), ErlangFile.class); | ||
if (file == null) return; | ||
|
||
Set<String> distinctBehaviourNames = ContainerUtil.newHashSet(); | ||
for (ErlangBehaviour behaviour : file.getBehaviours()) { | ||
if (!distinctBehaviourNames.add(behaviour.getName())) { | ||
PsiElement element = PsiTreeUtil.getParentOfType(behaviour, ErlangAttribute.class); | ||
if (element != null) { | ||
element.delete(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
-<warning>module(testDuplicate)</warning>. | ||
|
||
-behaviour(b1). | ||
-behaviour(b1). | ||
|
||
-export([init0/0, init1/1]). | ||
|
||
init0() -> | ||
erlang:error(not_implemented). | ||
|
||
init1(_) -> | ||
erlang:error(not_implemented). |
4 changes: 4 additions & 0 deletions
4
testData/highlighting/behaviour/testRemoveDuplicate-after.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-module(testRemoveDuplicate). | ||
|
||
-behaviour(b1). | ||
-behaviour(b2). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-module<caret>(testRemoveDuplicate). | ||
|
||
-behaviour(b1). | ||
-behaviour(b1). | ||
-behaviour(b1). | ||
-behaviour(b2). | ||
-behaviour(b2). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters