Page 1 of 1

${expand(...)} editor variable

Posted: Mon Dec 06, 2021 6:11 pm
by Krille
Hi,

right now, oXygen allows to set user-defined editor variables in a
global or project-specific scope, e.g. ${myvar}. And it throws an
exception, when trying to access an editor variable, that was not
initialized (set). It would be cool, to have an editor variable (or a
function) that would try to get a variable by name and if that
variable was not initialized, a default value would be
returned. Having such a variable (or function) would greatly leverage
framework writing, because it would enable us to write parametrizable
author actions, thus more generic frameworks.

The thing I have in mind would be something like,

Code: Select all

${expand(VARIABLE_NAME, DEFAULT)}
and would expand to the value of the variable called VARIABLE_NAME if
that variable is present, and to DEFAULT, if that variable is not
present.

An alternative improvement would be, to enable shipping variable
initialization along with a framework, while also allowing to override
such variables by setting them again (be it in a global or
project-specific way).

Could you imagine, adding such a feature? Or are there simple
alternatives?

Regards,
Chris

Re: ${expand(...)} editor variable

Posted: Tue Dec 07, 2021 11:43 am
by Radu
Hi Chris,

I added an internal issue to consider such an expand with fallback function:
EXM-49470 Editor variable function which provides a fallback if variable not defined

One thing which can be done is to use our API to provide your own editor variables expansion mechanism from a plugin.
For example this sample plugin for Oxygen provides a ${clipboard} editor variable expansion code via Javascript:

https://github.com/oxygenxml/wsaccess-j ... rVariables

Regards,
Radu

Re: ${expand(...)} editor variable

Posted: Tue Dec 14, 2021 6:15 pm
by Krille
Hi Radu,

thanks for this hint! After few days now, I got used to reading oXygen's Java API.

I found, that I can use code like this for trying to access editor variables in custom author mode actions:

Code: Select all

public class MyCustomOperation
    implements AuthorOperation {
    
    /* ... */
    
        public void doOperation(AuthorAccess authorAccess, ArgumentsMap args)
		throws AuthorOperationException, IllegalArgumentException {
		
		/* ... */
		
		UtilAccess utilAccess = authorAccess.getUtilAccess();
		String myvar = utilAccess.expandEditorVariables("${myvar}", null);

		/* ... */
	}
}
If the ${myvar} custom editor variable has not been initialized, it's name is returned by expandEditorVariables(). That's a bit weird (null or an Exception would be cleaner) but it works.

BTW: I do not use an custom EditorVariablesResolver, because the variable names are dynamically concatenated of prefixes and context read in the edited file.

Regrads,
Chris

Re: ${expand(...)} editor variable

Posted: Wed Dec 15, 2021 7:40 am
by Radu
Hi Chris,

Right. The "expandEditorVariables" method can also be called on any string like "abc${someVar1}def${someVar2}" and it's not feasible for it to return null if it cannot resolve a single variable, it just leaves the variable as it is because it may not be an Oxygen editor variable at all, it may be some user defined string literal which looks like an editor variable.

Regards,
Radu