Refactor only a portion of a file?

<oXygen/> general issues.
LaRae_Chasteen
Posts: 1
Joined: Tue Apr 09, 2019 9:14 pm

Refactor only a portion of a file?

Post by LaRae_Chasteen » Tue Apr 09, 2019 9:17 pm

Good day,
Is there any way to perform one of the built-in refactoring operations, but only on selected text within the open file?
Thank you,
LaRae Chasteen

Radu
Posts: 6641
Joined: Fri Jul 09, 2004 5:18 pm

Re: Refactor only a portion of a file?

Post by Radu » Wed Apr 10, 2019 8:11 am

Dear LaRae,

Unfortunately the XML refactor can only be applied on entire documents, not on selected content. I added an internal issue with your feedback and if we ever implement this I will update the forum thread. In the meantime you will probably need to use the "Find/Replace" dialog which works on a selection. Or extract the selected content to a separate file and apply the refactoring operation there (but the selected content needs to be wellformed XML).

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Oleksii
Posts: 51
Joined: Wed Jul 19, 2017 6:04 pm
Location: Austria
Contact:

Re: Refactor only a portion of a file?

Post by Oleksii » Tue Nov 05, 2019 7:10 pm

Are there any news on this topic in the meantime? This kind of feature would be extremely useful.
It would be nice to be able to customize something like currently available Surround with tags which you can invoke with a shortcut.

Radu
Posts: 6641
Joined: Fri Jul 09, 2004 5:18 pm

Re: Refactor only a portion of a file?

Post by Radu » Tue Nov 05, 2019 10:41 pm

Hi Oleksii,

If you are working with the XML doc in the Author visual editing mode, you can create custom Author operation based on our predefined XSLT and XQuery operations:

https://www.oxygenxml.com/doc/versions/ ... yoperation

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Oleksii
Posts: 51
Joined: Wed Jul 19, 2017 6:04 pm
Location: Austria
Contact:

Re: Refactor only a portion of a file?

Post by Oleksii » Mon Nov 11, 2019 12:40 pm

Hi Radu,

