Skip to content

Commit

Permalink
Complete with special values for matchStrategy and matchingRole
Browse files Browse the repository at this point in the history
  • Loading branch information
idegtiarenko committed Sep 29, 2015
1 parent 732327d commit 52f9e5e
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 35 deletions.
1 change: 1 addition & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<annotator language="HTML" implementationClass="com.gman.idea.plugin.concordion.annotator.ConcordionFullOgnlRequiredAnnotator"/>

<completion.contributor language="HTML" implementationClass="com.gman.idea.plugin.concordion.autocomplete.ConcordionCommandsCompletionContributor"/>
<completion.contributor language="HTML" implementationClass="com.gman.idea.plugin.concordion.autocomplete.ConcordionNonExpressionCompletionContributor"/>
<completion.contributor language="Concordion" implementationClass="com.gman.idea.plugin.concordion.autocomplete.ConcordionExpressionCompletionContributor"/>

<localInspection language="Concordion" id="usingMapKeyAsField" displayName="Using map key as a field"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.gman.idea.plugin.concordion;

import com.intellij.patterns.PatternCondition;
import com.intellij.psi.PsiElement;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.Set;

import static com.gman.idea.plugin.concordion.Concordion.isConcordionHtmlSpec;
import static java.util.Arrays.asList;

public class ConcordionPatternConditions {

public static final ConcordionHtmlSpec CONCORDION_HTML_SPEC = new ConcordionHtmlSpec();
public static final SpecificConcordionTag MATCH_STRATEGY_ATTRIBUTE = new SpecificConcordionTag("matchStrategy", "match-strategy");
public static final SpecificConcordionTag MATCHING_ROLE_ATTRIBUTE = new SpecificConcordionTag("matchingRole", "matching-Role");

private static final class ConcordionHtmlSpec extends PatternCondition<PsiElement> {

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<PsiElement> {

private final Set<String> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand All @@ -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<LookupElement> forCommands(String prefix, Collection<String> commands) {
return commands.stream()
.map(c -> prefix + ':' + c)
.map(c -> LookupElementBuilder.create(c).withInsertHandler(XmlAttributeInsertHandler.INSTANCE))
.collect(toList());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<CompletionParameters> {
@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<CompletionParameters> {
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
result.addAllElements(fromStrings("key"));
}
}

private static Iterable<LookupElement> fromStrings(String... strings) {
return stream(strings).map(LookupElementBuilder::create).collect(toList());
}
}
15 changes: 15 additions & 0 deletions testData/completion/MatchStrategy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html xmlns:c="http://www.concordion.org/2007/concordion">
<head>
<title>Test spec</title>
</head>
<body>

<table c:verifyRows="#var : values()" c:matchStrategy="<caret>">
<tr>
<td>Key</td>
<td>Value</td>
</tr>
</table>

</body>
</html>
15 changes: 15 additions & 0 deletions testData/completion/MatchStrategy.java
Original file line number Diff line number Diff line change
@@ -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<Object> values() {
return Collections.<Object>emptyList();
}
}
15 changes: 15 additions & 0 deletions testData/completion/MatchingRole.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html xmlns:c="http://www.concordion.org/2007/concordion">
<head>
<title>Test spec</title>
</head>
<body>

<table c:verifyRows="#var : values()" c:matchStrategy="KeyMatch">
<tr>
<td c:matchingRole="<caret>">Key</td>
<td>Value</td>
</tr>
</table>

</body>
</html>
12 changes: 12 additions & 0 deletions testData/completion/MatchingRole.java
Original file line number Diff line number Diff line change
@@ -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<Object> values() {
return Collections.<Object>emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 52f9e5e

Please sign in to comment.