Setting editor selection

Ask general questions here.
klaymore142
Posts: 7
Joined: Wed Jul 18, 2018 6:56 pm

Setting editor selection

Post by klaymore142 » Wed Jul 18, 2018 7:47 pm

We are currently building some UI tests for an IntelliJ plugin. One thing we need to be able to do is set the selection in an editor in IntelliJ - the editor contains code, and we want to find a snippet of code and select it.
The editor is a Java JComponent, which we are able to access in a user-code method as a JavaElement. I tried invoking a method on the component, but I always get "Specified method is not supported". Here is my example code:
public static void FindLine(RepoItemInfo editor, String snippet, int selectStart, int selectEnd)
        {
        	String editorText = editor.FindAdapter<JavaElement>().Element.GetAttributeValueText("Text");
        	String[] lines = editorText.Split(new char[] {'\n'});
        	int lineStart = 0;
        	for (int i = 0; i < lines.Length; i++) {
        		if (lines.Contains(snippet)) {
        			int begin = lineStart + selectStart;
        			int end = begin + selectEnd;
        			Report.Log(ReportLevel.Info, "User", "Found snippet '" + snippet + "' on line " + (i+1) + ". Selecting " + begin + ":" + end);
        			int current = (int)element.InvokeMethod("getCaretPosition", new Object[0]);
        			Report.Log(ReportLevel.Info, "User", "Current caret position: " + current);
        			JavaObjectWrapper javaEditor = (JavaObjectWrapper)editor.FindAdapter<JavaElement>().InvokeMethod("getEditor", new Object[0]);
        			JavaObjectWrapper selectionModel = (JavaObjectWrapper)javaEditor.GetProperty("mySelectionModel");
        			selectionModel.Invoke("setSelection", new Object[] {begin, end});
        			return;
        		}
        		lineStart += lines.Length + 1;
        	}
        	Validate.Fail("The snippet '" + snippet + "' was not found in the editor");
        }


You can see that I have tried several things - getCaretPosition() is a method on the EditorComponentImpl (the IntelliJ editor class which extends JTextComponent), as is getEditor(). I don't actually care about getCaretPosition(), but I tried it since it returns an int and should be working.

What am I missing?

Windows 7, Ranorex 8.1.1, C#

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Setting editor selection

Post by Support Team » Tue Jul 24, 2018 6:21 am

Hi klaymore142,

Thank you for reaching out to us in regards to setting a selection of text, I'd be happy to help.

A Ranorex snapshot would be extremely helpful so that I can analyze the object and its attributes. If needed, you can learn how to create a Ranorex snapshot here: https://www.ranorex.com/support/user-gu ... pshot.html

Are you seeing any of the methods you are trying to use in the dynamic capabilities for the object in Ranorex Spy? To find these dynamic capabilities, track the object and then within the advanced tab go to General - > Dynamic Capability -> Actions -> '...' to view the collection as seen below:

dynamic.png

I hope this helps and I look forward to hearing from you!

Regards,

Jon
You do not have the required permissions to view the files attached to this post.

klaymore142
Posts: 7
Joined: Wed Jul 18, 2018 6:56 pm

Re: Setting editor selection

Post by klaymore142 » Wed Jul 25, 2018 6:29 pm

Hi Jon,

Thanks for your reply. I have attached a snapshot of the application, with the editor open. Hopefully this will help.

To answer your question, no I do not see the methods I am trying to call when browsing the Actions collection for the Element. I do see a number of other methods which are defined on JTextComponent, such as "select" - but not the methods I need, such as "setSelectionStart". Note that both methods are declared as public void methods, and are defined in JTextComponent (not EditorComponentImpl).
Code.gif
actions.gif
IntelliJWithEditorObject.rxsnp
You do not have the required permissions to view the files attached to this post.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Setting editor selection

Post by Support Team » Fri Jul 27, 2018 10:40 pm

Hi klaymore142,

Thank you for the snapshot, it was very helpful!

I started experimenting with the standard IntelliJ editor this and found some attributes which looked very promising:

SelectionStart
SelectionEnd
SelectedText

I was able to pull values from these attributes and everything looked fine, but setting the value on these attributes did not work as well as I had hoped. I too was not able to invoke the methods you are looking for.

Would you mind creating a support query so that we may continue with a direct form of communication? Our support portal may be found at the below link and I ask that you please reference this forum post within your inquiry:

https://www.ranorex.com/support-query/

I hope this helps and I look forward to hearing from you!

Regards,

Jon