Skip to content

Commit

Permalink
Merge pull request apache#3153 from matthiasblaesing/lsp_client
Browse files Browse the repository at this point in the history
LSP Client Improvements (Foldmanager, Performance)
  • Loading branch information
matthiasblaesing authored Sep 20, 2021
2 parents 97f2dc3 + a087c6b commit 5bbe80c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,14 @@ public void runOnBackground(Runnable r) {
}

private static void scheduleBackgroundTask(RequestProcessor.Task req) {
WORKER.post(req, DELAY);
req.schedule(DELAY);
}

public static synchronized void rescheduleBackgroundTask(FileObject file, BackgroundTask task) {
RequestProcessor.Task req = backgroundTasksMapFor(file).get(task);

if (req != null) {
WORKER.post(req, DELAY);
scheduleBackgroundTask(req);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -112,6 +115,7 @@ public void run(LSPBindings bindings, FileObject file) {
if (doc == null) {
return ;
}
Set<FoldingRangeInfo2> foldingRangesSeen = new HashSet<>();
List<FoldingRange> ranges = computeRanges(bindings, file);
List<FoldInfo> infos = new ArrayList<>();
if (ranges != null) {
Expand All @@ -123,7 +127,20 @@ public void run(LSPBindings bindings, FileObject file) {
} else {
end = Utils.getOffset(doc, new Position(r.getEndLine(), r.getEndCharacter()));
}
infos.add(FoldInfo.range(start, end, FoldType.CODE_BLOCK));
// Map the fold range type to netbeans as far as possible
FoldType foldType;
if("comment".equals(r.getKind())) {
foldType = FoldType.COMMENT;
} else if ("imports".equals(r.getKind())) {
foldType = FoldType.IMPORT;
} else {
foldType = FoldType.CODE_BLOCK;
}
FoldingRangeInfo2 fri2 = new FoldingRangeInfo2(start, end, foldType);
if(! foldingRangesSeen.contains(fri2)) {
infos.add(FoldInfo.range(start, end, foldType));
foldingRangesSeen.add(fri2);
}
}
}
SwingUtilities.invokeLater(() -> {
Expand Down Expand Up @@ -163,4 +180,61 @@ public FoldManager createFoldManager() {
}

}

static class FoldingRangeInfo2 {
private int start;
private int end;
private FoldType type;

public FoldingRangeInfo2(int start, int end, FoldType type) {
this.start = start;
this.end = end;
this.type = type;
}

public int getStart() {
return start;
}

public int getEnd() {
return end;
}

public FoldType getType() {
return type;
}

@Override
public int hashCode() {
int hash = 3;
hash = 23 * hash + this.start;
hash = 23 * hash + this.end;
hash = 23 * hash + Objects.hashCode(this.type);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final FoldingRangeInfo2 other = (FoldingRangeInfo2) obj;
if (this.start != other.start) {
return false;
}
if (this.end != other.end) {
return false;
}
if (!Objects.equals(this.type, other.type)) {
return false;
}
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.DocumentSymbolParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.SymbolKind;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.netbeans.modules.lsp.client.LSPBindings;
Expand All @@ -52,7 +50,6 @@
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
Expand Down Expand Up @@ -137,7 +134,8 @@ public void run(LSPBindings bindings, FileObject file) {
List<Either<SymbolInformation, DocumentSymbol>> symbols = bindings.getTextDocumentService().documentSymbol(new DocumentSymbolParams(new TextDocumentIdentifier(uri))).get();

setKeys(symbols);
view.expandAll();

SwingUtilities.invokeLater(() -> view.expandAll());
} catch (ExecutionException ex) {
LOG.log(Level.FINE, null, ex);
setKeys(Collections.emptyList());
Expand Down Expand Up @@ -250,7 +248,10 @@ public ExplorerManager getExplorerManager() {
}

public void expandAll() {
boolean scrollsOnExpand = internalView.getScrollsOnExpand();
internalView.setScrollsOnExpand(false);
internalView.expandAll();
internalView.setScrollsOnExpand(scrollsOnExpand);
}
}

Expand Down

0 comments on commit 5bbe80c

Please sign in to comment.