How to get code statements without braces in SSR?

已回答

I wanted to replace functions with specific patterns with SSR. Below is what is tried to. 

 

Search & Replace templates: 

this.$gridOnChar$ = function(obj, strPreText, nChar, $strPostText$, nLLParam, nHLParam, $nRow$, $nCell$, nPivotIndex) {
$code$
};
this.$grid$_onkey = function(obj: nexacro.Grid, e: nexacro.KeyEventInfo) {
console.info('$grid$_onkey:obj', obj);
console.info('$grid$_onkey:e', e);
if (typeof e.fromreferenceobject.value == 'undefined') return;
var $strPostText$ = e.fromreferenceobject.value;
var $nRow$ = obj.currentrow;
var $nCell$ = obj.currentcell;
$code$
};

Exported as XML: 

<replaceConfiguration name="test" text="this.$gridOnChar$ = function(obj, strPreText, nChar, $strPostText$, nLLParam, nHLParam, $nRow$, $nCell$, nPivotIndex) {&#10;    $code$&#10;};" recursive="false" type="JavaScript" reformatAccordingToStyle="false" shortenFQN="false" replacement="this.$grid$_onkey = function(obj: nexacro.Grid, e: nexacro.KeyEventInfo) {&#10;&#9;console.info('$grid$_onkey:obj', obj);&#10;&#9;console.info('$grid$_onkey:e', e);&#10;&#9;if (typeof e.fromreferenceobject.value == 'undefined') return;&#10;&#9;var $strPostText$ = e.fromreferenceobject.value;&#10;&#9;var $nRow$ = obj.currentrow;&#10;    var $nCell$ = obj.currentcell;&#10;&#9;$expression$&#10;};">
  <constraint name="__context__" within="" contains="" />
  <constraint name="gridOnChar" regexp=".+_OnChar$" within="" contains="" />
  <constraint name="strPostText" within="" contains="" />
  <constraint name="nCell" within="" contains="" />
  <constraint name="nRow" within="" contains="" />
  <constraint name="code" within="" contains="" />
  <variableDefinition name="grid" script="&quot;gridOnChar.getText().replace('this.', '').replace('_OnChar', '');&quot;" />
  <variableDefinition name="expression" script="&quot;__log__.info(code)&quot;" />
</replaceConfiguration>

 

I expected it will be something like this. 

this.grdMenuList1_onkey = function(obj: nexacro.Grid, e: nexacro.KeyEventInfo) {
console.info('grdMenuList1_onkey:obj', obj);
console.info('grdMenuList1_onkey:e', e);
if (typeof e.fromreferenceobject.value == 'undefined') return;
var strPostText = e.fromreferenceobject.value;
var nRow = obj.currentrow;
var nCell = obj.currentcell;
// some comments...
console.info('grdMenuList1_OnChar:nCell:' + nCell + '|strPostText:' + strPostText);
if (nCell == 0) {
this.dsLangMenu.setColumn(0, "NM", strPostText);
}
};

 

But, actual result is like this. 

this.grdMenuList1_onkey = function(obj: nexacro.Grid, e: nexacro.KeyEventInfo) {
console.info('grdMenuList1_onkey:obj', obj);
console.info('grdMenuList1_onkey:e', e);
if (typeof e.fromreferenceobject.value == 'undefined') return;
var strPostText = e.fromreferenceobject.value;
var nRow = obj.currentrow;
var nCell = obj.currentcell;
{
// some comments...
console.info('grdMenuList1_OnChar:nCell:' + nCell + '|strPostText:' + strPostText);
if (nCell == 0) {
this.dsLangMenu.setColumn(0, "NM", strPostText);
}
}
};

 

How can I get the code statements part(including comments) without braces with a groovy script or other conditions in SSR? 

Thanks in advances.

0

Hello,

Have you tried to add braces in search pattern ("{ $code$ }"), so it will contain only code?

0

Hi @...

Do you mean something like this? I tried all the below ways and it didn't work :(

this.$gridOnChar$ = function(obj, strPreText, nChar, $strPostText$, nLLParam, nHLParam, $nRow$, $nCell$, nPivotIndex) {
{$code$}
};
this.$gridOnChar$ = function(obj, strPreText, nChar, $strPostText$, nLLParam, nHLParam, $nRow$, $nCell$, nPivotIndex) {
"{$code$}"
};
this.$gridOnChar$ = function(obj, strPreText, nChar, $strPostText$, nLLParam, nHLParam, $nRow$, $nCell$, nPivotIndex) {
("{$code$}")
};
this.$gridOnChar$ = function(obj, strPreText, nChar, $strPostText$, nLLParam, nHLParam, $nRow$, $nCell$, nPivotIndex) {$code$};

 

0

It's very strange because if I remove these lines from the original code it works fine. 

    // some comments...
console.info('grdMenuList1_OnChar:nCell:' + nCell + '|strPostText:' + strPostText);
0

What IDE version do you use? It seems to be working fine on my side with test project. If it's possible please attach sample project example for investigation.

0

Sorry for the late response. 

I uploaded a sample source code on GitHub. 

https://github.com/wonsuc/ssr-test

Below is my SSR template which I tried. 

