Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » web editor with code generation
icon5.gif  web editor with code generation [message #1720470] Mon, 18 January 2016 19:45 Go to next message
Johnny Bravo is currently offline Johnny BravoFriend
Messages: 5
Registered: January 2016
Junior Member
Hi,

I am trying to get started with a simple web editor that displays the generated language.
I have found the appropriate examples on github, like "ace-statemachine-resource", but I haven't quite figured out whats going on.

So when I start with the simple MyDsl example and implement the MyDslGenerator, how would I bind that to a web service (at /xtext-service/generate ) ?

Any help is appreciated!
Many thanks
Re: web editor with code generation [message #1720489 is a reply to message #1720470] Mon, 18 January 2016 21:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
according to http://www.eclipse.org/Xtext/documentation/310_web_integration.html enablegeneratorService is on by default

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: web editor with code generation [message #1720505 is a reply to message #1720489] Tue, 19 January 2016 05:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Here is what to todo

(1) Implement Generator

class MyDslGenerator extends AbstractGenerator {

	override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
		fsa.generateFile('/DEFAULT_ARTIFACT', 'People to greet: ' + 
			resource.allContents
				.filter(typeof(Greeting))
				.map[name]
				.join(', '))
	}
}


(2) Adapt Syling and UI

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta http-equiv="Content-Language" content="en-us">
	<title>Example Web Editor</title>
	<link rel="stylesheet" type="text/css" href="xtext/2.9.2-SNAPSHOT/xtext-ace.css"/>
	<link rel="stylesheet" type="text/css" href="style.css"/>
	<script src="webjars/requirejs/2.1.20/require.min.js"></script>
	<script type="text/javascript">
		var baseUrl = window.location.pathname;
		var fileIndex = baseUrl.indexOf("index.html");
		if (fileIndex > 0)
			baseUrl = baseUrl.slice(0, fileIndex);
		require.config({
			baseUrl: baseUrl,
			paths: {
				"jquery": "webjars/jquery/2.1.4/jquery.min",
				"ace/ext/language_tools": "webjars/ace/1.2.0/src/ext-language_tools",
				"xtext/xtext-ace": "xtext/2.9.2-SNAPSHOT/xtext-ace"
			}
		});
		require(["webjars/ace/1.2.0/src/ace"], function() {
			require(["xtext/xtext-ace"], function(xtext) {
				xtext.createEditor({
					
					baseUrl: baseUrl,
					syntaxDefinition: "xtext-resources/generated/mode-mydsl2"
				});
					jQuery('#generate-button').bind("click", function(e){
					
					jQuery('#generator-result').html('<iframe src="http://' + location.host + '/xtext-service/generate?resource=example1.mydsl2"></iframe>');
					e.preventDefault();
				});
			});
		});
	</script>
</head>
<body>


	
<div class="container">
	<div class="header">
		<h1>Example MyDsl Web Editor2</h1>
	</div>
	<div class="content">
		<div id="xtext-editor" data-editor-xtext-lang="mydsl2" data-editor-resource-id="example1.mydsl2"></div>
	</div>
	<div class="button-wrapper">
		<button id="generate-button" value="Generate" title="Generate">Generate</button>
		<div id="generator-result">
			Result
		</div>
	</div>
</div>



</body>
</html>



div.button-wrapper {
	display: block;
	position: absolute;
	top: 90px;
	bottom: 0;
	left: 640px;
	right: 0;
	padding: 4px;
	border: 1px solid #aaa;
}




Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: web editor with code generation [message #1720577 is a reply to message #1720505] Tue, 19 January 2016 13:24 Go to previous messageGo to next message
Johnny Bravo is currently offline Johnny BravoFriend
Messages: 5
Registered: January 2016
Junior Member
Yes this works. Thanks for the quick reply!
Re: web editor with code generation [message #1720660 is a reply to message #1720577] Wed, 20 January 2016 08:36 Go to previous message
Miro Spönemann is currently offline Miro SpönemannFriend
Messages: 78
Registered: March 2015
Location: Kiel, Germany
Member

As an alternative to the iframe code shown above, the generator can also be invoked with JavaScript code as described in
https://www.eclipse.org/Xtext/documentation/310_web_integration.html#invoking-services
Previous Topic:Open auto-complete without ctrl+space
Next Topic:AbstractMyDslProposalProvider
Goto Forum:
  


Current Time: Thu Apr 25 06:23:39 GMT 2024

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

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

Back to the top