skip auto edit of CompoundBracesBlocks in xtext ? - xtext

I have DSL that has comments like
{***** this is comment
When i type comment like {** and hit enter xtext autoedit provide } to close open {, I Can disable it by
commenting configureCompoundBracesBlocks method
#Override
protected void configureCompoundBracesBlocks(IEditStrategyAcceptor acceptor) {
// acceptor.accept(compoundMultiLineTerminals.newInstanceFor("{", "}").and("[", "]").and("(", ")"), IDocument.DEFAULT_CONTENT_TYPE);
}
But I want to auto close for remain all syntax. Is there any way to crack it down ??
here is my screen-cast how it like when i type comment on my DSL
Also here is my code what it like to get configureCompoundBracesBlocks
public class MyAutoEditStrategyProvider extends DefaultAutoEditStrategyProvider {
#Override
protected void configure(IEditStrategyAcceptor acceptor) {
configureCompoundBracesBlocks(acceptor);
}
#Override
protected void configureCompoundBracesBlocks(IEditStrategyAcceptor acceptor) {
acceptor.accept(compoundMultiLineTerminals.newInstanceFor("{", "}").and("[", "]").and("(", ")"), IDocument.DEFAULT_CONTENT_TYPE);
}
}

Related

Why hookOnComplete() not being called sometimes in my case?

I have a really complicated flux here, something like this:
private Object run(Flux<Pair<String, Flux<Record>>> flux) throws InterruptedException {
BlockingQueue<Object> result = new SynchronousQueue<>();
flux.subscribeOn(Schedulers.boundedElastic())
.parallel(2, 1)
.runOn(Schedulers.boundedElastic())
.flatMap(pair -> pair.getRight().parallel()
.runOn(Schedulers.boundedElastic())
.flatMap(record -> stringToError(record))
.doOnComplete(() -> System.out.println("Complete " + pair.getLeft()))) // #log
.sequential()
.buffer(1000)
.subscribe(new BaseSubscriber<List<Error>>() {
#Override
protected void hookOnComplete() {
result.offer(Boolean.TRUE);
}
#Override
protected void hookOnError(Throwable throwable) {
result.offer(throwable);
}
#Override
protected void hookOnCancel() {
throw new IllegalStateException();
}
});
return result.take();
}
It runs pretty well except that sometimes it blocked forever.
NO hookOnComplete(), NO hookOnError(), NO hookOnCancel().
And when this happened, line #log does already print all data I feed in. That's so strange.
I don't know how to deal with this.
Can anyone tells me what can I do and what could cause this?
By the way, I use reactor-core 3.3.2 here.

How is a listener added for a specific keyword?

I have a parser grammar and lexer grammar from which ANTLR4 4.7 generates a parser and lexer, no problem. The entry points in the listener logic are great but I'd like to listen for specific keywords in the language. No idea where to start adding that.
I am using the ANTLR4 plugin for Eclipse to generate the parser and lexer from the grammar.
At the bottom of the generated xyzBaseListener there are 4 non-specific methods that you can override to suit your need:
#Override public void enterEveryRule(ParserRuleContext ctx) { }
#Override public void exitEveryRule(ParserRuleContext ctx) { }
#Override public void visitTerminal(TerminalNode node) { }
#Override public void visitErrorNode(ErrorNode node) { }
e.g.
public class Tester extends xyzBaseListener
{
// ... other useful stuff
#Override public void visitTerminal(TerminalNode node) {
System.out.println("Saw token '" + node.getText() + "'");
}
}
The reference book has examples for how to subclass the base listener and wire up the listener.

Vaadin: MouseDown/MouseUp and KeyDown/KeyUp evens

Is it possible to handle MouseDown/MouseUp and KeyDown/KeyUp evens with Vaadin? I've found forum thread with the same question and looks like the answer is no, but it was 5 years ago - I hope something changed with later releases. Still I can't find anything in API. Maybe there's some workaround for intercepting such evens?
Well, after couple of days I came up with the acceptable (for me) solution. Required component has to be wrapped with extension-interceptor (credits to #petey for an idea in the comments) with KeyDownHandler inside. But the trick is not to add to the component itself (because it can miss triggering), but to the RootPanel. So here's a working example.
Extension:
public class InterceptorExtension extends AbstractExtension {
private boolean shiftKeyDown;
public InterceptorExtension(Tree tree) {
super.extend(tree);
registerRpc((InterceptorExtensionServerRpc) state -> shiftKeyDown = state);
}
public boolean isShiftKeyDown() {
return shiftKeyDown;
}
}
ServerRpc:
public interface InterceptorExtensionServerRpc extends ServerRpc {
void setShiftKeyDown(boolean state);
}
Connector:
#Connect(InterceptorExtension.class)
public class InterceptorExtensionConnector extends AbstractExtensionConnector {
#Override
protected void extend(final ServerConnector target) {
final InterceptorExtensionServerRpc rpcProxy = getRpcProxy(InterceptorTreeExtensionServerRpc.class);
final RootPanel rootPanel = RootPanel.get();
rootPanel.addDomHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
if (event.isShiftKeyDown()) {
rpcProxy.setShiftKeyDown(true);
}
}
}, KeyDownEvent.getType());
rootPanel.addDomHandler(new KeyUpHandler() {
#Override
public void onKeyUp(KeyUpEvent event) {
if (!event.isShiftKeyDown()) {
rpcProxy.setShiftKeyDown(false);
}
}
}, KeyUpEvent.getType());
}
}
Then whenever you want you can get Shift-button state on the server-side via InterceptorExtension#isShiftKeyDown.

