(ab)using "show if" to modify the AST.

Hello,

I have a somewhat peculiar use for 'show if'.  It works, but it feels kind of wrong. :-)  I'd like to know if what I'm doing is okay.

Basically, I am trying to model the 'using' namespace construct in C#.  It has two forms:

using << namespace >>;

using << namespace alias >> = << namespace >>;

My structure has two children/properties - a required namespace and an optional namespaceAlias.

My node cell layout looks like so:

[> using ?[> { namespaceAlias } = <] % namespace % ; <]

If the user begins by typing "using myAlias", so far, the editor believes the user has been filling in the namespace.

However, the instant the user hits the = sign for the first time, what they have been typing so far is actually the namespace alias, not the namespace, and I have to play a little trickery to switch them around.

I am currently doing this by (ab)using "show if" on the ?[> { namespaceAlias } = <] part of my editor.

show if: (scope, editorContext, node)->boolean {

  if (node.namespaceAlias != null) { return true; }

  if (node.namespace.name != null && node.namespace.name.endsWith("=")) {

    node.namespaceAlias = node.namespace.name;

    node.namespaceAlias = node.namespaceAlias.substring(0, node.namespaceAlias.length() - 1);

    node.namespace.name = null;

    return true;

  }

  return false;

}



This works... but I can't help but wonder if there's a better way to do this.  Is it safe to modify the AST from a 'show if' like this?

2 comments
Comment actions Permalink

Hello, Robert,

This looks like a hack. It's better not to modify code from read/only parts like show if blocks.

You can implement what you described in a better way. You can use left/right transforms. You can read about them in the documentation http://www.jetbrains.net/confluence/display/MPS/Editor+Actions#EditorActions-sidetransform You can take a look at an example which is very similar to what you want: LocalVariableDeclaration in baseLanguage (see the variableDeclarationInitilalizer root in the baseLanguage's action model).

Regards,

Konstantin

0
Comment actions Permalink

Hello, Konstantin,

Thank you!  This looks like exactly what is needed.  I will re-implement this using a side transform.

Thanks again! :-)

Robert

0

Please sign in to leave a comment.