PropertiesFormatter.java

/**
 * Copyright (c) 2004-2025 Carnegie Mellon University and others. (see Contributors file).
 * All Rights Reserved.
 * 
 * NO WARRANTY. ALL MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE
 * OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT
 * MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.
 * 
 * This program and the accompanying materials are made available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Created, in part, with funding and support from the United States Government. (see Acknowledgments file).
 * 
 * This program includes and/or can make use of certain third party source code, object code, documentation and other
 * files ("Third Party Software"). The Third Party Software that is used by this program is dependent upon your system
 * configuration. By using this program, You agree to comply with any and all relevant Third Party Software terms and
 * conditions contained in any such Third Party Software or separate license file distributed with such Third Party
 * Software. The parties who own the Third Party Software ("Third Party Licensors") are intended third party benefici-
 * aries to this license with respect to the terms applicable to their Third Party Software. Third Party Software li-
 * censes only apply to the Third Party Software and not any other portion of this program or this program as a whole.
 */
package org.osate.xtext.aadl2.properties.formatting2;

import com.google.inject.Inject;
import java.util.Arrays;
import java.util.function.Consumer;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.formatting2.AbstractFormatter2;
import org.eclipse.xtext.formatting2.IFormattableDocument;
import org.eclipse.xtext.formatting2.IHiddenRegionFormatter;
import org.eclipse.xtext.formatting2.ITextReplacer;
import org.eclipse.xtext.formatting2.ITextReplacerContext;
import org.eclipse.xtext.formatting2.internal.SinglelineCodeCommentReplacer;
import org.eclipse.xtext.formatting2.internal.SinglelineDocCommentReplacer;
import org.eclipse.xtext.formatting2.regionaccess.IComment;
import org.eclipse.xtext.formatting2.regionaccess.ILineRegion;
import org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion;
import org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.osate.aadl2.ArrayRange;
import org.osate.aadl2.BasicPropertyAssociation;
import org.osate.aadl2.ClassifierValue;
import org.osate.aadl2.ComputedValue;
import org.osate.aadl2.ContainmentPathElement;
import org.osate.aadl2.IntegerLiteral;
import org.osate.aadl2.ListValue;
import org.osate.aadl2.ModalPropertyValue;
import org.osate.aadl2.Operation;
import org.osate.aadl2.PropertyExpression;
import org.osate.aadl2.RangeValue;
import org.osate.aadl2.RealLiteral;
import org.osate.aadl2.RecordValue;
import org.osate.aadl2.ReferenceValue;
import org.osate.xtext.aadl2.properties.services.PropertiesGrammarAccess;

@SuppressWarnings("all")
public class PropertiesFormatter extends AbstractFormatter2 {
  @Inject
  @Extension
  private PropertiesGrammarAccess _propertiesGrammarAccess;

  @Override
  public ITextReplacer createCommentReplacer(final IComment comment) {
    final EObject grammarElement = comment.getGrammarElement();
    if ((grammarElement instanceof AbstractRule)) {
      int _length = IterableExtensions.<ILineRegion>head(comment.getLineRegions()).getIndentation().getLength();
      boolean _greaterThan = (_length > 0);
      if (_greaterThan) {
        return new SinglelineDocCommentReplacer(comment, "--") {
          @Override
          public ITextReplacerContext createReplacements(final ITextReplacerContext context) {
            return context;
          }
        };
      } else {
        return new SinglelineCodeCommentReplacer(comment, "--");
      }
    }
    final String elementName = new GrammarElementTitleSwitch().showQualified().showRule().doSwitch(grammarElement);
    String _simpleName = ITextReplacer.class.getSimpleName();
    String _plus = ("No " + _simpleName);
    String _plus_1 = (_plus + " configured for ");
    String _plus_2 = (_plus_1 + elementName);
    throw new IllegalStateException(_plus_2);
  }

