Xtext based language within Intellij Idea - intellij-idea

I want to make a plugin for a language for the Intellij Idea IDE. The language has been developped using Eclipse Xtext and is open source. A plugin already exists for Eclipse.
My goal is to port this language to Intellij Idea. I want to be able to use Intellij to create source files, to have the specific syntax highlighting and to be able to compile and run programs written with this language.
Is there a simple way to generate the Intellij Idea plugin using the Xtext project?
If not is there an efficient solution to be able to have the specific syntax highlighting in Intellij? (an automatic way if possible, I would prefer not rewriting everything everytime the Xtext project is updated)

Short answer
Yes, with a bit of work.
Long Answer
Sadly, Xtext uses antlr in the background and IntelliJ use their own grammar kit based on Parsing Expression Grammars. As such, the parsing and editor code generated by XText, as you might have guessed, will not work.
In order to get your language working in IntelliJ you will need to:
Create grammar *.bnf file
Generate lexer *.flex file, possibly tweak it and then run JFlex generator
Implement helper classes to provide, among others, file recognition via file extension, syntax highlighting, color settings page, folding, etc.
The *.flex file is generated from the bnf. Luckily, most of the classes in step 3 follow a very similar structure so they can be easily generated (more on that later). So basically, if you manage to generate the *.bnf file, you are 80% there.
Although from different technologies, the syntax of bnf files is very similar to XText files. I recently migrated some antlr grammars to IntelliJ's bnf and I had to do very small changes. Thus, it should be possible to autogenerate the bnf files from your XText ones.
That brings me back to point 3. Using XTend, Epsilon's EGL, or similar, it would be easy to generate all the boiler plate classes. As part of the migration I mentioned before I also did this. I am in the process of making the code public, so I will post it here when done and add some details.

Related

IntelliJ: custom language: combine fragments in other languages

I am creating a custom language plugin for IntelliJ.
I would like it to be possible for a file in the new language to contain fragments of text in other languages.
The specific languages I would like to support are HTML, JS, CSS, and SQL.
I would also like to support other custom languages (i.e. languages I would define the syntax for).
The main feature I want is syntax coloring, but if I can get stuff like "go to declaration" and refactoring out of the box then all the better.
My last requirement is that it would be possible to use my own code to tell IntelliJ which language a fragment contains; fragments containing different languages will not be distinguishable at the lexer / parser level.
In short, I would like to implement something similar to what PhpStorm does when it detects, say, SQL inside a string:
I looked at IntelliJ's source code and found the ILazyParseableElementType interface which seemed relevant, but I'm not sure if this is the way to go (and if so - how to use it in my code exactly...)
Any pointers would be highly appreciated...
The Intellij feature/terminology you are looking for is Language Injections.
Here is a github PR which implements a very similar feature for JFLex files.
In short you need to implement a LanguageInjector and add it to your plugin.xml as a <languageInjector implementation="YourImplClass">.

Template engine/pre-processor that works from *within* the target language

