Search in sources :

Example 6 with FormatterRequest

use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-core by eclipse.

the class FormatterReplacementsTest method testUndefinedIdentityEditsAreNotFiltered.

@Test
public void testUndefinedIdentityEditsAreNotFiltered() {
    GenericFormatter<IDList> formatter = new GenericFormatter<IDList>() {

        @Override
        protected void format(IDList model, ITextRegionExtensions regions, IFormattableDocument document) {
            regions.regionFor(model).ruleCallsTo(formatterTestLanguageGrammarAccess.getIDRule()).forEach(r -> {
                document.prepend(r, it -> it.setSpace(""));
            });
        }
    };
    IDList model = fact.createIDList();
    model.getIds().add("a");
    Resource r = resFact.createResource(URI.createURI("foo.ext"));
    new XtextResourceSet().getResources().add(r);
    r.getContents().add(model);
    FormatterRequest request = requestProvider.get();
    request.setAllowIdentityEdits(false);
    request.setTextRegionAccess(serializer.serializeToRegions(model));
    List<ITextReplacement> replacements = formatter.format(request);
    String actual = Joiner.on("").join(Lists.transform(replacements, it -> ("'" + it.getText() + "' -> '" + it.getReplacementText() + "'\n")));
    String expected = "'' -> ''\n" + "'' -> ''\n" + "'' -> ''\n";
    Assert.assertEquals(expected, actual);
}
Also used : URI(org.eclipse.emf.common.util.URI) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) FormatterTestLanguageGrammarAccess(org.eclipse.xtext.formatting2.internal.services.FormatterTestLanguageGrammarAccess) Inject(com.google.inject.Inject) TextRegionAccessBuilder(org.eclipse.xtext.formatting2.regionaccess.TextRegionAccessBuilder) RunWith(org.junit.runner.RunWith) ParseHelper(org.eclipse.xtext.testing.util.ParseHelper) XtextRunner(org.eclipse.xtext.testing.XtextRunner) Lists(com.google.common.collect.Lists) FormattertestlanguageFactory(org.eclipse.xtext.formatting2.internal.formattertestlanguage.FormattertestlanguageFactory) XtextResource(org.eclipse.xtext.resource.XtextResource) IResourceFactory(org.eclipse.xtext.resource.IResourceFactory) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) Test(org.junit.Test) FormatterTestLanguageInjectorProvider(org.eclipse.xtext.formatting2.internal.tests.FormatterTestLanguageInjectorProvider) List(java.util.List) IDList(org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList) Provider(com.google.inject.Provider) Serializer(org.eclipse.xtext.serializer.impl.Serializer) InjectWith(org.eclipse.xtext.testing.InjectWith) Resource(org.eclipse.emf.ecore.resource.Resource) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) Assert(org.junit.Assert) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) Joiner(com.google.common.base.Joiner) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) IDList(org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) Test(org.junit.Test)

Example 7 with FormatterRequest

use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-core by eclipse.

the class FormatterReplacementsTest method testIdentityEditsAreFiltered.

