Quick Fix - Access Specific Token [message #696090] |
Wed, 13 July 2011 08:54 |
Daniel Missing name Messages: 101 Registered: July 2011 |
Senior Member |
|
|
Hi everybody.
As always: A small szenario that I want to achieve.
I want to provide some Quick Fixes to fix some errors. As an example I want to prevent cycles within type hierarchies using this grammar:
// Eine Strukturdefinition
Type:
'type' name = ID
('extends' superType = [Type|QualifiedName])? '{'
'}'
;
I have written a validator which detects cycles within types:
public static final String INHERITANCE_LOOP = "inheritance-loop";
@Check
public void checkTypeInheritance(Type type) {
Type hierarchyLoopElement = getHierarchyLoop(type);
if (hierarchyLoopElement != null) {
error('Cycle detected ...',
MyDslPackage.Literals.TYPE__SUPER_TYPE, INHERITANCE_LOOP, hierarchyLoopElement.getName());
}
}
The strange thing is: MyDslPackage.Literals.TYPE__SUPER_TYPE marks the full type definition as an error, not only the superType part. I want to provide a quick fix to remove the 'extends SuperType' part from the source code:
@Fix(MyDslJavaValidator.INHERITANCE_LOOP)
public void breakInheritenceLoop(...) {
acceptor.accept(issue,
"..", "..", "..",
new IModification() {
@Override
public void apply(IModificationContext context) throws Exception {
IXtextDocument xtextDocument = context.getXtextDocument();
xtextDocument.replace(issue.getOffset(), issue.getLength(), "");
}
});
}
But this Quick Fix removes the complete type definition. How can I access the ranges of specific subtoken? Even if the TYPE__SUPER_TYPE reference would only mark the superType reference, how can I ensure the "extends" before the type reference will also get removed?
-Daniel
|
|
|
|
|
|
|
|
Re: Quick Fix - Access Specific Token [message #696520 is a reply to message #696267] |
Thu, 14 July 2011 07:47 |
Daniel Missing name Messages: 101 Registered: July 2011 |
Senior Member |
|
|
I already tried the semantic modification but it only throws an exception as I set the superType to null. It seems the generated serializer cannot take care of this modification. I also tried to add a new sub-rule for the complete inheritance block and set this new feature to null afterwards, no success.
Therefore I thought I need to modify the Serializer for writing correct code after modifications. (Like removing the extends keyword too if the subtype is null).
org.eclipse.emf.common.util.WrappedException: java.lang.RuntimeException: The context 'Model' is not valid for type 'Struct'
Recommended contexts for type 'Struct': Declaration, TypeDeclaration, Struct, StructElement, UnionElement
The context 'Model' is valid for types: ModelDeclaration
Semantic Object: ModelDeclaration'INHERITANCE'.declarations[4]->Struct'ERR1_A'
at org.eclipse.xtext.ui.editor.quickfix.IssueResolution.apply(IssueResolution.java:50)
at org.eclipse.xtext.ui.editor.quickfix.QuickAssistCompletionProposal.apply(QuickAssistCompletionProposal.java:32)
at org.eclipse.jface.text.contentassist.CompletionProposalPopup.insertProposal(CompletionProposalPopup.java:935)
...
Caused by: java.lang.RuntimeException: The context 'Model' is not valid for type 'Struct'
Recommended contexts for type 'Struct': Declaration, TypeDeclaration, Struct, StructElement, UnionElement
The context 'Model' is valid for types: ModelDeclaration
Semantic Object: ModelDeclaration'INHERITANCE'.declarations[4]->Struct'ERR1_A'
at org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic$ExceptionThrowingAcceptor.accept(ISerializationDiagnostic.java:66)
at at.mydsl.serializer.AbstractMyDslSemanticSequencer.createSequence(AbstractMyDslSemanticSequencer.java:623)
at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:68)
|
|
|
Re: Quick Fix - Access Specific Token [message #696530 is a reply to message #696520] |
Thu, 14 July 2011 08:21 |
Sebastian Zarnekow Messages: 3118 Registered: July 2009 |
Senior Member |
|
|
Hi Daniel,
I take you created a new project with Xtext 2.0 with the "Experimental
features" configuration. The new serializer as of June 23rd could not
handle partial serialization. I'd recommend to enable the
ParseTreeConstructorFragment instead of the SerializerFragment in your
workflow.
Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
On 14.07.11 09:47, Daniel wrote:
> I already tried the semantic modification but it only throws an
> exception as I set the superType to null. It seems the generated
> serializer cannot take care of this modification. I also tried to add a
> new sub-rule for the complete inheritance block and set this new feature
> to null afterwards, no success.
> Therefore I thought I need to modify the Serializer for writing correct
> code after modifications. (Like removing the extends keyword too if the
> subtype is null).
>
> org.eclipse.emf.common.util.WrappedException:
> java.lang.RuntimeException: The context 'Model' is not valid for type
> 'Struct'
> Recommended contexts for type 'Struct': Declaration, TypeDeclaration,
> Struct, StructElement, UnionElement
> The context 'Model' is valid for types: ModelDeclaration
>
> Semantic Object:
> ModelDeclaration'INHERITANCE'.declarations[4]->Struct'ERR1_A'
> at
> org.eclipse.xtext.ui.editor.quickfix.IssueResolution.apply(IssueResolution.java:50)
>
> at
> org.eclipse.xtext.ui.editor.quickfix.QuickAssistCompletionProposal.apply(QuickAssistCompletionProposal.java:32)
>
> at
> org.eclipse.jface.text.contentassist.CompletionProposalPopup.insertProposal(CompletionProposalPopup.java:935)
>
> ..
> Caused by: java.lang.RuntimeException: The context 'Model' is not valid
> for type 'Struct'
> Recommended contexts for type 'Struct': Declaration, TypeDeclaration,
> Struct, StructElement, UnionElement
> The context 'Model' is valid for types: ModelDeclaration
>
> Semantic Object:
> ModelDeclaration'INHERITANCE'.declarations[4]->Struct'ERR1_A'
> at
> org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic$ExceptionThrowingAcceptor.accept(ISerializationDiagnostic.java:66)
>
> at
> at.mydsl.serializer.AbstractMyDslSemanticSequencer.createSequence(AbstractMyDslSemanticSequencer.java:623)
>
> at
> org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:68)
>
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04031 seconds