From e43c7a80ea1cc6c8a264bee6f521e38f71da4dfc Mon Sep 17 00:00:00 2001 From: 2xsaiko Date: Tue, 21 Jul 2020 15:18:12 +0200 Subject: Fix losing current cursor position when renaming entries (#297) * Fix losing current cursor position when renaming entries * Set nextReference to null after applying it * Extract token map into wrapper type--- .../java/cuchaz/enigma/gui/panels/EditorPanel.java | 59 ++++++++++++++++------ 1 file changed, 44 insertions(+), 15 deletions(-) (limited to 'enigma-swing/src/main/java') diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/EditorPanel.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/EditorPanel.java index 9bf5fa72..3409fc14 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/EditorPanel.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/panels/EditorPanel.java @@ -69,6 +69,7 @@ public class EditorPanel { private final Gui gui; private EntryReference, Entry> cursorReference; + private EntryReference, Entry> nextReference; private boolean mouseIsPressed = false; private boolean shouldNavigateOnClick; @@ -332,13 +333,14 @@ public class EditorPanel { } else { this.displayError(res.unwrapErr()); } + this.nextReference = null; }); } public void displayError(ClassHandleError t) { this.setDisplayMode(DisplayMode.ERRORED); String str; - switch(t.type) { + switch (t.type) { case DECOMPILE: str = "editor.decompile_error"; break; @@ -404,21 +406,13 @@ public class EditorPanel { } public void onCaretMove(int pos, boolean fromClick) { + if (this.settingSource) return; if (this.controller.project == null) return; EntryRemapper mapper = this.controller.project.getMapper(); Token token = getToken(pos); - if (this.settingSource) { - EntryReference, Entry> ref = getCursorReference(); - EntryReference, Entry> refAtCursor = getReference(token); - if (this.editor.getDocument().getLength() != 0 && ref != null && !ref.equals(refAtCursor)) { - showReference0(ref); - } - return; - } else { - setCursorReference(getReference(token)); - } + setCursorReference(getReference(token)); Entry referenceEntry = this.cursorReference != null ? this.cursorReference.entry : null; @@ -484,17 +478,49 @@ public class EditorPanel { if (source == null) return; try { this.settingSource = true; + + int newCaretPos = 0; + if (this.source != null && this.source.getEntry().equals(source.getEntry())) { + int caretPos = this.editor.getCaretPosition(); + + if (this.source.getTokenStore().isCompatible(source.getTokenStore())) { + newCaretPos = this.source.getTokenStore().mapPosition(source.getTokenStore(), caretPos); + } else { + // if the class is the same but the token stores aren't + // compatible, then the user probably switched decompilers + + // check if there's a selected reference we can navigate to, + // but only if there's none already queued up for being selected + if (this.getCursorReference() != null && this.nextReference == null) { + this.nextReference = this.getCursorReference(); + } + + // otherwise fall back to just using the same average + // position in the file + float scale = (float) source.toString().length() / this.source.toString().length(); + newCaretPos = (int) (caretPos * scale); + } + } + this.source = source; this.editor.getHighlighter().removeAllHighlights(); this.editor.setText(source.toString()); + if (this.source != null) { + this.editor.setCaretPosition(newCaretPos); + } setHighlightedTokens(source.getHighlightedTokens()); + setCursorReference(getReference(getToken(this.editor.getCaretPosition()))); } finally { this.settingSource = false; } - showReference0(getCursorReference()); + + if (this.nextReference != null) { + this.showReference0(this.nextReference); + this.nextReference = null; + } } - public void setHighlightedTokens(Map> tokens) { + public void setHighlightedTokens(Map> tokens) { // remove any old highlighters this.editor.getHighlighter().removeAllHighlights(); @@ -526,8 +552,11 @@ public class EditorPanel { } public void showReference(EntryReference, Entry> reference) { - setCursorReference(reference); - showReference0(reference); + if (this.mode == DisplayMode.SUCCESS) { + showReference0(reference); + } else if (this.mode != DisplayMode.ERRORED) { + this.nextReference = reference; + } } /** -- cgit v1.2.3