org.eclipse.xtext.formatting2.FormatterPreferenceKeys Java Examples

The following examples show how to use org.eclipse.xtext.formatting2.FormatterPreferenceKeys. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractXtendFormatterTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public void assertFormatted(final Procedure1<? super MapBasedPreferenceValues> cfg, final CharSequence expectation, final CharSequence toBeFormatted, final String prefix, final String postfix, final boolean allowErrors) {
  final Procedure1<FormatterTestRequest> _function = (FormatterTestRequest it) -> {
    final Procedure1<MapBasedPreferenceValues> _function_1 = (MapBasedPreferenceValues it_1) -> {
      it_1.<Integer>put(FormatterPreferenceKeys.maxLineWidth, Integer.valueOf(80));
      it_1.<Boolean>put(XtendFormatterPreferenceKeys.keepOneLineMethods, Boolean.valueOf(false));
      if (cfg!=null) {
        cfg.apply(it_1);
      }
    };
    it.preferences(_function_1);
    it.setExpectation(((prefix + expectation) + postfix));
    it.setToBeFormatted(((prefix + toBeFormatted) + postfix));
    Collection<ITextRegion> _regions = it.getRequest().getRegions();
    int _length = prefix.length();
    int _length_1 = toBeFormatted.length();
    TextRegion _textRegion = new TextRegion(_length, _length_1);
    _regions.add(_textRegion);
    it.setAllowSyntaxErrors(allowErrors);
  };
  this.tester.assertFormatted(_function);
}
 
Example #2
Source File: XbaseFormatterTester.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public void assertFormattedExpression(Procedure1<? super FormatterTestRequest> test) {
	String prefix = "{" + Strings.newLine();
	String postfix = Strings.newLine() + "}";
	assertFormatted((FormatterTestRequest it) -> {
		test.apply(it);
		it.preferences((MapBasedPreferenceValues prefs) -> {
			prefs.put(FormatterPreferenceKeys.maxLineWidth, 80);
		});
		it.setExpectation(prefix + indent(it.getExpectationOrToBeFormatted().toString().trim(), "\t") + postfix);
		it.setToBeFormatted(prefix + indent(it.getToBeFormatted().toString().trim(), "\t") + postfix);
	});
}
 
Example #3
Source File: XtendFormatterBugTests.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void bug462628() {
  final Procedure1<FormatterTestRequest> _function = (FormatterTestRequest it) -> {
    final Procedure1<MapBasedPreferenceValues> _function_1 = (MapBasedPreferenceValues it_1) -> {
      it_1.<Integer>put(FormatterPreferenceKeys.maxLineWidth, Integer.valueOf(120));
    };
    it.preferences(_function_1);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def void format() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("mmmmmmmmmmmmmmmcontainsBlockExprmmmmmmmexprcasesemptymmmmexprmmdefaultmmmmmmmm &&");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("mexprmcasesmexists[multiline] && mexprmmdefaultmmultilineOrInNewLine");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    it.setToBeFormatted(_builder);
  };
  this.tester.assertFormatted(_function);
}
 
Example #4
Source File: XtendFormatterBugTests.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void bug403823_1() {
  final Procedure1<FormatterTestRequest> _function = (FormatterTestRequest it) -> {
    final Procedure1<MapBasedPreferenceValues> _function_1 = (MapBasedPreferenceValues it_1) -> {
      it_1.<Integer>put(FormatterPreferenceKeys.maxLineWidth, Integer.valueOf(120));
    };
    it.preferences(_function_1);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def void format(String a, String b, String c) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (a != b) ");
    _builder.append("\'", "\t\t");
    _builder.append("\'", "\t\t");
    _builder.append("\'", "\t\t");
    _builder.append("(");
    _builder.append("�", "\t\t");
    _builder.append("c");
    _builder.append("�", "\t\t");
    _builder.append(")");
    _builder.append("\'", "\t\t");
    _builder.append("\'", "\t\t");
    _builder.append("\'", "\t\t");
    _builder.append(" else \'\'");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    it.setToBeFormatted(_builder);
  };
  this.tester.assertFormatted(_function);
}
 
