How to get code statements without braces in SSR?
Answered
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) { $code$ };" recursive="false" type="JavaScript" reformatAccordingToStyle="false" shortenFQN="false" replacement="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; 	$expression$ };">
<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=""gridOnChar.getText().replace('this.', '').replace('_OnChar', '');"" />
<variableDefinition name="expression" script=""__log__.info(code)"" />
</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.
Please sign in to leave a comment.
Hello,
Have you tried to add braces in search pattern ("{ $code$ }"), so it will contain only code?
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$};It's very strange because if I remove these lines from the original code it works fine.
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.
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) { 	$code$ };" recursive="false" type="JavaScript" reformatAccordingToStyle="false" shortenFQN="false" replacement="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$ };"><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=""gridOnChar.getText().replace('this.', '').replace('_OnChar', '');"" />
<variableDefinition name="expression" script=""__log__.info(code)"" />
</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.
Could you please assist with this case?
What do you mean by assist?
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.
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.
To remove
this.you can usegridOnChar.getReferenceName().replace('_OnChar', '')for the script. But your "workaround" script works just as well.