Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Gets proposal for a position
Gets proposal for a position [message #1741025] Mon, 22 August 2016 12:12 Go to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
Hello community,

i created the example xtext project (greetings).
Now iam looking for the function, which returns all proposal for a position.

Someting like that: Then i will get a proposal "Hello" etc.

parser.getProposals("He", 2);


Can anybody give me a hint, how to use the parser?

Thanks Michael
Re: Gets proposal for a position [message #1741038 is a reply to message #1741025] Mon, 22 August 2016 13:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi you may digg into https://github.com/eclipse/xtext-core/blob/e1a4782403497dd27f73276c409049a3d3ecdcf9/org.eclipse.xtext.ide/src/org/eclipse/xtext/ide/server/contentassist/ContentAssistService.xtend or https://github.com/eclipse/xtext-web/blob/f4c4d9bb228014c01f8f468eab12e57ddcd26755/org.eclipse.xtext.web/src/main/java/org/eclipse/xtext/web/server/contentassist/ContentAssistService.xtend

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Gets proposal for a position [message #1741084 is a reply to message #1741038] Mon, 22 August 2016 20:29 Go to previous messageGo to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
Thanks for your quick reply:

Tried it:

package org.xtext.example.mydsl.tests;

import org.eclipse.xtext.junit4.InjectWith;
import org.eclipse.xtext.junit4.XtextRunner;
import org.eclipse.xtext.util.ITextRegion;
import org.eclipse.xtext.util.TextRegion;
import org.eclipse.xtext.web.server.contentassist.ContentAssistResult;
import org.eclipse.xtext.web.server.contentassist.ContentAssistService;
import org.eclipse.xtext.web.server.model.XtextWebDocumentAccess;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.xtext.example.mydsl.web.MyDslInjectorProvider;

import com.google.inject.Inject;

@RunWith(XtextRunner.class)
@InjectWith(MyDslInjectorProvider.class)
public class MyDslParsingTest {

	@Inject
	ContentAssistService contentAssistService;

	@Inject
	XtextWebDocumentAccess xtextWebDocumentAccess;

	@Test
	public void loadModel() {
		ITextRegion selection = new TextRegion(0, 3);
		ContentAssistResult result = contentAssistService.createProposals(xtextWebDocumentAccess, selection, 1, 1000);
		System.out.println(result);
	}
}



but:

java.lang.NullPointerException
	at org.eclipse.xtext.web.server.model.XtextWebDocumentAccess.doAccess(XtextWebDocumentAccess.java:191)
	at org.eclipse.xtext.web.server.model.XtextWebDocumentAccess.priorityReadOnly(XtextWebDocumentAccess.java:164)
	at org.eclipse.xtext.web.server.contentassist.ContentAssistService.createProposals(ContentAssistService.java:78)
	at org.xtext.example.mydsl1.tests.MyDslParsingTest.loadModel(MyDslParsingTest.java:33)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.eclipse.xtext.junit4.XtextRunner$1.evaluate(XtextRunner.java:49)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)



Is there a small example?
Re: Gets proposal for a position [message #1741086 is a reply to message #1741084] Mon, 22 August 2016 21:07 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
No i dont have a small example. I meant the code references more like a place to start digging than a injection and use reference

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Gets proposal for a position [message #1741093 is a reply to message #1741086] Tue, 23 August 2016 04:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i did just some playaround: btw. why do you want to do this?


@RunWith(XtextRunner)
@InjectWith(MyDslIdeInjectorProvider)
class MyDslParsingTest{
	
	static class MyDslIdeInjectorProvider extends MyDslInjectorProvider {
		
		override protected internalCreateInjector() {
			return new MyDslStandaloneSetup() {
			@Override
			override Injector createInjector() {
				return Guice.createInjector(createRuntimeModule(), new MyDslIdeModule);
			}
		}.createInjectorAndDoEMFRegistration();
		}
		
	}

	val List<ExecutorService> executorServices = newArrayList
	@Inject
	ParseHelper<Model> parseHelper
	
	@Inject Provider<ContentAssistContextFactory> contextFactoryProvider
	
	@Inject IdeContentProposalProvider proposalProvider
	
	ExecutorService executorService
	
	new() {
		executorService = Executors.newCachedThreadPool => [executorServices += it]
	}

	@Test 
	def void loadModel() {
		val model = '''
			Hello Xtext!
		'''
		val result = parseHelper.parse(model)
		Assert.assertNotNull(result)
		
		val contexts = (result.eResource as XtextResource).getContexts(new TextRegion(6,0), 6)
		val a = new IIdeContentProposalAcceptor {
			
			override accept(ContentAssistEntry e, int prio) {
				println(e.toString)
			}
			
			override canAcceptMoreProposals() {
				true
			}
			
		}
		proposalProvider.createProposals(contexts, a)
	}
	
	def ContentAssistContext[] getContexts(XtextResource resource, ITextRegion selection, int caretOffset) {
		val text = resource.parseResult.rootNode.text
		if (caretOffset > text.length)
			return #[]
		val contextFactory = contextFactoryProvider.get() => [it.pool = executorService]
		contextFactory.create(text, selection, caretOffset, resource)
	}
	
	@After
	def void tearDown() {
		executorServices.forEach[shutdown()]
		executorServices.clear()
	}

}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Gets proposal for a position [message #1741112 is a reply to message #1741093] Tue, 23 August 2016 07:49 Go to previous messageGo to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
Christian Dietrich wrote on Tue, 23 August 2016 04:31
i did just some playaround: btw. why do you want to do this?


Thanks a lot. I will try that.
I would like to integrate xText with Vaadin. I just need a Textfield (not a whole Editor) with auto completion or suggestion support. Additionally i need the parsing and error handling.

So the user enter "Hel" and now there is a suggestion to use the keyword "Hello"
	@Test 
	def void loadModel() {
		val model = '''
			Hel
		'''
		val result = parseHelper.parse(model)
		Assert.assertNotNull(result)
		
		val contexts = (result.eResource as XtextResource).getContexts(new TextRegion(3,0), 6)
		val a = new IIdeContentProposalAcceptor {
			
			override accept(ContentAssistEntry e, int prio) {
				println(e.toString)
			}
			
			override canAcceptMoreProposals() {
				true
			}
			
		}
		proposalProvider.createProposals(contexts, a)
	}


[Updated on: Tue, 23 August 2016 08:36]

Report message to a moderator

Re: Gets proposal for a position [message #1741132 is a reply to message #1741112] Tue, 23 August 2016 09:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
yoiu should have a look at the upcoming language server support in xtext 2.11 as well

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Gets proposal for a position [message #1741134 is a reply to message #1741132] Tue, 23 August 2016 09:41 Go to previous messageGo to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
Christian Dietrich wrote on Tue, 23 August 2016 09:33
yoiu should have a look at the upcoming language server support in xtext 2.11 as well


Sure, where can i find the server support?
Re: Gets proposal for a position [message #1741138 is a reply to message #1741134] Tue, 23 August 2016 10:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
this is still wip. http://typefox.io/the-language-server-protocol-in-java might give a starting point

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Gets proposal for a position [message #1741141 is a reply to message #1741138] Tue, 23 August 2016 10:35 Go to previous messageGo to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
Thanks for that, but i think is currently a little to powerful.

Iam just need 2 functions for my textfield.
Sugesstion support: e.g. if i enter 'Hell' then i get the suggestion 'Hello'.
And after typing i can parse the whole string and looking for erros. Thats it

Is this possible with the xtext runtime?
Re: Gets proposal for a position [message #1741144 is a reply to message #1741141] Tue, 23 August 2016 10:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
sure. that should be no problem as my test shows

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Gets proposal for a position [message #1741146 is a reply to message #1741144] Tue, 23 August 2016 10:50 Go to previous messageGo to next message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
Mhm sorry then iam don't get it Embarrassed


I changedt he model value from your test like:

But now the result is null. So how could i get the proposal "Hello" for the keyword?
	@Test 
	def void loadModel() {
		val model = '''
			Hel
		'''
		val result = parseHelper.parse(model)
		Assert.assertNotNull(result)
		
		val contexts = (result.eResource as XtextResource).getContexts(new TextRegion(3,0), 6)
		val a = new IIdeContentProposalAcceptor {
			
			override accept(ContentAssistEntry e, int prio) {
				println(e.toString)
			}
			
			override canAcceptMoreProposals() {
				true
			}
			
		}
		proposalProvider.createProposals(contexts, a)
	}
Re: Gets proposal for a position [message #1741149 is a reply to message #1741146] Tue, 23 August 2016 11:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
well that was you abusing parsehelper Wink

/*
 * generated by Xtext 2.10.0
 */
package org.xtext.example.mydsl5.tests

import com.google.inject.Guice
import com.google.inject.Inject
import com.google.inject.Injector
import com.google.inject.Provider
import java.util.List
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import org.eclipse.emf.common.util.URI
import org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext
import org.eclipse.xtext.ide.editor.contentassist.ContentAssistEntry
import org.eclipse.xtext.ide.editor.contentassist.IIdeContentProposalAcceptor
import org.eclipse.xtext.ide.editor.contentassist.IdeContentProposalProvider
import org.eclipse.xtext.ide.editor.contentassist.antlr.ContentAssistContextFactory
import org.eclipse.xtext.junit4.InjectWith
import org.eclipse.xtext.junit4.XtextRunner
import org.eclipse.xtext.junit4.util.ParseHelper
import org.eclipse.xtext.resource.FileExtensionProvider
import org.eclipse.xtext.resource.XtextResource
import org.eclipse.xtext.resource.XtextResourceSet
import org.eclipse.xtext.util.ITextRegion
import org.eclipse.xtext.util.StringInputStream
import org.eclipse.xtext.util.TextRegion
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith
import org.xtext.example.mydsl5.MyDslStandaloneSetup
import org.xtext.example.mydsl5.ide.MyDslIdeModule
import org.xtext.example.mydsl5.myDsl.Model

@RunWith(XtextRunner)
@InjectWith(MyDslIdeInjectorProvider)
class MyDslParsingTest{
	
	static class MyDslIdeInjectorProvider extends MyDslInjectorProvider {
		
		override protected internalCreateInjector() {
			return new MyDslStandaloneSetup() {
			@Override
			override Injector createInjector() {
				return Guice.createInjector(createRuntimeModule(), new MyDslIdeModule);
			}
		}.createInjectorAndDoEMFRegistration();
		}
		
	}

	val List<ExecutorService> executorServices = newArrayList
	
	@Inject Provider<ContentAssistContextFactory> contextFactoryProvider
	
	@Inject IdeContentProposalProvider proposalProvider
	
	ExecutorService executorService
	
	@Inject
	Provider<XtextResourceSet> rsp
	
	@Inject
	extension FileExtensionProvider
	
	new() {
		executorService = Executors.newCachedThreadPool => [executorServices += it]
	}

	@Test 
	def void loadModel() {
		
		val model = '''
			Hel
		'''
		val rs = rsp.get
		val resource = rs.createResource(URI.createURI("demo." + primaryFileExtension)) as XtextResource
		resource.load(new StringInputStream(model), null)
		
		val contexts = resource.getContexts(new TextRegion(3,0), 3)
		val a = new IIdeContentProposalAcceptor {
			
			override accept(ContentAssistEntry e, int prio) {
				println(e.toString)
			}
			
			override canAcceptMoreProposals() {
				true
			}
			
		}
		proposalProvider.createProposals(contexts, a)
	}
	
	def ContentAssistContext[] getContexts(XtextResource resource, ITextRegion selection, int caretOffset) {
		val text = resource.parseResult.rootNode.text
		if (caretOffset > text.length)
			return #[]
		val contextFactory = contextFactoryProvider.get() => [it.pool = executorService]
		contextFactory.create(text, selection, caretOffset, resource)
	}
	
	@After
	def void tearDown() {
		executorServices.forEach[shutdown()]
		executorServices.clear()
	}

}



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Gets proposal for a position [message #1741152 is a reply to message #1741149] Tue, 23 August 2016 11:30 Go to previous message
Michael Müller is currently offline Michael MüllerFriend
Messages: 20
Registered: August 2016
Junior Member
Thanks a lot for the support. Thats an good starting point Very Happy
Previous Topic:How to add xtext sdk with eclipse plugin?
Next Topic:Parsing exception in xtext grammar
Goto Forum:
  


Current Time: Thu Apr 18 21:01:38 GMT 2024

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

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

Back to the top