Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Format on Save in Editor
Format on Save in Editor [message #1763961] Tue, 23 May 2017 12:11 Go to next message
Koen Staal is currently offline Koen StaalFriend
Messages: 70
Registered: October 2014
Member
Hi, i want to format the file when it is saved in the editor.

The current implementation re-uses the default xtext-eclipse formatting but it has some unintended behavior. It will only format the selected text if there is text select.
We want the whole file formatted even when there is text selected.

public class DslEditor extends XtextEditor {
	
	@Override
	public void doSave(IProgressMonitor progressMonitor) {
		ITextOperationTarget target = getSourceViewer().getTextOperationTarget();
		if(target.canDoOperation(ISourceViewer.FORMAT) ) {
			target.doOperation(ISourceViewer.FORMAT);
		}		
		super.doSave(progressMonitor);		
	}


Is there a solution possible where i dont need to change the default formatting implementation in the TextOperationTarget?
Re: Format on Save in Editor [message #1764003 is a reply to message #1763961] Tue, 23 May 2017 20:54 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
simply setting the selection does not work?

sourceViewer.setSelectedRange(offset, length);
sourceViewer.getTextOperationTarget().doOperation(ISourceViewer.FORMAT);


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Format on Save in Editor [message #1764058 is a reply to message #1764003] Wed, 24 May 2017 13:12 Go to previous message
Koen Staal is currently offline Koen StaalFriend
Messages: 70
Registered: October 2014
Member
Thanks for your suggestion. With manipulating the selection we can indeed format the whole file.

Since we dont really want to change the selection we have to put the selection back after the format. The file can be changed dramatically, so the selection might not even work anymore after the format and we need to check/change it.

This is the current code but i am not sure yet if we will support this feature :).

	@Override
	public void doSave(IProgressMonitor progressMonitor) {
		ITextOperationTarget target = getSourceViewer().getTextOperationTarget();
		if(target.canDoOperation(ISourceViewer.FORMAT) ) {
			Point selection = getSourceViewer().getSelectedRange();			
			getSourceViewer().setSelectedRange(0, getDocument().getLength());			
			target.doOperation(ISourceViewer.FORMAT);			
						
			int max = getDocument().getLength();			
			if(max <= selection.x) {
				getSourceViewer().setSelectedRange(max, 0);
			} else {
				int selectRange =  (max >= selection.x + selection.y) ? selection.y : max - selection.x;
				getSourceViewer().setSelectedRange(selection.x, selectRange);
			}			
		}		
		super.doSave(progressMonitor);		
	}
Previous Topic:Xtext : Unicity element
Next Topic:Making change to DotExpression example
Goto Forum:
  


Current Time: Mon Nov 04 20:41:37 GMT 2024

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

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

Back to the top