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;
}
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());
}
}
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;
}
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);
}
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);
}
Aggregations