Skip to content

Commit

Permalink
"Import directive overrides auto-imported BIF" inspection
Browse files Browse the repository at this point in the history
  • Loading branch information
smolcoder committed Dec 22, 2014
1 parent b81c64a commit b649b3a
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--
~ Copyright 2012-2014 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.
-->

<html>
<body>
Import directive overrides pre R14 auto-imported BIF.
</body>
</html>
3 changes: 3 additions & 0 deletions src/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@
<localInspection language="Erlang" shortName="ErlangAmbiguousCallOfAutoimportedFunction" displayName="Ambiguous call of auto-imported BIF"
groupName="Erlang" enabledByDefault="true" level="ERROR"
implementationClass="org.intellij.erlang.inspection.ErlangAmbiguousCallOfAutoimportedFunctionInspection"/>
<localInspection language="Erlang" shortName="ErlangImportDirectiveOverridesAutoimportedBif" displayName="Import overrides auto-imported BIF"
groupName="Erlang" enabledByDefault="true" level="ERROR"
implementationClass="org.intellij.erlang.inspection.ErlangImportDirectiveOverridesAutoimportedBifInspection"/>

<!--warnings-->
<localInspection language="Erlang" shortName="ErlangUnresolvedFunction" displayName="Unresolved function"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2012-2014 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.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import org.intellij.erlang.bif.ErlangBifDescriptor;
import org.intellij.erlang.bif.ErlangBifTable;
import org.intellij.erlang.psi.ErlangFile;
import org.intellij.erlang.psi.ErlangImportFunction;
import org.intellij.erlang.psi.impl.ErlangPsiImplUtil;
import org.intellij.erlang.quickfixes.ErlangRemoveFunctionFromImportFixBase;
import org.intellij.erlang.sdk.ErlangSdkRelease;
import org.intellij.erlang.sdk.ErlangSdkType;
import org.jetbrains.annotations.NotNull;

public class ErlangImportDirectiveOverridesAutoimportedBifInspection extends ErlangInspectionBase {
@Override
protected boolean canRunOn(@NotNull ErlangFile file) {
ErlangSdkRelease release = ErlangSdkType.getRelease(file);
return release == null || release.isNewerThan(ErlangSdkRelease.V_R14A);
}

protected void checkFile(@NotNull ErlangFile file, @NotNull ProblemsHolder problemsHolder) {
for (ErlangImportFunction importFunction : file.getImportedFunctions()) {
ErlangBifDescriptor bifDescriptor = ErlangBifTable.getBif(
"erlang",
ErlangPsiImplUtil.getName(importFunction.getQAtom()),
ErlangPsiImplUtil.getArity(importFunction.getInteger()));
if (bifDescriptor == null || !bifDescriptor.isAutoImported()) continue;
problemsHolder.registerProblem(importFunction,
"Import directive overrides pre R14 auto-imported BIF '" + ErlangPsiImplUtil.createFunctionPresentation(importFunction) + "'",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
new ErlangRemoveFunctionFromImportFixBase.ErlangRemoveFunctionFromImportFix());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,19 @@ protected Collection<? extends ErlangAttribute> getAttributesForProcessing(@NotN
return ((ErlangFile) descriptorElement.getContainingFile()).getAttributes();
}
}

public static class ErlangRemoveFunctionFromImportFix extends ErlangRemoveFunctionFromImportFixBase {
@Nullable
@Override
protected String getSignature(@NotNull PsiElement function) {
ErlangImportFunction f = PsiTreeUtil.getParentOfType(function, ErlangImportFunction.class, false);
return f != null ? ErlangPsiImplUtil.createFunctionPresentation(f) : null;
}

@NotNull
@Override
protected Collection<? extends ErlangAttribute> getAttributesForProcessing(@NotNull PsiElement descriptorElement) {
return ContainerUtil.createMaybeSingletonList(PsiTreeUtil.getParentOfType(descriptorElement, ErlangAttribute.class));
}
}
}
3 changes: 3 additions & 0 deletions testData/highlighting/ImportAutoimported.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-import(incl, [dt_get_tag/0, crc32/1, <error>abs/1</error>]).
-export([foo/0]).
foo() -> ok.
1 change: 1 addition & 0 deletions testData/quickfixes/import/importAutoimported-after.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-import(incl, [dt_get_tag/0, crc32/1]).
1 change: 1 addition & 0 deletions testData/quickfixes/import/importAutoimported.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-import(incl, [dt_get_tag/0, crc32/1, abs<caret> / 1]).
15 changes: 15 additions & 0 deletions tests/org/intellij/erlang/highlighting/ErlangHighlightingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ public class ErlangHighlightingTest extends ErlangHighlightingTestBase {
public void testNoAutoImport4() { doTest(); }
public void testNoAutoImport5() { doTest(); }

private void doTestWithInclude() {
myFixture.configureByText("incl.erl",
"-module(incl).\n" +
"-export([crc32/1, abs/1, dt_get_tag/0, bar/0, abs/0]).\n" +
"\n" +
"crc32(Data) -> Data.\n" +
"abs(D) -> D.\n" +
"abs() -> zero.\n" +
"dt_get_tag() -> ok.\n" +
"bar() -> ok.");
doTest();
}

public void testImportAutoimported() { doTestWithInclude(); }

public void testErlang17SyntaxError() {
enableErlang17SyntaxInspection();
doTest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public static void setUpInspections(@NotNull CodeInsightTestFixture fixture) {
ErlangIoFormatInspection.class,
ErlangDuplicateFunctionExportInspection.class,
ErlangDefiningImportedFunctionInspection.class,
ErlangAmbiguousCallOfAutoimportedFunctionInspection.class
ErlangAmbiguousCallOfAutoimportedFunctionInspection.class,
ErlangImportDirectiveOverridesAutoimportedBifInspection.class
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
package org.intellij.erlang.quickfixes;

import org.intellij.erlang.inspection.ErlangDefiningImportedFunctionInspection;
import org.intellij.erlang.inspection.ErlangImportDirectiveOverridesAutoimportedBifInspection;

public class ErlangImportFixTest extends ErlangQuickFixTestBase {
@Override
protected void setUp() throws Exception {
super.setUp();
//noinspection unchecked
myFixture.enableInspections(ErlangDefiningImportedFunctionInspection.class);
myFixture.enableInspections(
ErlangDefiningImportedFunctionInspection.class,
ErlangImportDirectiveOverridesAutoimportedBifInspection.class
);
}

@Override
Expand Down

0 comments on commit b649b3a

Please sign in to comment.