Example #5
Source File: XtendFormatterBugTests.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void bug403340_1() {
  final Procedure1<FormatterTestRequest> _function = (FormatterTestRequest it) -> {
    final Procedure1<MapBasedPreferenceValues> _function_1 = (MapBasedPreferenceValues it_1) -> {
      it_1.<Integer>put(FormatterPreferenceKeys.maxLineWidth, Integer.valueOf(120));
    };
    it.preferences(_function_1);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def void format(String v1, String v2) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (v1 === v2) ");
    _builder.append("\'", "\t\t");
    _builder.append("\'", "\t\t");
    _builder.append("\'", "\t\t");
    _builder.append("Same");
    _builder.append("\'", "\t\t");
    _builder.append("\'", "\t\t");
    _builder.append("\'", "\t\t");
    _builder.append(" else ");
    _builder.append("\'", "\t\t");
    _builder.append("\'", "\t\t");
    _builder.append("\'", "\t\t");
    _builder.append("Not the Same");
    _builder.append("\'", "\t\t");
    _builder.append("\'", "\t\t");
    _builder.append("\'", "\t\t");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    it.setToBeFormatted(_builder);
  };
  this.tester.assertFormatted(_function);
}
 
Example #6
Source File: FormatterTest.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * This example shows how to test property-dependent formatting behavior.
 */
