Skip to main content


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] Tue, 31 January 2012 00:23 Go to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
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 08:55 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
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 20:10 Go to previous messageGo to next message
Michael Steiner is currently offline Michael SteinerFriend
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
Re: right place to add save actions? [message #1412967 is a reply to message #940606] Wed, 27 August 2014 12:57 Go to previous message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
The above made my day, thanks Smile
Previous Topic:Code Formatter and comments.
Next Topic:Non-empty model in the beginning
Goto Forum:
  


Current Time: Thu Apr 25 14:11:21 GMT 2024

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

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

Back to the top