Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Max line length Formatting
Max line length Formatting [message #1746360] Thu, 27 October 2016 13:24 Go to next message
Koen Staal is currently offline Koen StaalFriend
Messages: 70
Registered: October 2014
Member
How do i configure the formatter to use custom maximum line length?

The default seems to be stored in FormatterPreferenceKeys as 120. I tried to override the IPreferenceValuesProvider implementation and return the default value as 40. But this has no effect on line length when the formatting is called.
Re: Max line length Formatting [message #1746363 is a reply to message #1746360] Thu, 27 October 2016 13:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 10597
Registered: July 2009
Senior Member
How exactly does your binding and impl look like ?
Re: Max line length Formatting [message #1746412 is a reply to message #1746363] Fri, 28 October 2016 11:36 Go to previous messageGo to next message
Koen Staal is currently offline Koen StaalFriend
Messages: 70
Registered: October 2014
Member
The current exploratory implementation:

in MyDSLRuntimeModile

public void configureFormatterPreferences(com.google.inject.Binder binder) {
			binder.bind(org.eclipse.xtext.preferences.IPreferenceValuesProvider.class).annotatedWith(org.eclipse.xtext.formatting2.FormatterPreferences.class).to(nl.esi.poosl.xtext.formatting2.PooslFormatterPreferenceProvider.class);
		}


public class PooslFormatterPreferenceProvider implements IPreferenceValuesProvider {

	@Inject
	private IWhitespaceInformationProvider whitespaceInfo;

	@Inject
	private IPreferenceValuesProvider valuesProvider;

	@Override
	public IPreferenceValues getPreferenceValues(final Resource resource) {
		final IPreferenceValues preferenceValues = internalGetRawPreferenceValues(resource);
		final String indent = whitespaceInfo.getIndentationInformation(resource.getURI()).getIndentString();
		final String lineSep = whitespaceInfo.getLineSeparatorInformation(resource.getURI()).getLineSeparator();
		
		
		return new IPreferenceValues() {

			@Override
			public String getPreference(PreferenceKey key) {
				if (key == FormatterPreferenceKeys.indentation) {
					return indent;
				}
				if (key == FormatterPreferenceKeys.lineSeparator) {
					return lineSep;
				}				
				if (key == FormatterPreferenceKeys.maxLineWidth) {
					return "40";
				}
				return preferenceValues.getPreference(key);
			}

		};
	}

	protected IPreferenceValues internalGetRawPreferenceValues(final Resource resource) {
		return valuesProvider.getPreferenceValues(resource);
	}

}


The PooslFormatterPreferenceProvider is used by the AbstractFormatter2 but it doenst seem to have any effect.

Re: Max line length Formatting [message #1746413 is a reply to message #1746412] Fri, 28 October 2016 11:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 10597
Registered: July 2009
Senior Member
hmmm can you please file a ticket for that
Re: Max line length Formatting [message #1746414 is a reply to message #1746412] Fri, 28 October 2016 12:01 Go to previous messageGo to next message
Koen Staal is currently offline Koen StaalFriend
Messages: 70
Registered: October 2014
Member
Looking through the code the maxline length should be handled in FormattableDocument. createReplacements(ITextReplacerContext).

The ITextReplacerContext always returns null on canAutowrap() which seems to prevent xtext from applying the max line length.

Does this mean every Procedure1<IHiddenRegionFormatter> should also apply setautowrap


	final Procedure1<IHiddenRegionFormatter> oneSpace = new Procedure1<IHiddenRegionFormatter>() {
		@Override
		public void apply(final IHiddenRegionFormatter it) {			
			it.oneSpace();
			it.autowrap();
		}
	};


edit: After setting autowrap xtext seems to apply the max line length.

[Updated on: Fri, 28 October 2016 12:05]

Report message to a moderator

Re: Max line length Formatting [message #1746416 is a reply to message #1746414] Fri, 28 October 2016 12:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 10597
Registered: July 2009
Senior Member
hmmm sounds more like a feature to me @Moritz what do you think?
Re: Max line length Formatting [message #1746498 is a reply to message #1746416] Mon, 31 October 2016 08:51 Go to previous message
Moritz Eysholdt is currently offline Moritz EysholdtFriend
Messages: 160
Registered: July 2009
Location: Kiel, Germany
Senior Member
Hi Koen,

Quote:
Does this mean every Procedure1<IHiddenRegionFormatter> should also apply setautowrap


yes, exactly, it should.

The formatter only applies autowrap to HiddenRegions that are explicitly marked as such. Otherwise there are too many languages where autowrap would turn a document into a big mess.
If you need to have it enabled for all regions, you may get the first HiddenRegion of the document via

org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess.regionForRootEObject().getPreviousHiddenRegion()


and then iterate from HiddenRegion to HiddenRegion via
getNextHiddenRegion()


hth,
Moritz
Previous Topic:Embeded single line editor
Next Topic:Generate matlab script form arithmetic expression
Goto Forum:
  


Current Time: Tue May 30 11:19:58 GMT 2017

Powered by FUDForum. Page generated in 0.02699 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software