Search in sources :

Example 1 with ArrangementEngine

use of com.intellij.psi.codeStyle.arrangement.engine.ArrangementEngine 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 2 with ArrangementEngine

use of com.intellij.psi.codeStyle.arrangement.engine.ArrangementEngine in project android by JetBrains.

the class AndroidXmlFormatterTest method doTestArrangement.

private void doTestArrangement(String dst) {
    final VirtualFile f = myFixture.copyFileToProject(BASE_PATH + getTestName(true) + ".xml", dst);
    myFixture.configureFromExistingVirtualFile(f);
    final ArrangementEngine engine = ServiceManager.getService(getProject(), ArrangementEngine.class);
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            engine.arrange(myFixture.getEditor(), myFixture.getFile(), Arrays.asList(new TextRange(0, myFixture.getFile().getTextLength())));
        }
    });
    myFixture.checkResultByFile(BASE_PATH + getTestName(true) + "_after.xml");
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrangementEngine(com.intellij.psi.codeStyle.arrangement.engine.ArrangementEngine) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

TextRange (com.intellij.openapi.util.TextRange)2 ArrangementEngine (com.intellij.psi.codeStyle.arrangement.engine.ArrangementEngine)2 FoldRegion (com.intellij.openapi.editor.FoldRegion)1 FoldingModel (com.intellij.openapi.editor.FoldingModel)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)1 ArrangementGroupingRule (com.intellij.psi.codeStyle.arrangement.group.ArrangementGroupingRule)1 ArrangementSectionRule (com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule)1