Syntax coloring my DSL in Xtext - eclipse

I'm doing a small academic assignment, in which I'm supposed to implement an eclipse plugin for the NuSMV language (a model checking language) using Xtext. The xtext grammar was provided (with some minor bugs).
I've created an Xtext project and called it "smv.project" with language name of "NuSMV" and extension of "*.smv". I've coppied the provided Xtext grammar into smv.project/src/NuSMV.xtext, generated artifacts, and created runtime eclipse. Seems to work fine except those minor bugs (which are originated in the grammar in my opinion).
Now, my next step is to change the default colors in my DSL. Obviously, every user can do that for himself in the eclipse menus when using my plugin, but I wand the DEFAULT color to change. I've read the Xtext documentation for syntax coloring. It was short and had many terms I didn't understand. What I did understand is that there are 2 kinds of syntax coloring in Xtext, Lexical and Semantic. I didn't understand the meaning of each but it seemed to me that Lexical coloring is simpler, so I went for it.
The documentation said that I should implement the interface IHighlightingConfiguration. It showed an example of implementation called DefaultHighlightingConfiguration (full source can be seen here https://github.com/eclipse/xtext/blob/master/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/syntaxcoloring/DefaultHighlightingConfiguration.java). I've coppied this implementation into my project, to smv.project.ui/src/smv.project.ui.editor.syntaxcoloring. Of course, I changed the "package" line in the beginning of the file.
I've changed all red values to 255 to see difference.
I've generated the artifacts again, created runtime eclipse, created a *.smv file but the colors were the same.
My guess was that I need to set my new class in some variable or configuration class... I tried searching for "DefaultHighlightingConfiguration" in the example's repository and found out that it's used in the file XtextProposalProvider.java (full source https://github.com/eclipse/xtext/blob/87a6c7dc25074ca4ff28429c059e728ef420b926/plugins/org.eclipse.xtext.xtext.ui/src/org/eclipse/xtext/ui/contentassist/XtextProposalProvider.java) in line 235 - We're overriding getKeywordDisplayString(). So I've went to the already existing NuSMVProposalProvider.java in my repository and tried to add the same lines:
#Override
protected StyledString getKeywordDisplayString(Keyword keyword) {
return stylerFactory.createFromXtextStyle(keyword.getValue(),
defaultLexicalHighlightingConfiguration.keywordTextStyle());
}
Here is my full NuSMVProposalProvider.xtend:
/*
* generated by Xtext
*/
package smv.project.ui.contentassist
import smv.project.ui.contentassist.AbstractNuSMVProposalProvider
import javax.inject.Inject
import org.eclipse.xtext.ui.editor.syntaxcoloring.DefaultHighlightingConfiguration
import org.eclipse.jface.viewers.StyledString
import org.eclipse.xtext.Keyword
/**
* see http://www.eclipse.org/Xtext/documentation.html#contentAssist on how to customize content assistant
*/
class NuSMVProposalProvider extends AbstractNuSMVProposalProvider {
#Inject
private DefaultHighlightingConfiguration defaultLexicalHighlightingConfiguration;
#Override
protected StyledString getKeywordDisplayString(Keyword keyword) {
return stylerFactory.createFromXtextStyle(keyword.getValue(),
defaultLexicalHighlightingConfiguration.keywordTextStyle());
}
}
However, the next errors appeared:
"The annotation #Override is disallowed for this location."
"extraneous input 'keyword' expecting ')'"
"mismatched input '{' expecting '=>'"
in the first 3 lines, respectively.
My questions are:
What exactly should I do with my class that implements
IHighlightingConfiguration?
What is Lexical and Semantic highlighing?
Is there something else I need to know for my syntax coloring to work?
Thanks a lot!! :)
Chai

NuSMVProposalProvider wasn't the way to go.
I didn't have to add any code there at all.
What I was looking for was adding the next lines in NuSMVUiModule.java:
public Class<? extends IHighlightingConfiguration> bindIHighlightingConfiguration () {
return DefaultHighlightingConfiguration.class;
}
Thanks everyone for their comments!
Chai

Related

XText - Multiple grammars and example web editor

I have an XText project setup with multiple grammars and am able to edit each grammar with cross-linking within the IDE generated by the mwe2 workflow. When I run the example web editor, the first grammar defined in the mwe2 workflow functions correctly.
If I change the the generated index.html to use one of the alternate grammar extensions the editor no longer functions, and I get an error message that looks like:
Xtext service 'occurrences' failed: Unable to identify the Xtext
language for resource 3c05cdd1.ed
I'd like to understand what is wrong in order to better understand how I can begin minimal integration of editors for the grammars into our existing console.
Thanks!
Pierre Padovani
P.S. I've posted this same question on the Eclipse Community Forums, but my post seems to be stuck in a moderator queue.
Having found the thread which #ppadovani mentions, it seems that the only necessary change is to ignore all the generated Servlet.xtend files except the one whose grammar is working.
In that one which works, it is necessary to initialise run the WebSetup for the other languages.
override init() {
super.init()
// call createInjectorAndDoEMFRegistration for each WebSetup
new DerivedDslWebSetup().createInjectorAndDoEMFRegistration
// keep the rest as-is
val injector = new CoreDslWebSetup().createInjectorAndDoEMFRegistration()
disposableRegistry = injector.getInstance(DisposableRegistry)
}
Reference: https://www.eclipse.org/forums/index.php?t=msg&th=1096166&goto=1798287&#msg_1798287

cannot debug GWT native code within gwtmockito

I have a GwtMockitoTestCase and the debugger seems not to enter inside any vanilla GWT class like Widget, ResizeLayoutPanel, etc.
However, when running the same code inside DevMode, the debugger steps correctly through that code.
Does this have to do with GWT running inside a JRE? If not, could it be that my classpath is wrong somehow? Or maybe the gwt-user jar doesn't have debugging information?
I've also tried to extend a GWT class:
ResizeLayoutPanel w = new ResizeLayoutPanel() {
#Override
public void setWidget(Widget pW) {
super.setWidget(pW); (1)
}
};
And breakpoint on line (1) is working but pressing F5, it doesn't go inside ResizeLayoutPanel's setWidget method.
Thank you!
After digging in the GwtMockito code, it seems there are a certain set of classes that are stubbed and some methods' body is removed. Therefore it's not possible to debug those methods.
The question that remains is that somehow GWTMockito breaks the code coverage tool(EclEmma) which shows less code covered than it's supposed to. I've posted a separate question on this topic on SO: false code coverage reported using GwtMockito
Some technical background:
GwtMockitoClassLoader stubs some classes from GWT completely, please check GwtMockitoTestRunner#getClassesToStub() which includes Widget and ResizeLayoutPanel classes.
The stubbing process removes the body completely for the methods returning primitive values or void, see GwtMockitoClassLoader#onLoad. If the return is a java bean, it will return a mocked version for it.

Eclipse/IntelliJ: static imports of members

I'm refactoring something affecting many bits of my code (huge), and I ended up in needing to organise the import of my project so it can add a static import of a member in a class..
After having spent a full day trying to figure out how to do that, I can't find the way. It's not working neither in IntelliJ or in Eclipse.
In Eclipse I have added it to the Favorites settings, just like it is explained here:
Eclipse Optimize Imports to Include Static Imports
But, it only works on the Control + space, not on Control+Shift+O (to rearrange imports)
In Eclipse 4.2 at least it draws the line red and propose me the suggestion to add the import, but it still doing nothing on the "Organise Import" trigger.
In IntelliJ it is the same, adding the package and the class to the setting, fixes the Control+Alt+Space, but it doesn't deal with the "Optimise Import" feature.
I can't believe this. Has anybody had to deal with this?
You don't want to perform import static on just everything; if the class contains class members that are too general, then an import static will only make your code unreadable. Besides that, there may be name clashes (e.g. two classes having a static getInstance() method would be quite common.
With IntelliJ 13.1.3 you can hit Alt-ENTER, then choose to do a static import (older versions may require one or more CTR-Space before the Alt-ENTER. If you do this on a static member then only the static member is statically imported. If you do it on a class you can do to statically import everything (e.g. import static some.package.SomeClass.*). In the latter case it will remove the class in front of the static imports, unless there is a name clash.
Beware, even if there is no name clash, doing things like SomeInterface someInstance = getInstance() - where getInstance() was statically imported - will get your colleagues very nervous.
Please re-read the question you referred to. You missed the answer mentioning Ctrl + Shift + M. There is a difference between the commands "Organize Imports" and "Add import".

Eclipse C++ formatter puts new line before method identifiers

I ran into a problem with the Eclipse formatter. It won't format my code correctly when declaring methods within a class declaration. It puts a new line after the method's return type.
I already exported the style xml file and examined the settings in it, but none of the settings have any apparent connection to this problem, and the settings editor in Eclipse didn't show the same problem happening in it's sample code for method declarations.
Here is an example bit of code for what I want to have happen:
class MyClass
{
public:
MyClass();
void myMethod();
};
However, this is what I get:
class MyClass
{
public:
MyClass();
void
myMethod();
};
Again, in the styles editor, the code doesn't have this problem and looks just how I want it to, but in the actual code, the story is different.
I'm using version 3.8.0. Any help is appreciated.
Edit: I deleted those source files that were formatted incorrectly (after formatting the code several times to no avail) and replaced them with "identical" files with the same methods, same structure, etc. I formatted the code this time and it worked. This is probably a bug, but I'm leaving it up just in case anyone else encounters a similar problem or has a solution to avoiding this problem in the first place.
I hand edited two files under the main eclipse projects directory
.metadata\.plugins\org.eclipse.core.runtime\.settings
The two files:
file 1: org.eclipse.cdt.core.prefs, change this line from "insert" to "do not insert"
org.eclipse.cdt.core.formatter.insert_new_line_before_identifier_in_function_declaration=do not insert
file 2: org.eclipse.cdt.ui.prefs,
scan this file for "insert_new_line_before_identifier_in_function_declaration" and make a similar change from insert to do not insert next to it, should be obvious
Note I seen this problem on indigo and juno, the fix described above was in juno.
If you have a custom formatter config, export it first (settings>C/C++ General>Formatter>Edit>Export). Then change the following line to "do not insert". Save the XML.
<setting id="org.eclipse.cdt.core.formatter.insert_new_line_before_identifier_in_function_declaration" value="do not insert"/>
Delete the current config and import the one you changed.
There's a specific preference in the formatter options starting from cdt 9.8 included in Eclipse 2019-06.

CA: Suppress results from generated code not working in VS2010 beta 2

I'm trying to run codeanalysis on an assembly that contains an entity model (edmx file). In project properties I have checked the "Suppress results from generated code" option, but I am still getting a lot of CA errors pertaining to the auto-generated EF code.
Has anyone experienced this? And is there a work-around?
Just put the attribute on your class definition.
But how to do it, since your file can get overridden any time. Use a separate file, since all generated classes are partial classes. Open a separate file, and write something like:
[GeneratedCode("EntityModelCodeGenerator", "4.0.0.0")]
public partial class YourEntitiesContextName : ObjectContext
{
}
This will skip code analysis on your particular generated class. StyleCop for instance is more smart and doesn't touch files that have .designer/.generated part in their name or regions that have generated word in their name.
Well, "Suppress results from generated code" really means "Don't look at types with GeneratedCodeAttribute". EF's code generator hasn't added this, historically (though I've suggested it to the team). But you can add it if you use custom T4.