How to generate code for a XExpression subtype?

I have a simple DSL that should generate async code for expressions (this is the simplest example I could come up with to illustrate my point). I just added to the scripting example an new async statement:
grammar org.xtext.scripting.Scripting with org.eclipse.xtext.xbase.Xbase
generate scripting "http://www.xtext.org/scripting/Scripting"
import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase
Script returns xbase::XBlockExpression:
{Script}
(expressions+=XExpressionOrVarDeclaration ';'?)*;
XExpression returns xbase::XExpression:
super | Async
;
Async:
'async' expression=XExpression
;
The idea would be that the async code is executed in another thread.
My question is, how can I generate code for the Async.expression using the ScriptingJvmModelInferrer?
In the simplest case I would just wrap the code from the Async.expression like this?
AsyncRunner.exec(new Runnable() {
#Override
public void run() {
// the Async.expression would end up here
}
})
Where is the hook to do that?
You have to make 3 changes:
Extend the compiler to deal with your language. The key point is to handle the Async expression.
class ScriptingCompiler extends XbaseCompiler {
override protected doInternalToJavaStatement(XExpression expr, ITreeAppendable it, boolean isReferenced) {
switch expr {
Async : {
newLine
append('''
AsyncRunner.exec(new Runnable() {
#Override
public void run() {''')
expr.expression.doInternalToJavaStatement(it, false)
newLine
append('}});')
}
default :
super.doInternalToJavaStatement(expr, it, isReferenced)
}
}
override protected internalToConvertedExpression(XExpression obj, ITreeAppendable it) {
if (hasName(obj))
append(getName(obj))
else
super.internalToConvertedExpression(obj, it)
}
}
The type of the expression has to be specified
class ScriptingTypeComputer extends XbaseWithAnnotationsTypeComputer {
override computeTypes(XExpression expression, ITypeComputationState state) {
if(expression instanceof Async) {
super.computeTypes(expression.expression, state);
} else {
super.computeTypes(expression, state)
}
}
}
Both extensions have to be injected:
class ScriptingRuntimeModule extends AbstractScriptingRuntimeModule {
def Class<? extends XbaseCompiler> bindXbaseCompiler() {
return ScriptingCompiler
}
def Class<? extends ITypeComputer> bindITypeComputer() {
return ScriptingTypeComputer
}
}
If you extend Xbase you ususally don't apapt the JvmModelInferrer for Compilation but you extend XbaseTypeComputer and XbaseCompiler.doInternalToJavaStatement/internalToConvertedExpression (depending on what you actually introduce)

AndroidAnnotations in CursorAdapter

I am developing for android using android annotations but I don't unterstand how to use it with CursorAdapters.
There is already a example for BaseAdapters, but if I add #EBean to a class that extents CursorAdapter I get the error message "#EBean annotated element should have a constructor with one parameter max, of type android.content.Context". CursorAdapter already has two constructors.
public class SongInfoAdapter extends CursorAdapter {
...
#Override
public void bindView(View view, Context context, Cursor cursor) {
...
rowData.id.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
itemOnClick(rowData);
}
});
}
public void itemOnClick(RowDataHolder rowData) {
switch(audioPlayer.getPlayingplayer()) {
case AudioPlayer.RIGHT:
case AudioPlayer.NONE:
audioPlayer.load(rowData.songInfo, AudioPlayer.LEFT);
break;
case AudioPlayer.LEFT:
audioPlayer.load(rowData.songInfo, AudioPlayer.RIGHT);
break;
}
}
...
}
AudioPlayer is a class that uses annotations (#EBean), but I can't write
#Bean
AudioPlayer audioPlayer;
because I can't use annotations in this class. How can I use AndroidAnnotations in CursorAdapter?
Many thanks in advance .
Create a constructor that takes one argument, the context.
SongInfoAdapter (Context context) {
super(context, null, FLAG_AUTO_REQUERY);
}
Create an init method and set the cursor for the adapter in init.
public void init(Cursor c) {
this.changeCursor(c);
}
Now you can annotate SongInfoAdapter with #EBean and use annotations.

Resources