@Override public boolean isEnabled(@NotNull ArrangementSettingsToken token, @Nullable ArrangementMatchCondition current) { if (SUPPORTED_TYPES.contains(token)) { return true; } ArrangementSettingsToken type = null; if (current != null) { type = ArrangementUtil.parseType(current); } if (type == null) { type = NO_TYPE; } if (SUPPORTED_ORDERS.contains(token)) { return !TYPES_WITH_DISABLED_ORDER.contains(type); } if (StdArrangementTokens.Regexp.NAME.equals(token)) { return !TYPES_WITH_DISABLED_NAME_MATCH.contains(type); } Set<ArrangementSettingsToken> modifiers = MODIFIERS_BY_TYPE.get(type); return modifiers != null && modifiers.contains(token); }
@Override public int compare(ArrangementMatchCondition c1, ArrangementMatchCondition c2) { boolean isAtom1 = c1 instanceof ArrangementAtomMatchCondition; boolean isAtom2 = c2 instanceof ArrangementAtomMatchCondition; if (isAtom1 ^ isAtom2) { return isAtom1 ? 1 : -1; // Composite conditions before atom conditions. } else if (!isAtom1) { return 0; } ArrangementAtomMatchCondition atom1 = (ArrangementAtomMatchCondition)c1; ArrangementAtomMatchCondition atom2 = (ArrangementAtomMatchCondition)c2; int cmp = atom1.getType().compareTo(atom2.getType()); if (cmp == 0) { cmp = atom1.getValue().toString().compareTo(atom2.getValue().toString()); } return cmp; }
@Nullable private ArrangementMatchCondition deserializeCondition(@NotNull Element matcherElement) { String name = matcherElement.getName(); if (COMPOSITE_CONDITION_NAME.equals(name)) { ArrangementCompositeMatchCondition composite = new ArrangementCompositeMatchCondition(); for (Object child : matcherElement.getChildren()) { ArrangementMatchCondition deserialised = deserializeCondition((Element)child); if (deserialised != null) { composite.addOperand(deserialised); } } return composite; } else { return deserializeAtomCondition(matcherElement); } }
@Nullable private Pair<ArrangementMatchCondition, ArrangementSettingsToken> buildCondition() { List<ArrangementMatchCondition> conditions = ContainerUtilRt.newArrayList(); ArrangementSettingsToken orderType = null; for (ArrangementUiComponent component : myComponents.values()) { if (!component.isEnabled() || !component.isSelected()) { continue; } ArrangementSettingsToken token = component.getToken(); if (token != null && StdArrangementTokenType.ORDER.is(token)) { orderType = token; } else { conditions.add(component.getMatchCondition()); } } if (!conditions.isEmpty()) { if (orderType == null) { orderType = StdArrangementTokens.Order.KEEP; } return Pair.create(ArrangementUtil.combine(conditions.toArray(new ArrangementMatchCondition[conditions.size()])), orderType); } else { return null; } }
/** * Disable conditions not applicable at the current context (e.g. disable 'synchronized' if no 'method' is selected). */ private void refreshConditions() { Pair<ArrangementMatchCondition, ArrangementSettingsToken> pair = buildCondition(); ArrangementMatchCondition condition = pair == null ? null : pair.first; for (ArrangementUiComponent component : myComponents.values()) { ArrangementSettingsToken token = component.getToken(); if (token == null) { continue; } boolean enabled = isEnabled(condition, token); component.setEnabled(enabled); if (!enabled) { component.setSelected(false); } } }
@Nullable private Pair<ArrangementMatchCondition, ArrangementSettingsToken> buildCondition() { List<ArrangementMatchCondition> conditions = ContainerUtilRt.newArrayList(); ArrangementSettingsToken orderType = null; for (ArrangementUiComponent component : myComponents.values()) { if (!component.isEnabled() || !component.isSelected()) { continue; } ArrangementSettingsToken token = component.getToken(); if (token != null && StdArrangementTokens.Order.is(token)) { orderType = token; } else { conditions.add(component.getMatchCondition()); } } if (orderType != null && !conditions.isEmpty()) { return Pair.create(ArrangementUtil.combine(conditions.toArray(new ArrangementMatchCondition[conditions.size()])), orderType); } else { return null; } }
/** * Disable conditions not applicable at the current context (e.g. disable 'synchronized' if no 'method' is selected). */ private void refreshConditions() { Pair<ArrangementMatchCondition, ArrangementSettingsToken> pair = buildCondition(); ArrangementMatchCondition condition = pair == null ? null : pair.first; for (ArrangementUiComponent component : myComponents.values()) { ArrangementSettingsToken token = component.getToken(); if (token == null) { continue; } boolean enabled = mySettingsManager.isEnabled(token, condition); component.setEnabled(enabled); if (!enabled) { component.setSelected(false); } } }
@Nullable private ArrangementMatchCondition deserializeCondition(@Nonnull Element matcherElement) { String name = matcherElement.getName(); if (COMPOSITE_CONDITION_NAME.equals(name)) { ArrangementCompositeMatchCondition composite = new ArrangementCompositeMatchCondition(); for (Object child : matcherElement.getChildren()) { ArrangementMatchCondition deserialised = deserializeCondition((Element)child); if (deserialised != null) { composite.addOperand(deserialised); } } return composite; } else { return deserializeAtomCondition(matcherElement); } }
@Override public boolean isEnabled(@NotNull ArrangementSettingsToken token, @Nullable ArrangementMatchCondition current) { if(SUPPORTED_TYPES.contains(token) || SUPPORTED_ORDERS.contains(token) || StdArrangementTokens.Regexp.NAME.equals(token)) { return true; } ArrangementSettingsToken type = null; if(current != null) { type = ArrangementUtil.parseType(current); } if(type == null) { type = NO_TYPE; } Set<ArrangementSettingsToken> modifiers = MODIFIERS_BY_TYPE.get(type); return modifiers != null && modifiers.contains(token); }
@NotNull protected static StdArrangementMatchRule nameRule(@NotNull String nameFilter, @NotNull ArrangementSettingsToken... tokens) { if (tokens.length == 0) { return new StdArrangementMatchRule(new StdArrangementEntryMatcher(atom(nameFilter))); } else { ArrangementAtomMatchCondition[] conditions = new ArrangementAtomMatchCondition[tokens.length + 1]; conditions[0] = atom(nameFilter); for (int i = 0; i < tokens.length; i++) conditions[i + 1] = atom(tokens[i]); ArrangementMatchCondition compositeCondition = ArrangementUtil.combine(conditions); return new StdArrangementMatchRule(new StdArrangementEntryMatcher(compositeCondition)); } }
private void doTest(@NotNull ArrangementMatchCondition condition) { Element element = mySerializer.serialize(new StdArrangementEntryMatcher(condition)); assertNotNull(String.format("Can't serialize match condition '%s'", condition), element); ArrangementEntryMatcher matcher = mySerializer.deserialize(element); assertNotNull(matcher); assertEquals(condition, ((StdArrangementEntryMatcher)matcher).getCondition()); }
@NotNull public ArrangementEntryMatcher buildMatcher(@NotNull ArrangementMatchCondition condition) throws IllegalArgumentException { ArrangementEntryMatcher matcher = ArrangementUtil.buildMatcher(condition); if (matcher == null) { matcher = myDelegate.buildMatcher(condition); } return matcher; }
@Override public void visit(@NotNull ArrangementCompositeMatchCondition condition) { Element composite = new Element(COMPOSITE_CONDITION_NAME); register(composite); parent = composite; List<ArrangementMatchCondition> operands = ContainerUtilRt.newArrayList(condition.getOperands()); ContainerUtil.sort(operands, CONDITION_COMPARATOR); for (ArrangementMatchCondition c : operands) { c.invite(this); } }
@NotNull public StdArrangementMatchRule createDefaultSectionRule() { final ArrangementAtomMatchCondition type = new ArrangementAtomMatchCondition(START_SECTION); final ArrangementAtomMatchCondition text = new ArrangementAtomMatchCondition(TEXT, createDefaultSectionText()); final ArrangementMatchCondition condition = ArrangementUtil.combine(type, text); return new StdArrangementMatchRule(new StdArrangementEntryMatcher(condition)); }
@Override public boolean isEnabled(@NotNull ArrangementSettingsToken token, @Nullable ArrangementMatchCondition current) { if (SUPPORTED_TYPES.contains(token) || SUPPORTED_ORDERS.contains(token) || StdArrangementTokens.Regexp.NAME.equals(token)) { return true; } ArrangementSettingsToken type = null; if (current != null) { type = ArrangementUtil.parseType(current); } if (type == null) { type = NO_TYPE; } Set<ArrangementSettingsToken> modifiers = MODIFIERS_BY_TYPE.get(type); return modifiers != null && modifiers.contains(token); }
@Nullable private ArrangementMatchCondition deserializeAtomCondition(@NotNull Element matcherElement) { String id = matcherElement.getName(); ArrangementSettingsToken token = StdArrangementTokens.byId(id); boolean processInnerText = true; if (token != null && (StdArrangementTokens.General.TYPE.equals(token) || StdArrangementTokens.General.MODIFIER.equals(token))) { // Backward compatibility with old arrangement settings format. id = matcherElement.getText(); if (StringUtil.isEmpty(id)) { LOG.warn("Can't deserialize match condition at legacy format"); return null; } token = StdArrangementTokens.byId(id); processInnerText = false; } if (token == null) { token = myMixin.deserializeToken(id); } if (token == null) { LOG.warn(String.format("Can't deserialize match condition with id '%s'", id)); return null; } Object value = token; String text = matcherElement.getText(); if (text != null && processInnerText) { text = StringUtil.unescapeStringCharacters(matcherElement.getText()); if (!StringUtil.isEmpty(text)) { value = text; } } return new ArrangementAtomMatchCondition(token, value); }
@Nonnull public ArrangementEntryMatcher buildMatcher(@Nonnull ArrangementMatchCondition condition) throws IllegalArgumentException { ArrangementEntryMatcher matcher = ArrangementUtil.buildMatcher(condition); if (matcher == null) { matcher = myDelegate.buildMatcher(condition); } return matcher; }
@Nullable private static StdArrangementMatchRule createSectionRule(@Nullable String comment, @Nonnull ArrangementSettingsToken token) { if (StringUtil.isEmpty(comment)) { return null; } final ArrangementAtomMatchCondition type = new ArrangementAtomMatchCondition(token); final ArrangementAtomMatchCondition text = new ArrangementAtomMatchCondition(StdArrangementTokens.Regexp.TEXT, comment); final ArrangementMatchCondition condition = ArrangementUtil.combine(type, text); return new StdArrangementMatchRule(new StdArrangementEntryMatcher(condition)); }
@Nullable private ArrangementMatchCondition deserializeAtomCondition(@Nonnull Element matcherElement) { String id = matcherElement.getName(); ArrangementSettingsToken token = StdArrangementTokens.byId(id); boolean processInnerText = true; if (token != null && (StdArrangementTokens.General.TYPE.equals(token) || StdArrangementTokens.General.MODIFIER.equals(token))) { // Backward compatibility with old arrangement settings format. id = matcherElement.getText(); if (StringUtil.isEmpty(id)) { LOG.warn("Can't deserialize match condition at legacy format"); return null; } token = StdArrangementTokens.byId(id); processInnerText = false; } if (token == null) { token = myMixin.deserializeToken(id); } if (token == null) { LOG.warn(String.format("Can't deserialize match condition with id '%s'", id)); return null; } Object value = token; String text = matcherElement.getText(); if (text != null && processInnerText) { text = StringUtil.unescapeStringCharacters(matcherElement.getText()); if (!StringUtil.isEmpty(text)) { value = text; } } return new ArrangementAtomMatchCondition(token, value); }
@Override public void visit(@Nonnull ArrangementCompositeMatchCondition condition) { Element composite = new Element(COMPOSITE_CONDITION_NAME); register(composite); parent = composite; List<ArrangementMatchCondition> operands = ContainerUtilRt.newArrayList(condition.getOperands()); ContainerUtil.sort(operands, CONDITION_COMPARATOR); for (ArrangementMatchCondition c : operands) { c.invite(this); } }
private void doTest(@Nonnull ArrangementMatchCondition condition) { Element element = mySerializer.serialize(new StdArrangementEntryMatcher(condition)); assertNotNull(String.format("Can't serialize match condition '%s'", condition), element); ArrangementEntryMatcher matcher = mySerializer.deserialize(element); assertNotNull(matcher); assertEquals(condition, ((StdArrangementEntryMatcher)matcher).getCondition()); }
@Nonnull public StdArrangementMatchRule createDefaultSectionRule() { final ArrangementAtomMatchCondition type = new ArrangementAtomMatchCondition(START_SECTION); final ArrangementAtomMatchCondition text = new ArrangementAtomMatchCondition(TEXT, createDefaultSectionText()); final ArrangementMatchCondition condition = ArrangementUtil.combine(type, text); return new StdArrangementMatchRule(new StdArrangementEntryMatcher(condition)); }
@NotNull @Override public ArrangementEntryMatcher buildMatcher(@NotNull ArrangementMatchCondition condition) throws IllegalArgumentException { throw new IllegalArgumentException("Can't build a matcher for condition " + condition); }
@NotNull protected static StdArrangementMatchRule rule(@NotNull ArrangementAtomMatchCondition... conditions) { ArrangementMatchCondition compositeCondition = ArrangementUtil.combine(conditions); return new StdArrangementMatchRule(new StdArrangementEntryMatcher(compositeCondition)); }
public boolean isEnabled(@NotNull ArrangementSettingsToken token, @Nullable ArrangementMatchCondition current) { if (myRuleAliasMutex.contains(token)) { return true; } return myDelegate.isEnabled(token, current); }
@NotNull ArrangementMatchCondition getMatchCondition();
@NotNull private static StdArrangementMatchRule createSectionRule(@NotNull String comment, @NotNull ArrangementSettingsToken token) { final ArrangementAtomMatchCondition text = new ArrangementAtomMatchCondition(StdArrangementTokens.Regexp.TEXT, comment); final ArrangementMatchCondition condition = ArrangementUtil.combine(new ArrangementAtomMatchCondition(token), text); return new StdArrangementMatchRule(new StdArrangementEntryMatcher(condition)); }
@NotNull private static Element serialize(@NotNull ArrangementMatchCondition condition) { MySerializationVisitor visitor = new MySerializationVisitor(); condition.invite(visitor); return visitor.result; }
@SuppressWarnings("MethodMayBeStatic") @Nullable public StdArrangementEntryMatcher deserialize(@NotNull Element matcherElement) { ArrangementMatchCondition condition = deserializeCondition(matcherElement); return condition == null ? null : new StdArrangementEntryMatcher(condition); }
@Nullable private ArrangementMatchCondition deserializeAtomCondition(@NotNull Element matcherElement) { String id = matcherElement.getName(); ArrangementSettingsToken token = StdArrangementTokens.byId(id); boolean processInnerText = true; if (token != null && (StdArrangementTokens.General.TYPE.equals(token) || StdArrangementTokens.General.MODIFIER.equals(token))) { // Backward compatibility with old arrangement settings format. id = matcherElement.getText(); if (StringUtil.isEmpty(id)) { LOG.warn("Can't deserialize match condition at legacy format"); return null; } token = StdArrangementTokens.byId(id); processInnerText = false; } if (token == null) { token = myMixin.deserializeToken(id); } if (token == null) { LOG.warn(String.format("Can't deserialize match condition with id '%s'", id)); return null; } Object value = token; String text = matcherElement.getText(); if (text != null && processInnerText) { text = StringUtil.unescapeStringCharacters(matcherElement.getText()); if (!StringUtil.isEmpty(text)) { final Boolean booleanValue = parseBooleanValue(text); if (booleanValue != null) { value = booleanValue; } else { value = text; } } } return new ArrangementAtomMatchCondition(token, value); }
@Nullable public ArrangementSectionRuleData getSectionRuleData(@NotNull StdArrangementMatchRule element) { final ArrangementMatchCondition condition = element.getMatcher().getCondition(); return getSectionRuleData(condition); }
private boolean isEnabled(@Nullable ArrangementMatchCondition condition, @NotNull ArrangementSettingsToken token) { return ArrangementSectionRuleManager.isEnabled(token) || mySettingsManager.isEnabled(token, condition); }