From 52f9e5e48db7e3a377815bd7db5ba7db4dfaec6d Mon Sep 17 00:00:00 2001 From: Ievgen Degtiarenko Date: Mon, 28 Sep 2015 21:20:52 -0400 Subject: [PATCH] Complete with special values for matchStrategy and matchingRole --- resources/META-INF/plugin.xml | 1 + .../ConcordionPatternConditions.java | 48 ++++++++++++++++++ ...ncordionCommandsCompletionContributor.java | 27 +++++----- .../autocomplete/ConcordionHtmlSpec.java | 22 --------- ...ionNonExpressionCompletionContributor.java | 49 +++++++++++++++++++ testData/completion/MatchStrategy.html | 15 ++++++ testData/completion/MatchStrategy.java | 15 ++++++ testData/completion/MatchingRole.html | 15 ++++++ testData/completion/MatchingRole.java | 12 +++++ .../CompleteWithConcordionCommandsTest.java | 1 - .../CompleteWithSpecialValuesTest.java | 38 ++++++++++++++ 11 files changed, 208 insertions(+), 35 deletions(-) create mode 100644 src/com/gman/idea/plugin/concordion/ConcordionPatternConditions.java delete mode 100644 src/com/gman/idea/plugin/concordion/autocomplete/ConcordionHtmlSpec.java create mode 100644 src/com/gman/idea/plugin/concordion/autocomplete/ConcordionNonExpressionCompletionContributor.java create mode 100644 testData/completion/MatchStrategy.html create mode 100644 testData/completion/MatchStrategy.java create mode 100644 testData/completion/MatchingRole.html create mode 100644 testData/completion/MatchingRole.java create mode 100644 tests/com/gman/idea/plugin/concordion/completion/CompleteWithSpecialValuesTest.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index e3923c9..04752e6 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -43,6 +43,7 @@ + { + + public ConcordionHtmlSpec() { + super(ConcordionHtmlSpec.class.getName()); + } + + @Override + public boolean accepts(@NotNull PsiElement element, ProcessingContext context) { + return isConcordionHtmlSpec(element.getContainingFile()); + } + }; + + private static final class SpecificConcordionTag extends PatternCondition { + + private final Set expectedTagNames = new HashSet<>(); + + public SpecificConcordionTag(String... expectedTagNames) { + super(SpecificConcordionTag.class.getName()); + this.expectedTagNames.addAll(asList(expectedTagNames)); + } + + @Override + public boolean accepts(@NotNull PsiElement element, ProcessingContext context) { + String localName = ((XmlAttribute) element.getParent().getParent()).getLocalName(); + return expectedTagNames.contains(localName); + } + } +} diff --git a/src/com/gman/idea/plugin/concordion/autocomplete/ConcordionCommandsCompletionContributor.java b/src/com/gman/idea/plugin/concordion/autocomplete/ConcordionCommandsCompletionContributor.java index 8540a07..312fab1 100644 --- a/src/com/gman/idea/plugin/concordion/autocomplete/ConcordionCommandsCompletionContributor.java +++ b/src/com/gman/idea/plugin/concordion/autocomplete/ConcordionCommandsCompletionContributor.java @@ -4,22 +4,23 @@ import com.intellij.codeInsight.completion.*; import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.LookupElementBuilder; -import com.intellij.patterns.PlatformPatterns; -import com.intellij.psi.impl.source.html.HtmlFileImpl; import com.intellij.psi.xml.XmlAttribute; -import com.intellij.psi.xml.XmlToken; import com.intellij.util.ProcessingContext; import org.jetbrains.annotations.NotNull; +import java.util.Collection; + import static com.gman.idea.plugin.concordion.Concordion.*; +import static com.gman.idea.plugin.concordion.ConcordionPatternConditions.CONCORDION_HTML_SPEC; import static com.intellij.patterns.PlatformPatterns.psiElement; +import static java.util.stream.Collectors.toList; public class ConcordionCommandsCompletionContributor extends CompletionContributor { public ConcordionCommandsCompletionContributor() { extend( CompletionType.BASIC, - psiElement().withParent(XmlAttribute.class).with(ConcordionHtmlSpec.INSTANCE), + psiElement().withParent(XmlAttribute.class).with(CONCORDION_HTML_SPEC), new ConcordionCommandCompletionProvider() ); } @@ -28,15 +29,17 @@ private static final class ConcordionCommandCompletionProvider extends Completio @Override protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) { - String schemaPrefix = concordionSchemaPrefixOf(parameters.getOriginalFile()); - - for (String command : Concordion.COMMANDS) { - result.addElement(createLookupElement(schemaPrefix, command)); - } + result.addAllElements(forCommands( + concordionSchemaPrefixOf(parameters.getOriginalFile()), + Concordion.COMMANDS + )); } + } - private LookupElement createLookupElement(String schemaPrefix, String command) { - return LookupElementBuilder.create(schemaPrefix + ':' + command).withInsertHandler(XmlAttributeInsertHandler.INSTANCE); - } + private static Iterable forCommands(String prefix, Collection commands) { + return commands.stream() + .map(c -> prefix + ':' + c) + .map(c -> LookupElementBuilder.create(c).withInsertHandler(XmlAttributeInsertHandler.INSTANCE)) + .collect(toList()); } } diff --git a/src/com/gman/idea/plugin/concordion/autocomplete/ConcordionHtmlSpec.java b/src/com/gman/idea/plugin/concordion/autocomplete/ConcordionHtmlSpec.java deleted file mode 100644 index 95109d2..0000000 --- a/src/com/gman/idea/plugin/concordion/autocomplete/ConcordionHtmlSpec.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.gman.idea.plugin.concordion.autocomplete; - -import com.intellij.patterns.PatternCondition; -import com.intellij.psi.PsiElement; -import com.intellij.util.ProcessingContext; -import org.jetbrains.annotations.NotNull; - -import static com.gman.idea.plugin.concordion.Concordion.isConcordionHtmlSpec; - -public class ConcordionHtmlSpec extends PatternCondition { - - public static final ConcordionHtmlSpec INSTANCE = new ConcordionHtmlSpec(); - - public ConcordionHtmlSpec() { - super(ConcordionHtmlSpec.class.getSimpleName()); - } - - @Override - public boolean accepts(@NotNull PsiElement element, ProcessingContext context) { - return isConcordionHtmlSpec(element.getContainingFile()); - } -} diff --git a/src/com/gman/idea/plugin/concordion/autocomplete/ConcordionNonExpressionCompletionContributor.java b/src/com/gman/idea/plugin/concordion/autocomplete/ConcordionNonExpressionCompletionContributor.java new file mode 100644 index 0000000..8cb954e --- /dev/null +++ b/src/com/gman/idea/plugin/concordion/autocomplete/ConcordionNonExpressionCompletionContributor.java @@ -0,0 +1,49 @@ +package com.gman.idea.plugin.concordion.autocomplete; + +import com.intellij.codeInsight.completion.*; +import com.intellij.codeInsight.lookup.LookupElement; +import com.intellij.codeInsight.lookup.LookupElementBuilder; +import com.intellij.psi.xml.XmlAttributeValue; +import com.intellij.util.ProcessingContext; +import org.jetbrains.annotations.NotNull; + +import static com.gman.idea.plugin.concordion.ConcordionPatternConditions.MATCHING_ROLE_ATTRIBUTE; +import static com.gman.idea.plugin.concordion.ConcordionPatternConditions.MATCH_STRATEGY_ATTRIBUTE; +import static com.intellij.patterns.PlatformPatterns.psiElement; +import static java.util.Arrays.stream; +import static java.util.stream.Collectors.toList; + +public class ConcordionNonExpressionCompletionContributor extends CompletionContributor { + + public ConcordionNonExpressionCompletionContributor() { + extend( + CompletionType.BASIC, + psiElement().withParent(XmlAttributeValue.class).with(MATCH_STRATEGY_ATTRIBUTE), + new ConcordionMatchStrategyProvider() + ); + extend( + CompletionType.BASIC, + psiElement().withParent(XmlAttributeValue.class).with(MATCHING_ROLE_ATTRIBUTE), + new ConcordionMatchingRoleProvider() + ); + } + + private static final class ConcordionMatchStrategyProvider extends CompletionProvider { + @Override + protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) { + result.addAllElements(fromStrings("Default", "BestMatch", "KeyMatch")); + //TODO contribute by classes extending RowsMatchStrategy + } + } + + private static final class ConcordionMatchingRoleProvider extends CompletionProvider { + @Override + protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) { + result.addAllElements(fromStrings("key")); + } + } + + private static Iterable fromStrings(String... strings) { + return stream(strings).map(LookupElementBuilder::create).collect(toList()); + } +} diff --git a/testData/completion/MatchStrategy.html b/testData/completion/MatchStrategy.html new file mode 100644 index 0000000..8dbeb1d --- /dev/null +++ b/testData/completion/MatchStrategy.html @@ -0,0 +1,15 @@ + + + Test spec + + + + + + + + +
KeyValue
+ + + \ No newline at end of file diff --git a/testData/completion/MatchStrategy.java b/testData/completion/MatchStrategy.java new file mode 100644 index 0000000..1747b41 --- /dev/null +++ b/testData/completion/MatchStrategy.java @@ -0,0 +1,15 @@ +package com.test; + +import org.concordion.integration.junit4.ConcordionRunner; +import org.junit.runner.RunWith; + +import java.lang.Object; +import java.util.Collections; + +@RunWith(ConcordionRunner.class) +public class MatchStrategy { + + public List values() { + return Collections.emptyList(); + } +} diff --git a/testData/completion/MatchingRole.html b/testData/completion/MatchingRole.html new file mode 100644 index 0000000..6c1ad2e --- /dev/null +++ b/testData/completion/MatchingRole.html @@ -0,0 +1,15 @@ + + + Test spec + + + + + + + + +
KeyValue
+ + + \ No newline at end of file diff --git a/testData/completion/MatchingRole.java b/testData/completion/MatchingRole.java new file mode 100644 index 0000000..8193ea9 --- /dev/null +++ b/testData/completion/MatchingRole.java @@ -0,0 +1,12 @@ +package com.test; + +import org.concordion.integration.junit4.ConcordionRunner; +import org.junit.runner.RunWith; + +@RunWith(ConcordionRunner.class) +public class MatchingRole { + + public List values() { + return Collections.emptyList(); + } +} diff --git a/tests/com/gman/idea/plugin/concordion/completion/CompleteWithConcordionCommandsTest.java b/tests/com/gman/idea/plugin/concordion/completion/CompleteWithConcordionCommandsTest.java index 6defc47..2595854 100644 --- a/tests/com/gman/idea/plugin/concordion/completion/CompleteWithConcordionCommandsTest.java +++ b/tests/com/gman/idea/plugin/concordion/completion/CompleteWithConcordionCommandsTest.java @@ -5,7 +5,6 @@ import com.intellij.codeInsight.completion.CompletionType; import com.intellij.openapi.vfs.VirtualFile; -import java.util.Arrays; import java.util.List; import static java.util.stream.Collectors.toList; diff --git a/tests/com/gman/idea/plugin/concordion/completion/CompleteWithSpecialValuesTest.java b/tests/com/gman/idea/plugin/concordion/completion/CompleteWithSpecialValuesTest.java new file mode 100644 index 0000000..b5d955e --- /dev/null +++ b/tests/com/gman/idea/plugin/concordion/completion/CompleteWithSpecialValuesTest.java @@ -0,0 +1,38 @@ +package com.gman.idea.plugin.concordion.completion; + +import com.gman.idea.plugin.concordion.ConcordionCodeInsightFixtureTestCase; +import com.intellij.codeInsight.completion.CompletionType; +import com.intellij.openapi.vfs.VirtualFile; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; + +public class CompleteWithSpecialValuesTest extends ConcordionCodeInsightFixtureTestCase { + + @Override + protected String getTestDataPath() { + return "testData/completion"; + } + + public void testShouldCompleteMatchStrategyWithPredefinedStrategies() { + + copyJavaRunnerToConcordionProject("MatchStrategy.java"); + VirtualFile htmlSpec = copyHtmlSpecToConcordionProject("MatchStrategy.html"); + + myFixture.configureFromExistingVirtualFile(htmlSpec); + myFixture.complete(CompletionType.BASIC, 1); + + assertThat(myFixture.getLookupElementStrings()).containsAll(asList("Default", "BestMatch", "KeyMatch")); + } + + public void testShouldCompleteMatchingRoleWithKeyRole() { + + copyJavaRunnerToConcordionProject("MatchingRole.java"); + VirtualFile htmlSpec = copyHtmlSpecToConcordionProject("MatchingRole.html"); + + myFixture.configureFromExistingVirtualFile(htmlSpec); + myFixture.complete(CompletionType.BASIC, 1); + + assertThat(myFixture.getLookupElementStrings()).contains("key"); + } +}