Search in sources :

Example 6 with ArrangementSectionRule

use of com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule in project intellij-community by JetBrains.

the class ArrangementSectionRulesControl method getSections.

public List<ArrangementSectionRule> getSections() {
    if (getModel().getSize() <= 0) {
        return Collections.emptyList();
    }
    final List<ArrangementSectionRule> result = ContainerUtil.newArrayList();
    final List<StdArrangementMatchRule> buffer = ContainerUtil.newArrayList();
    String currentSectionStart = null;
    for (int i = 0; i < getModel().getSize(); i++) {
        final Object element = getModel().getElementAt(i);
        if (element instanceof StdArrangementMatchRule) {
            final ArrangementSectionRuleData sectionRule = mySectionRuleManager == null ? null : mySectionRuleManager.getSectionRuleData((StdArrangementMatchRule) element);
            if (sectionRule != null) {
                if (sectionRule.isSectionStart()) {
                    appendBufferedSectionRules(result, buffer, currentSectionStart);
                    currentSectionStart = sectionRule.getText();
                } else {
                    result.add(ArrangementSectionRule.create(StringUtil.notNullize(currentSectionStart), sectionRule.getText(), buffer));
                    buffer.clear();
                    currentSectionStart = null;
                }
            } else {
                if (currentSectionStart == null) {
                    result.add(ArrangementSectionRule.create((StdArrangementMatchRule) element));
                } else {
                    buffer.add((StdArrangementMatchRule) element);
                }
            }
        }
    }
    appendBufferedSectionRules(result, buffer, currentSectionStart);
    return result;
}
Also used : ArrangementSectionRuleData(com.intellij.application.options.codeStyle.arrangement.match.ArrangementSectionRuleManager.ArrangementSectionRuleData) ArrangementSectionRule(com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule) StdArrangementMatchRule(com.intellij.psi.codeStyle.arrangement.match.StdArrangementMatchRule)

Example 7 with ArrangementSectionRule

use of com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule in project intellij-community by JetBrains.

the class AbstractRearrangerTest method doTest.

protected void doTest(@NotNull Map<String, ?> args) {
    String text = (String) args.get("initial");
    String expected = (String) args.get("expected");
    @SuppressWarnings("unchecked") List<TextRange> ranges = (List<TextRange>) args.get("ranges");
    Info info = parse(text);
    if (!isEmpty(ranges) && !isEmpty(info.ranges)) {
        fail("Duplicate ranges set: explicit: " + ranges + ", " + "derived: " + info.ranges + ", text:\n" + text);
    }
    if (isEmpty(info.ranges)) {
        info.ranges = !isEmpty(ranges) ? ranges : Arrays.asList(TextRange.from(0, text.length()));
    }
    myFixture.configureByText(fileType, info.text);
    final FoldingModel foldingModel = myFixture.getEditor().getFoldingModel();
    for (final FoldingInfo foldingInfo : info.foldings) {
        foldingModel.runBatchFoldingOperation(() -> {
            FoldRegion region = foldingModel.addFoldRegion(foldingInfo.start, foldingInfo.end, foldingInfo.placeholder);
            if (region != null)
                region.setExpanded(false);
        });
    }
    @SuppressWarnings("unchecked") List<ArrangementGroupingRule> groupingRules = (List<ArrangementGroupingRule>) args.get("groups");
    if (groupingRules == null)
        groupingRules = Collections.emptyList();
    List<?> rules = (List<?>) args.get("rules");
    List<ArrangementSectionRule> sectionRules = getSectionRules(rules);
    @SuppressWarnings("unchecked") List<StdArrangementRuleAliasToken> aliases = (List<StdArrangementRuleAliasToken>) args.get("aliases");
    CommonCodeStyleSettings settings = CodeStyleSettingsManager.getInstance(myFixture.getProject()).getCurrentSettings().getCommonSettings(language);
    final StdArrangementSettings arrangementSettings = aliases == null ? new StdArrangementSettings(groupingRules, sectionRules) : new StdArrangementExtendableSettings(groupingRules, sectionRules, aliases);
    settings.setArrangementSettings(arrangementSettings);
    ArrangementEngine engine = ServiceManager.getService(myFixture.getProject(), ArrangementEngine.class);
    CommandProcessor.getInstance().executeCommand(getProject(), () -> engine.arrange(myFixture.getEditor(), myFixture.getFile(), info.ranges), null, null);
    // Check expectation.
    Info after = parse(expected);
    assertEquals(after.text, myFixture.getEditor().getDocument().getText());
    for (FoldingInfo it : after.foldings) {
        FoldRegion foldRegion = foldingModel.getCollapsedRegionAtOffset(it.start);
        assertNotNull("Expected to find fold region at offset " + it.start, foldRegion);
        assertEquals(it.end, foldRegion.getEndOffset());
    }
}
Also used : ArrangementGroupingRule(com.intellij.psi.codeStyle.arrangement.group.ArrangementGroupingRule) ArrangementEngine(com.intellij.psi.codeStyle.arrangement.engine.ArrangementEngine) FoldRegion(com.intellij.openapi.editor.FoldRegion) TextRange(com.intellij.openapi.util.TextRange) FoldingModel(com.intellij.openapi.editor.FoldingModel) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) ArrangementSectionRule(com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule)

