Merge pull request #87 from eclipse/cd_bug482110

[482110] fixed AbstractFormatter2.shouldFormat
This commit is contained in:
Christian Dietrich 2016-08-18 16:00:34 +02:00 committed by GitHub
commit 95dd656cb6
3 changed files with 95 additions and 1 deletions

View file

@ -19,6 +19,9 @@ import org.junit.Test
import org.junit.runner.RunWith
import static org.eclipse.xtext.formatting2.FormatterPreferenceKeys.*
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions
import org.eclipse.xtext.formatting2.IFormattableDocument
/**
* @author Moritz Eysholdt - Initial contribution and API
@ -150,4 +153,38 @@ class FormattableDocumentTest {
expectation = '''!idlist!'''
]
}
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=482110
@Test def void shouldFormat() {
assertFormatted[
request.regions += new TextRegion(0, 6)
toBeFormatted = '''idlist'''
formatter = new GenericFormatter {
override protected format(EObject model, ITextRegionExtensions regionAccess, IFormattableDocument document) {
throw new IllegalStateException("this method should never be called")
}
override shouldFormat(Object obj, IFormattableDocument document) {
false
}
}
expectation = '''idlist'''
]
}
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=482110
@Test(expected=IllegalStateException) def void shouldFormat_02() {
assertFormatted[
request.regions += new TextRegion(0, 6)
toBeFormatted = '''idlist'''
formatter = new GenericFormatter {
override protected format(EObject model, ITextRegionExtensions regionAccess, IFormattableDocument document) {
throw new IllegalStateException("this method should never be called")
}
override shouldFormat(Object obj, IFormattableDocument document) {
true
}
}
expectation = '''idlist'''
]
}
}

View file

@ -11,6 +11,7 @@ import com.google.inject.Inject;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.TerminalRule;
import org.eclipse.xtext.formatting2.FormatterPreferenceKeys;
@ -329,4 +330,60 @@ public class FormattableDocumentTest {
};
this._genericFormatterTester.assertFormatted(_function);
}
@Test
public void shouldFormat() {
final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
FormatterRequest _request = it.getRequest();
Collection<ITextRegion> _regions = _request.getRegions();
TextRegion _textRegion = new TextRegion(0, 6);
_regions.add(_textRegion);
StringConcatenation _builder = new StringConcatenation();
_builder.append("idlist");
it.setToBeFormatted(_builder);
it.setFormatter(new GenericFormatter() {
@Override
protected void format(final EObject model, final ITextRegionExtensions regionAccess, final IFormattableDocument document) {
throw new IllegalStateException("this method should never be called");
}
@Override
public boolean shouldFormat(final Object obj, final IFormattableDocument document) {
return false;
}
});
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("idlist");
it.setExpectation(_builder_1);
};
this._genericFormatterTester.assertFormatted(_function);
}
@Test(expected = IllegalStateException.class)
public void shouldFormat_02() {
final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
FormatterRequest _request = it.getRequest();
Collection<ITextRegion> _regions = _request.getRegions();
TextRegion _textRegion = new TextRegion(0, 6);
_regions.add(_textRegion);
StringConcatenation _builder = new StringConcatenation();
_builder.append("idlist");
it.setToBeFormatted(_builder);
it.setFormatter(new GenericFormatter() {
@Override
protected void format(final EObject model, final ITextRegionExtensions regionAccess, final IFormattableDocument document) {
throw new IllegalStateException("this method should never be called");
}
@Override
public boolean shouldFormat(final Object obj, final IFormattableDocument document) {
return true;
}
});
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("idlist");
it.setExpectation(_builder_1);
};
this._genericFormatterTester.assertFormatted(_function);
}
}

View file

@ -199,7 +199,7 @@ public abstract class AbstractFormatter2 implements IFormatter2 {
List<EObject> contents = resource.getContents();
if (!contents.isEmpty()) {
EObject model = contents.get(0);
format(model, document);
document.format(model);
}
}