Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fixes Inline Hint caret position #90

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public class JeddictCompletionProvider implements CompletionProvider {
private static final PreferencesManager prefsManager = PreferencesManager.getInstance();

private static final String HIGHLIGHTED_TEXT_KEY = "HIGHLIGHTED_TEXT_KEY";
private static final String HIGHLIGHTED_TEXT_LOC_KEY = "HIGHLIGHTED_TEXT_LOC_KEY";
private static final Object KEY_PRE_TEXT = new Object();

public static OffsetsBag getPreTextBag(Document doc, JTextComponent component) {
Expand All @@ -126,19 +127,23 @@ public void keyPressed(KeyEvent e) {
&& PreferencesManager.getInstance().isInlineHintEnabled()) {
try {
String text = (String) doc.getProperty(HIGHLIGHTED_TEXT_KEY);
if (text != null) {
Integer textLocation = (Integer) doc.getProperty(HIGHLIGHTED_TEXT_LOC_KEY);
if (text != null && textLocation != null) {
Document doc = component.getDocument();
int caretPosition = component.getCaretPosition();
doc.insertString(caretPosition, text, null);

Reformat reformat = Reformat.get(component.getDocument());
reformat.lock();
try {
reformat.reformat(caretPosition, caretPosition + text.length());
} finally {
reformat.unlock();

if (textLocation.equals(caretPosition)) {
doc.insertString(caretPosition, text, null);

Reformat reformat = Reformat.get(component.getDocument());
reformat.lock();
try {
reformat.reformat(caretPosition, caretPosition + text.length());
} finally {
reformat.unlock();
}
e.consume();
}
e.consume(); // Prevent default Tab action
}
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
Expand Down Expand Up @@ -728,7 +733,8 @@ public void highlightMultiline(JTextComponent component, int caretOffset, String
preTextBag.addHighlight(startOffset, startOffset + 1,
AttributesUtilities.createImmutable("virtual-text-prepend", snippet));
doc.putProperty(HIGHLIGHTED_TEXT_KEY, snippet);

doc.putProperty(HIGHLIGHTED_TEXT_LOC_KEY, startOffset);

// Set the updated highlights in the document
getPreTextBag(doc, component).clear();
getPreTextBag(doc, component).setHighlights(preTextBag);
Expand Down