<replaceConfiguration name="test" text="this.$gridOnChar$ = function(obj, strPreText, nChar, $strPostText$, nLLParam, nHLParam, $nRow$, $nCell$, nPivotIndex) {&#10;&#9;$code$&#10;};" recursive="false" type="JavaScript" reformatAccordingToStyle="false" shortenFQN="false" replacement="this.$grid$_onkey = function(obj: nexacro.Grid, e: nexacro.KeyEventInfo) {&#10;&#9;console.info('$grid$_onkey:obj', obj);&#10;&#9;console.info('$grid$_onkey:e', e);&#10;&#9;if (typeof e.fromreferenceobject.value == 'undefined') return;&#10;&#9;var $strPostText$ = e.fromreferenceobject.value;&#10;&#9;var $nRow$ = obj.currentrow;&#10;&#9;var $nCell$ = obj.currentcell;&#10;&#9;$code$&#10;};">
  <constraint name="__context__" within="" contains="" />
  <constraint name="gridOnChar" regexp=".+_OnChar$" within="" contains="" />
  <constraint name="strPostText" within="" contains="" />
  <constraint name="nCell" within="" contains="" />
  <constraint name="nRow" within="" contains="" />
  <constraint name="code" within="" contains="" />
  <variableDefinition name="grid" script="&quot;gridOnChar.getText().replace('this.', '').replace('_OnChar', '');&quot;" />
  <variableDefinition name="expression" script="&quot;__log__.info(code)&quot;" />
</replaceConfiguration>

Below is my IntelliJ version. (I tried it with the EAP version at the workplace and it's not working either.)

IntelliJ IDEA 2022.2.1 (Ultimate Edition)
Build #IU-222.3739.54, built on August 16, 2022
Runtime version: 17.0.3+7-b469.37 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.4
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 10
Metal Rendering is ON
Non-Bundled Plugins:
    com.intellij.ko (222.168)
    io.codearte.props2yaml (1.0.2)
    cn.sugarapp.plugins.yaml2props (1.0.1)
    com.jetbrains.php (222.3739.61)
    cn.yiiguxing.plugin.translate (3.3.5)
    LivePlugin (0.8.1 beta)

Kotlin: 222-1.7.10-release-334-IJ3739.54

Thank you for your supporting.

0
Hello!

Could you please assist with this case?
0

What do you mean by assist?

0

Sorry, it was a message for developers.

It appears to be a bug where the $code$ variable matches the entire function body. https://youtrack.jetbrains.com/issue/IDEA-303994
As a workaround introduce an extra $code2$ variable in the body.

<replaceConfiguration name="this.$gridOnChar$ = function(obj, strPre…" uuid="098f6bcd-4621-3373-8ade-4e832627b4f6" text="this.$gridOnChar$ = function(obj, strPreText, nChar, $strPostText$, nLLParam, nHLParam, $nRow$, $nCell$, nPivotIndex) {&#10;  $code1$&#10;&#9;$code2$&#10;};" recursive="false" type="JavaScript" search_injected="false" reformatAccordingToStyle="false" shortenFQN="true" replacement="this.$grid$_onkey = function(obj: nexacro.Grid, e: nexacro.KeyEventInfo) {&#10;&#9;console.info('$grid$_onkey:obj', obj);&#10;&#9;console.info('$grid$_onkey:e', e);&#10;&#9;if (typeof e.fromreferenceobject.value == 'undefined') return;&#10;&#9;var $strPostText$ = e.fromreferenceobject.value;&#10;&#9;var $nRow$ = obj.currentrow;&#10;&#9;var $nCell$ = obj.currentcell;&#10;&#9;$code1$&#10;  $code2$&#10;};">
  <constraint name="__context__" within="" contains="" />
  <constraint name="gridOnChar" regexp=".+_OnChar$" within="" contains="" />
  <constraint name="strPostText" within="" contains="" />
  <constraint name="nCell" within="" contains="" />
  <constraint name="nRow" within="" contains="" />
  <constraint name="code1" within="" contains="" />
  <constraint name="code2" minCount="0" maxCount="2147483647" within="" contains="" />
  <variableDefinition name="grid" script="&quot;gridOnChar.getText().replace('this.', '').replace('_OnChar', '');&quot;" />
</replaceConfiguration>
0

Thank you so much! It works like a charm. 

Can I ask one more question? 

I only wanted to get 'grdMenuList1_OnChar' string in $gridOnChar$ variable, but it always like 'this.grdMenuList1_OnChar' with including 'this' scope. 

I tried many ways like below. 

1. this.$gridOnChar$ (regex for $gridOnChar$: .*_OnChar$) => Not working. 

2. $scope$.$gridOnChar$ (regex for $gridOnChar$: .*_OnChar$) => Not working. 

3. $gridOnChar$ (regex for $gridOnChar$: (?<=this\.).*_OnChar$) => Not working. 

As a workaround I'm just using '.replace("this.", "")' script to get rid of 'this.' for the replaced template.

I'm very appreciative of your support. 

0

To remove this. you can use gridOnChar.getReferenceName().replace('_OnChar', '') for the script. But your "workaround" script works just as well.

0

请先登录再写评论。