From 05e993c9429035fd8003985fffcff01570f98ff7 Mon Sep 17 00:00:00 2001 From: UNV Date: Sun, 28 Jul 2024 19:43:59 +0300 Subject: [PATCH] Code reformat using IJ4 code style (part 5). --- .../src/main/java/module-info.java | 24 +- .../org/intellij/grammar/BnfFileType.java | 56 +-- .../intellij/grammar/BnfFileTypeFactory.java | 8 +- .../java/org/intellij/grammar/BnfIcons.java | 8 +- .../org/intellij/grammar/BnfLanguage.java | 19 +- .../intellij/grammar/BnfParserDefinition.java | 105 +++-- .../org/intellij/grammar/KnownAttribute.java | 404 +++++++++--------- .../psi/BnfAttrPatternRefSearcher.java | 56 ++- .../intellij/grammar/psi/BnfComposite.java | 5 +- .../grammar/psi/BnfCompositeElementType.java | 6 +- .../org/intellij/grammar/psi/BnfFile.java | 48 ++- .../intellij/grammar/psi/BnfNamedElement.java | 8 +- .../intellij/grammar/psi/BnfTokenType.java | 6 +- .../grammar/psi/impl/BnfReferenceImpl.java | 7 +- .../grammar/psi/impl/GrammarPsiImplUtil.java | 117 +++-- .../grammar/psi/impl/GrammarUtil.java | 398 ++++++++--------- .../ConvertResourceBundleToYamlAction.java | 88 ++-- .../consulo/devkit/action/InternalAction.java | 44 +- .../consulo/devkit/action/InternalGroup.java | 12 +- .../daemon/ImplToAPILineMarkerProvider.java | 103 +++-- .../daemon/ServiceLineMarkerProvider.java | 69 ++- .../daemon/UIColorLineMarkerProvider.java | 204 ++++----- ...nOverrideImplementsAnnotationsHandler.java | 34 +- .../java/consulo/devkit/dom/Permission.java | 10 +- .../consulo/devkit/dom/PermissionOption.java | 4 +- 25 files changed, 941 insertions(+), 902 deletions(-) diff --git a/grammar-kit-core/src/main/java/module-info.java b/grammar-kit-core/src/main/java/module-info.java index a120d988..1aa78c40 100644 --- a/grammar-kit-core/src/main/java/module-info.java +++ b/grammar-kit-core/src/main/java/module-info.java @@ -3,17 +3,17 @@ * @since 10/02/2023 */ module consulo.devkit.grammar.kit.core { - requires consulo.language.api; - requires consulo.language.impl; + requires consulo.language.api; + requires consulo.language.impl; - exports consulo.devkit.grammarKit.generator; - exports consulo.devkit.grammarKit.icon; - exports org.intellij.grammar; - exports org.intellij.grammar.analysis; - exports org.intellij.grammar.config; - exports org.intellij.grammar.generator; - exports org.intellij.grammar.java; - exports org.intellij.grammar.parser; - exports org.intellij.grammar.psi; - exports org.intellij.grammar.psi.impl; + exports consulo.devkit.grammarKit.generator; + exports consulo.devkit.grammarKit.icon; + exports org.intellij.grammar; + exports org.intellij.grammar.analysis; + exports org.intellij.grammar.config; + exports org.intellij.grammar.generator; + exports org.intellij.grammar.java; + exports org.intellij.grammar.parser; + exports org.intellij.grammar.psi; + exports org.intellij.grammar.psi.impl; } \ No newline at end of file diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/BnfFileType.java b/grammar-kit-core/src/main/java/org/intellij/grammar/BnfFileType.java index e8710300..85eb9242 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/BnfFileType.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/BnfFileType.java @@ -29,32 +29,32 @@ * Time: 22:46 */ public class BnfFileType extends LanguageFileType { - public static final BnfFileType INSTANCE = new BnfFileType(); - - protected BnfFileType() { - super(BnfLanguage.INSTANCE); - } - - @Nonnull - @Override - public String getId() { - return "BNF"; - } - - @Nonnull - @Override - public LocalizeValue getDescription() { - return LocalizeValue.localizeTODO("BNF Grammar"); - } - - @Nonnull - @Override - public String getDefaultExtension() { - return "bnf"; - } - - @Override - public Image getIcon() { - return GrammarKitIconGroup.grammarfile(); - } + public static final BnfFileType INSTANCE = new BnfFileType(); + + protected BnfFileType() { + super(BnfLanguage.INSTANCE); + } + + @Nonnull + @Override + public String getId() { + return "BNF"; + } + + @Nonnull + @Override + public LocalizeValue getDescription() { + return LocalizeValue.localizeTODO("BNF Grammar"); + } + + @Nonnull + @Override + public String getDefaultExtension() { + return "bnf"; + } + + @Override + public Image getIcon() { + return GrammarKitIconGroup.grammarfile(); + } } diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/BnfFileTypeFactory.java b/grammar-kit-core/src/main/java/org/intellij/grammar/BnfFileTypeFactory.java index 243ead79..2be9f9bb 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/BnfFileTypeFactory.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/BnfFileTypeFactory.java @@ -29,8 +29,8 @@ */ @ExtensionImpl public class BnfFileTypeFactory extends FileTypeFactory { - @Override - public void createFileTypes(@Nonnull FileTypeConsumer fileTypeConsumer) { - fileTypeConsumer.consume(BnfFileType.INSTANCE); - } + @Override + public void createFileTypes(@Nonnull FileTypeConsumer fileTypeConsumer) { + fileTypeConsumer.consume(BnfFileType.INSTANCE); + } } diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/BnfIcons.java b/grammar-kit-core/src/main/java/org/intellij/grammar/BnfIcons.java index 93cab2ea..fba843b2 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/BnfIcons.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/BnfIcons.java @@ -23,9 +23,9 @@ * Date: 17.07.11 2:55 */ public interface BnfIcons { - Image RULE = AllIcons.Nodes.Method; - Image EXTERNAL_RULE = AllIcons.Nodes.AbstractMethod; - Image ATTRIBUTE = AllIcons.Nodes.Attribute; + Image RULE = AllIcons.Nodes.Method; + Image EXTERNAL_RULE = AllIcons.Nodes.AbstractMethod; + Image ATTRIBUTE = AllIcons.Nodes.Attribute; - Image RELATED_METHOD = AllIcons.Gutter.ImplementedMethod; + Image RELATED_METHOD = AllIcons.Gutter.ImplementedMethod; } diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/BnfLanguage.java b/grammar-kit-core/src/main/java/org/intellij/grammar/BnfLanguage.java index 35dc57c1..24a483fb 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/BnfLanguage.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/BnfLanguage.java @@ -26,16 +26,15 @@ * Time: 22:44 */ public class BnfLanguage extends Language { + public static final BnfLanguage INSTANCE = new BnfLanguage(); - public static final BnfLanguage INSTANCE = new BnfLanguage(); + protected BnfLanguage() { + super("BNF"); + } - protected BnfLanguage() { - super("BNF"); - } - - @Nonnull - @Override - public String getDisplayName() { - return "Grammar-Kit BNF"; - } + @Nonnull + @Override + public String getDisplayName() { + return "Grammar-Kit BNF"; + } } diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/BnfParserDefinition.java b/grammar-kit-core/src/main/java/org/intellij/grammar/BnfParserDefinition.java index 438de8d7..b5386233 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/BnfParserDefinition.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/BnfParserDefinition.java @@ -39,67 +39,66 @@ */ @ExtensionImpl public class BnfParserDefinition implements ParserDefinition { + public static final IFileElementType BNF_FILE_ELEMENT_TYPE = new IFileElementType("BNF_FILE", BnfLanguage.INSTANCE); + public static final TokenSet WS = TokenSet.create(TokenType.WHITE_SPACE); + public static final IElementType BNF_LINE_COMMENT = BnfTypes.BNF_LINE_COMMENT; + public static final IElementType BNF_BLOCK_COMMENT = BnfTypes.BNF_BLOCK_COMMENT; + public static final TokenSet COMMENTS = TokenSet.create(BNF_LINE_COMMENT, BNF_BLOCK_COMMENT); + public static final TokenSet LITERALS = TokenSet.create(BnfTypes.BNF_STRING); - public static final IFileElementType BNF_FILE_ELEMENT_TYPE = new IFileElementType("BNF_FILE", BnfLanguage.INSTANCE); - public static final TokenSet WS = TokenSet.create(TokenType.WHITE_SPACE); - public static final IElementType BNF_LINE_COMMENT = BnfTypes.BNF_LINE_COMMENT; - public static final IElementType BNF_BLOCK_COMMENT = BnfTypes.BNF_BLOCK_COMMENT; - public static final TokenSet COMMENTS = TokenSet.create(BNF_LINE_COMMENT, BNF_BLOCK_COMMENT); - public static final TokenSet LITERALS = TokenSet.create(BnfTypes.BNF_STRING); + @Nonnull + @Override + public Language getLanguage() { + return BnfLanguage.INSTANCE; + } - @Nonnull - @Override - public Language getLanguage() { - return BnfLanguage.INSTANCE; - } + @Nonnull + @Override + public Lexer createLexer(LanguageVersion languageVersion) { + return new BnfLexer(); + } - @Nonnull - @Override - public Lexer createLexer(LanguageVersion languageVersion) { - return new BnfLexer(); - } + @Override + public PsiParser createParser(LanguageVersion languageVersion) { + return new GrammarParser(); + } - @Override - public PsiParser createParser(LanguageVersion languageVersion) { - return new GrammarParser(); - } + @Override + public IFileElementType getFileNodeType() { + return BNF_FILE_ELEMENT_TYPE; + } - @Override - public IFileElementType getFileNodeType() { - return BNF_FILE_ELEMENT_TYPE; - } + @Nonnull + @Override + public TokenSet getWhitespaceTokens(LanguageVersion languageVersion) { + return WS; + } - @Nonnull - @Override - public TokenSet getWhitespaceTokens(LanguageVersion languageVersion) { - return WS; - } + @Nonnull + @Override + public TokenSet getCommentTokens(LanguageVersion languageVersion) { + return COMMENTS; + } - @Nonnull - @Override - public TokenSet getCommentTokens(LanguageVersion languageVersion) { - return COMMENTS; - } + @Nonnull + @Override + public TokenSet getStringLiteralElements(LanguageVersion languageVersion) { + return LITERALS; + } - @Nonnull - @Override - public TokenSet getStringLiteralElements(LanguageVersion languageVersion) { - return LITERALS; - } + @Nonnull + @Override + public PsiElement createElement(ASTNode astNode) { + return BnfTypes.Factory.createElement(astNode); + } - @Nonnull - @Override - public PsiElement createElement(ASTNode astNode) { - return BnfTypes.Factory.createElement(astNode); - } + @Override + public PsiFile createFile(FileViewProvider fileViewProvider) { + return new BnfFileImpl(fileViewProvider); + } - @Override - public PsiFile createFile(FileViewProvider fileViewProvider) { - return new BnfFileImpl(fileViewProvider); - } - - @Override - public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode astNode, ASTNode astNode1) { - return SpaceRequirements.MAY; - } + @Override + public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode astNode, ASTNode astNode1) { + return SpaceRequirements.MAY; + } } diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/KnownAttribute.java b/grammar-kit-core/src/main/java/org/intellij/grammar/KnownAttribute.java index 14ba8c4a..3c413113 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/KnownAttribute.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/KnownAttribute.java @@ -21,217 +21,233 @@ */ @SuppressWarnings("StaticVariableOfConcreteClass") public class KnownAttribute { - private static final Map ourAttributes = new TreeMap<>(); - - @Nonnull - public static Collection getAttributes() { - return Collections.unmodifiableCollection(ourAttributes.values()); - } - - @Nullable - public static KnownAttribute getAttribute(@Nullable String name) { - return name == null ? null : ourAttributes.get(name); - } - - private static final ListValue EMPTY_LIST = new ListValue(); - - public static final KnownAttribute CLASS_HEADER = create(true, String.class, "classHeader", BnfConstants.CLASS_HEADER_DEF); - public static final KnownAttribute GENERATE = create(true, ListValue.class, "generate", EMPTY_LIST); - public static final KnownAttribute GENERATE_PSI = create(true, Boolean.class, "generatePsi", true); - public static final KnownAttribute GENERATE_TOKENS = create(true, Boolean.class, "generateTokens", true); - public static final KnownAttribute GENERATE_TOKEN_ACCESSORS = create(true, Boolean.class, "generateTokenAccessors", false); - public static final KnownAttribute GENERATE_FIRST_CHECK = create(true, Integer.class, "generateFirstCheck", 2); - public static final KnownAttribute EXTENDED_PIN = create(true, Boolean.class, "extendedPin", true); - - public static final KnownAttribute PARSER_IMPORTS = create(true, ListValue.class, "parserImports", EMPTY_LIST); - public static final KnownAttribute VERSION = create(true, String.class, "version", ""); - public static final KnownAttribute PSI_CLASS_PREFIX = create(true, String.class, "psiClassPrefix", ""); - public static final KnownAttribute PSI_IMPL_CLASS_SUFFIX = create(true, String.class, "psiImplClassSuffix", "Impl"); - public static final KnownAttribute PSI_TREE_UTIL_CLASS = - create(true, String.class, "psiTreeUtilClass", PlatformClass.PSI_TREE_UTIL); - public static final KnownAttribute PSI_PACKAGE = create(true, String.class, "psiPackage", "generated.psi"); - public static final KnownAttribute PSI_IMPL_PACKAGE = create(true, String.class, "psiImplPackage", "generated.psi.impl"); - public static final KnownAttribute PSI_VISITOR_NAME = create(true, String.class, "psiVisitorName", "Visitor"); - public static final KnownAttribute PSI_IMPL_UTIL_CLASS = create(true, String.class, "psiImplUtilClass", (String)null); - public static final KnownAttribute ELEMENT_TYPE_CLASS = - create(true, String.class, "elementTypeClass", PlatformClass.IELEMENT_TYPE); - public static final KnownAttribute TOKEN_TYPE_CLASS = create(true, String.class, "tokenTypeClass", PlatformClass.IELEMENT_TYPE); - public static final KnownAttribute PARSER_CLASS = create(true, String.class, "parserClass", "generated.GeneratedParser"); - public static final KnownAttribute PARSER_UTIL_CLASS = - create(true, String.class, "parserUtilClass", PlatformClass.GENERATED_PARSER_UTIL_BASE); - public static final KnownAttribute ELEMENT_TYPE_HOLDER_CLASS = - create(true, String.class, "elementTypeHolderClass", "generated.GeneratedTypes"); - public static final KnownAttribute ELEMENT_TYPE_PREFIX = create(true, String.class, "elementTypePrefix", ""); - public static final KnownAttribute ELEMENT_TYPE_FACTORY = create(true, String.class, "elementTypeFactory", (String)null); - public static final KnownAttribute TOKEN_TYPE_FACTORY = create(true, String.class, "tokenTypeFactory", (String)null); - - public static final KnownAttribute EXTENDS = create(false, String.class, "extends", PlatformClass.AST_WRAPPER_PSI_ELEMENT); - public static final KnownAttribute IMPLEMENTS = - create(false, ListValue.class, "implements", ListValue.singleValue(null, PlatformClass.PSI_ELEMENT)); - public static final KnownAttribute ELEMENT_TYPE = create(false, String.class, "elementType", (String)null); - public static final KnownAttribute PIN = create(false, Object.class, "pin", (Object)(-1)); - public static final KnownAttribute MIXIN = create(false, String.class, "mixin", (String)null); - public static final KnownAttribute RECOVER_WHILE = create(false, String.class, "recoverWhile", (String)null); - public static final KnownAttribute NAME = create(false, String.class, "name", (String)null); - - public static final KnownAttribute EXTRA_ROOT = create(false, Boolean.class, "extraRoot", false); - public static final KnownAttribute RIGHT_ASSOCIATIVE = create(false, Boolean.class, "rightAssociative", false); - public static final KnownAttribute CONSUME_TOKEN_METHOD = create(false, String.class, "consumeTokenMethod", "consumeToken"); - - public static final KnownAttribute STUB_CLASS = create(false, String.class, "stubClass", (String)null); - - public static final KnownAttribute METHODS = create(false, ListValue.class, "methods", EMPTY_LIST); - public static final KnownAttribute HOOKS = create(false, ListValue.class, "hooks", EMPTY_LIST); - public static final KnownAttribute TOKENS = create(true, ListValue.class, "tokens", EMPTY_LIST); - - private final boolean myGlobal; - private final String myName; - private final Class myClazz; - private final T myDefaultValue; - - public static KnownAttribute create(Class clazz, String name, @Nullable T defaultValue) { - return new KnownAttribute<>(name, clazz, defaultValue); - } - - public static KnownAttribute create(boolean global, Class clazz, String name, @Nonnull PlatformClass platformClass) { - if (clazz != String.class) { - throw new IllegalArgumentException(clazz.toString()); - } - return new PlatformClassKnownAttribute(global, name, platformClass); - } - - private static KnownAttribute create(boolean global, Class clazz, String name, @Nullable T defaultValue) { - return new KnownAttribute<>(global, name, clazz, defaultValue); - } - - protected KnownAttribute(String name, Class clazz, T defaultValue) { - myName = name; - myClazz = clazz; - myDefaultValue = defaultValue; - myGlobal = false; - } - - protected KnownAttribute(boolean global, String name, Class clazz, T defaultValue) { - myName = name; - myClazz = clazz; - myDefaultValue = defaultValue; - myGlobal = global; - KnownAttribute prev = ourAttributes.put(name, this); - assert prev == null : name + " attribute already defined"; - } - - @Nonnull - public String getName() { - return myName; - } - - public boolean isGlobal() { - return myGlobal; - } - - public T getDefaultValue(String version) { - return myDefaultValue; - } - - public T ensureValue(Object o, String version) { - if (o == null) { - return getDefaultValue(version); - } - if (myClazz == ListValue.class && o instanceof String) { - return (T)ListValue.singleValue(null, (String)o); - } - if (myClazz.isInstance(o)) { - return (T)o; - } - return getDefaultValue(version); - } - - @Override - public String toString() { - return myName; - } - - public String getDescription() { - try { - InputStream resourceAsStream = getClass().getResourceAsStream("/messages/attributeDescriptions/" + getName() + ".html"); - return resourceAsStream == null ? null : FileUtil.loadTextAndClose(resourceAsStream); - } - catch (IOException e) { - return null; - } - } - - // returns a non-registered attribute for migration purposes - @Nonnull - public KnownAttribute alias(String deprecatedName) { - return new KnownAttribute<>(deprecatedName, myClazz, null); - } - - @Nullable - public static KnownAttribute getCompatibleAttribute(String name) { - return getAttribute(name); - } + private static final Map ourAttributes = new TreeMap<>(); - public static class ListValueObject { - private final Object myValue; + @Nonnull + public static Collection getAttributes() { + return Collections.unmodifiableCollection(ourAttributes.values()); + } - public ListValueObject(@Nonnull Object value) { - myValue = value; + @Nullable + public static KnownAttribute getAttribute(@Nullable String name) { + return name == null ? null : ourAttributes.get(name); } - public String getStringValue(String version) { - if (myValue instanceof PlatformClass) { - return ((PlatformClass)myValue).select(version); - } - return (String)myValue; - } - } + private static final ListValue EMPTY_LIST = new ListValue(); + + public static final KnownAttribute CLASS_HEADER = + create(true, String.class, "classHeader", BnfConstants.CLASS_HEADER_DEF); + public static final KnownAttribute GENERATE = create(true, ListValue.class, "generate", EMPTY_LIST); + public static final KnownAttribute GENERATE_PSI = create(true, Boolean.class, "generatePsi", true); + public static final KnownAttribute GENERATE_TOKENS = create(true, Boolean.class, "generateTokens", true); + public static final KnownAttribute GENERATE_TOKEN_ACCESSORS = + create(true, Boolean.class, "generateTokenAccessors", false); + public static final KnownAttribute GENERATE_FIRST_CHECK = + create(true, Integer.class, "generateFirstCheck", 2); + public static final KnownAttribute EXTENDED_PIN = create(true, Boolean.class, "extendedPin", true); + + public static final KnownAttribute PARSER_IMPORTS = create(true, ListValue.class, "parserImports", EMPTY_LIST); + public static final KnownAttribute VERSION = create(true, String.class, "version", ""); + public static final KnownAttribute PSI_CLASS_PREFIX = create(true, String.class, "psiClassPrefix", ""); + public static final KnownAttribute PSI_IMPL_CLASS_SUFFIX = + create(true, String.class, "psiImplClassSuffix", "Impl"); + public static final KnownAttribute PSI_TREE_UTIL_CLASS = + create(true, String.class, "psiTreeUtilClass", PlatformClass.PSI_TREE_UTIL); + public static final KnownAttribute PSI_PACKAGE = + create(true, String.class, "psiPackage", "generated.psi"); + public static final KnownAttribute PSI_IMPL_PACKAGE = + create(true, String.class, "psiImplPackage", "generated.psi.impl"); + public static final KnownAttribute PSI_VISITOR_NAME = + create(true, String.class, "psiVisitorName", "Visitor"); + public static final KnownAttribute PSI_IMPL_UTIL_CLASS = + create(true, String.class, "psiImplUtilClass", (String)null); + public static final KnownAttribute ELEMENT_TYPE_CLASS = + create(true, String.class, "elementTypeClass", PlatformClass.IELEMENT_TYPE); + public static final KnownAttribute TOKEN_TYPE_CLASS = + create(true, String.class, "tokenTypeClass", PlatformClass.IELEMENT_TYPE); + public static final KnownAttribute PARSER_CLASS = + create(true, String.class, "parserClass", "generated.GeneratedParser"); + public static final KnownAttribute PARSER_UTIL_CLASS = + create(true, String.class, "parserUtilClass", PlatformClass.GENERATED_PARSER_UTIL_BASE); + public static final KnownAttribute ELEMENT_TYPE_HOLDER_CLASS = + create(true, String.class, "elementTypeHolderClass", "generated.GeneratedTypes"); + public static final KnownAttribute ELEMENT_TYPE_PREFIX = + create(true, String.class, "elementTypePrefix", ""); + public static final KnownAttribute ELEMENT_TYPE_FACTORY = + create(true, String.class, "elementTypeFactory", (String)null); + public static final KnownAttribute TOKEN_TYPE_FACTORY = + create(true, String.class, "tokenTypeFactory", (String)null); + + public static final KnownAttribute EXTENDS = + create(false, String.class, "extends", PlatformClass.AST_WRAPPER_PSI_ELEMENT); + public static final KnownAttribute IMPLEMENTS = + create(false, ListValue.class, "implements", ListValue.singleValue(null, PlatformClass.PSI_ELEMENT)); + public static final KnownAttribute ELEMENT_TYPE = create(false, String.class, "elementType", (String)null); + public static final KnownAttribute PIN = create(false, Object.class, "pin", (Object)(-1)); + public static final KnownAttribute MIXIN = create(false, String.class, "mixin", (String)null); + public static final KnownAttribute RECOVER_WHILE = create(false, String.class, "recoverWhile", (String)null); + public static final KnownAttribute NAME = create(false, String.class, "name", (String)null); + + public static final KnownAttribute EXTRA_ROOT = create(false, Boolean.class, "extraRoot", false); + public static final KnownAttribute RIGHT_ASSOCIATIVE = + create(false, Boolean.class, "rightAssociative", false); + public static final KnownAttribute CONSUME_TOKEN_METHOD = + create(false, String.class, "consumeTokenMethod", "consumeToken"); + + public static final KnownAttribute STUB_CLASS = create(false, String.class, "stubClass", (String)null); + + public static final KnownAttribute METHODS = create(false, ListValue.class, "methods", EMPTY_LIST); + public static final KnownAttribute HOOKS = create(false, ListValue.class, "hooks", EMPTY_LIST); + public static final KnownAttribute TOKENS = create(true, ListValue.class, "tokens", EMPTY_LIST); + + private final boolean myGlobal; + private final String myName; + private final Class myClazz; + private final T myDefaultValue; + + public static KnownAttribute create(Class clazz, String name, @Nullable T defaultValue) { + return new KnownAttribute<>(name, clazz, defaultValue); + } - public static class ListValue extends LinkedList> { - @Nonnull - public static ListValue singleValue(Object s1, Object s2) { - ListValue t = new ListValue(); - t.add(Pair.create(s1 == null ? null : new ListValueObject(s1), s2 == null ? null : new ListValueObject(s2))); - return t; + public static KnownAttribute create(boolean global, Class clazz, String name, @Nonnull PlatformClass platformClass) { + if (clazz != String.class) { + throw new IllegalArgumentException(clazz.toString()); + } + return new PlatformClassKnownAttribute(global, name, platformClass); + } + + private static KnownAttribute create(boolean global, Class clazz, String name, @Nullable T defaultValue) { + return new KnownAttribute<>(global, name, clazz, defaultValue); + } + + protected KnownAttribute(String name, Class clazz, T defaultValue) { + myName = name; + myClazz = clazz; + myDefaultValue = defaultValue; + myGlobal = false; + } + + protected KnownAttribute(boolean global, String name, Class clazz, T defaultValue) { + myName = name; + myClazz = clazz; + myDefaultValue = defaultValue; + myGlobal = global; + KnownAttribute prev = ourAttributes.put(name, this); + assert prev == null : name + " attribute already defined"; } @Nonnull - public List asStrings(String version) { - List t = new ArrayList<>(); - for (Pair pair : this) { - if (pair.first != null) { - t.add(pair.first.getStringValue(version)); + public String getName() { + return myName; + } + + public boolean isGlobal() { + return myGlobal; + } + + public T getDefaultValue(String version) { + return myDefaultValue; + } + + public T ensureValue(Object o, String version) { + if (o == null) { + return getDefaultValue(version); + } + if (myClazz == ListValue.class && o instanceof String) { + return (T)ListValue.singleValue(null, (String)o); } - else if (pair.second != null) { - t.add(pair.second.getStringValue(version)); + if (myClazz.isInstance(o)) { + return (T)o; } - } - return t; + return getDefaultValue(version); } - @Nonnull - public Map asMap(String version) { - return asMap(version, false); + @Override + public String toString() { + return myName; } - @Nonnull - public Map asInverseMap(String version) { - return asMap(version, true); + public String getDescription() { + try { + InputStream resourceAsStream = getClass().getResourceAsStream("/messages/attributeDescriptions/" + getName() + ".html"); + return resourceAsStream == null ? null : FileUtil.loadTextAndClose(resourceAsStream); + } + catch (IOException e) { + return null; + } } + // returns a non-registered attribute for migration purposes @Nonnull - private Map asMap(String version, boolean inverse) { - Map t = new LinkedHashMap<>(); - for (Pair pair : this) { - ListValueObject v1 = inverse ? pair.second : pair.first; - String key = v1 == null ? null : v1.getStringValue(version); - ListValueObject v2 = inverse ? pair.first : pair.second; - String value = v2 == null ? null : v2.getStringValue(version); - if (key != null) { - t.put(key, value); + public KnownAttribute alias(String deprecatedName) { + return new KnownAttribute<>(deprecatedName, myClazz, null); + } + + @Nullable + public static KnownAttribute getCompatibleAttribute(String name) { + return getAttribute(name); + } + + public static class ListValueObject { + private final Object myValue; + + public ListValueObject(@Nonnull Object value) { + myValue = value; + } + + public String getStringValue(String version) { + if (myValue instanceof PlatformClass) { + return ((PlatformClass)myValue).select(version); + } + return (String)myValue; + } + } + + public static class ListValue extends LinkedList> { + @Nonnull + public static ListValue singleValue(Object s1, Object s2) { + ListValue t = new ListValue(); + t.add(Pair.create(s1 == null ? null : new ListValueObject(s1), s2 == null ? null : new ListValueObject(s2))); + return t; + } + + @Nonnull + public List asStrings(String version) { + List t = new ArrayList<>(); + for (Pair pair : this) { + if (pair.first != null) { + t.add(pair.first.getStringValue(version)); + } + else if (pair.second != null) { + t.add(pair.second.getStringValue(version)); + } + } + return t; + } + + @Nonnull + public Map asMap(String version) { + return asMap(version, false); + } + + @Nonnull + public Map asInverseMap(String version) { + return asMap(version, true); + } + + @Nonnull + private Map asMap(String version, boolean inverse) { + Map t = new LinkedHashMap<>(); + for (Pair pair : this) { + ListValueObject v1 = inverse ? pair.second : pair.first; + String key = v1 == null ? null : v1.getStringValue(version); + ListValueObject v2 = inverse ? pair.first : pair.second; + String value = v2 == null ? null : v2.getStringValue(version); + if (key != null) { + t.put(key, value); + } + } + return Collections.unmodifiableMap(t); } - } - return Collections.unmodifiableMap(t); } - } } diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfAttrPatternRefSearcher.java b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfAttrPatternRefSearcher.java index c5f34c33..2253ad35 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfAttrPatternRefSearcher.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfAttrPatternRefSearcher.java @@ -34,33 +34,45 @@ * @author gregsh */ @ExtensionImpl -public class BnfAttrPatternRefSearcher extends QueryExecutorBase implements ReferencesSearchQueryExecutor { - public BnfAttrPatternRefSearcher() { - super(true); - } +public class BnfAttrPatternRefSearcher extends QueryExecutorBase + implements ReferencesSearchQueryExecutor { + public BnfAttrPatternRefSearcher() { + super(true); + } - @Override - public void processQuery(@Nonnull ReferencesSearch.SearchParameters queryParameters, @Nonnull final Processor consumer) { - final PsiElement target = queryParameters.getElementToSearch(); - if (!(target instanceof BnfRule)) return; + @Override + public void processQuery( + @Nonnull ReferencesSearch.SearchParameters queryParameters, + @Nonnull final Processor consumer + ) { + final PsiElement target = queryParameters.getElementToSearch(); + if (!(target instanceof BnfRule)) { + return; + } - SearchScope scope = queryParameters.getEffectiveSearchScope(); - if (!(scope instanceof LocalSearchScope)) return; + SearchScope scope = queryParameters.getEffectiveSearchScope(); + if (!(scope instanceof LocalSearchScope)) { + return; + } - PsiFile file = target.getContainingFile(); - if (!(file instanceof BnfFile)) return; + PsiFile file = target.getContainingFile(); + if (!(file instanceof BnfFile)) { + return; + } - for (BnfAttrs attrs : ((BnfFile)file).getAttributes()) { - for (BnfAttr attr : attrs.getAttrList()) { - BnfAttrPattern pattern = attr.getAttrPattern(); - if (pattern == null) continue; - BnfStringLiteralExpression patternExpression = pattern.getLiteralExpression(); + for (BnfAttrs attrs : ((BnfFile)file).getAttributes()) { + for (BnfAttr attr : attrs.getAttrList()) { + BnfAttrPattern pattern = attr.getAttrPattern(); + if (pattern == null) { + continue; + } + BnfStringLiteralExpression patternExpression = pattern.getLiteralExpression(); - PsiReference ref = BnfStringImpl.matchesElement(patternExpression, target) ? patternExpression.getReference() : null; - if (ref != null && ref.isReferenceTo(target)) { - if (!consumer.process(ref)) return; + PsiReference ref = BnfStringImpl.matchesElement(patternExpression, target) ? patternExpression.getReference() : null; + if (ref != null && ref.isReferenceTo(target) && !consumer.process(ref)) { + return; + } + } } - } } - } } diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfComposite.java b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfComposite.java index d54a0fb3..72f60ecd 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfComposite.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfComposite.java @@ -12,7 +12,6 @@ * Date: 13.07.11 * Time: 19:02 */ -public interface BnfComposite extends PsiElement -{ - R accept(@Nonnull BnfVisitor visitor); +public interface BnfComposite extends PsiElement { + R accept(@Nonnull BnfVisitor visitor); } diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfCompositeElementType.java b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfCompositeElementType.java index d6f210f0..9d25aecf 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfCompositeElementType.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfCompositeElementType.java @@ -25,7 +25,7 @@ * Time: 19:11 */ public class BnfCompositeElementType extends IElementType { - public BnfCompositeElementType(String debug) { - super(debug, BnfLanguage.INSTANCE); - } + public BnfCompositeElementType(String debug) { + super(debug, BnfLanguage.INSTANCE); + } } diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfFile.java b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfFile.java index 1d1b34d7..6b4c661a 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfFile.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfFile.java @@ -27,23 +27,33 @@ * @author gregsh */ public interface BnfFile extends PsiFile { - @Nonnull - List getRules(); - - @Nonnull - List getAttributes(); - - @Nullable - BnfRule getRule(@Nullable String ruleName); - - @Nullable - default String getVersion() { - return findAttributeValue(null, null, KnownAttribute.VERSION, null); - } - - @Nullable - BnfAttr findAttribute(@Nullable String version, @Nullable BnfRule rule, @Nonnull KnownAttribute knownAttribute, @Nullable String match); - - @Nullable - T findAttributeValue(@Nullable String version, @Nullable BnfRule rule, @Nonnull KnownAttribute knownAttribute, @Nullable String match); + @Nonnull + List getRules(); + + @Nonnull + List getAttributes(); + + @Nullable + BnfRule getRule(@Nullable String ruleName); + + @Nullable + default String getVersion() { + return findAttributeValue(null, null, KnownAttribute.VERSION, null); + } + + @Nullable + BnfAttr findAttribute( + @Nullable String version, + @Nullable BnfRule rule, + @Nonnull KnownAttribute knownAttribute, + @Nullable String match + ); + + @Nullable + T findAttributeValue( + @Nullable String version, + @Nullable BnfRule rule, + @Nonnull KnownAttribute knownAttribute, + @Nullable String match + ); } diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfNamedElement.java b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfNamedElement.java index 2a83a0a0..e14bfd33 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfNamedElement.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfNamedElement.java @@ -26,9 +26,9 @@ * Time: 19:02 */ public interface BnfNamedElement extends BnfComposite, PsiNameIdentifierOwner { - @Nonnull - String getName(); + @Nonnull + String getName(); - @Nonnull - PsiElement getId(); + @Nonnull + PsiElement getId(); } diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfTokenType.java b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfTokenType.java index ceffb119..1cd38c14 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfTokenType.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/BnfTokenType.java @@ -25,7 +25,7 @@ * Time: 19:12 */ public class BnfTokenType extends IElementType { - public BnfTokenType(String debug) { - super(debug, BnfLanguage.INSTANCE); - } + public BnfTokenType(String debug) { + super(debug, BnfLanguage.INSTANCE); + } } diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/impl/BnfReferenceImpl.java b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/impl/BnfReferenceImpl.java index 4c9d7b3e..b7d3df7f 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/impl/BnfReferenceImpl.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/impl/BnfReferenceImpl.java @@ -46,8 +46,11 @@ public PsiElement resolve() { String version = containingFile instanceof BnfFile bnfFile ? bnfFile.getVersion() : null; if (result == null && GrammarUtil.isExternalReference(myElement)) { PsiElement parent = myElement.getParent(); - int paramCount = parent instanceof BnfSequence bnfSequence ? bnfSequence.getExpressionList().size() - 1 - : parent instanceof BnfExternalExpression bnfExternalExpression ? bnfExternalExpression.getExpressionList().size() - 1 : 0; + int paramCount = parent instanceof BnfSequence bnfSequence + ? bnfSequence.getExpressionList().size() - 1 + : parent instanceof BnfExternalExpression bnfExternalExpression + ? bnfExternalExpression.getExpressionList().size() - 1 + : 0; BnfRule rule = PsiTreeUtil.getParentOfType(myElement, BnfRule.class); String parserClass = ParserGeneratorUtil.getAttribute(version, rule, KnownAttribute.PARSER_UTIL_CLASS); // paramCount + 2 (builder and level) diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/impl/GrammarPsiImplUtil.java b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/impl/GrammarPsiImplUtil.java index e89ec493..a0f563f1 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/impl/GrammarPsiImplUtil.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/impl/GrammarPsiImplUtil.java @@ -36,69 +36,68 @@ * @author gregsh */ public class GrammarPsiImplUtil { - @Nonnull - public static PsiReference[] getReferences(BnfListEntry o) { - BnfAttr attr = PsiTreeUtil.getParentOfType(o, BnfAttr.class); - if (attr == null || !Comparing.equal(KnownAttribute.METHODS.getName(), attr.getName())) { - return PsiReference.EMPTY_ARRAY; - } - PsiElement id = o.getId(); - BnfLiteralExpression value = o.getLiteralExpression(); - if (id == null || value != null) { - return PsiReference.EMPTY_ARRAY; - } - PsiFile containingFile = o.getContainingFile(); - String version = containingFile instanceof BnfFile ? ((BnfFile)containingFile).getVersion() : null; - - final String psiImplUtilClass = getRootAttribute(version, attr, KnownAttribute.PSI_IMPL_UTIL_CLASS); - final JavaHelper javaHelper = JavaHelper.getJavaHelper(o); + @Nonnull + public static PsiReference[] getReferences(BnfListEntry o) { + BnfAttr attr = PsiTreeUtil.getParentOfType(o, BnfAttr.class); + if (attr == null || !Comparing.equal(KnownAttribute.METHODS.getName(), attr.getName())) { + return PsiReference.EMPTY_ARRAY; + } + PsiElement id = o.getId(); + BnfLiteralExpression value = o.getLiteralExpression(); + if (id == null || value != null) { + return PsiReference.EMPTY_ARRAY; + } + PsiFile containingFile = o.getContainingFile(); + String version = containingFile instanceof BnfFile bnfFile ? bnfFile.getVersion() : null; - return new PsiReference[]{ - new PsiPolyVariantReferenceBase<>(o, TextRange.from(id.getStartOffsetInParent(), id.getTextLength())) { + final String psiImplUtilClass = getRootAttribute(version, attr, KnownAttribute.PSI_IMPL_UTIL_CLASS); + final JavaHelper javaHelper = JavaHelper.getJavaHelper(o); - private List getTargetMethods(String methodName) { - BnfRule rule = PsiTreeUtil.getParentOfType(getElement(), BnfRule.class); - String mixinClass = rule == null ? null : getAttribute(version, rule, KnownAttribute.MIXIN); - List implMethods = findRuleImplMethods(version, javaHelper, psiImplUtilClass, methodName, rule); - if (!implMethods.isEmpty()) { - return implMethods; - } - List mixinMethods = - javaHelper.findClassMethods(version, mixinClass, JavaHelper.MethodType.INSTANCE, methodName, -1); - return ContainerUtil.concat(implMethods, mixinMethods); - } + return new PsiReference[]{ + new PsiPolyVariantReferenceBase<>(o, TextRange.from(id.getStartOffsetInParent(), id.getTextLength())) { + private List getTargetMethods(String methodName) { + BnfRule rule = PsiTreeUtil.getParentOfType(getElement(), BnfRule.class); + String mixinClass = rule == null ? null : getAttribute(version, rule, KnownAttribute.MIXIN); + List implMethods = findRuleImplMethods(version, javaHelper, psiImplUtilClass, methodName, rule); + if (!implMethods.isEmpty()) { + return implMethods; + } + List mixinMethods = + javaHelper.findClassMethods(version, mixinClass, JavaHelper.MethodType.INSTANCE, methodName, -1); + return ContainerUtil.concat(implMethods, mixinMethods); + } - @Nonnull - @Override - public ResolveResult[] multiResolve(boolean b) { - return PsiElementResolveResult.createResults(getTargetMethods(getElement().getText())); - } + @Nonnull + @Override + public ResolveResult[] multiResolve(boolean b) { + return PsiElementResolveResult.createResults(getTargetMethods(getElement().getText())); + } - // TODO [VISTALL] remove that - // @Nonnull - // @Override - // public Object[] getVariants() { - // List list = ContainerUtil.newArrayList(); - // for (NavigatablePsiElement element : getTargetMethods("*")) { - // list.add(LookupElementBuilder.createWithIcon((PsiNamedElement)element)); - // } - // return ArrayUtil.toObjectArray(list); - // } + // TODO [VISTALL] remove that + // @Nonnull + // @Override + // public Object[] getVariants() { + // List list = ContainerUtil.newArrayList(); + // for (NavigatablePsiElement element : getTargetMethods("*")) { + // list.add(LookupElementBuilder.createWithIcon((PsiNamedElement)element)); + // } + // return ArrayUtil.toObjectArray(list); + // } - @Override - public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { - BnfListEntry element = getElement(); - PsiElement id = ObjectUtil.assertNotNull(element.getId()); - id.replace(BnfElementFactory.createLeafFromText(element.getProject(), newElementName)); - return element; - } - } - }; - } + @Override + public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { + BnfListEntry element = getElement(); + PsiElement id = ObjectUtil.assertNotNull(element.getId()); + id.replace(BnfElementFactory.createLeafFromText(element.getProject(), newElementName)); + return element; + } + } + }; + } - @Nonnull - public static List getArguments(@Nonnull BnfExternalExpression expr) { - List expressions = expr.getExpressionList(); - return expressions.subList(1, expressions.size()); - } + @Nonnull + public static List getArguments(@Nonnull BnfExternalExpression expr) { + List expressions = expr.getExpressionList(); + return expressions.subList(1, expressions.size()); + } } diff --git a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/impl/GrammarUtil.java b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/impl/GrammarUtil.java index 401551f7..c8f6782b 100644 --- a/grammar-kit-core/src/main/java/org/intellij/grammar/psi/impl/GrammarUtil.java +++ b/grammar-kit-core/src/main/java/org/intellij/grammar/psi/impl/GrammarUtil.java @@ -35,239 +35,243 @@ * @author gregsh */ public class GrammarUtil { + public static final BnfExpression[] EMPTY_EXPRESSIONS_ARRAY = new BnfExpression[0]; - public static final BnfExpression[] EMPTY_EXPRESSIONS_ARRAY = new BnfExpression[0]; - - public static PsiElement getDummyAwarePrevSibling(PsiElement child) { - PsiElement prevSibling = child.getPrevSibling(); - while (prevSibling instanceof GeneratedParserUtilBase.DummyBlock) { - prevSibling = prevSibling.getLastChild(); - } - if (prevSibling != null) { - return prevSibling; - } - PsiElement parent = child.getParent(); - while (parent instanceof GeneratedParserUtilBase.DummyBlock && parent.getPrevSibling() == null) { - parent = parent.getParent(); + public static PsiElement getDummyAwarePrevSibling(PsiElement child) { + PsiElement prevSibling = child.getPrevSibling(); + while (prevSibling instanceof GeneratedParserUtilBase.DummyBlock) { + prevSibling = prevSibling.getLastChild(); + } + if (prevSibling != null) { + return prevSibling; + } + PsiElement parent = child.getParent(); + while (parent instanceof GeneratedParserUtilBase.DummyBlock && parent.getPrevSibling() == null) { + parent = parent.getParent(); + } + return parent == null ? null : parent.getPrevSibling(); } - return parent == null ? null : parent.getPrevSibling(); - } - public static boolean equalsElement(BnfExpression e1, BnfExpression e2) { - if (e1 == null) { - return e2 == null; - } - if (e2 == null) { - return false; - } - if (ParserGeneratorUtil.getEffectiveType(e1) != ParserGeneratorUtil.getEffectiveType(e2)) { - return false; - } - if (isOneTokenExpression(e1)) { - return e1.getText().equals(e2.getText()); - } - else { - for (PsiElement c1 = e1.getFirstChild(), c2 = e2.getFirstChild(); ; ) { - boolean f1 = c1 == null || c1 instanceof BnfExpression; - boolean f2 = c2 == null || c2 instanceof BnfExpression; - if (f1 && f2 && !equalsElement((BnfExpression)c1, (BnfExpression)c2)) { - return false; + public static boolean equalsElement(BnfExpression e1, BnfExpression e2) { + if (e1 == null) { + return e2 == null; } - if (!f1 || f2) { - c1 = c1 == null ? null : c1.getNextSibling(); + if (e2 == null || ParserGeneratorUtil.getEffectiveType(e1) != ParserGeneratorUtil.getEffectiveType(e2)) { + return false; } - if (f1 || !f2) { - c2 = c2 == null ? null : c2.getNextSibling(); + if (isOneTokenExpression(e1)) { + return e1.getText().equals(e2.getText()); } - if (c1 == null && c2 == null) { - return true; + else { + for (PsiElement c1 = e1.getFirstChild(), c2 = e2.getFirstChild(); ; ) { + boolean f1 = c1 == null || c1 instanceof BnfExpression; + boolean f2 = c2 == null || c2 instanceof BnfExpression; + if (f1 && f2 && !equalsElement((BnfExpression)c1, (BnfExpression)c2)) { + return false; + } + if (!f1 || f2) { + c1 = c1 == null ? null : c1.getNextSibling(); + } + if (f1 || !f2) { + c2 = c2 == null ? null : c2.getNextSibling(); + } + if (c1 == null && c2 == null) { + return true; + } + } } - } } - } - - public static boolean isInAttributesReference(@Nullable PsiElement element) { - return PsiTreeUtil.getParentOfType(element, BnfRule.class, BnfAttrs.class) instanceof BnfAttrs; - } - public static boolean isOneTokenExpression(@Nullable BnfExpression e1) { - return e1 instanceof BnfLiteralExpression || e1 instanceof BnfReferenceOrToken; - } - - public static boolean isExternalReference(@Nullable PsiElement psiElement) { - PsiElement parent = psiElement == null ? null : psiElement.getParent(); - if (parent instanceof BnfExternalExpression && ((BnfExternalExpression)parent).getRefElement() == psiElement) { - return true; - } - if (parent instanceof BnfSequence && parent.getFirstChild() == psiElement) { - parent = parent.getParent(); + public static boolean isInAttributesReference(@Nullable PsiElement element) { + return PsiTreeUtil.getParentOfType(element, BnfRule.class, BnfAttrs.class) instanceof BnfAttrs; } - return parent instanceof BnfRule && ParserGeneratorUtil.Rule.isExternal((BnfRule)parent); - } - - public static List getExternalRuleExpressions(@Nonnull BnfRule subRule) { - BnfExpression expression = subRule.getExpression(); - return expression instanceof BnfSequence ? ((BnfSequence)expression).getExpressionList() : Collections.singletonList(expression); - } - public static List collectMetaParameters(BnfRule rule, BnfExpression expression) { - if (!ParserGeneratorUtil.Rule.isMeta(rule) && !ParserGeneratorUtil.Rule.isExternal(rule)) { - return Collections.emptyList(); + public static boolean isOneTokenExpression(@Nullable BnfExpression e1) { + return e1 instanceof BnfLiteralExpression || e1 instanceof BnfReferenceOrToken; } - List result = new SmartList<>(); - for (BnfExternalExpression o : bnfTraverserNoAttrs(expression).filter(BnfExternalExpression.class)) { - if (o.getArguments().isEmpty()) { - String text = "<<" + o.getRefElement().getText() + ">>"; - if (!result.contains(text)) { - result.add(text); + + public static boolean isExternalReference(@Nullable PsiElement psiElement) { + PsiElement parent = psiElement == null ? null : psiElement.getParent(); + if (parent instanceof BnfExternalExpression bnfExternalExpression && bnfExternalExpression.getRefElement() == psiElement) { + return true; } - } - } - if (ParserGeneratorUtil.Rule.isMeta(rule)) { - String attr = getAttribute(null, rule, KnownAttribute.RECOVER_WHILE); - if (isDoubleAngles(attr) && !result.contains(attr)) { - result.add(attr); - } + if (parent instanceof BnfSequence && parent.getFirstChild() == psiElement) { + parent = parent.getParent(); + } + return parent instanceof BnfRule bnfRule && ParserGeneratorUtil.Rule.isExternal(bnfRule); } - return result; - } - @Nonnull - public static String unquote(@Nonnull String str) { - return StringUtil.unquoteString(str); - } - - public static boolean isDoubleAngles(@Nullable String str) { - return str != null && str.startsWith("<<") && str.endsWith(">>"); - } - - public static boolean processExpressionNames(BnfRule rule, - String funcName, - BnfExpression expression, - PairProcessor processor) { - if (isAtomicExpression(expression)) { - return true; + public static List getExternalRuleExpressions(@Nonnull BnfRule subRule) { + BnfExpression expression = subRule.getExpression(); + return expression instanceof BnfSequence bnfSequence ? bnfSequence.getExpressionList() : Collections.singletonList(expression); } - BnfExpression nonTrivialExpression = expression; - for (BnfExpression e = expression, n = getTrivialNodeChild(e); n != null; e = n, n = getTrivialNodeChild(e)) { - if (!processor.process(funcName, e)) { - return false; - } - nonTrivialExpression = n; + + public static List collectMetaParameters(BnfRule rule, BnfExpression expression) { + if (!ParserGeneratorUtil.Rule.isMeta(rule) && !ParserGeneratorUtil.Rule.isExternal(rule)) { + return Collections.emptyList(); + } + List result = new SmartList<>(); + for (BnfExternalExpression o : bnfTraverserNoAttrs(expression).filter(BnfExternalExpression.class)) { + if (o.getArguments().isEmpty()) { + String text = "<<" + o.getRefElement().getText() + ">>"; + if (!result.contains(text)) { + result.add(text); + } + } + } + if (ParserGeneratorUtil.Rule.isMeta(rule)) { + String attr = getAttribute(null, rule, KnownAttribute.RECOVER_WHILE); + if (isDoubleAngles(attr) && !result.contains(attr)) { + result.add(attr); + } + } + return result; } - boolean isMeta = nonTrivialExpression instanceof BnfExternalExpression; - List children = getChildExpressions(nonTrivialExpression); - for (int i = isMeta ? 1 : 0, size = children.size(); i < size; i++) { - BnfExpression child = children.get(i); - if (isAtomicExpression(child)) { - continue; - } - String nextName = ParserGeneratorUtil.isTokenSequence(null, rule, child) ? funcName : - getNextName(funcName, isMeta ? i - 1 : i); - if (!processExpressionNames(rule, nextName, child, processor)) { - return false; - } + + @Nonnull + public static String unquote(@Nonnull String str) { + return StringUtil.unquoteString(str); } - return processor.process(funcName, nonTrivialExpression); - } - public static boolean processPinnedExpressions(final BnfRule rule, final Processor processor) { - return processPinnedExpressions(rule, (bnfExpression, pinMatcher) -> processor.process(bnfExpression)); - } + public static boolean isDoubleAngles(@Nullable String str) { + return str != null && str.startsWith("<<") && str.endsWith(">>"); + } - public static boolean processPinnedExpressions(final BnfRule rule, final PairProcessor processor) { - return processExpressionNames(rule, getFuncName(rule), rule.getExpression(), (funcName, expression) -> { - if (!(expression instanceof BnfSequence)) { - return true; - } - List children = getChildExpressions(expression); - if (children.size() < 2) { - return true; - } - PinMatcher pinMatcher = new PinMatcher(null, rule, BNF_SEQUENCE, funcName); - boolean pinApplied = false; - for (int i = 0, childExpressionsSize = children.size(); i < childExpressionsSize; i++) { - BnfExpression child = children.get(i); - if (!pinApplied && pinMatcher.matches(i, child)) { - pinApplied = true; - if (!processor.process(child, pinMatcher)) { - return false; - } + public static boolean processExpressionNames( + BnfRule rule, + String funcName, + BnfExpression expression, + PairProcessor processor + ) { + if (isAtomicExpression(expression)) { + return true; } - } - return true; - }); - } - - public static boolean isAtomicExpression(BnfExpression tree) { - return tree instanceof BnfReferenceOrToken || - tree instanceof BnfLiteralExpression; - } + BnfExpression nonTrivialExpression = expression; + for (BnfExpression e = expression, n = getTrivialNodeChild(e); n != null; e = n, n = getTrivialNodeChild(e)) { + if (!processor.process(funcName, e)) { + return false; + } + nonTrivialExpression = n; + } + boolean isMeta = nonTrivialExpression instanceof BnfExternalExpression; + List children = getChildExpressions(nonTrivialExpression); + for (int i = isMeta ? 1 : 0, size = children.size(); i < size; i++) { + BnfExpression child = children.get(i); + if (isAtomicExpression(child)) { + continue; + } + String nextName = ParserGeneratorUtil.isTokenSequence(null, rule, child) + ? funcName + : getNextName(funcName, isMeta ? i - 1 : i); + if (!processExpressionNames(rule, nextName, child, processor)) { + return false; + } + } + return processor.process(funcName, nonTrivialExpression); + } - public static SyntaxTraverser bnfTraverser(PsiElement root) { - return psiTraverser().withRoot(root). - forceDisregardTypes(Conditions.equalTo(GeneratedParserUtilBase.DUMMY_BLOCK)). - filter(Conditions.instanceOf(BnfComposite.class)); - } + public static boolean processPinnedExpressions(final BnfRule rule, final Processor processor) { + return processPinnedExpressions(rule, (bnfExpression, pinMatcher) -> processor.process(bnfExpression)); + } - public static SyntaxTraverser bnfTraverserNoAttrs(PsiElement root) { - return bnfTraverser(root).forceIgnore(Conditions.instanceOf(BnfAttrs.class)); - } + public static boolean processPinnedExpressions(final BnfRule rule, final PairProcessor processor) { + return processExpressionNames(rule, getFuncName(rule), rule.getExpression(), (funcName, expression) -> { + if (!(expression instanceof BnfSequence)) { + return true; + } + List children = getChildExpressions(expression); + if (children.size() < 2) { + return true; + } + PinMatcher pinMatcher = new PinMatcher(null, rule, BNF_SEQUENCE, funcName); + boolean pinApplied = false; + for (int i = 0, childExpressionsSize = children.size(); i < childExpressionsSize; i++) { + BnfExpression child = children.get(i); + if (!pinApplied && pinMatcher.matches(i, child)) { + pinApplied = true; + if (!processor.process(child, pinMatcher)) { + return false; + } + } + } + return true; + }); + } - public static String getMethodName(BnfRule rule, PsiElement element) { - final BnfExpression target = PsiTreeUtil.getParentOfType(element, BnfExpression.class, false); - String funcName = getFuncName(rule); - if (target == null) { - return funcName; + public static boolean isAtomicExpression(BnfExpression tree) { + return tree instanceof BnfReferenceOrToken || + tree instanceof BnfLiteralExpression; } - final Ref ref = Ref.create(null); - processExpressionNames(rule, funcName, rule.getExpression(), (funcName1, expression) -> { - if (target == expression) { - ref.set(funcName1); - return false; - } - return true; - }); - return ref.get(); - } - @Nonnull - public static String getIdText(@Nullable PsiElement id) { - return id == null ? "" : stripQuotesAroundId(id.getText()); - } + public static SyntaxTraverser bnfTraverser(PsiElement root) { + return psiTraverser().withRoot(root) + .forceDisregardTypes(Conditions.equalTo(GeneratedParserUtilBase.DUMMY_BLOCK)) + .filter(Conditions.instanceOf(BnfComposite.class)); + } - @Contract("!null->!null") - public static String stripQuotesAroundId(String text) { - return isIdQuoted(text) ? text.substring(1, text.length() - 1) : text; - } + public static SyntaxTraverser bnfTraverserNoAttrs(PsiElement root) { + return bnfTraverser(root).forceIgnore(Conditions.instanceOf(BnfAttrs.class)); + } - public static boolean isIdQuoted(@Nullable String text) { - return text != null && text.startsWith("<") && text.endsWith(">"); - } + public static String getMethodName(BnfRule rule, PsiElement element) { + final BnfExpression target = PsiTreeUtil.getParentOfType(element, BnfExpression.class, false); + String funcName = getFuncName(rule); + if (target == null) { + return funcName; + } + final Ref ref = Ref.create(null); + processExpressionNames( + rule, + funcName, + rule.getExpression(), + (funcName1, expression) -> { + if (target == expression) { + ref.set(funcName1); + return false; + } + return true; + } + ); + return ref.get(); + } - public static class FakeElementType extends IElementType { - public FakeElementType(String debugName, Language language) { - super(debugName, language, false); + @Nonnull + public static String getIdText(@Nullable PsiElement id) { + return id == null ? "" : stripQuotesAroundId(id.getText()); } - } - public static class FakeBnfExpression extends LeafPsiElement implements BnfExpression { - public FakeBnfExpression(@Nonnull String text) { - this(BnfTypes.BNF_EXPRESSION, text); + @Contract("!null->!null") + public static String stripQuotesAroundId(String text) { + return isIdQuoted(text) ? text.substring(1, text.length() - 1) : text; } - public FakeBnfExpression(@Nonnull IElementType elementType, @Nonnull String text) { - super(elementType, text); + public static boolean isIdQuoted(@Nullable String text) { + return text != null && text.startsWith("<") && text.endsWith(">"); } - @Override - public R accept(@Nonnull BnfVisitor visitor) { - return visitor.visitExpression(this); + public static class FakeElementType extends IElementType { + public FakeElementType(String debugName, Language language) { + super(debugName, language, false); + } } - @Override - public String toString() { - return getText(); + public static class FakeBnfExpression extends LeafPsiElement implements BnfExpression { + public FakeBnfExpression(@Nonnull String text) { + this(BnfTypes.BNF_EXPRESSION, text); + } + + public FakeBnfExpression(@Nonnull IElementType elementType, @Nonnull String text) { + super(elementType, text); + } + + @Override + public R accept(@Nonnull BnfVisitor visitor) { + return visitor.visitExpression(this); + } + + @Override + public String toString() { + return getText(); + } } - } } diff --git a/plugin/src/main/java/consulo/devkit/action/ConvertResourceBundleToYamlAction.java b/plugin/src/main/java/consulo/devkit/action/ConvertResourceBundleToYamlAction.java index b6dc70f7..eb746cc9 100644 --- a/plugin/src/main/java/consulo/devkit/action/ConvertResourceBundleToYamlAction.java +++ b/plugin/src/main/java/consulo/devkit/action/ConvertResourceBundleToYamlAction.java @@ -25,63 +25,63 @@ * @since 2019-04-11 */ public class ConvertResourceBundleToYamlAction extends InternalAction { - public ConvertResourceBundleToYamlAction() { - super("Convert resource bundle to YAML"); - } + public ConvertResourceBundleToYamlAction() { + super("Convert resource bundle to YAML"); + } - @RequiredUIAccess - @Override - public void actionPerformed(@Nonnull AnActionEvent event) { - PsiFile file = event.getData(CommonDataKeys.PSI_FILE); + @RequiredUIAccess + @Override + public void actionPerformed(@Nonnull AnActionEvent event) { + PsiFile file = event.getData(CommonDataKeys.PSI_FILE); - PropertiesFile propertiesFile = (PropertiesFile)file; + PropertiesFile propertiesFile = (PropertiesFile)file; - assert propertiesFile != null; + assert propertiesFile != null; - HashMap> messages = new LinkedHashMap<>(); + HashMap> messages = new LinkedHashMap<>(); - for (IProperty property : propertiesFile.getProperties()) { - Map messageInfo = new LinkedHashMap<>(); - messageInfo.put("text", property.getUnescapedValue()); + for (IProperty property : propertiesFile.getProperties()) { + Map messageInfo = new LinkedHashMap<>(); + messageInfo.put("text", property.getUnescapedValue()); - messages.put(property.getUnescapedKey(), messageInfo); - } + messages.put(property.getUnescapedKey(), messageInfo); + } - DumperOptions dumperOptions = new DumperOptions(); - dumperOptions.setPrettyFlow(true); - dumperOptions.setAllowUnicode(true); - dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); - dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - dumperOptions.setWidth(1024); - dumperOptions.setMaxSimpleKeyLength(1024); - Yaml yaml = new Yaml(dumperOptions); + DumperOptions dumperOptions = new DumperOptions(); + dumperOptions.setPrettyFlow(true); + dumperOptions.setAllowUnicode(true); + dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); + dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + dumperOptions.setWidth(1024); + dumperOptions.setMaxSimpleKeyLength(1024); + Yaml yaml = new Yaml(dumperOptions); - File ioFile = VirtualFileUtil.virtualToIoFile(file.getVirtualFile()); + File ioFile = VirtualFileUtil.virtualToIoFile(file.getVirtualFile()); - File parentFile = ioFile.getParentFile(); + File parentFile = ioFile.getParentFile(); - File result = new File(parentFile, file.getVirtualFile().getNameWithoutExtension() + ".yaml"); - try (Writer fileWriter = new OutputStreamWriter(new FileOutputStream(result), StandardCharsets.UTF_8)) { - yaml.dump(messages, fileWriter); - } - catch (IOException ignored) { - } + File result = new File(parentFile, file.getVirtualFile().getNameWithoutExtension() + ".yaml"); + try (Writer fileWriter = new OutputStreamWriter(new FileOutputStream(result), StandardCharsets.UTF_8)) { + yaml.dump(messages, fileWriter); + } + catch (IOException ignored) { + } - LocalFileSystem.getInstance().refreshIoFiles(Arrays.asList(result)); - } + LocalFileSystem.getInstance().refreshIoFiles(Arrays.asList(result)); + } - @RequiredUIAccess - @Override - public void update(@Nonnull AnActionEvent event) { - super.update(event); + @RequiredUIAccess + @Override + public void update(@Nonnull AnActionEvent event) { + super.update(event); - if (!event.getPresentation().isEnabled()) { - return; - } + if (!event.getPresentation().isEnabled()) { + return; + } - PsiFile file = event.getData(CommonDataKeys.PSI_FILE); - if (file == null || file.getFileType() != PropertiesFileType.INSTANCE) { - event.getPresentation().setEnabledAndVisible(false); + PsiFile file = event.getData(CommonDataKeys.PSI_FILE); + if (file == null || file.getFileType() != PropertiesFileType.INSTANCE) { + event.getPresentation().setEnabledAndVisible(false); + } } - } } diff --git a/plugin/src/main/java/consulo/devkit/action/InternalAction.java b/plugin/src/main/java/consulo/devkit/action/InternalAction.java index c9cf9e89..141b9c63 100644 --- a/plugin/src/main/java/consulo/devkit/action/InternalAction.java +++ b/plugin/src/main/java/consulo/devkit/action/InternalAction.java @@ -33,26 +33,26 @@ * @since 28-Jan-17 */ public abstract class InternalAction extends AnAction { - protected InternalAction() { - } - - protected InternalAction(Image icon) { - super(icon); - } - - protected InternalAction(@Nullable String text) { - super(text); - } - - protected InternalAction(@Nullable String text, @Nullable String description, @Nullable Image icon) { - super(text, description, icon); - } - - @RequiredUIAccess - @Override - public void update(@Nonnull AnActionEvent e) { - Project project = e.getData(Project.KEY); - Module module = e.getData(CommonDataKeys.MODULE); - e.getPresentation().setEnabledAndVisible(project != null && PluginModuleUtil.isConsuloOrPluginProject(project, module)); - } + protected InternalAction() { + } + + protected InternalAction(Image icon) { + super(icon); + } + + protected InternalAction(@Nullable String text) { + super(text); + } + + protected InternalAction(@Nullable String text, @Nullable String description, @Nullable Image icon) { + super(text, description, icon); + } + + @RequiredUIAccess + @Override + public void update(@Nonnull AnActionEvent e) { + Project project = e.getData(Project.KEY); + Module module = e.getData(CommonDataKeys.MODULE); + e.getPresentation().setEnabledAndVisible(project != null && PluginModuleUtil.isConsuloOrPluginProject(project, module)); + } } diff --git a/plugin/src/main/java/consulo/devkit/action/InternalGroup.java b/plugin/src/main/java/consulo/devkit/action/InternalGroup.java index ee787884..74f13c0a 100644 --- a/plugin/src/main/java/consulo/devkit/action/InternalGroup.java +++ b/plugin/src/main/java/consulo/devkit/action/InternalGroup.java @@ -29,10 +29,10 @@ * @since 28-Jan-17 */ public class InternalGroup extends DefaultActionGroup { - @RequiredUIAccess - @Override - public void update(@Nonnull AnActionEvent e) { - Project project = e.getData(Project.KEY); - e.getPresentation().setVisible(project != null && PluginModuleUtil.isConsuloOrPluginProject(project, null)); - } + @RequiredUIAccess + @Override + public void update(@Nonnull AnActionEvent e) { + Project project = e.getData(Project.KEY); + e.getPresentation().setVisible(project != null && PluginModuleUtil.isConsuloOrPluginProject(project, null)); + } } diff --git a/plugin/src/main/java/consulo/devkit/codeInsight/daemon/ImplToAPILineMarkerProvider.java b/plugin/src/main/java/consulo/devkit/codeInsight/daemon/ImplToAPILineMarkerProvider.java index 0c000641..b527a270 100644 --- a/plugin/src/main/java/consulo/devkit/codeInsight/daemon/ImplToAPILineMarkerProvider.java +++ b/plugin/src/main/java/consulo/devkit/codeInsight/daemon/ImplToAPILineMarkerProvider.java @@ -32,62 +32,61 @@ */ @ExtensionImpl public class ImplToAPILineMarkerProvider implements LineMarkerProvider { - @Override - @RequiredReadAction - public boolean isAvailable(@Nonnull PsiFile file) { - return PluginModuleUtil.isConsuloOrPluginProject(file); - } - - @RequiredReadAction - @Nullable - @Override - public LineMarkerInfo getLineMarkerInfo(@Nonnull PsiElement psiElement) { - if (PsiUtilCore.getElementType(psiElement) == JavaTokenType.IDENTIFIER && psiElement.getParent() instanceof PsiClass) { - PsiClass psiClass = (PsiClass)psiElement.getParent(); - - Pair apiInfo = findAPIElement(psiClass); - if (apiInfo != null) { - String navigationText = "Navigate to @" + StringUtil.getShortName(apiInfo.getSecond()); - return new LineMarkerInfo<>(psiElement, - psiElement.getTextRange(), - AllIcons.Nodes.Plugin, - Pass.LINE_MARKERS, - element -> navigationText, - (mouseEvent, element) -> - { - Pair target = findAPIElement(psiClass); - if (target != null) { - PsiNavigateUtil.navigate(target.getFirst()); - } - }, - GutterIconRenderer.Alignment.RIGHT); - } + @Override + @RequiredReadAction + public boolean isAvailable(@Nonnull PsiFile file) { + return PluginModuleUtil.isConsuloOrPluginProject(file); } - return null; - } - @Nullable - private Pair findAPIElement(PsiClass psiClass) { - for (Pair apiPair : ValhallaClasses.ApiToImpl) { - if (AnnotationUtil.isAnnotated(psiClass, apiPair.getSecond(), 0)) { - PsiAnnotation annotationInHierarchy = AnnotationUtil.findAnnotationInHierarchy(psiClass, Set.of(apiPair.getFirst())); - if (annotationInHierarchy != null) { - // PsiClass apiType = PsiTreeUtil.getParentOfType(annotationInHierarchy, PsiClass.class); - // if(apiType != null) - // { - // return apiType; - // } - return Pair.create(annotationInHierarchy, apiPair.getFirst()); + @RequiredReadAction + @Nullable + @Override + public LineMarkerInfo getLineMarkerInfo(@Nonnull PsiElement psiElement) { + if (PsiUtilCore.getElementType(psiElement) == JavaTokenType.IDENTIFIER && psiElement.getParent() instanceof PsiClass psiClass) { + Pair apiInfo = findAPIElement(psiClass); + if (apiInfo != null) { + String navigationText = "Navigate to @" + StringUtil.getShortName(apiInfo.getSecond()); + return new LineMarkerInfo<>( + psiElement, + psiElement.getTextRange(), + AllIcons.Nodes.Plugin, + Pass.LINE_MARKERS, + element -> navigationText, + (mouseEvent, element) -> { + Pair target = findAPIElement(psiClass); + if (target != null) { + PsiNavigateUtil.navigate(target.getFirst()); + } + }, + GutterIconRenderer.Alignment.RIGHT + ); + } } - } + return null; } - return null; - } + @Nullable + private Pair findAPIElement(PsiClass psiClass) { + for (Pair apiPair : ValhallaClasses.ApiToImpl) { + if (AnnotationUtil.isAnnotated(psiClass, apiPair.getSecond(), 0)) { + PsiAnnotation annotationInHierarchy = AnnotationUtil.findAnnotationInHierarchy(psiClass, Set.of(apiPair.getFirst())); + if (annotationInHierarchy != null) { + // PsiClass apiType = PsiTreeUtil.getParentOfType(annotationInHierarchy, PsiClass.class); + // if(apiType != null) + // { + // return apiType; + // } + return Pair.create(annotationInHierarchy, apiPair.getFirst()); + } + } + } + + return null; + } - @Nonnull - @Override - public Language getLanguage() { - return JavaLanguage.INSTANCE; - } + @Nonnull + @Override + public Language getLanguage() { + return JavaLanguage.INSTANCE; + } } diff --git a/plugin/src/main/java/consulo/devkit/codeInsight/daemon/ServiceLineMarkerProvider.java b/plugin/src/main/java/consulo/devkit/codeInsight/daemon/ServiceLineMarkerProvider.java index 74007394..4a7c5941 100644 --- a/plugin/src/main/java/consulo/devkit/codeInsight/daemon/ServiceLineMarkerProvider.java +++ b/plugin/src/main/java/consulo/devkit/codeInsight/daemon/ServiceLineMarkerProvider.java @@ -28,42 +28,41 @@ */ @ExtensionImpl public class ServiceLineMarkerProvider implements LineMarkerProvider { - @Override - @RequiredReadAction - public boolean isAvailable(@Nonnull PsiFile file) { - return PluginModuleUtil.isConsuloOrPluginProject(file); - } - - @RequiredReadAction - @Nullable - @Override - public LineMarkerInfo getLineMarkerInfo(@Nonnull PsiElement psiElement) { - if (PsiUtilCore.getElementType(psiElement) == JavaTokenType.IDENTIFIER && psiElement.getParent() instanceof PsiClass) { - PsiClass psiClass = (PsiClass)psiElement.getParent(); + @Override + @RequiredReadAction + public boolean isAvailable(@Nonnull PsiFile file) { + return PluginModuleUtil.isConsuloOrPluginProject(file); + } - ServiceInfo info = ServiceLocator.findAnyService(psiClass); - if (info != null) { - return new LineMarkerInfo<>(psiElement, - psiElement.getTextRange(), - AllIcons.Nodes.Plugin, - Pass.LINE_MARKERS, - element -> "Service", - (mouseEvent, element) -> - { - ServiceInfo info2 = ServiceLocator.findAnyService((PsiClass)element.getParent()); - if (info2 != null) { - PsiNavigateUtil.navigate(info2.getNavigatableElement()); - } - }, - GutterIconRenderer.Alignment.RIGHT); - } + @RequiredReadAction + @Nullable + @Override + public LineMarkerInfo getLineMarkerInfo(@Nonnull PsiElement psiElement) { + if (PsiUtilCore.getElementType(psiElement) == JavaTokenType.IDENTIFIER && psiElement.getParent() instanceof PsiClass psiClass) { + ServiceInfo info = ServiceLocator.findAnyService(psiClass); + if (info != null) { + return new LineMarkerInfo<>( + psiElement, + psiElement.getTextRange(), + AllIcons.Nodes.Plugin, + Pass.LINE_MARKERS, + element -> "Service", + (mouseEvent, element) -> { + ServiceInfo info2 = ServiceLocator.findAnyService((PsiClass)element.getParent()); + if (info2 != null) { + PsiNavigateUtil.navigate(info2.getNavigatableElement()); + } + }, + GutterIconRenderer.Alignment.RIGHT + ); + } + } + return null; } - return null; - } - @Nonnull - @Override - public Language getLanguage() { - return JavaLanguage.INSTANCE; - } + @Nonnull + @Override + public Language getLanguage() { + return JavaLanguage.INSTANCE; + } } diff --git a/plugin/src/main/java/consulo/devkit/codeInsight/daemon/UIColorLineMarkerProvider.java b/plugin/src/main/java/consulo/devkit/codeInsight/daemon/UIColorLineMarkerProvider.java index 13add927..27c52e4c 100644 --- a/plugin/src/main/java/consulo/devkit/codeInsight/daemon/UIColorLineMarkerProvider.java +++ b/plugin/src/main/java/consulo/devkit/codeInsight/daemon/UIColorLineMarkerProvider.java @@ -38,128 +38,128 @@ */ @ExtensionImpl public class UIColorLineMarkerProvider implements ElementColorProvider { - @RequiredReadAction - @Override - public ColorValue getColorFrom(@Nonnull PsiElement element) { - return getColorFromExpression(element); - } - - public static boolean isColorType(@Nullable PsiType type) { - if (type != null) { - final PsiClass aClass = PsiTypesUtil.getPsiClass(type); - if (aClass != null) { - final String fqn = aClass.getQualifiedName(); - if (RGBColor.class.getName().equals(fqn)) { - return true; - } - } - } - return false; - } - - @Nullable - public static ColorValue getColorFromExpression(@Nullable PsiElement element) { - if (element instanceof PsiNewExpression) { - final PsiNewExpression expr = (PsiNewExpression)element; - if (isColorType(expr.getType())) { - return getColor(expr.getArgumentList()); - } + @RequiredReadAction + @Override + public ColorValue getColorFrom(@Nonnull PsiElement element) { + return getColorFromExpression(element); } - return null; - } - - @Nullable - private static ColorValue getColor(PsiExpressionList list) { - try { - final PsiExpression[] args = list.getExpressions(); - final PsiType[] types = list.getExpressionTypes(); - ColorConstructors type = getConstructorType(types); - if (type != null) { - switch (type) { - case INTx3: - return new RGBColor(getInt(args[0]), getInt(args[1]), getInt(args[2])); - case INTx3_FLOAT: - float alpha = getFloat(args[3]); - return new RGBColor(getInt(args[0]), getInt(args[1]), getInt(args[2]), alpha); + + public static boolean isColorType(@Nullable PsiType type) { + if (type != null) { + final PsiClass aClass = PsiTypesUtil.getPsiClass(type); + if (aClass != null) { + final String fqn = aClass.getQualifiedName(); + if (RGBColor.class.getName().equals(fqn)) { + return true; + } + } } - } - } - catch (Exception ignore) { + return false; } - return null; - } - - @Nullable - private static ColorConstructors getConstructorType(PsiType[] types) { - int len = types.length; - if (len == 0) { - return null; + + @Nullable + public static ColorValue getColorFromExpression(@Nullable PsiElement element) { + if (element instanceof PsiNewExpression) { + final PsiNewExpression expr = (PsiNewExpression)element; + if (isColorType(expr.getType())) { + return getColor(expr.getArgumentList()); + } + } + return null; } - switch (len) { - case 3: - return ColorConstructors.INTx3; - case 4: - return ColorConstructors.INTx3_FLOAT; + @Nullable + private static ColorValue getColor(PsiExpressionList list) { + try { + final PsiExpression[] args = list.getExpressions(); + final PsiType[] types = list.getExpressionTypes(); + ColorConstructors type = getConstructorType(types); + if (type != null) { + switch (type) { + case INTx3: + return new RGBColor(getInt(args[0]), getInt(args[1]), getInt(args[2])); + case INTx3_FLOAT: + float alpha = getFloat(args[3]); + return new RGBColor(getInt(args[0]), getInt(args[1]), getInt(args[2]), alpha); + } + } + } + catch (Exception ignore) { + } + return null; } - return null; - } + @Nullable + private static ColorConstructors getConstructorType(PsiType[] types) { + int len = types.length; + if (len == 0) { + return null; + } + + switch (len) { + case 3: + return ColorConstructors.INTx3; + case 4: + return ColorConstructors.INTx3_FLOAT; + } - public static int getInt(PsiExpression expr) { - return (Integer)getObject(expr); - } + return null; + } - public static float getFloat(PsiExpression expr) { - return (Float)getObject(expr); - } + public static int getInt(PsiExpression expr) { + return (Integer)getObject(expr); + } - private static Object getObject(PsiExpression expr) { - return JavaConstantExpressionEvaluator.computeConstantExpression(expr, true); - } + public static float getFloat(PsiExpression expr) { + return (Float)getObject(expr); + } - @RequiredWriteAction - @Override - public void setColorTo(@Nonnull PsiElement element, @Nonnull ColorValue color) { - PsiExpressionList argumentList = ((PsiNewExpression)element).getArgumentList(); - assert argumentList != null; + private static Object getObject(PsiExpression expr) { + return JavaConstantExpressionEvaluator.computeConstantExpression(expr, true); + } - PsiExpression[] expr = argumentList.getExpressions(); - ColorConstructors type = getConstructorType(argumentList.getExpressionTypes()); + @RequiredWriteAction + @Override + public void setColorTo(@Nonnull PsiElement element, @Nonnull ColorValue color) { + PsiExpressionList argumentList = ((PsiNewExpression)element).getArgumentList(); + assert argumentList != null; - assert type != null; + PsiExpression[] expr = argumentList.getExpressions(); + ColorConstructors type = getConstructorType(argumentList.getExpressionTypes()); - switch (type) { - case INTx3: - case INTx3_FLOAT: - RGBColor rgb = color.toRGB(); - replaceInt(expr[0], rgb.getRed()); - replaceInt(expr[1], rgb.getGreen()); - replaceInt(expr[2], rgb.getBlue()); + assert type != null; - if (type == ColorConstructors.INTx3_FLOAT) { - replaceFloat(expr[3], rgb.getAlpha()); + switch (type) { + case INTx3: + case INTx3_FLOAT: + RGBColor rgb = color.toRGB(); + replaceInt(expr[0], rgb.getRed()); + replaceInt(expr[1], rgb.getGreen()); + replaceInt(expr[2], rgb.getBlue()); + + if (type == ColorConstructors.INTx3_FLOAT) { + replaceFloat(expr[3], rgb.getAlpha()); + } } } - } - private static void replaceInt(PsiExpression expr, int newValue) { - PsiElementFactory factory = JavaPsiFacade.getElementFactory(expr.getProject()); - if (getInt(expr) != newValue) { - String text = Integer.toString(newValue); - expr.replace(factory.createExpressionFromText(text, null)); + private static void replaceInt(PsiExpression expr, int newValue) { + PsiElementFactory factory = JavaPsiFacade.getElementFactory(expr.getProject()); + if (getInt(expr) != newValue) { + String text = Integer.toString(newValue); + expr.replace(factory.createExpressionFromText(text, null)); + } } - } - private static void replaceFloat(PsiExpression expr, float newValue) { - PsiElementFactory factory = JavaPsiFacade.getElementFactory(expr.getProject()); - if (getFloat(expr) != newValue) { - expr.replace(factory.createExpressionFromText(String.valueOf(newValue) + "f", null)); + private static void replaceFloat(PsiExpression expr, float newValue) { + PsiElementFactory factory = JavaPsiFacade.getElementFactory(expr.getProject()); + if (getFloat(expr) != newValue) { + expr.replace(factory.createExpressionFromText(String.valueOf(newValue) + "f", null)); + } } - } - private enum ColorConstructors { - INTx3, - INTx3_FLOAT - } + private enum ColorConstructors { + INTx3, + INTx3_FLOAT + } } diff --git a/plugin/src/main/java/consulo/devkit/codeInsight/generation/XActionOverrideImplementsAnnotationsHandler.java b/plugin/src/main/java/consulo/devkit/codeInsight/generation/XActionOverrideImplementsAnnotationsHandler.java index 36294053..f57fa4f6 100644 --- a/plugin/src/main/java/consulo/devkit/codeInsight/generation/XActionOverrideImplementsAnnotationsHandler.java +++ b/plugin/src/main/java/consulo/devkit/codeInsight/generation/XActionOverrideImplementsAnnotationsHandler.java @@ -32,24 +32,24 @@ */ @ExtensionImpl public class XActionOverrideImplementsAnnotationsHandler implements OverrideImplementsAnnotationsHandler { - private static final String[] ourAnnotations = new String[]{ - RequiredReadAction.class.getName(), - RequiredWriteAction.class.getName(), - RequiredUIAccess.class.getName(), - RequiredUIAccess.class.getName() - }; + private static final String[] ourAnnotations = new String[]{ + RequiredReadAction.class.getName(), + RequiredWriteAction.class.getName(), + RequiredUIAccess.class.getName(), + RequiredUIAccess.class.getName() + }; - @Override - public String[] getAnnotations(Project project) { - return ourAnnotations; - } + @Override + public String[] getAnnotations(Project project) { + return ourAnnotations; + } - @Nonnull - @Override - public String[] annotationsToRemove(Project project, @Nonnull String s) { - if (ArrayUtil.contains(s, ourAnnotations)) { - return ourAnnotations; + @Nonnull + @Override + public String[] annotationsToRemove(Project project, @Nonnull String s) { + if (ArrayUtil.contains(s, ourAnnotations)) { + return ourAnnotations; + } + return ArrayUtil.EMPTY_STRING_ARRAY; } - return ArrayUtil.EMPTY_STRING_ARRAY; - } } diff --git a/plugin/src/main/java/consulo/devkit/dom/Permission.java b/plugin/src/main/java/consulo/devkit/dom/Permission.java index a57c27d4..3fe46c0f 100644 --- a/plugin/src/main/java/consulo/devkit/dom/Permission.java +++ b/plugin/src/main/java/consulo/devkit/dom/Permission.java @@ -10,10 +10,10 @@ * @since 01/11/2021 */ public interface Permission extends DomElement { - @Required - @Attribute("type") - GenericAttributeValue getType(); + @Required + @Attribute("type") + GenericAttributeValue getType(); - @SubTagList("permission-option") - List getPermissionOptions(); + @SubTagList("permission-option") + List getPermissionOptions(); } diff --git a/plugin/src/main/java/consulo/devkit/dom/PermissionOption.java b/plugin/src/main/java/consulo/devkit/dom/PermissionOption.java index 8e07a6d1..5338dcda 100644 --- a/plugin/src/main/java/consulo/devkit/dom/PermissionOption.java +++ b/plugin/src/main/java/consulo/devkit/dom/PermissionOption.java @@ -7,7 +7,7 @@ * @since 01/11/2021 */ public interface PermissionOption extends DomElement { - String getValue(); + String getValue(); - void setValue(String value); + void setValue(String value); }