I’m writing plugin for eclipse which should be XML editor with 2 pages: the first is xml editor (extends TextEditor) and the second is some kind of builder. To imagine what i’m talking about you can take standard faces-config eclipse editor: my first page is “Source” page, and my second page is something like “ManagedBean” page. Currently to synchronize changes i’ve made on builder page i just take text from editor, change it correspondently and then put this text back to editor. Something like this:
String editorText = editor.getDocumentProvider().getDocument(editor.getEditorInput()).get();
String changedText = editorText.substring(0, editorText.lastIndexOf(smth));
changedText += newText + editorText.substring(editorText.lastIndexOf(smth));
editor.getDocumentProvider().getDocument(editor.getEditorInput()).set(changedText)
It works but it looks not very elegant. I want to apply only changes. Is it possible?
EDIT: now i see that it doesn’t work 🙂 I mean it works in simple cases and in case everything is written more or less correct in editor. But if tags are written in a really bad manner (with line breaks in most unexpected places while still preserving xml correctness) it doesn’t. So the only way to save correct changes is to transform whole DOM and write it to editor. Actually this was the first thing i tried but in this case all custom formatting is gone: indents are saved but line breaks inside tags (between attributes) are gone. This:
<myTag attr1="1"
attr2="2">
becomes this:
<myTag attr1="1" attr2="2">
So i really need to know how to update only part of document, the part i’ve changed.
It’s strange that nobody answered as the solution was pretty simple. It’s
replace()method ofTextEditor‘s document:The only problem is to find these
offsetandlengthbut in my case it’s not a problem as my document isn’t very complicated and simple regexp does the trick. 🙂