Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Access to Resource URI within QuickfixProvider(Quick Fix and access to name of Resource)
Access to Resource URI within QuickfixProvider [message #1005472] Fri, 25 January 2013 10:17 Go to next message
Michael Colburn is currently offline Michael Colburn
Messages: 8
Registered: November 2012
Junior Member
For the grammar I am working on, we have an initial rule that has the keyword 'Template' and name= QualifiedName. We want to ensure that the name of the Template is identical to the filename of the resource containing the Template, but without the file extension. I am able to check this via a JavaValidator by accessing eResource. But, I wish to have a quick fix that would change the name of the Template to match the resource filename (without the file extension). Within the JavaValidator, it is possible to access eResource, get the URI, and through the URI to get the filename. But, this does not seem possible within the QuickfixProvider.

Is there a way to gain access to the eResource of the resource being fixed from within QuickfixProvider, e.g. through an injector?

Thank you in advance!
Re: Access to Resource URI within QuickfixProvider [message #1005483 is a reply to message #1005472] Fri, 25 January 2013 10:57 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26000
Registered: July 2009
Senior Member
Michael,

It should be pretty straight forward from the context, e.g.,

@Fix(EcoreValidator.DIAGNOSTIC_SOURCE + '.' +
EcoreValidator.CONSISTENT_TYPE_CLASS_NOT_PERMITTED)
public void convertToReference(final Issue issue, final
IssueResolutionAcceptor acceptor)
{
IModificationContext modificationContext =
getModificationContextFactory().createModificationContext(issue);
final IXtextDocument xtextDocument =
modificationContext.getXtextDocument();
xtextDocument.readOnly
(new IUnitOfWork.Void<XtextResource>()
{
@Override
public void process(XtextResource xtextResource) throws Exception
{


On 25/01/2013 4:17 PM, Michael Colburn wrote:
> For the grammar I am working on, we have an initial rule that has the
> keyword 'Template' and name= QualifiedName. We want to ensure that
> the name of the Template is identical to the filename of the resource
> containing the Template, but without the file extension. I am able to
> check this via a JavaValidator by accessing eResource. But, I wish to
> have a quick fix that would change the name of the Template to match
> the resource filename (without the file extension). Within the
> JavaValidator, it is possible to access eResource, get the URI, and
> through the URI to get the filename. But, this does not seem possible
> within the QuickfixProvider.
>
> Is there a way to gain access to the eResource of the resource being
> fixed from within QuickfixProvider, e.g. through an injector?
> Thank you in advance!
Re: Access to Resource URI within QuickfixProvider [message #1005496 is a reply to message #1005483] Fri, 25 January 2013 12:07 Go to previous messageGo to next message
Michael Colburn is currently offline Michael Colburn
Messages: 8
Registered: November 2012
Junior Member
Ed,

Thanks for the quick response. I am attempting to do as you suggested, but can't get it to work.

Here is my method within the QuickfixProvider:

	
@Fix(WfdslJavaValidator.INVALID_TEMPLATE_NAME)
public void fixTemplateName(final Issue issue, IssueResolutionAcceptor acceptor) {
	acceptor.accept(issue, "Rename template.", 
            "Change template name to match filename.", null, new IModification() {
	IModificationContext modificationContext =
				 
                   getModificationContextFactory().createModificationContext(issue);
			public void apply(IModificationContext context) 
                            throws BadLocationException {
				IXtextDocument xtextDocument = 
                                   context.getXtextDocument();
				getFilename(xtextDocument);
				xtextDocument.replace(issue.getOffset(), 
                             issue.getLength(), issue.getData()[0]);
			}
		});
	}


Note that I found a workaround to my problem, which was to pass in the filename via the data parameter in the error method called in the JavaValidator and retrieve the value via issue.getData()[0].

But, I am interested in learning how to gain access to the resource from within the QuickfixProvider using the approach you provided.

Here is what I have tried:

	
@Fix(WfdslJavaValidator.INVALID_TEMPLATE_NAME)
public void fixTemplateName(final Issue issue, IssueResolutionAcceptor acceptor) {
		acceptor.accept(issue, "Rename template.", 
                       "Change template name to match filename.", 
                        null, new IModification() {
	      IModificationContext modificationContext =
					
                 getModificationContextFactory().createModificationContext(issue);

			public void apply(IModificationContext context) 
                           throws BadLocationException {
				IXtextDocument xtextDocument = 
                                  context.getXtextDocument();
				xtextDocument.replace(
                                    issue.getOffset(), 
                                    issue.getLength(), 
                                    getFilename(xtextDocument));
			}
		});
	}
    private String getFilename(IXtextDocument xtextDocument) {
    	String filename = "";
	    xtextDocument.readOnly(new IUnitOfWork.Void<XtextResource>() {
			@Override
			public void process(XtextResource state) throws Exception {
                                // here is where I should be able to 
                                // set the filename.
                                // I used a println just to show that myself 
                                // that I can access the resource URI here.
				System.out.println(state.getURI().lastSegment());
			}
	    });
	    return filename;
   }


In order for this to work, it would be necessary to set filename from within the process method. But this does not seem possible since filename is not visible within the process method and the return type of the process method is void. Since it overrides an abstract method 'process', I can't change the return type.

I am assuming that I am incorrect in how I am attempting to apply your proposed solution.

Thanks in advance!
Re: Access to Resource URI within QuickfixProvider [message #1005534 is a reply to message #1005496] Sat, 26 January 2013 03:24 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 26000
Registered: July 2009
Senior Member
Michael,

The IUnitOfWork.Void is a specialized interface for IUnitOfWork which
hides the use the Void, the box type for void. You can use IUnitOfWork
directly

public interface IUnitOfWork<R,P> {
/**
* @param state - The state, which is synchronized while this
method is executed
* <b>Do not return any references to something contained in this
resource</b>
* @return
* @throws Exception
*/
R exec(P state) throws Exception;



On 25/01/2013 6:07 PM, Michael Colburn wrote:
> Ed,
>
> Thanks for the quick response. I am attempting to do as you
> suggested, but can't get it to work.
>
> Here is my method within the QuickfixProvider:
>
>
> @Fix(WfdslJavaValidator.INVALID_TEMPLATE_NAME)
> public void fixTemplateName(final Issue issue, IssueResolutionAcceptor
> acceptor) {
> acceptor.accept(issue, "Rename template.", "Change
> template name to match filename.", null, new IModification() {
> IModificationContext modificationContext =
> getModificationContextFactory().createModificationContext(issue);
> public void apply(IModificationContext context)
> throws BadLocationException {
> IXtextDocument xtextDocument =
> context.getXtextDocument();
> getFilename(xtextDocument);
> xtextDocument.replace(issue.getOffset(),
> issue.getLength(), issue.getData()[0]);
> }
> });
> }
>
> Note that I found a workaround to my problem, which was to pass in the
> filename via the data parameter in the error method called in the
> JavaValidator and retrieve the value via issue.getData()[0].
> But, I am interested in learning how to gain access to the resource
> from within the QuickfixProvider using the approach you provided.
>
> Here is what I have tried:
>
>
> @Fix(WfdslJavaValidator.INVALID_TEMPLATE_NAME)
> public void fixTemplateName(final Issue issue, IssueResolutionAcceptor
> acceptor) {
> acceptor.accept(issue, "Rename template.",
> "Change template name to match filename.",
> null, new IModification() {
> IModificationContext modificationContext =
>
> getModificationContextFactory().createModificationContext(issue);
>
> public void apply(IModificationContext context)
> throws BadLocationException {
> IXtextDocument xtextDocument =
> context.getXtextDocument();
> xtextDocument.replace(
> issue.getOffset(),
> issue.getLength(),
> getFilename(xtextDocument));
> }
> });
> }
> private String getFilename(IXtextDocument xtextDocument) {
> String filename = "";
> xtextDocument.readOnly(new IUnitOfWork.Void<XtextResource>() {
> @Override
> public void process(XtextResource state) throws Exception {
> // here is where I should be able to
> // set the filename.
> // I used a println just to show that
> myself // that I can access the
> resource URI here.
> System.out.println(state.getURI().lastSegment());
> }
> });
> return filename;
> }
>
>
> In order for this to work, it would be necessary to set filename from
> within the process method. But this does not seem possible since
> filename is not visible within the process method and the return type
> of the process method is void. Since it overrides an abstract method
> 'process', I can't change the return type.
>
> I am assuming that I am incorrect in how I am attempting to apply your
> proposed solution.
>
> Thanks in advance!
Previous Topic:Serialization/Formatter Problem
Next Topic:How to obtain the Eclipse automatically-updated EMF model?
Goto Forum:
  


Current Time: Fri Aug 22 11:47:47 EDT 2014

Powered by FUDForum. Page generated in 0.01820 seconds