Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » right place to add save actions?
right place to add save actions? [message #786926] Mon, 30 January 2012 19:23 Go to next message
Henrik Lindberg is currently offline Henrik Lindberg
Messages: 2499
Registered: July 2009
Senior Member
Hi,
I want to add a couple of save actions to my editor (to perform actions
like 'remove trailing whitespace', 'ensure file ends with a newline',
and 'replace funky whitespace characters with regular space' - to
mention a few).

What is the right approach to doing this. By default, it seems that the
IDE SaveAction ends up calling doSaveDocument(...) on
XtextDocumentProvider that is inherited from FileDocumentProvider.

Is it a good approach to override the document provider with my own,
implement doSaveDocument, and perform an update of the document using an
IUnitOfWork before passing on to the super doSaveDocument ?

Or is the right approach to save it twice? I.e. first save "as is", then
run a modification, and then trigger the save again?

Any pointers or bits of advice?

Regards
- henrik
Re: right place to add save actions? [message #787188 is a reply to message #786926] Tue, 31 January 2012 03:55 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2809
Registered: July 2009
Senior Member
Hi Henrik,

there is no need to save twice. I'd go with modifying the document's
content and calling super.doSave...

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 31.01.12 01:23, schrieb Henrik Lindberg:
> Hi,
> I want to add a couple of save actions to my editor (to perform actions
> like 'remove trailing whitespace', 'ensure file ends with a newline',
> and 'replace funky whitespace characters with regular space' - to
> mention a few).
>
> What is the right approach to doing this. By default, it seems that the
> IDE SaveAction ends up calling doSaveDocument(...) on
> XtextDocumentProvider that is inherited from FileDocumentProvider.
>
> Is it a good approach to override the document provider with my own,
> implement doSaveDocument, and perform an update of the document using an
> IUnitOfWork before passing on to the super doSaveDocument ?
>
> Or is the right approach to save it twice? I.e. first save "as is", then
> run a modification, and then trigger the save again?
>
> Any pointers or bits of advice?
>
> Regards
> - henrik
Re: right place to add save actions? [message #940606 is a reply to message #787188] Thu, 11 October 2012 16:10 Go to previous message
Michael Steiner is currently offline Michael Steiner
Messages: 3
Registered: April 2010
Junior Member
Hi,

I want to implement the save action "Format source code" (i.e. "Format on save"). Therefore, I call the Xtext format command in a custom XtextDocumentProvider:

public class MyDslDocumentProvider extends XtextDocumentProvider {
	private static final String XTEXT_FORMAT_ACTION_COMMAND_ID = "org.eclipse.xtext.ui.FormatAction";

	@Override
	protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite)
			throws CoreException {
		// auto-format
		IHandlerService service = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
		try {
			service.executeCommand(XTEXT_FORMAT_ACTION_COMMAND_ID, null);
		} catch (ExecutionException | NotDefinedException | NotEnabledException | NotHandledException e) {
			e.printStackTrace();
		}

		// save
		super.doSaveDocument(monitor, element, document, overwrite);
	}
}


Is this the right approach or is there a better way?

Regards,
Michael
Previous Topic:Conditional Statement Body
Next Topic:Xtext ecore cross references
Goto Forum:
  


Current Time: Wed Aug 20 05:03:11 EDT 2014

Powered by FUDForum. Page generated in 0.01831 seconds