Skip to content

Commit 1b4b2b7

Browse files
committed
Hierarchical doc symbols in Eclipse by default
1 parent b72e4b3 commit 1b4b2b7

File tree

5 files changed

+57
-34
lines changed

5 files changed

+57
-34
lines changed

eclipse-language-servers/org.springframework.tooling.boot.ls/src/org/springframework/tooling/boot/ls/prefs/PrefsInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void initializeDefaultPreferences() {
6464
preferenceStore.setDefault(Constants.PREF_PROPS_COMPLETIONS_ELIDE_PREFIX, false);
6565
preferenceStore.setDefault(Constants.PREF_CRON_INLAY_HINTS, true);
6666
preferenceStore.setDefault(Constants.PREF_COMPLETION_JAVA_INJECT_BEAN, true);
67-
preferenceStore.setDefault(Constants.PREF_BEANS_STRUCTURE_TREE, false);
67+
preferenceStore.setDefault(Constants.PREF_BEANS_STRUCTURE_TREE, true);
6868
preferenceStore.setDefault(Constants.PREF_SYMBOLS_FROM_NEW_INDEX, true);
6969
}
7070

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
eclipse.preferences.version=1
22
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3-
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
4-
org.eclipse.jdt.core.compiler.compliance=11
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
4+
org.eclipse.jdt.core.compiler.compliance=17
55
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
66
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
77
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
88
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
99
org.eclipse.jdt.core.compiler.release=enabled
10-
org.eclipse.jdt.core.compiler.source=11
10+
org.eclipse.jdt.core.compiler.source=17

eclipse-language-servers/org.springframework.tooling.ls.eclipse.gotosymbol/src/org/springframework/tooling/ls/eclipse/gotosymbol/dialogs/GotoSymbolDialogModel.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@
1717
import java.util.List;
1818

