Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Problem with quick fixes(define a quick fixes by myself)
Problem with quick fixes [message #881746] Tue, 05 June 2012 07:33 Go to next message
frank fotso is currently offline frank fotsoFriend
Messages: 91
Registered: May 2012
Member
hi,i want to use the feature (quick fixes) of xtext, and for that i wanted to do a simple example of putting a warning when a specific name doesn't start with a capital letter. this is the code of

- the javavalidator class:

  package org.xtext.example.validation;

import org.eclipse.xtext.validation.Check;
import org.xtext.example.project2.EEPROM_DEVICESTYPE;
import org.xtext.example.project2.Project2Package;
 

public class Project2JavaValidator extends AbstractProject2JavaValidator {

	//@Check
//public void checkNameStartsWithCapital(EEPROM_DEVICESTYPE  eEPROM_DEVICESTYPE) {
//		if (eEPROM_DEVICESTYPE.getName() == null || eEPROM_DEVICESTYPE.getName().length() == 0)return;
//		if (!Character.isUpperCase(eEPROM_DEVICESTYPE.getName().charAt(0))) {warning("Name in this case should start with a capital letter.", Project2Package.EEPROM_DEVICESTYPE__NAME, [u]INVALID_NAME [/u], eEPROM_DEVICESTYPE.getName());


my problem here is with the underline "INVALID_NAME" saying that INVALID_NAME cannot be resolved to a variable.i don't know how to fixe it.

and that error has an impact on the quickfixProvider class cince the INVALID_NAME is not good. That is the quickfixProvider class

package org.xtext.example.ui.quickfix;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.xtext.ui.editor.model.IXtextDocument;
import org.eclipse.xtext.ui.editor.model.edit.IModification;
import org.eclipse.xtext.ui.editor.model.edit.IModificationContext;
import org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider;
import org.eclipse.xtext.ui.editor.quickfix.Fix;
import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor;
import org.eclipse.xtext.validation.Issue;
import org.xtext.example.validation.Project2JavaValidator;

public class Project2QuickfixProvider extends DefaultQuickfixProvider {

	@Fix(Project2JavaValidator.INVALID_NAME)
	public void capitalizeName(final Issue issue, IssueResolutionAcceptor acceptor) {
		acceptor.accept(issue, "Capitalize name", "Capitalize the name.", "upcase.png", new IModification() {
			public void apply(IModificationContext context) throws BadLocationException {
				IXtextDocument xtextDocument = context.getXtextDocument();
				String firstLetter = xtextDocument.get(issue.getOffset(), 1);
				xtextDocument.replace(issue.getOffset(), 1, firstLetter.toUpperCase());
			}
		});
}

}
 




can somebody tell why the name " INVALID_NAME" is not correct, i even change it, bbut i still have de same error.

My best regard,
Frank
  • Attachment: Project2.zip
    (Size: 265.74KB, Downloaded 86 times)
Re: Problem with quick fixes [message #881794 is a reply to message #881746] Tue, 05 June 2012 09:09 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3108
Registered: July 2009
Senior Member
Hi Frank,

INVALID_NAME is a string constant that you'll have to define somewhere.
It has to be accessible from the quickfix provider and the validator. It
should be unique as its value is used to identify fixable issues in your
language.

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

Am 05.06.12 09:33, schrieb frank fotso:
> hi,i want to use the feature (quick fixes) of xtext, and for that i wanted to do a simple example of putting a warning when a specific name doesn't start with a capital letter. this is the code of
>
> - the javavalidator class:
>
> package org.xtext.example.validation;
>
> import org.eclipse.xtext.validation.Check;
> import org.xtext.example.project2.EEPROM_DEVICESTYPE;
> import org.xtext.example.project2.Project2Package;
>
>
> public class Project2JavaValidator extends AbstractProject2JavaValidator {
>
> //@Check
> //public void checkNameStartsWithCapital(EEPROM_DEVICESTYPE eEPROM_DEVICESTYPE) {
> // if (eEPROM_DEVICESTYPE.getName() == null || eEPROM_DEVICESTYPE.getName().length() == 0)return;
> // if (!Character.isUpperCase(eEPROM_DEVICESTYPE.getName().charAt(0))) {warning("Name in this case should start with a capital letter.", Project2Package.EEPROM_DEVICESTYPE__NAME, INVALID_NAME , eEPROM_DEVICESTYPE.getName());
>
> my problem here is with the underline "INVALID_NAME" saying that INVALID_NAME cannot be resolved to a variable.i don't know how to fixe it.
>
> and that error has an impact on the quickfixProvider class cince the INVALID_NAME is not good. That is the quickfixProvider class
>
> package org.xtext.example.ui.quickfix;
>
> import org.eclipse.jface.text.BadLocationException;
> import org.eclipse.xtext.ui.editor.model.IXtextDocument;
> import org.eclipse.xtext.ui.editor.model.edit.IModification;
> import org.eclipse.xtext.ui.editor.model.edit.IModificationContext;
> import org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider;
> import org.eclipse.xtext.ui.editor.quickfix.Fix;
> import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor;
> import org.eclipse.xtext.validation.Issue;
> import org.xtext.example.validation.Project2JavaValidator;
>
> public class Project2QuickfixProvider extends DefaultQuickfixProvider {
>
> @Fix(Project2JavaValidator.INVALID_NAME)
> public void capitalizeName(final Issue issue, IssueResolutionAcceptor acceptor) {
> acceptor.accept(issue, "Capitalize name", "Capitalize the name.", "upcase.png", new IModification() {
> public void apply(IModificationContext context) throws BadLocationException {
> IXtextDocument xtextDocument = context.getXtextDocument();
> String firstLetter = xtextDocument.get(issue.getOffset(), 1);
> xtextDocument.replace(issue.getOffset(), 1, firstLetter.toUpperCase());
> }
> });
> }
>
> }
>
>
>
>
> can somebody tell why the name " INVALID_NAME" is not correct, i even change it, bbut i still have de same error.
>
> My best regard,
> Frank
Re: Problem with quick fixes [message #881801 is a reply to message #881794] Tue, 05 June 2012 09:22 Go to previous messageGo to next message
frank fotso is currently offline frank fotsoFriend
Messages: 91
Registered: May 2012
Member
Thks Sabastian,
i have declare the constant String INVALID_NAME as follow
 org.xtext.example.validation;

import org.eclipse.xtext.validation.Check;
import org.xtext.example.project2.EEPROM_DEVICESTYPE;
import org.xtext.example.project2.Project2Package;
 

public class Project2JavaValidator extends AbstractProject2JavaValidator {

	public static final String INVALID_NAME = "org.xtext.example.project2.InvalidTypeName";

	@Check
public void checkNameStartsWithCapital(EEPROM_DEVICESTYPE  eEPROM_DEVICESTYPE) {
		if (eEPROM_DEVICESTYPE.getName() == null || eEPROM_DEVICESTYPE.getName().length() == 0)return;
		if (!Character.isUpperCase(eEPROM_DEVICESTYPE.getName().charAt(0))) {warning("Name in this case should start with a capital letter.", Project2Package.EEPROM_DEVICESTYPE__NAME, INVALID_NAME , eEPROM_DEVICESTYPE.getName());
	//	if (!Character.isUpperCase(name.getLocalName().charAt(0))) {
		//	warning("Name should start with a capital", Project2Package.Literals.EEPROM_BLOCKLOCAL__NAME);
		//}
	}
		}

	private void warning(String message, int eepromDevicestypeName,
			String invalidName, String name) {
		// TODO Auto-generated method stub
		
	}
	}	 



and change the quickfuixProvider class to the one follow:

 package org.xtext.example.ui.quickfix;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.xtext.ui.editor.model.IXtextDocument;
import org.eclipse.xtext.ui.editor.model.edit.IModification;
import org.eclipse.xtext.ui.editor.model.edit.IModificationContext;
import org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider;
import org.eclipse.xtext.ui.editor.quickfix.Fix;
import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor;
import org.eclipse.xtext.validation.Issue;
import org.xtext.example.validation.Project2JavaValidator;

public class Project2QuickfixProvider extends DefaultQuickfixProvider {

	@Fix(Project2JavaValidator.INVALID_NAME)
	public void capitalizeName(final Issue issue, IssueResolutionAcceptor acceptor) {
		acceptor.accept(issue, "Capitalize name", "Capitalize the name.", "upcase.png", new IModification() {
			public void apply(IModificationContext context) throws BadLocationException {
				IXtextDocument xtextDocument = context.getXtextDocument();
				String firstLetter = xtextDocument.get(issue.getOffset(), 1);
				xtextDocument.replace(issue.getOffset(), 1, firstLetter.toUpperCase());
			}
		});
}

}  

now all the errors have dissapear and my file is correct but when i execute the project , the editor file that i create even if the name start with a small letter is correct which is not normal since it was supposed to put a warning telling that the name should start with a capital letter.
Re: Problem with quick fixes [message #881814 is a reply to message #881801] Tue, 05 June 2012 09:43 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3108
Registered: July 2009
Senior Member
That's because you have this method in your validator that does nothing:

Am 05.06.12 11:22, schrieb frank fotso:
> private void warning(String message, int eepromDevicestypeName,
> String invalidName, String name) {
> // TODO Auto-generated method stub
>
> }

You may want to use Project2Package.Listeral.sEEPROM_BLOCKLOCAL_NAME
instead.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Problem with quick fixes [message #881919 is a reply to message #881814] Tue, 05 June 2012 13:54 Go to previous messageGo to next message
frank fotso is currently offline frank fotsoFriend
Messages: 91
Registered: May 2012
Member
Thks sebastian
i did as you said and now the warning appear but when i try de quick fixes butmessage appearing that there are no quick fixe for that warning , but i've define it in the quickFixProvider class as follows:


  package org.xtext.example.ui.quickfix;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.xtext.ui.editor.model.IXtextDocument;
import org.eclipse.xtext.ui.editor.model.edit.IModification;
import org.eclipse.xtext.ui.editor.model.edit.IModificationContext;
import org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider;
import org.eclipse.xtext.ui.editor.quickfix.Fix;
import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor;
import org.eclipse.xtext.validation.Issue;
import org.xtext.example.validation.Project2JavaValidator;

public class Project2QuickfixProvider extends DefaultQuickfixProvider {

	@Fix(value = "INVALID_NAME")
	public void capitalizeName(final Issue issue, IssueResolutionAcceptor acceptor) {
		acceptor.accept(issue, "Capitalize name", "Capitalize the name.", "upcase.png", new IModification() {
			public void apply(IModificationContext context) throws BadLocationException {
				IXtextDocument xtextDocument = context.getXtextDocument();
				String firstLetter = xtextDocument.get(issue.getOffset(), 1);
				xtextDocument.replace(issue.getOffset(), 1, firstLetter.toUpperCase());
			}
		});
} 
Re: Problem with quick fixes [message #881930 is a reply to message #881919] Tue, 05 June 2012 14:07 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
For starters - do you have a binding for specialized quick fix provider
in your UI module?

- henrik

On 2012-05-06 15:54, frank fotso wrote:
> Thks sebastian
> i did as you said and now the warning appear but when i try de quick
> fixes butmessage appearing that there are no quick fixe for that warning
> , but i've define it in the quickFixProvider class as follows:
>
>
> package org.xtext.example.ui.quickfix;
>
> import org.eclipse.jface.text.BadLocationException;
> import org.eclipse.xtext.ui.editor.model.IXtextDocument;
> import org.eclipse.xtext.ui.editor.model.edit.IModification;
> import org.eclipse.xtext.ui.editor.model.edit.IModificationContext;
> import org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider;
> import org.eclipse.xtext.ui.editor.quickfix.Fix;
> import org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor;
> import org.eclipse.xtext.validation.Issue;
> import org.xtext.example.validation.Project2JavaValidator;
>
> public class Project2QuickfixProvider extends DefaultQuickfixProvider {
>
> @Fix(value = "INVALID_NAME")
> public void capitalizeName(final Issue issue, IssueResolutionAcceptor
> acceptor) {
> acceptor.accept(issue, "Capitalize name", "Capitalize the name.",
> "upcase.png", new IModification() {
> public void apply(IModificationContext context) throws
> BadLocationException {
> IXtextDocument xtextDocument = context.getXtextDocument();
> String firstLetter = xtextDocument.get(issue.getOffset(), 1);
> xtextDocument.replace(issue.getOffset(), 1, firstLetter.toUpperCase());
> }
> });
> }
Re: Problem with quick fixes [message #881934 is a reply to message #881930] Tue, 05 June 2012 14:14 Go to previous message
frank fotso is currently offline frank fotsoFriend
Messages: 91
Registered: May 2012
Member
is oki i've solved the problem

it was due to the encoding mode and something else , now everything is oki

Thks for the help guyz

Best regards,
FRank
Previous Topic:Please help with simple grammar
Next Topic:Outline "within" Project Explorer?
Goto Forum:
  


Current Time: Thu Oct 22 18:42:42 GMT 2020

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

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

Back to the top