Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Formatter2 - QualifiedName in Crossreference has spaces around delimiters - how to replace?
Formatter2 - QualifiedName in Crossreference has spaces around delimiters - how to replace? [message #1816857] Fri, 08 November 2019 15:26 Go to next message
Brandon Lewis is currently offline Brandon LewisFriend
Messages: 268
Registered: May 2012
Senior Member
I don't want to say I'm at the last of my headaches with Formatter2, but I'm getting close.

I've put off getting proper formatting of my QualifiedNames used in cross references for a while now, but now I can't put it off any longer.

I have no idea why Xtext is inserting these spaces for this text. It does it whenever I programmatically generate a file from my model (it seems to serialize with the a whitespace formatter).

The grammar item in question is:

VLNV returns ecore::EString:
Name '.' Name '.' ID '.' NMTOKEN
;

'abstractionRef' ':' abstractionRef=[AbstractionDefinitionType|VLNV]

In my formatter code, I have retrieved the region for my feature. "region" here being an NodeSemanticRegion. Sinc my content is inside a NodeSemanticRegion, every example on the net regarding surround, prepend, append etc wont' work, because I'm not dealing with a linked list of Hidden and Semantic Regions- it's inside this single SemanticRegion that I need to alter the text.

If I examine the region.text, I see in the debugger, my qualified name written like this:

com . lib . item . 1.0


So there are single white spaces between each QualifiedName delimiter. FWIW, my QualifiedNameConvertor is formatting the strings correctly as I can confirm with Content Assist (and when I use my QualifiedNames from Content Assist - everything works!)

I've spent at least 2 (now 4) hours trying to eliminate those spaces and I can't figure it out. Hardly any of the API for region have any comment or text whatsoever.

Something like region.createWhiteSpaceReplacer seems like it would be a fit, but no docs.

This also seems like it's trivially basic to anyone who knows what they are doing - clearly not myself.

I've tried:

    val keywords = regionFor.keywords(".")
    for (keyword : keywords) {
      keyword.surround[highPriority; noSpace]
    }


But on this region, there are no '.' keywords returned.

I can't seem to replace region.text .replace(" ", "") either. Doesn't seem theres a setText api to region.

Lost. Totally lost. Over white space surround a period.

[Updated on: Fri, 08 November 2019 15:40]

Report message to a moderator

Re: Formatter2 - QualifiedName in Crossreference has spaces around delimiters - how to replace? [message #1816861 is a reply to message #1816857] Fri, 08 November 2019 16:15 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i am not aware of a direct way to achieve this, but you can do something like (the same as you would do for terminals)

class MyDslFormatter extends AbstractFormatter2 {
	
	@Inject extension MyDslGrammarAccess

	def dispatch void format(Model model, extension IFormattableDocument document) {
		for (g : model.greetings) {
			g.format
		}
		for (r : model.refs) {
			r.format
		}
	}
	def dispatch void format(Greeting g, extension IFormattableDocument document) {
		document.addReplacer(new AbstractTextReplacer(document, g.regionFor.assignment(greetingAccess.nameAssignment_1)) {

			override createReplacements(ITextReplacerContext context) {
				context.addReplacement(region.replaceWith(region.text.replaceAll("\\s+", "")))
				return context
			}

		})
	}
	def dispatch void format(Ref r, extension IFormattableDocument document) {
		document.addReplacer(new AbstractTextReplacer(document, r.regionFor.assignment(refAccess.refAssignment_1)) {

			override createReplacements(ITextReplacerContext context) {
				context.addReplacement(region.replaceWith(region.text.replaceAll("\\s+", "")))
				return context
			}

		})
	}
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Unresolved Proxy to Object in Another Document
Next Topic:How to configure MonarchTokensProvider for my Xtext-DSL and Monaco editor
Goto Forum:
  


Current Time: Fri Apr 19 20:27:42 GMT 2024

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

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

Back to the top