I'm using Eclipse as my IDE, and I now have looked at about a dozen template engines, and they all seem to work in a similar way:
You create a template file in the template language
The IDE plugin, if any, recognize the template syntax, rather then the target language syntax.
The generated/modified file, in the target language, is read-only, since it will get replaced on next generation.
This greatly limits the use of template engines/pre-processor, because you loose the IDE support for the target language.
Generally, the templating syntax will be simple enough anyway, that you could easily get by without IDE support for it. What is really needed, is to get the target language support from within the template.
In other words, is there some pre-processor/template engine, that does not require you to create "template files", but rather register itself in the IDE between the file system and the target language support, modifying the content of the file on the fly such that the target language plugin sees the file after generation, while the file is saved in the file system as it was before the generation, that is, it contains the template on the file system, rather then the generated code.
In other other words, I want to write the templates in the target language, and have them recognized as such by the IDE editor, such that the editor sees the code after generation (maybe hide the template instructions in comments?).
Such a plugin would then be totally generic, and independent of the actual file type containing the template (as long as it's a text file). All that would be needed, is then some configuration file telling the template engine in which files/directories to look for templates.
Alternatively, if no such product exist, does it seem like something that could be implemented in Eclipse as a plugin, or is it more a case that there are no such plugin because it would be basically impossible?
Here a list of a few things I've looked at, all of which don't seem to support the desired use case:
Xtend
eclipseME - Preprocessing
Java Comment Preprocessor
Preprocessing Java with Munge Munge
Velocity
Jamon
JET
StringTemplate
Xpand
Freemarker
Mustache Java
Jade4J
Closure Templates in Java
Rythm Engine
Related question: Saving self-written code - xtend

Domain specific language IDE

I've recently developed a domain-specific language using flex and bison. I would like to create a user interface for editing script files using this language. In particular I would like to have common functionalities such as file handling, menus, buttons, syntax highlighting, error checking and so on. Do you know any tool which can be used to develop such kind of application? I would prefer one which can give me a prototype rapidly.
such as file handling, menus, buttons, syntax highlighting, error
checking
I think that file handling, menus, buttons and highlighting are your least concerns. What you call "error checking" on the other hand. That can be a tough nut. I will try to give you some pointers to how you can (in a somewhat primitive manner) detect errors on the fly as the user inputs source code in the editor.
I assume you wish for something like Eclipses (for java at least) real time analysis of the written code in the editor? I'm not familiar with how Eclipse work internally but this is probably done by some pre-compilation process that processes all source code again and again as you change it.
One way to prototype this (and indeed build a non-prototype as well) would be to use Flex and Bison, and I notice you already is familiar with these tools. You can build you grammar and create action code for all interesting parts so you can find syntax deviations fairly easy. After this you make your editor run the flex and bison generated c-code as the user writes the source code in you IDE and have some way of displaying the output. Either in a terminal-like status window or directly in the text-editing field (as Eclipse does) (the latter case is probably a pain to build but by no means impossible and would give you IDE a professional touch).
Suppose you would like to build an IDE for ADA 95 the following Flex and Bison (Actually Lex and Yacc) code could help you do exactly this (it's a decent syntax analyzer that reports errors (what and where)):
http://www.adaic.org/resources/add_content/standards/95lrm/lexer9x.l
http://www.adaic.org/resources/add_content/standards/95lrm/grammar9x.y
Hope this helps.
Edit:
to have cool error highlighting and such in the text-editor field of your IDE you could let your bison generated syntax analyser generate something thats easy to parse, like XML, that contains the type of error and where it lies (row and column for example) and then use that to display the errors... you simply embed an XML parser in the IDE (lots of free one available) and extract the data you need and change the display accordingly... That shouldn't be rocket science when I think about it.

Purpose of antlr in xtext

I'm new to Xtext and wondering what's the purpose of antlr is in xtext. As I've understand so far, antlr generate a parser based on the grammar and the parser then deal with the text models. Right?
And what about the other generated stuff like the editor or the ecore. Are there other components behind xtext which generate them?
Xtext needs a parser generator to produce a parser for the language you define. They could have built one of their own. They chose to use ANTLR instead.
I don't know what other third party machinery they might have chosen to use.
I've been hacking one Xtext based plugin and from what I saw I think it works like this:
Xtext has it's own BNF syntax, which is very similar to ANTLR one. In fact its it's subset.
Xtext takes your grammar, and generates the ANTLR one from it(.g file). The generated ANTLR grammar adds specific actions to your BNF rules. The actions code interacts with the Xtext runtime and (maybe) with the Eclipse itself. The .g file is processed using some older version of ANTLR and .java file is generated. This file is then compiled.

ANTLR and content assist in Eclipse

I have a project in Eclipse where I have an editor for a custom language. I am using ANTLR to generate the compiler for it. What I need is to add content assist to the editor.
The input is a source code in the custom language, and the position of the character where the user requested content assist. The source code is most of time incomplete as the user can ask for content assist any time. What I need is to calculate the list of possible tokens that are valid for the given position.
It is possible to write a custom code to do the calculation, but that code would have to be manually kept in sync with the grammar. I figured the parser is doing something similar. It has to be able to determine at a given context what are the acceptable tokens. Is it possible to "reuse" that? What is the best practice in creating content assist anyway?
Thanks,
Balint
Have a look at Xtext. Xtext uses Antlr3 under the hood and provides content assist for the Antlr based languages. Have a look especially into package org.eclipse.xtext.ui.editor.contentassist.
You may consider to redefine your grammar with Xtext, which would provide the content assist out-of-the-box. It is not possible to reuse the Antlr grammar of a custom language.