Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Formatting Multiple Consecutive Keywords
Formatting Multiple Consecutive Keywords [message #1770581] Mon, 14 August 2017 15:57 Go to next message
Joe Seibel is currently offline Joe SeibelFriend
Messages: 3
Registered: July 2009
Junior Member
I've been following the itemis blog post showing how to provide content assist for multiple consecutive keywords and I am having difficulty figuring out how to format those keywords. Using the domainmodel example from the blog post, I created a simple file with extra spaces between the consecutive keywords:
entity Foo {
	is      composed     of Foo
}

I would like to be able to do something like this in the formatter:
def dispatch void format(Relation relation, extension IFormattableDocument document) {
	relation.regionFor.keyword("composed").surround[oneSpace]
}

This doesn't work because the call to keyword("composed") returns null. I printed the textRegionAccess and this is the result:
Columns: 1:offset 2:length 3:kind 4: text 5:grammarElement
Kind: H=IHiddenRegion S=ISemanticRegion B/E=IEObjectRegion

 0  0 H
      B DomainModel          DomainModel
       B Entity'Foo'          DomainModel:elements+=AbstractElement path:DomainModel/elements[0]
 0  6   S "entity"             Entity:'entity'
 6  1   H " "                  Whitespace:TerminalRule'WS'
 7  3   S "Foo"                Entity:name=ValidID
10  1   H " "                  Whitespace:TerminalRule'WS'
11  1   S "{"                  Entity:'{'
12  2   H "\n\t"               Whitespace:TerminalRule'WS'
        B Relation             Entity:relations+=Relation path:Entity'Foo'/relations[0]=DomainModel/elements[0]
14 23    S "is      compose..." Relation:IsComposedOf
37  1    H " "                  Whitespace:TerminalRule'WS'
38  3    S "Foo"                Relation:referencedEntity=[Entity|ID]
        E Relation             Entity:relations+=Relation path:Entity'Foo'/relations[0]=DomainModel/elements[0]
41  1   H "\n"                 Whitespace:TerminalRule'WS'
42  1   S "}"                  Entity:'}'
       E Entity'Foo'          DomainModel:elements+=AbstractElement path:DomainModel/elements[0]
      E DomainModel          DomainModel
43  1 H "\n"                 Whitespace:TerminalRule'WS'

As you can see, is composed of is one semantic region with no hidden regions between the keywords. For reference, here are the grammar rules of interest:
Relation:
	(DependsOn | IsComposedOf) referencedEntity=[Entity]
;

DependsOn:
	'depends' 'on'
;

IsComposedOf:
	'is' 'composed' 'of'
;
Re: Formatting Multiple Consecutive Keywords [message #1770595 is a reply to message #1770581] Mon, 14 August 2017 17:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

is there a reason you factor out the keywords to a separate datatype rule?
there is no support to format in within these

=> simply inline these should do the trick.

please feel free to file an enhancement request at https://github.com/eclipse/xtext-core


as a workaround you might try something like the following as well



import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess;
import org.eclipse.xtext.formatting2.regionaccess.TextRegionAccessBuilder;
import org.eclipse.xtext.formatting2.regionaccess.internal.NodeModelBasedRegionAccessBuilder;
import org.eclipse.xtext.formatting2.regionaccess.internal.TextRegionAccessBuildingSequencer;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.serializer.ISerializationContext;
import org.eclipse.xtext.serializer.acceptor.ISequenceAcceptor;

import com.google.inject.Inject;

public class CustomTextRegionAccessBuilder extends TextRegionAccessBuilder {
	
	@Inject org.xtext.example.mydsl2.services.MyDslGrammarAccess ga;

	private TextRegionAccessBuildingSequencer fromSequencer;

	private NodeModelBasedRegionAccessBuilder fromNodeModel;

	public TextRegionAccessBuilder forNodeModel(XtextResource resource) {
		this.fromNodeModel = new NodeModelBasedRegionAccessBuilder() {
			protected boolean include(org.eclipse.xtext.nodemodel.INode node) {
				if (node.getGrammarElement() == ga.getRelationAccess().getIsComposedOfParserRuleCall_0_1()) {
					return false;
				}
				return super.include(node);
			};
		}.withResource(resource);
		return this;
	}

	public ISequenceAcceptor forSequence(ISerializationContext ctx, EObject root) {
		//TODO
		return this.fromSequencer = new TextRegionAccessBuildingSequencer() {
		}.withRoot(ctx, root);
	}

	public ITextRegionAccess create() {
		if (fromNodeModel != null)
			return fromNodeModel.create();
		if (fromSequencer != null)
			return fromSequencer.getRegionAccess();
		throw new IllegalStateException();
	}

}


class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {
		
	def Class<? extends TextRegionAccessBuilder> bindTextRegionAccessBuilder() {
		CustomTextRegionAccessBuilder
	}
}




Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Formatting Multiple Consecutive Keywords [message #1770596 is a reply to message #1770595] Mon, 14 August 2017 18:27 Go to previous messageGo to next message
Joe Seibel is currently offline Joe SeibelFriend
Messages: 3
Registered: July 2009
Junior Member
The reason that I factored out the keywords into a separate rule was to support multiple keywords in content assist as demonstrated here: https://blogs.itemis.com/en/xtext-hint-content-assist-for-multiple-consecutive-keywords
Re: Formatting Multiple Consecutive Keywords [message #1770597 is a reply to message #1770596] Mon, 14 August 2017 18:43 Go to previous messageGo to next message
Joe Seibel is currently offline Joe SeibelFriend
Messages: 3
Registered: July 2009
Junior Member
Thanks for the workaround. It worked for me.
Re: Formatting Multiple Consecutive Keywords [message #1791315 is a reply to message #1770595] Thu, 28 June 2018 05:32 Go to previous messageGo to next message
Jo W is currently offline Jo WFriend
Messages: 4
Registered: June 2018
Junior Member
Hello, I to am having trouble formatting multiple keywords, the work around you posted did work, however, what if we had our grammar set up like this, I tried the workaround, but it didn't work for Case I, is there a similar work around for Case I (Thanks!):

Case I (didn't work):

RuleA:
keyword=keywords 'is' value=SCINOT (units=Units)?;

keywords:
keywordA | keywordB | keywordC;

keywordA:
('multiple' 'keyword' 'A')

keywordB:
('multiple' 'keyword' 'B')

keywordC:
('multiple' 'keyword' 'C')

Case II (the work around did suffice for this case below)

RuleA:
keywordA | keywordB | keywordC 'is' value=SCINOT (units=Units)?;

keywordA:
('multiple' 'keyword' 'A')

keywordB:
('multiple' 'keyword' 'B')

keywordC:
('multiple' 'keyword' 'C')

Re: Formatting Multiple Consecutive Keywords [message #1791565 is a reply to message #1791315] Mon, 02 July 2018 14:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
for you the same question: what is the reason to factor the keywords out.
and: how does your workaround class look like?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Formatting Multiple Consecutive Keywords [message #1791615 is a reply to message #1791565] Tue, 03 July 2018 02:57 Go to previous message
Jo W is currently offline Jo WFriend
Messages: 4
Registered: June 2018
Junior Member
Hey I managed to get it to work using your work around by going down one more level, thanks for the response!
Previous Topic:Codepage problem
Next Topic:Creating multiple embedded editors with (slightly) specific content assist
Goto Forum:
  


Current Time: Tue Mar 19 09:53:29 GMT 2024

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

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

Back to the top