Search in sources :

Example 1 with CharacterRangeElements

use of org.eclipse.xtext.services.XtextGrammarAccess.CharacterRangeElements in project xtext-core by eclipse.

the class XtextFormatter method configureFormatting.

@Override
protected void configureFormatting(FormattingConfig cfg) {
    XtextGrammarAccess g = (XtextGrammarAccess) getGrammarAccess();
    cfg.setAutoLinewrap(120);
    // Comments
    cfg.setLinewrap(0, 1, 2).before(g.getSL_COMMENTRule());
    cfg.setLinewrap(0, 1, 2).before(g.getML_COMMENTRule());
    cfg.setLinewrap(0, 1, 1).after(g.getML_COMMENTRule());
    // general keywords
    for (Pair<Keyword, Keyword> pair : g.findKeywordPairs("(", ")")) {
        cfg.setNoSpace().after(pair.getFirst());
        cfg.setNoSpace().before(pair.getSecond());
    }
    for (Pair<Keyword, Keyword> pair : g.findKeywordPairs("[", "]")) {
        cfg.setNoSpace().after(pair.getFirst());
        cfg.setNoSpace().before(pair.getSecond());
    }
    for (Pair<Keyword, Keyword> pair : g.findKeywordPairs("<", ">")) {
        cfg.setNoSpace().after(pair.getFirst());
        cfg.setNoSpace().before(pair.getSecond());
    }
    for (Keyword comma : g.findKeywords(",")) {
        cfg.setNoSpace().before(comma);
    }
    for (Pair<Keyword, Keyword> pair : g.findKeywordPairs(":", ";")) {
        cfg.setNoSpace().before(pair.getFirst());
        cfg.setNoSpace().before(pair.getSecond());
        cfg.setLinewrap().after(pair.getFirst());
        cfg.setLinewrap().after(pair.getSecond());
        cfg.setIndentation(pair.getFirst(), pair.getSecond());
        cfg.setLinewrap(0, 0, 1).range(pair.getFirst(), pair.getSecond());
    }
    // Grammar
    GrammarElements gr = g.getGrammarAccess();
    cfg.setNoLinewrap().between(gr.getWithKeyword_2_0(), gr.getUsedGrammarsAssignment_2_1());
    cfg.setNoSpace().between(gr.getDefinesHiddenTokensAssignment_3_0(), gr.getLeftParenthesisKeyword_3_1());
    cfg.setLinewrap(2).between(gr.getNameAssignment_1(), gr.getMetamodelDeclarationsAssignment_4());
    cfg.setLinewrap(2).between(gr.getGroup_2(), gr.getMetamodelDeclarationsAssignment_4());
    cfg.setLinewrap(2).between(gr.getGroup_3(), gr.getMetamodelDeclarationsAssignment_4());
    cfg.setLinewrap().after(gr.getMetamodelDeclarationsAssignment_4());
    cfg.setLinewrap(2).between(gr.getMetamodelDeclarationsAssignment_4(), gr.getRulesAssignment_5());
    cfg.setLinewrap(2).before(gr.getRulesAssignment_5());
    // ParserRule
    ParserRuleElements pr = g.getParserRuleAccess();
    cfg.setNoSpace().before(pr.getLeftParenthesisKeyword_2_1());
    // TypeRef
    TypeRefElements typeRef = g.getTypeRefAccess();
    cfg.setNoSpace().around(typeRef.getColonColonKeyword_0_1());
    // UnorderedGroup
    UnorderedGroupElements uge = g.getUnorderedGroupAccess();
    cfg.setNoSpace().around(uge.getUnorderedGroupElementsAction_1_0());
    // AbstractToken
    AbstractTokenWithCardinalityElements at = g.getAbstractTokenWithCardinalityAccess();
    cfg.setNoSpace().before(at.getCardinalityAsteriskKeyword_1_0_1());
    cfg.setNoSpace().before(at.getCardinalityPlusSignKeyword_1_0_2());
    cfg.setNoSpace().before(at.getCardinalityQuestionMarkKeyword_1_0_0());
    // Action
    ActionElements ac = g.getActionAccess();
    cfg.setNoSpace().around(ac.getOperatorAssignment_2_2());
    cfg.setNoSpace().around(ac.getFullStopKeyword_2_0());
    cfg.setNoSpace().after(ac.getLeftCurlyBracketKeyword_0());
    cfg.setNoSpace().before(ac.getRightCurlyBracketKeyword_3());
    // Assignment
    AssignmentElements as = g.getAssignmentAccess();
    cfg.setNoSpace().around(as.getOperatorAssignment_2());
    // CrossReference
    CrossReferenceElements cr = g.getCrossReferenceAccess();
    cfg.setNoSpace().around(cr.getVerticalLineKeyword_2_0());
    // TerminalToken
    TerminalTokenElements tt = g.getTerminalTokenAccess();
    cfg.setNoSpace().before(tt.getCardinalityAssignment_1());
    // NegatedToken
    NegatedTokenElements ne = g.getNegatedTokenAccess();
    cfg.setNoSpace().after(ne.getExclamationMarkKeyword_0());
    // UntilToken
    UntilTokenElements ut = g.getUntilTokenAccess();
    cfg.setNoSpace().around(ut.getHyphenMinusGreaterThanSignKeyword_0());
    // CharacterRange
    CharacterRangeElements cre = g.getCharacterRangeAccess();
    cfg.setNoSpace().around(cre.getFullStopFullStopKeyword_1_1());
    // EnumLiteralDeclaration
    EnumLiteralDeclarationElements eld = g.getEnumLiteralDeclarationAccess();
    cfg.setNoSpace().around(eld.getEqualsSignKeyword_1_0());
    // GuardCondition
    NegationElements na = g.getNegationAccess();
    cfg.setNoSpace().after(na.getExclamationMarkKeyword_1_1());
    // RuleCall
    RuleCallElements rca = g.getRuleCallAccess();
    cfg.setNoSpace().before(rca.getLessThanSignKeyword_1_0());
    // NamedArgument
    NamedArgumentElements naa = g.getNamedArgumentAccess();
    cfg.setNoSpace().around(naa.getCalledByNameAssignment_0_1());
    cfg.setNoSpace().around(naa.getValueAssignment_1());
    cfg.setNoSpace().around(naa.getParameterAssignment_0_0());
    // @Override
    AnnotationElements a = g.getAnnotationAccess();
    cfg.setNoSpace().between(a.getCommercialAtKeyword_0(), a.getNameAssignment_1());
    cfg.setLinewrap().after(g.getAnnotationRule());
    cfg.setLinewrap(2).before(g.getAnnotationRule());
// saveDebugGraphvizDiagram("XtextFormatting.dot");
}
Also used : AnnotationElements(org.eclipse.xtext.services.XtextGrammarAccess.AnnotationElements) Keyword(org.eclipse.xtext.Keyword) ParserRuleElements(org.eclipse.xtext.services.XtextGrammarAccess.ParserRuleElements) CharacterRangeElements(org.eclipse.xtext.services.XtextGrammarAccess.CharacterRangeElements) NamedArgumentElements(org.eclipse.xtext.services.XtextGrammarAccess.NamedArgumentElements) TypeRefElements(org.eclipse.xtext.services.XtextGrammarAccess.TypeRefElements) AbstractTokenWithCardinalityElements(org.eclipse.xtext.services.XtextGrammarAccess.AbstractTokenWithCardinalityElements) XtextGrammarAccess(org.eclipse.xtext.services.XtextGrammarAccess) UnorderedGroupElements(org.eclipse.xtext.services.XtextGrammarAccess.UnorderedGroupElements) ActionElements(org.eclipse.xtext.services.XtextGrammarAccess.ActionElements) EnumLiteralDeclarationElements(org.eclipse.xtext.services.XtextGrammarAccess.EnumLiteralDeclarationElements) RuleCallElements(org.eclipse.xtext.services.XtextGrammarAccess.RuleCallElements) GrammarElements(org.eclipse.xtext.services.XtextGrammarAccess.GrammarElements) CrossReferenceElements(org.eclipse.xtext.services.XtextGrammarAccess.CrossReferenceElements) AssignmentElements(org.eclipse.xtext.services.XtextGrammarAccess.AssignmentElements) NegatedTokenElements(org.eclipse.xtext.services.XtextGrammarAccess.NegatedTokenElements) NegationElements(org.eclipse.xtext.services.XtextGrammarAccess.NegationElements) TerminalTokenElements(org.eclipse.xtext.services.XtextGrammarAccess.TerminalTokenElements) UntilTokenElements(org.eclipse.xtext.services.XtextGrammarAccess.UntilTokenElements)