yes, I know about this possibility in Author mode and it would be an alternative. The point is that we are working with XML in MEI (https://dme.mozarteum.at/movi/navigator/525/001/01) namespace. Currently, we do not have an appropriate CSS for displaying it which could be also a bit tricky because an MEI document structure consists mainly of elements and their attributes. So we are working in the text mode only.

Regards,
Oleksii

Radu
Posts: 6641
Joined: Fri Jul 09, 2004 5:18 pm

Re: Refactor only a portion of a file?

Post by Radu » Tue Nov 19, 2019 3:56 pm

Hi Oleksii,

I understand, the only work around right now is to create an Oxygen plugin which adds some contextual menu actions in the Text editing mode which obtain the selected content, process it using XSLT and then replace the selection with the processed content.
We have lots of API, you can implement Oxygen plugins even using Javascript:

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

If you have someone willing to work on this I can guide them.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Oleksii
Posts: 51
Joined: Wed Jul 19, 2017 6:04 pm
Location: Austria
Contact:

Re: Refactor only a portion of a file?

Post by Oleksii » Tue Nov 19, 2019 6:50 pm

Hi Radu,

thank you, I'll take a closer look then.
As an even shorter workaround could be also to use a code template (options >> templates >> code templates) with ${selection} but especially if it could be evaluated through
${xpath_eval()}.
I tried to do

Code: Select all

${xpath_eval(
 ${selection}//* 
)}
or

Code: Select all

${xpath_eval(
let $n := ${selection}
return $n
)}
just to access the selection but obviously it is not serialized as an element. I also searched for XPath functions to convert string to a node-set or document node but didn't find anything. Maybe if you look behind the scenes of the ${selection} and ${xpath_eval()} you'll have an idea?

Regards,
Oleksii

Radu
Posts: 6641
Joined: Fri Jul 09, 2004 5:18 pm

Re: Refactor only a portion of a file?

Post by Radu » Wed Nov 20, 2019 10:49 am

Hi Oleksii,

If you want to go down that path of using xpath_eval, there is a Saxon function called parse-xml, it's used something like:

Code: Select all

${xpath_eval(parse-xml('${selection}')//*)}
but from my tests it seems the serialized output contains some extra namespaces, I managed to remove them with something like:

Code: Select all

${xpath_eval(
replace(
'${xpath_eval(parse-xml('${selection}')//*)}', 
' xmlns:xml="http://www.w3.org/XML/1998/namespace"', 
'')
)}
I will add an internal issue to see why those xmlns:xml namespace mappings get serialized.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Oleksii
Posts: 51
Joined: Wed Jul 19, 2017 6:04 pm
Location: Austria
Contact:

Re: Refactor only a portion of a file?

Post by Oleksii » Thu Nov 21, 2019 12:15 pm

Hi Radu,

I solved my particular problem for now as I needed the following value:

Code: Select all

${xpath_eval(
	(parse-xml('${selection}')//*[@xml:id])[1]/@xml:id/substring-after(., '_')
)}
But the example with replace() didn't work for me. I played around, also checking maybe the problem is something with a sequence handling but it didn't work either as I'm getting an empty sequence as a result of the evaluation:

Code: Select all

${xpath_eval(
		for $n in
			parse-xml('${selection}')//*
		return
			replace($n, ' xmlns:xml="http://www.w3.org/XML/1998/namespace"', 
		'' )
)}
Just replace() works, however:

Code: Select all

${xpath_eval(
	replace('abc', 'a', '')
)}
here is an xml snippet:

Code: Select all

<mei meiversion="3.0.0" xmlns="http://www.music-encoding.org/ns/mei">
	<staff dme.parts="11" n="7" xml:id="staff_18930">
		<layer n="1" xml:id="layer_18936">
			<mRest tstamp="1" xml:id="mRest_18942"/>
		</layer>
	</staff>
</mei>
The result is :

Code: Select all

<mei meiversion="3.0.0" xmlns="http://www.music-encoding.org/ns/mei">
	
		
			
		
	
			
		
</mei>

Radu
Posts: 6641
Joined: Fri Jul 09, 2004 5:18 pm

Re: Refactor only a portion of a file?

Post by Radu » Fri Nov 22, 2019 3:26 pm

Hi Oleksii,

Running replace on a node like you do:

Code: Select all

replace($n, ' xmlns:xml="http://www.w3.org/XML/1998/namespace"
is most probably invalid, you can run the replace on plain strings but once you have called the parse-xml, you have nodes there and not serialized XML strings.

This is why in the approach I gave you I first run the xpath evaluation to obtain the node set and the return of the method serializes the node set to an XML string, then I use an outer xpath evaluation which uses replace to replace in that returned string the xml prefix mapping which should not be there in the first place. So maybe you can take a look again at my example which uses the xpath_eval twice, an xpath eval which replaces and the inner xpath eval which actually does the processing.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Oleksii
Posts: 51
Joined: Wed Jul 19, 2017 6:04 pm
Location: Austria
Contact:

Re: Refactor only a portion of a file?

Post by Oleksii » Tue Nov 26, 2019 7:30 pm

Hi Radu,

the first thing I did was copy+paste of your code examples. :) As it didn't work for me (still getting empty string) I tried to play around.
I also noticed that you use xpath_eval() twice. It is strange it doesn't work for me.
oXygen 20.1

Regards,
Oleksii

Radu
Posts: 6641
Joined: Fri Jul 09, 2004 5:18 pm

Re: Refactor only a portion of a file?

Post by Radu » Wed Nov 27, 2019 9:17 am

Hi Oleksii,

In Oxygen 21.1 we made a major change in how our editor variables are evaluated, resulting in more flexibility when it comes to computing such constructs. So I was using in my tests Oxygen 21.1.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Post Reply