Page 1 of 1

Custom plugin: adding attributes in the specified order

Posted: Tue Oct 11, 2022 2:43 pm
by honyk
Dear All,

in my custom plugin action, I am trying to set several attributes, but all of them are sorted alphabetically in the result XML code. Is there any way to keep the insertion order?
For example, if my 'ulink' element has 'url' attribute and additional 'type' attribute is added, it is placed before the 'url', not after it.

I am adding attributes using this command:

Code: Select all

 authorAccess.getDocumentController().setAttribute("type", new AttrValue("variable"), uLinkNode);
Thanks, Jan

Re: Custom plugin: adding attributes in the specified order

Posted: Tue Oct 11, 2022 3:11 pm
by Radu
Hi Jan,

The attributes order is not relevant for the semantic meaning of the XML document.
From what I think the Author mode should kind of preserve by default the order in which the attributes were added.
Is it possible that in the Preferences->"Editor / Format / XML" page you have checked the "Sort attributes" checkbox?

Regards,
Radu

Re: Custom plugin: adding attributes in the specified order

Posted: Tue Oct 11, 2022 4:34 pm
by honyk
That 'Sort attributes' option is disabled. I agree the order is just a cosmetic issue. My action is a kind of shortcut to common manual action. Such side-effect breaks the consistency :-(

Re: Custom plugin: adding attributes in the specified order

Posted: Wed Oct 12, 2022 7:56 am
by Radu
Hi,

So I made this operation to experiment a bit:

Code: Select all

  public void doOperation(AuthorAccess authorAccess, ArgumentsMap args)
      throws AuthorOperationException {
    try {
      AuthorElement elem = (AuthorElement) authorAccess.getDocumentController().getNodeAtOffset(authorAccess.getEditorAccess().getCaretOffset());
      authorAccess.getDocumentController().setAttribute("zz", new AttrValue("val1"), elem);
      authorAccess.getDocumentController().setAttribute("yy", new AttrValue("val2"), elem);
      authorAccess.getDocumentController().setAttribute("aa", new AttrValue("val3"), elem);
    } catch (BadLocationException e) {
      e.printStackTrace();
    }
  }
I invoked it on an element in the Author mode and the XML in the Text editing mode looks like this:

Code: Select all

<body zz="val1" yy="val2" aa="val3"/>
So the order in which you add the attributes is obeyed at serialization.

Regards,
Radu

Re: Custom plugin: adding attributes in the specified order

Posted: Wed Oct 12, 2022 10:02 am
by honyk
You are correct! During my experiments, I haven't switched to Text mode and now I can see it is correct there all the time.
I've finally found a dedicated option for this behavior: Preferences...|Editor|Edit Modes|Author: Sort attributes alphabetically for "Full Tags with Attributes" which is my default mode. When unchecked, I have the correct order also in Author mode.

Thanks a lot!

Re: Custom plugin: adding attributes in the specified order

Posted: Wed Oct 12, 2022 10:21 am
by Radu
Hi Jan,
Thanks for posting the solution, I somehow forgot about this extra setting.
Regards,
Radu