Aggregations

Keyword (org.eclipse.xtext.Keyword)1 XtextGrammarAccess (org.eclipse.xtext.services.XtextGrammarAccess)1 AbstractTokenWithCardinalityElements (org.eclipse.xtext.services.XtextGrammarAccess.AbstractTokenWithCardinalityElements)1 ActionElements (org.eclipse.xtext.services.XtextGrammarAccess.ActionElements)1 AnnotationElements (org.eclipse.xtext.services.XtextGrammarAccess.AnnotationElements)1 AssignmentElements (org.eclipse.xtext.services.XtextGrammarAccess.AssignmentElements)1 CharacterRangeElements (org.eclipse.xtext.services.XtextGrammarAccess.CharacterRangeElements)1 CrossReferenceElements (org.eclipse.xtext.services.XtextGrammarAccess.CrossReferenceElements)1 EnumLiteralDeclarationElements (org.eclipse.xtext.services.XtextGrammarAccess.EnumLiteralDeclarationElements)1 GrammarElements (org.eclipse.xtext.services.XtextGrammarAccess.GrammarElements)1 NamedArgumentElements (org.eclipse.xtext.services.XtextGrammarAccess.NamedArgumentElements)1 NegatedTokenElements (org.eclipse.xtext.services.XtextGrammarAccess.NegatedTokenElements)1 NegationElements (org.eclipse.xtext.services.XtextGrammarAccess.NegationElements)1 ParserRuleElements (org.eclipse.xtext.services.XtextGrammarAccess.ParserRuleElements)1 RuleCallElements (org.eclipse.xtext.services.XtextGrammarAccess.RuleCallElements)1 TerminalTokenElements (org.eclipse.xtext.services.XtextGrammarAccess.TerminalTokenElements)1 TypeRefElements (org.eclipse.xtext.services.XtextGrammarAccess.TypeRefElements)1 UnorderedGroupElements (org.eclipse.xtext.services.XtextGrammarAccess.UnorderedGroupElements)1 UntilTokenElements (org.eclipse.xtext.services.XtextGrammarAccess.UntilTokenElements)1