@Test
public void testIdentityEditsAreFiltered() throws Exception {
    GenericFormatter<IDList> formatter = new GenericFormatter<IDList>() {

        @Override
        protected void format(IDList model, ITextRegionExtensions regions, IFormattableDocument document) {
            regions.regionFor(model).ruleCallsTo(formatterTestLanguageGrammarAccess.getIDRule()).forEach(r -> {
                document.prepend(r, it -> {
                    it.autowrap();
                    it.oneSpace();
                });
            });
        }
    };
    IDList parsed = parseHelper.parse("idlist  aaa bbb");
    FormatterRequest request = requestProvider.get();
    request.setAllowIdentityEdits(false);
    request.setTextRegionAccess(regionBuilder.forNodeModel((XtextResource) parsed.eResource()).create());
    List<ITextReplacement> replacements = formatter.format(request);
    String actual = Joiner.on("").join(Lists.transform(replacements, it -> ("'" + it.getText() + "' -> '" + it.getReplacementText() + "'\n")));
    String expected = "'  ' -> ' '\n";
    Assert.assertEquals(expected, actual);
}
Also used : URI(org.eclipse.emf.common.util.URI) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) FormatterTestLanguageGrammarAccess(org.eclipse.xtext.formatting2.internal.services.FormatterTestLanguageGrammarAccess) Inject(com.google.inject.Inject) TextRegionAccessBuilder(org.eclipse.xtext.formatting2.regionaccess.TextRegionAccessBuilder) RunWith(org.junit.runner.RunWith) ParseHelper(org.eclipse.xtext.testing.util.ParseHelper) XtextRunner(org.eclipse.xtext.testing.XtextRunner) Lists(com.google.common.collect.Lists) FormattertestlanguageFactory(org.eclipse.xtext.formatting2.internal.formattertestlanguage.FormattertestlanguageFactory) XtextResource(org.eclipse.xtext.resource.XtextResource) IResourceFactory(org.eclipse.xtext.resource.IResourceFactory) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) Test(org.junit.Test) FormatterTestLanguageInjectorProvider(org.eclipse.xtext.formatting2.internal.tests.FormatterTestLanguageInjectorProvider) List(java.util.List) IDList(org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList) Provider(com.google.inject.Provider) Serializer(org.eclipse.xtext.serializer.impl.Serializer) InjectWith(org.eclipse.xtext.testing.InjectWith) Resource(org.eclipse.emf.ecore.resource.Resource) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) Assert(org.junit.Assert) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) Joiner(com.google.common.base.Joiner) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) IFormattableDocument(org.eclipse.xtext.formatting2.IFormattableDocument) IDList(org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList) ITextRegionExtensions(org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest) Test(org.junit.Test)

Example 8 with FormatterRequest

use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-core by eclipse.

the class RegionDiffFormatter method createFormatterRequest.

protected FormatterRequest createFormatterRequest(ITextRegionAccess rewritten) {
    FormatterRequest request = formatterRequest.get();
    request.setTextRegionAccess(rewritten);
    return request;
}
Also used : FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 9 with FormatterRequest

use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-core by eclipse.

the class RegionDiffFormatter method createFormatterRequest.

protected FormatterRequest createFormatterRequest(ITextRegionAccessDiff rewritten) {
    FormatterRequest request = formatterRequest.get();
    request.setTextRegionAccess(rewritten);
    request.setRegions(collectRegionsToFormat(rewritten));
    return request;
}
Also used : FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Example 10 with FormatterRequest

use of org.eclipse.xtext.formatting2.FormatterRequest in project xtext-core by eclipse.

the class FormattingService method format2.

protected List<ITextReplacement> format2(XtextResource resource, ITextRegion selection, ITypedPreferenceValues preferences) {
    FormatterRequest request = formatterRequestProvider.get();
    request.setAllowIdentityEdits(false);
    request.setFormatUndefinedHiddenRegionsOnly(false);
    if (selection != null) {
        request.setRegions(Collections.singletonList(selection));
    }
    if (preferences != null) {
        request.setPreferences(preferences);
    }
    ITextRegionAccess regionAccess = regionBuilder.forNodeModel(resource).create();
    request.setTextRegionAccess(regionAccess);
    IFormatter2 formatter2 = formatter2Provider.get();
    List<ITextReplacement> replacements = formatter2.format(request);
    return replacements;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) IFormatter2(org.eclipse.xtext.formatting2.IFormatter2) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) FormatterRequest(org.eclipse.xtext.formatting2.FormatterRequest)

Aggregations

FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)17 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)13 XtextResource (org.eclipse.xtext.resource.XtextResource)7 IFormatter2 (org.eclipse.xtext.formatting2.IFormatter2)5 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)5 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)4 IDList (org.eclipse.xtext.formatting2.internal.formattertestlanguage.IDList)4 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)4 Test (org.junit.Test)4 Resource (org.eclipse.emf.ecore.resource.Resource)3 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)3 Joiner (com.google.common.base.Joiner)2 Lists (com.google.common.collect.Lists)2 Inject (com.google.inject.Inject)2 Provider (com.google.inject.Provider)2 List (java.util.List)2 URI (org.eclipse.emf.common.util.URI)2 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)2 IHiddenRegionFormatter (org.eclipse.xtext.formatting2.IHiddenRegionFormatter)2 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)2