Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

LSP client: Prevent NullPointerException when caret is not present on text component #5393

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
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 @@ -20,6 +20,7 @@
package org.netbeans.modules.lsp.client.bindings;

import java.awt.event.ActionEvent;
import javax.swing.text.Caret;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import org.netbeans.api.editor.EditorActionNames;
Expand Down Expand Up @@ -115,7 +116,11 @@ private static int findOccurrencePosition(boolean directionForward, Document doc
private static void navigateToOccurence(boolean next, JTextComponent txt) {
if (txt != null && txt.getDocument() != null) {
Document doc = txt.getDocument();
int position = txt.getCaretPosition();
Caret caret = txt.getCaret();
if(caret == null) {
return;
}
int position = caret.getDot();
int goTo = findOccurrencePosition(next, doc, position);
if (goTo > 0) {
txt.setCaretPosition(goTo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import org.eclipse.lsp4j.DocumentHighlight;
Expand Down Expand Up @@ -63,28 +64,33 @@ public class MarkOccurrences implements BackgroundTask, CaretListener, PropertyC
private Document doc;
private int caretPos;

@SuppressWarnings("LeakingThisInConstructor")
public MarkOccurrences(JTextComponent component) {
this.component = component;
doc = component.getDocument();
Caret caret = component.getCaret();
caretPos = caret != null ? caret.getDot() : -1;
component.addCaretListener(this);
component.addPropertyChangeListener(this);
doc = component.getDocument();
caretPos = component.getCaretPosition();
}

@Override
public void run(LSPBindings bindings, FileObject file) {
Document doc;
int caretPos;
Document localDoc;
int localCaretPos;
synchronized (this) {
doc = this.doc;
caretPos = this.caretPos;
localDoc = this.doc;
localCaretPos = this.caretPos;
}
getHighlightsBag(doc).setHighlights(computeHighlights(doc, caretPos));
getHighlightsBag(localDoc).setHighlights(computeHighlights(localDoc, localCaretPos));
}

private OffsetsBag computeHighlights(Document doc, int caretPos) {
AttributeSet attr = getColoring(doc);
OffsetsBag result = new OffsetsBag(doc);
if(caretPos < 0) {
return result;
}
FileObject file = NbEditorUtilities.getFileObject(doc);
if (file == null) {
return result;
Expand Down Expand Up @@ -128,7 +134,11 @@ private AttributeSet getColoring(Document doc) {

@Override
public synchronized void caretUpdate(CaretEvent e) {
caretPos = e.getDot();
if(e != null) {
caretPos = e.getDot();
} else {
caretPos = -1;
}
WORKER.post(() -> {
FileObject file = NbEditorUtilities.getFileObject(doc);

Expand All @@ -149,19 +159,23 @@ static OffsetsBag getHighlightsBag(Document doc) {
OffsetsBag bag = (OffsetsBag) doc.getProperty(MarkOccurrences.class);

if (bag == null) {
doc.putProperty(MarkOccurrences.class, bag = new OffsetsBag(doc, false));
bag = new OffsetsBag(doc, false);
doc.putProperty(MarkOccurrences.class, bag);

Object stream = doc.getProperty(Document.StreamDescriptionProperty);
final OffsetsBag bagFin = bag;
DocumentListener l = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
bagFin.removeHighlights(e.getOffset(), e.getOffset(), false);
}

@Override
public void removeUpdate(DocumentEvent e) {
bagFin.removeHighlights(e.getOffset(), e.getOffset(), false);
}

@Override
public void changedUpdate(DocumentEvent e) {
}
};
Expand All @@ -180,6 +194,7 @@ public void changedUpdate(DocumentEvent e) {
@MimeRegistration(mimeType = "", service = HighlightsLayerFactory.class)
public static class HighlightsLayerFactoryImpl implements HighlightsLayerFactory {

@Override
public HighlightsLayer[] createLayers(HighlightsLayerFactory.Context context) {
return new HighlightsLayer[]{
//the mark occurrences layer should be "above" current row and "below" the search layers:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import javax.swing.SwingUtilities;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.Document;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.ReferenceContext;
Expand Down Expand Up @@ -70,7 +71,11 @@ public void doFindUsages(Lookup lookup) {
AbstractDocument abstractDoc = (doc instanceof AbstractDocument) ? ((AbstractDocument) doc) : null;
FileObject file = NbEditorUtilities.getFileObject(doc);
LSPBindings bindings = LSPBindings.getBindings(file);
int caretPos = c.getCaretPosition();
Caret caret = c.getCaret();
if(caret == null) {
return;
}
int caretPos = caret.getDot();
Position pos = Utils.createPosition(doc, caretPos);
ReferenceParams params = new ReferenceParams();
params.setTextDocument(new TextDocumentIdentifier(Utils.toURI(file)));
Expand Down Expand Up @@ -118,7 +123,11 @@ public void doRename(Lookup lookup) {
AbstractDocument abstractDoc = (doc instanceof AbstractDocument) ? ((AbstractDocument) doc) : null;
FileObject file = NbEditorUtilities.getFileObject(doc);
LSPBindings bindings = LSPBindings.getBindings(file);
int caretPos = c.getCaretPosition();
Caret caret = c.getCaret();
if(caret == null) {
return;
}
int caretPos = caret.getDot();
Position pos = Utils.createPosition(doc, caretPos);
String name;
if(abstractDoc != null) {
Expand Down