@Test
public void example3() {
  final Procedure1<FormatterTestRequest> _function = (FormatterTestRequest it) -> {
    final Procedure1<MapBasedPreferenceValues> _function_1 = (MapBasedPreferenceValues it_1) -> {
      it_1.<String>put(FormatterPreferenceKeys.indentation, " ");
    };
    it.preferences(_function_1);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("entity Foo {");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("op foo():String {");
    _builder.newLine();
    _builder.append("  ");
    _builder.append("\"xx\"");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    it.setExpectation(_builder);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("entity Foo {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("op foo():String {");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.append("\"xx\"");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    it.setToBeFormatted(_builder_1);
  };
  this._formatterTestHelper.assertFormatted(_function);
}
 
Example #7
Source File: XtendFormatterBugTests.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testtestCoreIssue527_02() {
  final Procedure1<FormatterTestRequest> _function = (FormatterTestRequest it) -> {
    final Procedure1<MapBasedPreferenceValues> _function_1 = (MapBasedPreferenceValues it_1) -> {
      it_1.<Integer>put(FormatterPreferenceKeys.maxLineWidth, Integer.valueOf(80));
    };
    it.preferences(_function_1);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class ModuleDeclaration {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public List<Object> moduleComponents");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class AbstractTypeDeclaration {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class AliasDeclaration extends AbstractTypeDeclaration {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class JavaGenerator {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def void generateCommon(String projectSourceRootPath,");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("ModuleDeclaration module) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("for (element : module.moduleComponents.filter(AbstractTypeDeclaration).");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("filter [ e |");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("!(e instanceof AliasDeclaration)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("]) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    it.setToBeFormatted(_builder);
  };
  this.tester.assertFormatted(_function);
}
 
Example #8
Source File: XtendFormatterBugTests.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void bug403823() {
  final Procedure1<FormatterTestRequest> _function = (FormatterTestRequest it) -> {
    final Procedure1<MapBasedPreferenceValues> _function_1 = (MapBasedPreferenceValues it_1) -> {
      it_1.<Integer>put(FormatterPreferenceKeys.maxLineWidth, Integer.valueOf(120));
    };
    it.preferences(_function_1);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def void format(String a, String b, String c) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (a != b)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.append("(");
    _builder.append("�", "\t\t\t");
    _builder.append("c");
    _builder.append("�", "\t\t\t");
    _builder.append(")");
    _builder.append("\'", "\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("else");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("\'\'");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    it.setToBeFormatted(_builder);
  };
  this.tester.assertFormatted(_function);
}
 
Example #9
Source File: XtendFormatterBugTests.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void bug403340() {
  final Procedure1<FormatterTestRequest> _function = (FormatterTestRequest it) -> {
    final Procedure1<MapBasedPreferenceValues> _function_1 = (MapBasedPreferenceValues it_1) -> {
      it_1.<Integer>put(FormatterPreferenceKeys.maxLineWidth, Integer.valueOf(120));
    };
    it.preferences(_function_1);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def void format(String v1, String v2) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (v1 === v2)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.append("Same");
    _builder.append("\'", "\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.append("else");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.append("Not the Same");
    _builder.append("\'", "\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.append("\'", "\t\t\t");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    it.setToBeFormatted(_builder);
  };
  this.tester.assertFormatted(_function);
}
 
Example #10
Source File: RichStringFormatter.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected void _format(final RichString richString, final IFormattableDocument doc) {
  RichString _containerOfType = EcoreUtil2.<RichString>getContainerOfType(richString.eContainer(), RichString.class);
  boolean _tripleNotEquals = (_containerOfType != null);
  if (_tripleNotEquals) {
    return;
  }
  boolean _hasSyntaxError = this.hasSyntaxError(this._iTextRegionExtensions.regionForEObject(richString));
  if (_hasSyntaxError) {
    return;
  }
  ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
  final RichStringToLineModel impl = new RichStringToLineModel(_textRegionAccess, richString);
  DefaultIndentationHandler _defaultIndentationHandler = new DefaultIndentationHandler();
  this.factory.richStringProcessor.process(richString, impl, _defaultIndentationHandler);
  impl.finish();
  EList<XExpression> _expressions = richString.getExpressions();
  for (final XExpression e : _expressions) {
    this.format(e, doc);
  }
  final List<Line> lines = impl.getModel().getLines();
  final boolean canIndent = ((!lines.isEmpty()) && StringExtensions.isNullOrEmpty(IterableExtensions.<Line>last(lines).getContent()));
  for (final Line line : lines) {
    int _rootIndentLenght = impl.getModel().getRootIndentLenght();
    boolean _greaterThan = (_rootIndentLenght > 0);
    if (_greaterThan) {
      int _xifexpression = (int) 0;
      if ((canIndent && Objects.equal(line, IterableExtensions.<Line>head(lines)))) {
        _xifexpression = 1;
      } else {
        _xifexpression = 0;
      }
      final int increaseIndentationChange = _xifexpression;
      int _xifexpression_1 = (int) 0;
      if ((canIndent && Objects.equal(line, IterableExtensions.<Line>last(lines)))) {
        _xifexpression_1 = 1;
      } else {
        _xifexpression_1 = 0;
      }
      final int decraseIndentationChange = _xifexpression_1;
      int _xifexpression_2 = (int) 0;
      boolean _isLeadingSemanticNewLine = line.isLeadingSemanticNewLine();
      if (_isLeadingSemanticNewLine) {
        int _offset = line.getOffset();
        int _newLineCharCount = line.getNewLineCharCount();
        _xifexpression_2 = (_offset + _newLineCharCount);
      } else {
        _xifexpression_2 = line.getOffset();
      }
      final int nloffset = _xifexpression_2;
      final int i = Math.min(line.getIndentLength(), impl.getModel().getRootIndentLenght());
      int _xifexpression_3 = (int) 0;
      boolean _isLeadingSemanticNewLine_1 = line.isLeadingSemanticNewLine();
      if (_isLeadingSemanticNewLine_1) {
        _xifexpression_3 = i;
      } else {
        int _newLineCharCount_1 = line.getNewLineCharCount();
        _xifexpression_3 = (_newLineCharCount_1 + i);
      }
      final int nllength = _xifexpression_3;
      boolean _isLeadingSemanticNewLine_2 = line.isLeadingSemanticNewLine();
      if (_isLeadingSemanticNewLine_2) {
        this.setNewLines(doc, nloffset, nllength, increaseIndentationChange, decraseIndentationChange, 0);
      } else {
        this.setNewLines(doc, nloffset, nllength, increaseIndentationChange, decraseIndentationChange, 1);
      }
      boolean _isEmpty = line.getChunks().isEmpty();
      boolean _not = (!_isEmpty);
      if (_not) {
        final int offset = (nloffset + nllength);
        int _indentLength = line.getIndentLength();
        int _rootIndentLenght_1 = impl.getModel().getRootIndentLenght();
        final int length = (_indentLength - _rootIndentLenght_1);
        final Function1<Chunk, CharSequence> _function = (Chunk chunk) -> {
          CharSequence _switchResult = null;
          boolean _matched = false;
          if (chunk instanceof SemanticWhitespace) {
            _matched=true;
            _switchResult = ((SemanticWhitespace)chunk).getText();
          }
          if (!_matched) {
            if (chunk instanceof TemplateWhitespace) {
              _matched=true;
              _switchResult = doc.getFormatter().<String>getPreference(FormatterPreferenceKeys.indentation);
            }
          }
          return _switchResult;
        };
        final String text = IterableExtensions.join(ListExtensions.<Chunk, CharSequence>map(line.getChunks(), _function));
        if ((length >= 0)) {
          this.setSpace(doc, offset, length, text);
        } else {
          RuntimeException _runtimeException = new RuntimeException(("Programmatic error: length == " + Integer.valueOf(length)));
          EcorePlugin.INSTANCE.log(_runtimeException);
        }
      }
    }
  }
}
 
Example #11
Source File: SubDocument.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public IFormattableSubDocument requireFitsInLine(int offset, int length) {
	Integer maxLineWidth = getRequest().getPreferences().getPreference(FormatterPreferenceKeys.maxLineWidth);
	return requireFitsInLine(offset, length, maxLineWidth);
}
 
Example #12
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected ITextReplacerContext createReplacements(ITextReplacerContext previous) {
	Integer maxLineWidth = getRequest().getPreferences().getPreference(FormatterPreferenceKeys.maxLineWidth);
	ITextReplacerContext context = previous.withDocument(this);
	ITextReplacerContext wrappable = null;
	Set<ITextReplacer> wrapped = Sets.newHashSet();
	Iterator<ITextReplacer> replacers = getReplacers().iterator();
	while (replacers.hasNext()) {
		ITextReplacer replacer = replacers.next();
		context = context.withReplacer(replacer);
		if (wrappable != null && context.isWrapSincePrevious()) {
			wrappable = null;
		}
		if (wrappable != null && needsAutowrap(wrappable, context, maxLineWidth)) {
			// TODO: raise report if replacer claims it can do autowrap but
			// then doesn't
			while (context != wrappable) {
				context = context.getPreviousContext();
			}
			replacer = context.getReplacer();
			replacers = getReplacers().iteratorAfter(replacer);
			context.setAutowrap(true);
			wrappable = null;
		}
		ITextReplacerContext nextContext = replacer.createReplacements(context);
		if (wrappable != null && context.isWrapInRegion()) {
			wrappable = null;
		} else {
			Integer canAutowrap = context.canAutowrap();
			if (canAutowrap != null && canAutowrap >= 0 && !context.isAutowrap() && !wrapped.contains(replacer)) {
				boolean can = true;
				if (wrappable != null) {
					int lastEndOffset = wrappable.canAutowrap()
							+ wrappable.getReplacer().getRegion().getEndOffset();
					int thisEndOffset = canAutowrap + context.getReplacer().getRegion().getEndOffset();
					can = lastEndOffset < thisEndOffset;
				}
				if (can) {
					wrappable = context;
					wrapped.add(replacer);
				}
			}
		}
		context = nextContext;
	}
	return context.withDocument(previous.getDocument());
}
 
Example #13
Source File: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public String getIndentationString(int indentationLevel) {
	AbstractFormatter2 formatter = document.getFormatter();
	return Strings.repeat(formatter.getPreference(FormatterPreferenceKeys.indentation), indentationLevel);
}
 
Example #14
Source File: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public String getNewLinesString(int count) {
	return Strings.repeat(document.getFormatter().getPreference(FormatterPreferenceKeys.lineSeparator), count);
}
 
Example #15
Source File: AbstractSARLRuntimeModule.java    From sarl with Apache License 2.0 4 votes vote down vote up
public Class<? extends FormatterPreferenceKeys> bindFormatterPreferenceKeys() {
	return SARLFormatterPreferenceKeys.class;
}