Enable registering inspections for a MetaLanguage

This commit is contained in:
Dmitry Jemerov
2017-04-06 17:03:38 +02:00
parent adc4b93d18
commit 1cdfe7aa92
4 changed files with 43 additions and 10 deletions
@@ -20,6 +20,7 @@ import com.intellij.lang.MetaLanguage;
import com.intellij.util.containers.HashSet;
import org.jetbrains.uast.UastLanguagePlugin;
import java.util.Collection;
import java.util.Set;
/**
@@ -39,4 +40,9 @@ public class UastMetaLanguage extends MetaLanguage {
public boolean matchesLanguage(Language language) {
return myLanguages.contains(language);
}
@Override
public Collection<Language> getMatchingLanguages() {
return myLanguages;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 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.
@@ -26,6 +26,7 @@ import com.intellij.codeInspection.reference.RefManagerImpl;
import com.intellij.codeInspection.reference.RefVisitor;
import com.intellij.concurrency.JobLauncher;
import com.intellij.lang.Language;
import com.intellij.lang.MetaLanguage;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.progress.ProgressIndicator;
@@ -281,15 +282,28 @@ public class InspectionEngine {
// unknown language in plugin.xml, ignore
result = Collections.singleton(langId);
}
else if (language instanceof MetaLanguage) {
Collection<Language> matchingLanguages = ((MetaLanguage) language).getMatchingLanguages();
result = new HashSet<>();
for (Language matchingLanguage : matchingLanguages) {
result.addAll(getLanguageWithDialects(wrapper, matchingLanguage));
}
}
else {
List<Language> dialects = language.getDialects();
boolean applyToDialects = wrapper.applyToDialects();
result = applyToDialects && !dialects.isEmpty() ? new THashSet<>(1 + dialects.size()) : new SmartHashSet<>();
result.add(langId);
if (applyToDialects) {
for (Language dialect : dialects) {
result.add(dialect.getID());
}
result = getLanguageWithDialects(wrapper, language);
}
return result;
}
@NotNull
private static Set<String> getLanguageWithDialects(@NotNull LocalInspectionToolWrapper wrapper, Language language) {
Set<String> result;List<Language> dialects = language.getDialects();
boolean applyToDialects = wrapper.applyToDialects();
result = applyToDialects && !dialects.isEmpty() ? new THashSet<>(1 + dialects.size()) : new SmartHashSet<>();
result.add(language.getID());
if (applyToDialects) {
for (Language dialect : dialects) {
result.add(dialect.getID());
}
}
return result;
@@ -19,6 +19,9 @@ import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.extensions.Extensions;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.stream.Collectors;
/**
* Allows to register a language extension for a group of languages defined by a certain criterion.
* To use this, specify the ID of a meta-language in the "language" attribute of an extension in plugin.xml.
@@ -41,4 +44,14 @@ public abstract class MetaLanguage extends Language {
* Checks if the given language matches the criterion of this meta-language.
*/
public abstract boolean matchesLanguage(@NotNull Language language);
/**
* Returns the list of all languages matching this meta-language.
*/
public Collection<Language> getMatchingLanguages() {
return Language.getRegisteredLanguages()
.stream()
.filter(language -> matchesLanguage(language))
.collect(Collectors.toList());
}
}
+1 -1
View File
@@ -125,7 +125,7 @@
<localInspection language="JAVA" shortName="UnsafeReturnStatementVisitor" displayName="Unsafe return statements visitor"
groupKey="inspections.group.name" enabledByDefault="true" level="WARNING"
implementationClass="org.jetbrains.idea.devkit.inspections.internal.UnsafeReturnStatementVisitorInspection"/>
<localInspection language="JAVA" shortName="StatefulEp" displayName="Stateful Extension"
<localInspection language="UAST" shortName="StatefulEp" displayName="Stateful Extension"
groupKey="inspections.group.name"
enabledByDefault="true" level="WARNING"
implementationClass="org.jetbrains.idea.devkit.inspections.StatefulEpInspection"/>