Skip to content

Commit

Permalink
Internal/private reference violation
Browse files Browse the repository at this point in the history
  • Loading branch information
a2ndrade committed Oct 14, 2018
1 parent 0dc8a2b commit 9d17a3d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
5 changes: 5 additions & 0 deletions q-intellij-plugin.ipr
Expand Up @@ -20,6 +20,11 @@
<option name="REPORT_ANNOTATION_NOT_PROPAGATED_TO_OVERRIDERS" value="true" />
<option name="REPORT_NULLS_PASSED_TO_NON_ANNOTATED_METHOD" value="true" />
</inspection_tool>
<inspection_tool class="SpellCheckingInspection" enabled="true" level="TYPO" enabled_by_default="true">
<option name="processCode" value="false" />
<option name="processLiterals" value="true" />
<option name="processComments" value="true" />
</inspection_tool>
<inspection_tool class="UnusedImport" enabled="true" level="ERROR" enabled_by_default="true" />
</profile>
<version value="1.0" />
Expand Down
Expand Up @@ -116,11 +116,7 @@ public String getLocationString() {
@Nullable
@Override
public Icon getIcon(boolean unused) {
final String name = element.getName();
if (name.startsWith("i.") || name.contains(".i.")) {
return AllIcons.Nodes.C_private;
}
return AllIcons.Nodes.C_public;
return element.isInternal() ? AllIcons.Nodes.C_private : AllIcons.Nodes.C_public;
}

@Nullable
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/com/appian/intellij/k/KUnresolvedIdsAnnotator.java
Expand Up @@ -2,6 +2,10 @@

import static com.appian.intellij.k.KCompletionContributor.isSystemFn;

import java.io.IOException;
import java.util.Optional;
import java.util.Properties;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -10,14 +14,22 @@
import com.appian.intellij.k.psi.KUserId;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.util.PsiTreeUtil;

public class KUnresolvedIdsAnnotator implements Annotator {

static final String Q_INTELLIJ_PLUGIN_CONFIG = ".q-intellij-plugin";
private static final String[] IMPLICIT_VARS = new String[] {"x", "y", "z"};

private enum LinterPreset {
NONE,
APPIAN
}
private static LinterPreset linterPreset;

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
if (!(element instanceof KUserId)) {
Expand All @@ -29,6 +41,7 @@ public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder hold
}
final PsiElement declaration = findDeclaration(usage);
if (declaration != null) {
checkInternalReferenceViolation(usage, declaration, holder);
return;
}
final String variableName = usage.getName();
Expand All @@ -51,6 +64,41 @@ public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder hold
holder.createWeakWarningAnnotation(usage, String.format("`%s` might not have been defined", variableName));
}

private void checkInternalReferenceViolation(KUserId usage, PsiElement declaration, AnnotationHolder holder) {
final LinterPreset linter = getLinterPreset(usage);
if (linter != LinterPreset.APPIAN) {
return;
}
boolean isInternal = usage.isInternal() && !usage.getContainingFile().isEquivalentTo(declaration.getContainingFile());
if (isInternal) {
holder.createWarningAnnotation(usage,
String.format("`%s` is an internal function. It should only be accessed within %s", usage.getName(),
declaration.getContainingFile().getVirtualFile().getName()));
}
}

private LinterPreset getLinterPreset(KUserId usage) {
if (linterPreset != null) {
return linterPreset;
}
return linterPreset = Optional.of(usage)
.map(PsiElement::getProject)
.map(Project::getBaseDir)
.map(p -> p.findChild(Q_INTELLIJ_PLUGIN_CONFIG))
.map(configFile -> {
final Properties props = new Properties();
try {
props.load(configFile.getInputStream());
if ("appian".equals(props.getProperty("preset"))) {
return LinterPreset.APPIAN;
}
} catch (IOException ignore) {
}
return LinterPreset.NONE;
})
.orElse(LinterPreset.NONE);
}

@Nullable
private PsiElement findDeclaration(KUserId usage) {
final PsiReference[] references = usage.getReferences();
Expand Down
Expand Up @@ -77,4 +77,9 @@ public static boolean isDeclaration(final KUserId element) {
return false;
}

public static boolean isInternal(final KUserId element) {
final String name = element.getName();
return (name.startsWith("i.") || name.contains(".i."));
}

}
2 changes: 1 addition & 1 deletion src/main/resources/com/appian/intellij/k/k.bnf
Expand Up @@ -136,5 +136,5 @@ private atom ::= symbol | number | char
user_id ::= (user_identifier | q_system_function) {
mixin="com.appian.intellij.k.psi.impl.KNamedElementImpl"
implements="com.appian.intellij.k.psi.KNamedElement"
methods=[getName setName getNameIdentifier getPresentation isDeclaration]
methods=[getName setName getNameIdentifier getPresentation isDeclaration isInternal]
}

0 comments on commit 9d17a3d

Please sign in to comment.