1919
import org.eclipse.core.runtime.Assert;
20+
import org.eclipse.jface.text.BadLocationException;
21+
import org.eclipse.jface.text.IDocument;
2022
import org.eclipse.lsp4e.LSPEclipseUtils;
23+
import org.eclipse.lsp4e.LanguageServerPlugin;
24+
import org.eclipse.lsp4e.ui.UI;
2125
import org.eclipse.lsp4j.Location;
26+
import org.eclipse.lsp4j.Range;
2227
import org.eclipse.lsp4j.WorkspaceSymbolLocation;
2328
import org.eclipse.lsp4j.jsonrpc.messages.Either;
2429
import org.eclipse.ui.IWorkbenchPage;
2530
import org.eclipse.ui.PlatformUI;
31+
import org.eclipse.ui.texteditor.ITextEditor;
2632
import org.springframework.tooling.ls.eclipse.gotosymbol.GotoSymbolPlugin;
2733
import org.springframework.tooling.ls.eclipse.gotosymbol.favourites.FavouritesPreference;
2834
import org.springsource.ide.eclipse.commons.core.util.FuzzyMatcher;
@@ -59,11 +65,13 @@ public static class Match<T> {
5965
final double score;
6066
final String query;
6167
final T value;
62-
public Match(double score, String query, T value) {
68+
final List<Match<T>> children;
69+
public Match(double score, String query, T value, List<Match<T>> children) {
6370
super();
6471
this.score = score;
6572
this.query = query;
6673
this.value = value;
74+
this.children = children;
6775
}
6876

6977
}
@@ -114,6 +122,21 @@ else if (symbolInformation != null && symbolInformation.isWorkspaceSymbol()) {
114122

115123
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
116124
LSPEclipseUtils.openInEditor(location, page);
125+
} else if (symbolInformation.isDocumentSymbol()) {
126+
ITextEditor editor = UI.getActiveTextEditor();
127+
if (editor != null) {
128+
IDocument doc = LSPEclipseUtils.getDocument(editor);
129+
if (doc != null) {
130+
try {
131+
Range range = symbolInformation.getDocumentSymbol().getSelectionRange();
132+
int offset = LSPEclipseUtils.toOffset(range.getStart(), doc);
133+
int endOffset = LSPEclipseUtils.toOffset(range.getEnd(), doc);
134+
editor.selectAndReveal(offset, endOffset - offset);
135+
} catch (BadLocationException e) {
136+
LanguageServerPlugin.logError(e);
137+
}
138+
}
139+
}
117140
}
118141
return true;
119142
};
@@ -185,17 +208,23 @@ protected Collection<Match<SymbolContainer>> compute() {
185208
query = "";
186209
}
187210
query = query.toLowerCase();
211+
212+
return computeMatches(unfilteredSymbols.getValues(), query);
213+
}
214+
private List<Match<SymbolContainer>> computeMatches(Collection<SymbolContainer> c, String query) {
188215
List<Match<SymbolContainer>> matches = new ArrayList<>();
189-
for (SymbolContainer symbol : unfilteredSymbols.getValues()) {
216+
for (SymbolContainer symbol : c) {
217+
190218
String name = symbol.getName();
191219

192220
if (name != null) {
193221
name = name.toLowerCase();
194222
}
195223

196224
double score = FuzzyMatcher.matchScore(query, name);
197-
if (score != 0.0) {
198-
matches.add(new Match<SymbolContainer>(score, query, symbol));
225+
List<Match<SymbolContainer>> childrenMatches = computeMatches(symbol.getChildren(), query);
226+
if (score != 0.0 || !childrenMatches.isEmpty()) {
227+
matches.add(new Match<SymbolContainer>(score, query, symbol, childrenMatches));
199228
}
200229
}
201230
Collections.sort(matches, MATCH_COMPARATOR);

eclipse-language-servers/org.springframework.tooling.ls.eclipse.gotosymbol/src/org/springframework/tooling/ls/eclipse/gotosymbol/dialogs/GotoSymbolSection.java

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ private static class SymbolsContentProvider implements ITreeContentProvider {
8888

8989
@Override
9090
public Object[] getChildren(Object parentElement) {
91+
if (parentElement instanceof Match<?> m) {
92+
return m.children.toArray(new Object[m.children.size()]);
93+
}
9194
return null;
9295
}
9396

@@ -98,6 +101,9 @@ public Object getParent(Object element) {
98101

99102
@Override
100103
public boolean hasChildren(Object element) {
104+
if (element instanceof Match<?> m ) {
105+
return !m.children.isEmpty();
106+
}
101107
return false;
102108
}
103109

@@ -302,31 +308,6 @@ public void createContents(Composite dialogArea) {
302308
if (!viewer.getControl().isDisposed()) viewer.refresh();
303309
})));
304310

305-
//TODO: somehow show selection in local file, (but not in other file ?)
306-
// viewer.addSelectionChangedListener(event -> {
307-
// IStructuredSelection selection = (IStructuredSelection) event.getSelection();
308-
// if (selection.isEmpty()) {
309-
// return;
310-
// }
311-
// SymbolInformation symbolInformation = (SymbolInformation) selection.getFirstElement();
312-
// Location location = symbolInformation.getLocation();
313-
//
314-
// IResource targetResource = LSPEclipseUtils.findResourceFor(location.getUri());
315-
// if (targetResource == null) {
316-
// return;
317-
// }
318-
// IDocument targetDocument = FileBuffers.getTextFileBufferManager()
319-
// .getTextFileBuffer(targetResource.getFullPath(), LocationKind.IFILE).getDocument();
320-
// if (targetDocument != null) {
321-
// try {
322-
// int offset = LSPEclipseUtils.toOffset(location.getRange().getStart(), targetDocument);
323-
// int endOffset = LSPEclipseUtils.toOffset(location.getRange().getEnd(), targetDocument);
324-
// fTextEditor.selectAndReveal(offset, endOffset - offset);
325-
// } catch (BadLocationException e) {
326-
// LanguageServerPlugin.logError(e);
327-
// }
328-
// }
329-
// });
330311
installWidgetListeners(pattern, viewer);
331312

332313
//Status label

eclipse-language-servers/org.springframework.tooling.ls.eclipse.gotosymbol/src/org/springframework/tooling/ls/eclipse/gotosymbol/dialogs/SymbolContainer.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*******************************************************************************/
1111
package org.springframework.tooling.ls.eclipse.gotosymbol.dialogs;
1212

13+
import java.util.List;
14+
1315
import org.eclipse.lsp4j.DocumentSymbol;
1416
import org.eclipse.lsp4j.SymbolInformation;
1517
import org.eclipse.lsp4j.WorkspaceSymbol;
@@ -20,6 +22,8 @@ public class SymbolContainer {
2022
private DocumentSymbol documentSymbol;
2123
private WorkspaceSymbol workspaceSymbol;
2224

25+
private List<SymbolContainer> children = List.of();
26+
2327
public static SymbolContainer fromSymbolInformation(SymbolInformation symbolInformation) {
2428
return new SymbolContainer(symbolInformation);
2529
}
@@ -38,6 +42,11 @@ private SymbolContainer(SymbolInformation symbolInformation) {
3842

3943
private SymbolContainer(DocumentSymbol documentSymbol) {
4044
this.documentSymbol = documentSymbol;
45+
if (documentSymbol.getChildren() == null) {
46+
this.children = List.of();
47+
} else {
48+
this.children = documentSymbol.getChildren().stream().map(SymbolContainer::new).toList();
49+
}
4150
}
4251

4352
private SymbolContainer(WorkspaceSymbol workspaceSymbol) {
@@ -100,5 +109,9 @@ else if (documentSymbol != null) {
100109
return workspaceSymbol;
101110
}
102111
}
103-
112+
113+
public List<SymbolContainer> getChildren() {
114+
return children;
115+
}
116+
104117
}

0 commit comments

Comments
 (0)