Example 8 with ArrangementSectionRule

use of com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule in project intellij-community by JetBrains.

the class ArrangementSectionDetector method processComment.

/**
   * Check if comment can be recognized as section start/end
   * @return true for section comment, false otherwise
   */
public boolean processComment(@NotNull PsiComment comment) {
    final TextRange range = comment.getTextRange();
    final TextRange expandedRange = myDocument == null ? range : ArrangementUtil.expandToLineIfPossible(range, myDocument);
    final TextRange sectionTextRange = new TextRange(expandedRange.getStartOffset(), expandedRange.getEndOffset());
    final String commentText = comment.getText().trim();
    final ArrangementSectionRule openSectionRule = isSectionStartComment(mySettings, commentText);
    if (openSectionRule != null) {
        mySectionEntryProducer.consume(new ArrangementSectionEntryTemplate(comment, START_SECTION, sectionTextRange, commentText));
        myOpenedSections.push(openSectionRule);
        return true;
    }
    if (!myOpenedSections.isEmpty()) {
        final ArrangementSectionRule lastSection = myOpenedSections.peek();
        if (lastSection.getEndComment() != null && StringUtil.equals(commentText, lastSection.getEndComment())) {
            mySectionEntryProducer.consume(new ArrangementSectionEntryTemplate(comment, END_SECTION, sectionTextRange, commentText));
            myOpenedSections.pop();
            return true;
        }
    }
    return false;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) ArrangementSectionRule(com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule)

Example 9 with ArrangementSectionRule

use of com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule in project intellij-community by JetBrains.

the class ArrangementSectionRulesControl method createRuleAliasEditDialog.

@NotNull
public ArrangementRuleAliasDialog createRuleAliasEditDialog() {
    final Set<String> tokenIds = new THashSet<>();
    final List<ArrangementSectionRule> sections = getSections();
    for (ArrangementSectionRule section : sections) {
        for (StdArrangementMatchRule rule : section.getMatchRules()) {
            rule.getMatcher().getCondition().invite(new ArrangementMatchConditionVisitor() {

                @Override
                public void visit(@NotNull ArrangementAtomMatchCondition condition) {
                    if (ArrangementUtil.isAliasedCondition(condition)) {
                        tokenIds.add(condition.getType().getId());
                    }
                }

                @Override
                public void visit(@NotNull ArrangementCompositeMatchCondition condition) {
                    for (ArrangementMatchCondition operand : condition.getOperands()) {
                        operand.invite(this);
                    }
                }
            });
        }
    }
    final Collection<StdArrangementRuleAliasToken> aliases = getRulesAliases();
    assert aliases != null;
    return new ArrangementRuleAliasDialog(null, mySettingsManager, myColorsProvider, aliases, tokenIds);
}
Also used : ArrangementMatchConditionVisitor(com.intellij.psi.codeStyle.arrangement.model.ArrangementMatchConditionVisitor) ArrangementAtomMatchCondition(com.intellij.psi.codeStyle.arrangement.model.ArrangementAtomMatchCondition) ArrangementRuleAliasDialog(com.intellij.application.options.codeStyle.arrangement.match.tokens.ArrangementRuleAliasDialog) ArrangementCompositeMatchCondition(com.intellij.psi.codeStyle.arrangement.model.ArrangementCompositeMatchCondition) THashSet(gnu.trove.THashSet) ArrangementSectionRule(com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule) StdArrangementMatchRule(com.intellij.psi.codeStyle.arrangement.match.StdArrangementMatchRule) ArrangementMatchCondition(com.intellij.psi.codeStyle.arrangement.model.ArrangementMatchCondition) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with ArrangementSectionRule

