Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #795 from eclipse/cd_formatterExperiments
Browse files Browse the repository at this point in the history
change baseclass for xbase formatter
  • Loading branch information
cdietrich authored Aug 15, 2022
2 parents 51db579 + 038fb0d commit fceed55
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 349 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Workflow {
}
formatter = {
generateStub = true
generateXtendStub = true
generateXtendStub = false
}
junitSupport = {
generateXtendStub = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2017, 2022 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.purexbase.formatting2;

import org.eclipse.xtext.formatting2.IFormattableDocument;
import org.eclipse.xtext.purexbase.pureXbase.Model;
import org.eclipse.xtext.purexbase.services.PureXbaseGrammarAccess;
import org.eclipse.xtext.xbase.XBlockExpression;
import org.eclipse.xtext.xbase.XExpression;
import org.eclipse.xtext.xbase.formatting2.XbaseFormatter;

import com.google.inject.Inject;

public class PureXbaseFormatter extends XbaseFormatter {
@Inject
private PureXbaseGrammarAccess pureXbaseGrammarAccess;

protected void _format(Model model, IFormattableDocument document) {
document.format(model.getImportSection());
document.format(model.getBlock());
}

@Override
protected void _format(XBlockExpression xBlockExpression, IFormattableDocument document) {
regionFor(xBlockExpression)
.keywords(pureXbaseGrammarAccess.getSpecialBlockExpressionAccess().getSemicolonKeyword_1_1())
.forEach(it -> {
document.append(it, this::newLine);
});
for (XExpression xExpression : xBlockExpression.getExpressions()) {
document.format(xExpression);
}
}

@Override
public void format(Object expr, IFormattableDocument format) {
formatUsingPolymorphicDispatcher(expr, format);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*******************************************************************************
* Copyright (c) 2022 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.xbase.tests.formatting;

import org.eclipse.xtext.formatting2.IFormattableDocument;
import org.eclipse.xtext.testing.InjectWith;
import org.eclipse.xtext.testing.XtextRunner;
import org.eclipse.xtext.testing.formatter.FormatterTestRequest;
import org.eclipse.xtext.xbase.XPostfixOperation;
import org.eclipse.xtext.xbase.XbasePackage;
import org.eclipse.xtext.xbase.formatting2.XbaseFormatter;
import org.eclipse.xtext.xbase.tests.formatting.XbaseFormatterJavaTest.XbaseFormatterJavaTestInjectorProvider;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.inject.Guice;
import com.google.inject.Injector;

/**
* @author cdietrich - Initial contribution and API
*/
@RunWith(XtextRunner.class)
@InjectWith(XbaseFormatterJavaTestInjectorProvider.class)
public class XbaseFormatterJavaTest extends XbaseFormatterTest {

public static class XbaseFormatterJavaTestInjectorProvider extends XbaseFormatterTestInjectorProvider {
@Override
protected Injector internalCreateInjector() {
return new FunctionTypeRefAwareTestStandaloneSetup() {
@Override
public Injector createInjector() {
return Guice.createInjector(new XbaseTestRuntimeModule() {
@Override
public void configure(com.google.inject.Binder binder) {
super.configure(binder);
binder.bind(XbaseFormatter.class).to(XbaseFormatterJava.class);
}
});
}
}.createInjectorAndDoEMFRegistration();
}
}

public static class XbaseFormatterJava extends XbaseFormatter {
@Override
public void format(Object expr, IFormattableDocument format) {
formatUsingPolymorphicDispatcher(expr, format);
}

@Override
protected void _format(XPostfixOperation expr, IFormattableDocument doc) {
doc.prepend(regionFor(expr).feature(XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE), this::oneSpace);
doc.format(expr.getOperand());
}
}

@Override
@Test
public void formatPostfix() {
xbaseFormatterTester.assertFormattedExpression((FormatterTestRequest it) -> {
String expectation =
"val i = j ++\n";
it.setExpectation(expectation);
String toBeFormatted =
"val i = j++\n";
it.setToBeFormatted(toBeFormatted);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@InjectWith(XbaseFormatterTestInjectorProvider.class)
public class XbaseFormatterTest {
@Inject
private XbaseFormatterTester xbaseFormatterTester;
protected XbaseFormatterTester xbaseFormatterTester;

@Test
public void formatGenerics() {
Expand Down
Loading

0 comments on commit fceed55

Please sign in to comment.