  protected void _format(final ModalPropertyValue modalPropertyValue, @Extension final IFormattableDocument document) {
    this.format(modalPropertyValue.getOwnedValue(), document);
    final ISemanticRegion leftParenthesis = this.textRegionExtensions.regionFor(modalPropertyValue).keyword(this._propertiesGrammarAccess.getOptionalModalPropertyValueAccess().getLeftParenthesisKeyword_1_1());
    final ISemanticRegion rightParenthesis = this.textRegionExtensions.regionFor(modalPropertyValue).keyword(this._propertiesGrammarAccess.getOptionalModalPropertyValueAccess().getRightParenthesisKeyword_1_4());
    if (((leftParenthesis != null) && (rightParenthesis != null))) {
      final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
      };
      document.surround(this.textRegionExtensions.regionFor(modalPropertyValue).keyword(this._propertiesGrammarAccess.getInModesKeywordsAccess().getInKeyword_0()), _function);
      final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
        it.indent();
      };
      document.<ISemanticRegion, ISemanticRegion>interior(leftParenthesis, rightParenthesis, _function_1);
      final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
      };
      final Procedure1<IHiddenRegionFormatter> _function_3 = (IHiddenRegionFormatter it) -> {
        it.noSpace();
        it.setNewLines(0, 0, 1);
        it.autowrap();
      };
      document.append(document.prepend(leftParenthesis, _function_2), _function_3);
      final Consumer<ISemanticRegion> _function_4 = (ISemanticRegion it) -> {
        final Procedure1<IHiddenRegionFormatter> _function_5 = (IHiddenRegionFormatter it_1) -> {
          it_1.noSpace();
        };
        final Procedure1<IHiddenRegionFormatter> _function_6 = (IHiddenRegionFormatter it_1) -> {
          it_1.oneSpace();
          it_1.setNewLines(0, 0, 1);
          it_1.autowrap();
        };
        document.append(document.prepend(it, _function_5), _function_6);
      };
      this.textRegionExtensions.regionFor(modalPropertyValue).keywords(this._propertiesGrammarAccess.getOptionalModalPropertyValueAccess().getCommaKeyword_1_3_0()).forEach(_function_4);
      boolean _isMultiline = rightParenthesis.getPreviousHiddenRegion().isMultiline();
      if (_isMultiline) {
        final Procedure1<IHiddenRegionFormatter> _function_5 = (IHiddenRegionFormatter it) -> {
          it.setNewLines(1);
        };
        document.prepend(rightParenthesis, _function_5);
      } else {
        final Procedure1<IHiddenRegionFormatter> _function_6 = (IHiddenRegionFormatter it) -> {
          it.noSpace();
        };
        document.prepend(rightParenthesis, _function_6);
      }
    }
  }

  protected void _format(final ReferenceValue referenceValue, @Extension final IFormattableDocument document) {
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
      it.oneSpace();
    };
    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.append(document.prepend(this.textRegionExtensions.regionFor(referenceValue).keyword(this._propertiesGrammarAccess.getReferenceTermAccess().getLeftParenthesisKeyword_1()), _function), _function_1);
    this.format(referenceValue.getPath(), document);
    final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.prepend(this.textRegionExtensions.regionFor(referenceValue).keyword(this._propertiesGrammarAccess.getReferenceTermAccess().getRightParenthesisKeyword_3()), _function_2);
  }

  protected void _format(final RecordValue recordValue, @Extension final IFormattableDocument document) {
    final ISemanticRegion leftBracket = this.textRegionExtensions.regionFor(recordValue).keyword(this._propertiesGrammarAccess.getRecordTermAccess().getLeftSquareBracketKeyword_0());
    final ISemanticRegion rightBracket = this.textRegionExtensions.regionFor(recordValue).keyword(this._propertiesGrammarAccess.getRecordTermAccess().getRightSquareBracketKeyword_2());
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
      it.indent();
    };
    document.<ISemanticRegion, ISemanticRegion>interior(leftBracket, rightBracket, _function);
    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
      it.setNewLines(0, 0, 1);
      it.autowrap();
    };
    document.append(leftBracket, _function_1);
    final Consumer<BasicPropertyAssociation> _function_2 = (BasicPropertyAssociation it) -> {
      final Procedure1<IHiddenRegionFormatter> _function_3 = (IHiddenRegionFormatter it_1) -> {
        it_1.oneSpace();
        it_1.setNewLines(0, 0, 1);
        it_1.autowrap();
      };
      document.<BasicPropertyAssociation>prepend(it, _function_3);
    };
    IterableExtensions.<BasicPropertyAssociation>tail(recordValue.getOwnedFieldValues()).forEach(_function_2);
    final Consumer<BasicPropertyAssociation> _function_3 = (BasicPropertyAssociation it) -> {
      this.format(it, document);
    };
    recordValue.getOwnedFieldValues().forEach(_function_3);
    if ((rightBracket != null)) {
      boolean _isMultiline = rightBracket.getPreviousHiddenRegion().isMultiline();
      if (_isMultiline) {
        final Procedure1<IHiddenRegionFormatter> _function_4 = (IHiddenRegionFormatter it) -> {
          it.setNewLines(1);
        };
        document.prepend(rightBracket, _function_4);
      } else {
        final Procedure1<IHiddenRegionFormatter> _function_5 = (IHiddenRegionFormatter it) -> {
          it.noSpace();
        };
        document.prepend(rightBracket, _function_5);
      }
    }
  }

  protected void _format(final ComputedValue computedValue, @Extension final IFormattableDocument document) {
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
      it.oneSpace();
    };
    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.append(document.prepend(this.textRegionExtensions.regionFor(computedValue).keyword(this._propertiesGrammarAccess.getComputedTermAccess().getLeftParenthesisKeyword_1()), _function), _function_1);
    final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.prepend(this.textRegionExtensions.regionFor(computedValue).keyword(this._propertiesGrammarAccess.getComputedTermAccess().getRightParenthesisKeyword_3()), _function_2);
  }

  protected void _format(final ClassifierValue classifierValue, @Extension final IFormattableDocument document) {
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
      it.oneSpace();
    };
    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.append(document.prepend(this.textRegionExtensions.regionFor(classifierValue).keyword(this._propertiesGrammarAccess.getComponentClassifierTermAccess().getLeftParenthesisKeyword_1()), _function), _function_1);
    final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.prepend(this.textRegionExtensions.regionFor(classifierValue).keyword(this._propertiesGrammarAccess.getComponentClassifierTermAccess().getRightParenthesisKeyword_3()), _function_2);
  }

  protected void _format(final ListValue listValue, @Extension final IFormattableDocument document) {
    final ISemanticRegion leftParenthesis = this.textRegionExtensions.regionFor(listValue).keyword(this._propertiesGrammarAccess.getListTermAccess().getLeftParenthesisKeyword_1());
    boolean _isEmpty = listValue.getOwnedListElements().isEmpty();
    if (_isEmpty) {
      final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
        it.noSpace();
      };
      document.append(leftParenthesis, _function);
    } else {
      final ISemanticRegion rightParenthesis = this.textRegionExtensions.regionFor(listValue).keyword(this._propertiesGrammarAccess.getListTermAccess().getRightParenthesisKeyword_3());
      final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
        it.indent();
      };
      document.<ISemanticRegion, ISemanticRegion>interior(leftParenthesis, rightParenthesis, _function_1);
      final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it) -> {
        it.noSpace();
        it.setNewLines(0, 0, 1);
        it.autowrap();
      };
      document.append(leftParenthesis, _function_2);
      final Consumer<ISemanticRegion> _function_3 = (ISemanticRegion it) -> {
        final Procedure1<IHiddenRegionFormatter> _function_4 = (IHiddenRegionFormatter it_1) -> {
          it_1.noSpace();
        };
        final Procedure1<IHiddenRegionFormatter> _function_5 = (IHiddenRegionFormatter it_1) -> {
          it_1.oneSpace();
          it_1.setNewLines(0, 0, 1);
          it_1.autowrap();
        };
        document.append(document.prepend(it, _function_4), _function_5);
      };
      this.textRegionExtensions.regionFor(listValue).keywords(this._propertiesGrammarAccess.getListTermAccess().getCommaKeyword_2_1_0()).forEach(_function_3);
      final Consumer<PropertyExpression> _function_4 = (PropertyExpression it) -> {
        this.format(it, document);
      };
      listValue.getOwnedListElements().forEach(_function_4);
      if ((rightParenthesis != null)) {
        boolean _isMultiline = rightParenthesis.getPreviousHiddenRegion().isMultiline();
        if (_isMultiline) {
          final Procedure1<IHiddenRegionFormatter> _function_5 = (IHiddenRegionFormatter it) -> {
            it.setNewLines(1);
          };
          document.prepend(rightParenthesis, _function_5);
        } else {
          final Procedure1<IHiddenRegionFormatter> _function_6 = (IHiddenRegionFormatter it) -> {
            it.noSpace();
          };
          document.prepend(rightParenthesis, _function_6);
        }
      }
    }
  }

  protected void _format(final BasicPropertyAssociation basicPropertyAssociation, @Extension final IFormattableDocument document) {
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
      it.oneSpace();
    };
    document.surround(this.textRegionExtensions.regionFor(basicPropertyAssociation).keyword(this._propertiesGrammarAccess.getFieldPropertyAssociationAccess().getEqualsSignGreaterThanSignKeyword_1()), _function);
    this.format(basicPropertyAssociation.getOwnedValue(), document);
    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.prepend(this.textRegionExtensions.regionFor(basicPropertyAssociation).keyword(this._propertiesGrammarAccess.getFieldPropertyAssociationAccess().getSemicolonKeyword_3()), _function_1);
  }

  protected void _format(final ContainmentPathElement containmentPathElement, @Extension final IFormattableDocument document) {
    final Consumer<ArrayRange> _function = (ArrayRange it) -> {
      final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it_1) -> {
        it_1.noSpace();
      };
      document.<ArrayRange>prepend(it, _function_1);
      this.format(it, document);
    };
    containmentPathElement.getArrayRanges().forEach(_function);
    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.surround(this.textRegionExtensions.regionFor(containmentPathElement).keyword(this._propertiesGrammarAccess.getContainmentPathElementAccess().getFullStopKeyword_1_0()), _function_1);
    this.format(containmentPathElement.getPath(), document);
  }

  protected void _format(final ArrayRange arrayRange, @Extension final IFormattableDocument document) {
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.append(this.textRegionExtensions.regionFor(arrayRange).keyword(this._propertiesGrammarAccess.getArrayRangeAccess().getLeftSquareBracketKeyword_1()), _function);
    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
      it.oneSpace();
    };
    document.surround(this.textRegionExtensions.regionFor(arrayRange).keyword(this._propertiesGrammarAccess.getArrayRangeAccess().getFullStopFullStopKeyword_3_0()), _function_1);
    final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.prepend(this.textRegionExtensions.regionFor(arrayRange).keyword(this._propertiesGrammarAccess.getArrayRangeAccess().getRightSquareBracketKeyword_4()), _function_2);
  }

  protected void _format(final Operation operation, @Extension final IFormattableDocument document) {
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.append(this.textRegionExtensions.regionFor(operation).assignment(this._propertiesGrammarAccess.getSignedConstantAccess().getOpAssignment_0()), _function);
  }

  protected void _format(final IntegerLiteral integerLiteral, @Extension final IFormattableDocument document) {
    final ISemanticRegion unitAssignment = this.textRegionExtensions.regionFor(integerLiteral).assignment(this._propertiesGrammarAccess.getIntegerTermAccess().getUnitAssignment_1());
    if (((unitAssignment != null) && (unitAssignment.getText().length() <= 2))) {
      final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
        it.noSpace();
      };
      document.prepend(unitAssignment, _function);
    } else {
      final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
      };
      document.prepend(unitAssignment, _function_1);
    }
  }

  protected void _format(final RealLiteral realLiteral, @Extension final IFormattableDocument document) {
    final ISemanticRegion unitAssignment = this.textRegionExtensions.regionFor(realLiteral).assignment(this._propertiesGrammarAccess.getRealTermAccess().getUnitAssignment_1());
    if (((unitAssignment != null) && (unitAssignment.getText().length() <= 2))) {
      final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
        it.noSpace();
      };
      document.prepend(unitAssignment, _function);
    } else {
      final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
      };
      document.prepend(unitAssignment, _function_1);
    }
  }

  protected void _format(final RangeValue rangeValue, @Extension final IFormattableDocument document) {
    this.format(rangeValue.getMinimum(), document);
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
      it.oneSpace();
    };
    document.surround(this.textRegionExtensions.regionFor(rangeValue).keyword(this._propertiesGrammarAccess.getNumericRangeTermAccess().getFullStopFullStopKeyword_1()), _function);
    this.format(rangeValue.getMaximum(), document);
    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
      it.oneSpace();
    };
    document.surround(this.textRegionExtensions.regionFor(rangeValue).keyword(this._propertiesGrammarAccess.getNumericRangeTermAccess().getDeltaKeyword_3_0()), _function_1);
    this.format(rangeValue.getDelta(), document);
  }

  public void format(final Object integerLiteral, final IFormattableDocument document) {
    if (integerLiteral instanceof IntegerLiteral) {
      _format((IntegerLiteral)integerLiteral, document);
      return;
    } else if (integerLiteral instanceof RealLiteral) {
      _format((RealLiteral)integerLiteral, document);
      return;
    } else if (integerLiteral instanceof ClassifierValue) {
      _format((ClassifierValue)integerLiteral, document);
      return;
    } else if (integerLiteral instanceof ComputedValue) {
      _format((ComputedValue)integerLiteral, document);
      return;
    } else if (integerLiteral instanceof ModalPropertyValue) {
      _format((ModalPropertyValue)integerLiteral, document);
      return;
    } else if (integerLiteral instanceof RangeValue) {
      _format((RangeValue)integerLiteral, document);
      return;
    } else if (integerLiteral instanceof RecordValue) {
      _format((RecordValue)integerLiteral, document);
      return;
    } else if (integerLiteral instanceof ReferenceValue) {
      _format((ReferenceValue)integerLiteral, document);
      return;
    } else if (integerLiteral instanceof ListValue) {
      _format((ListValue)integerLiteral, document);
      return;
    } else if (integerLiteral instanceof Operation) {
      _format((Operation)integerLiteral, document);
      return;
    } else if (integerLiteral instanceof XtextResource) {
      _format((XtextResource)integerLiteral, document);
      return;
    } else if (integerLiteral instanceof ArrayRange) {
      _format((ArrayRange)integerLiteral, document);
      return;
    } else if (integerLiteral instanceof BasicPropertyAssociation) {
      _format((BasicPropertyAssociation)integerLiteral, document);
      return;
    } else if (integerLiteral instanceof ContainmentPathElement) {
      _format((ContainmentPathElement)integerLiteral, document);
      return;
    } else if (integerLiteral instanceof EObject) {
      _format((EObject)integerLiteral, document);
      return;
    } else if (integerLiteral == null) {
      _format((Void)null, document);
      return;
    } else if (integerLiteral != null) {
      _format(integerLiteral, document);
      return;
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.<Object>asList(integerLiteral, document).toString());
    }
  }
}