use of com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule in project intellij-community by JetBrains.

the class ArrangementSettingsSerializationTest method testUseCustomTokenSerialize.

@Test
public void testUseCustomTokenSerialize() throws IOException {
    final StdArrangementRuleAliasToken visibility = visibilityToken();
    final StdArrangementRuleAliasToken modifiers = modifiersToken();
    final Set<StdArrangementRuleAliasToken> tokens = ContainerUtil.newHashSet(visibility, modifiers);
    final ArrayList<ArrangementGroupingRule> groupings = ContainerUtil.newArrayList(new ArrangementGroupingRule(OVERRIDDEN_METHODS, BY_NAME));
    final ArrayList<ArrangementSectionRule> rules = ContainerUtil.newArrayList(section(true, FIELD, visibility));
    final StdArrangementExtendableSettings settings = extendableSettings(groupings, rules, tokens);
    final ArrayList<ArrangementSectionRule> defaultRules = ContainerUtil.newArrayList(section(true, FIELD));
    final StdArrangementExtendableSettings defaultSettings = extendableSettings(groupings, defaultRules, tokens);
    final Element holder = doSerializationTest(settings, defaultSettings);
    final String expected = "<holder>\n" + "  <rules>\n" + "    <section>\n" + "      <rule>\n" + "        <match>\n" + "          <AND>\n" + "            <FIELD>true</FIELD>\n" + "            <visibility />\n" + "          </AND>\n" + "        </match>\n" + "        <order>BY_NAME</order>\n" + "      </rule>\n" + "    </section>\n" + "  </rules>\n" + "</holder>";
    assertXmlOutputEquals(expected, holder);
}
Also used : StdArrangementRuleAliasToken(com.intellij.psi.codeStyle.arrangement.std.StdArrangementRuleAliasToken) ArrangementGroupingRule(com.intellij.psi.codeStyle.arrangement.group.ArrangementGroupingRule) StdArrangementExtendableSettings(com.intellij.psi.codeStyle.arrangement.std.StdArrangementExtendableSettings) Element(org.jdom.Element) ArrangementSectionRule(com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule) Test(org.junit.Test)

Aggregations

ArrangementSectionRule (com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule)13 ArrangementGroupingRule (com.intellij.psi.codeStyle.arrangement.group.ArrangementGroupingRule)6 Test (org.junit.Test)6 StdArrangementExtendableSettings (com.intellij.psi.codeStyle.arrangement.std.StdArrangementExtendableSettings)5 StdArrangementRuleAliasToken (com.intellij.psi.codeStyle.arrangement.std.StdArrangementRuleAliasToken)5 Element (org.jdom.Element)5 StdArrangementMatchRule (com.intellij.psi.codeStyle.arrangement.match.StdArrangementMatchRule)3 TextRange (com.intellij.openapi.util.TextRange)2 HashSet (com.intellij.util.containers.HashSet)2 NotNull (org.jetbrains.annotations.NotNull)2 ArrangementSectionRuleData (com.intellij.application.options.codeStyle.arrangement.match.ArrangementSectionRuleManager.ArrangementSectionRuleData)1 ArrangementRuleAliasDialog (com.intellij.application.options.codeStyle.arrangement.match.tokens.ArrangementRuleAliasDialog)1 Language (com.intellij.lang.Language)1 FoldRegion (com.intellij.openapi.editor.FoldRegion)1 FoldingModel (com.intellij.openapi.editor.FoldingModel)1 Pair (com.intellij.openapi.util.Pair)1 PsiElement (com.intellij.psi.PsiElement)1 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)1 ArrangementEngine (com.intellij.psi.codeStyle.arrangement.engine.ArrangementEngine)1 ArrangementMatchRule (com.intellij.psi.codeStyle.arrangement.match.ArrangementMatchRule)1