Skip to content

Commit

Permalink
refact: enable NonNullByDefault for lsp4e.outline package
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Jul 21, 2024
1 parent 2f8a79b commit e027b34
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ public void sendNotification(@NonNull Consumer<LanguageServer> fn) {
*
* @return Async result
*/
public <T> CompletableFuture<T> execute(@NonNull Function<LanguageServer, ? extends CompletableFuture<T>> fn) {
public <T> @NonNull CompletableFuture<T> execute(@NonNull Function<LanguageServer, ? extends CompletableFuture<T>> fn) {
// Send the request on the dispatch thread
CompletableFuture<T> lsRequest = executeImpl(fn);
// then additionally make sure the response is delivered on a thread from the default ForkJoinPool.
Expand Down
47 changes: 24 additions & 23 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/CNFOutlinePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
*******************************************************************************/
package org.eclipse.lsp4e.outline;

import static org.eclipse.lsp4e.internal.NullSafetyHelper.*;

import java.util.ArrayList;

import org.eclipse.core.runtime.Adapters;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
Expand Down Expand Up @@ -62,18 +64,17 @@ public class CNFOutlinePage implements IContentOutlinePage, ILabelProviderListen
public static final String SHOW_KIND_PREFERENCE = ID + ".showKind"; //$NON-NLS-1$
public static final String SORT_OUTLINE_PREFERENCE = ID + ".sortOutline"; //$NON-NLS-1$

private CommonViewer outlineViewer;
private CommonViewer outlineViewer = lateNonNull();

private final IEclipsePreferences preferences;
private final ITextEditor textEditor;
private final ITextViewer textEditorViewer;
private final @Nullable ITextEditor textEditor;
private final @Nullable ITextViewer textEditorViewer;

private final IDocument document;
private final @Nullable IDocument document;

@NonNull
private final LanguageServerWrapper wrapper;

public CNFOutlinePage(@NonNull LanguageServerWrapper wrapper, @Nullable ITextEditor textEditor) {
public CNFOutlinePage(LanguageServerWrapper wrapper, @Nullable ITextEditor textEditor) {
preferences = InstanceScope.INSTANCE.getNode(LanguageServerPlugin.PLUGIN_ID);
preferences.addPreferenceChangeListener(this);
this.textEditor = textEditor;
Expand All @@ -83,23 +84,24 @@ public CNFOutlinePage(@NonNull LanguageServerWrapper wrapper, @Nullable ITextEdi
}

@Override
public void createControl(Composite parent) {
public void createControl(@NonNullByDefault({}) Composite parent) {
outlineViewer = new CommonViewer(ID, parent, SWT.NONE);
if (document != null) {
outlineViewer.setInput(new OutlineViewerInput(document, wrapper, textEditor));
}
outlineViewer.setSorter(new CommonViewerSorter());
outlineViewer.getLabelProvider().addListener(this);
final var textEditor = this.textEditor;
if (textEditor != null) {
outlineViewer.addOpenListener(event -> {
if (preferences.getBoolean(LINK_WITH_EDITOR_PREFERENCE, true))
textEditor.setFocus();
});
outlineViewer.addSelectionChangedListener(event -> {
final var document = this.document;
if (document != null && preferences.getBoolean(LINK_WITH_EDITOR_PREFERENCE, true)
&& outlineViewer.getTree().isFocusControl() && outlineViewer.getSelection() != null) {
Object selection = ((TreeSelection) outlineViewer.getSelection()).getFirstElement();
Range range = getRangeSelection(selection);
&& outlineViewer.getTree().isFocusControl() && outlineViewer.getSelection() instanceof TreeSelection sel) {
Range range = getRangeSelection(sel.getFirstElement());
if (range != null) {
try {
int startOffset = document.getLineOffset(range.getStart().getLine())
Expand All @@ -113,6 +115,7 @@ public void createControl(Composite parent) {
}
}
});
final var textEditorViewer = this.textEditorViewer;
if (textEditorViewer != null) {
editorSelectionChangedListener = new EditorSelectionChangedListener();
editorSelectionChangedListener.install(textEditorViewer.getSelectionProvider());
Expand All @@ -127,7 +130,7 @@ public void createControl(Composite parent) {
* the selected symbol.
* @return the range of the given selection and null otherwise.
*/
private Range getRangeSelection(Object selection) {
private @Nullable Range getRangeSelection(@Nullable Object selection) {
if (selection == null) {
return null;
}
Expand All @@ -143,9 +146,6 @@ private Range getRangeSelection(Object selection) {
class EditorSelectionChangedListener implements ISelectionChangedListener {

public void install(ISelectionProvider selectionProvider) {
if (selectionProvider == null)
return;

if (selectionProvider instanceof IPostSelectionProvider provider) {
provider.addPostSelectionChangedListener(this);
} else {
Expand All @@ -154,9 +154,6 @@ public void install(ISelectionProvider selectionProvider) {
}

public void uninstall(ISelectionProvider selectionProvider) {
if (selectionProvider == null)
return;

if (selectionProvider instanceof IPostSelectionProvider provider) {
provider.removePostSelectionChangedListener(this);
} else {
Expand All @@ -166,6 +163,10 @@ public void uninstall(ISelectionProvider selectionProvider) {

@Override
public void selectionChanged(SelectionChangedEvent event) {
final var document = CNFOutlinePage.this.document;
if(document == null) {
return;
}
ISelection selection = event.getSelection();
if (selection instanceof ITextSelection textSelection) {
if (!preferences.getBoolean(LINK_WITH_EDITOR_PREFERENCE, true)) {
Expand All @@ -179,15 +180,15 @@ public void selectionChanged(SelectionChangedEvent event) {
}
}

private EditorSelectionChangedListener editorSelectionChangedListener;
private @Nullable EditorSelectionChangedListener editorSelectionChangedListener;

public static void refreshTreeSelection(TreeViewer viewer, int offset, IDocument document) {
final var contentProvider = (ITreeContentProvider) viewer.getContentProvider();
if (contentProvider == null) {
return;
}

Object[] objects = contentProvider.getElements(null);
Object @Nullable [] objects = contentProvider.getElements(null);
final var path = new ArrayList<Object>();
while (objects != null && objects.length > 0) {
boolean found = false;
Expand Down Expand Up @@ -219,7 +220,7 @@ public static void refreshTreeSelection(TreeViewer viewer, int offset, IDocument
}

@SuppressWarnings("unused")
private static Range toRange(Object object) {
private static @Nullable Range toRange(Object object) {
Range range = null;
@Nullable
SymbolInformation symbol = object instanceof SymbolInformation symbolInformation ? symbolInformation
Expand Down Expand Up @@ -266,7 +267,7 @@ public Control getControl() {
}

@Override
public void setActionBars(IActionBars actionBars) {
public void setActionBars(@NonNullByDefault({}) IActionBars actionBars) {
// nothing to do yet, comment requested by sonar
}

Expand Down Expand Up @@ -302,7 +303,7 @@ public void labelProviderChanged(LabelProviderChangedEvent event) {

@Override
public void preferenceChange(final PreferenceChangeEvent event) {
if (SORT_OUTLINE_PREFERENCE.equals(event.getKey()) && outlineViewer != null) {
if (SORT_OUTLINE_PREFERENCE.equals(event.getKey()) && castNullable(outlineViewer) != null) {
outlineViewer.refresh();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.concurrent.TimeoutException;

import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.IDocument;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.LanguageServerPlugin;
Expand All @@ -45,7 +45,7 @@ public class EditorToOutlineAdapterFactory implements IAdapterFactory {
private static final Map<IEditorPart, LanguageServerWrapper> LANG_SERVER_CACHE = Collections.synchronizedMap(new HashMap<>());

@Override
public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
public <T> @Nullable T getAdapter(@Nullable Object adaptableObject, @Nullable Class<T> adapterType) {
if (adapterType == IContentOutlinePage.class && adaptableObject instanceof IEditorPart editorPart) {
final IEditorInput editorInput = editorPart.getEditorInput();

Expand Down Expand Up @@ -86,7 +86,7 @@ public Class<?>[] getAdapterList() {
return new Class<?>[] { IContentOutlinePage.class };
}

private static CNFOutlinePage createOutlinePage(IEditorPart editorPart, @NonNull LanguageServerWrapper wrapper) {
private static CNFOutlinePage createOutlinePage(IEditorPart editorPart, LanguageServerWrapper wrapper) {
return new CNFOutlinePage(wrapper, UI.asTextEditor(editorPart));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
package org.eclipse.lsp4e.outline;

import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.ui.views.contentoutline.ContentOutline;

public class HasCNFOutlinePage extends PropertyTester {

@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
public boolean test(@Nullable Object receiver, String property, Object[] args, @Nullable Object expectedValue) {
if (receiver instanceof ContentOutline outline) {
return outline.getCurrentPage() instanceof CNFOutlinePage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.lsp4e.outline;

import static org.eclipse.lsp4e.internal.NullSafetyHelper.lateNonNull;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
Expand Down Expand Up @@ -68,20 +70,13 @@ public class LSSymbolsContentProvider implements ICommonContentProvider, ITreeCo

public static final String VIEWER_PROPERTY_IS_QUICK_OUTLINE = "isQuickOutline"; //$NON-NLS-1$

@NonNullByDefault
public static final class OutlineViewerInput {

public final IDocument document;
public final LanguageServerWrapper wrapper;

@Nullable
public final ITextEditor textEditor;

@Nullable
public final IFile documentFile;

@Nullable
private final URI documentURI;
public final @Nullable ITextEditor textEditor;
public final @Nullable IFile documentFile;
private final @Nullable URI documentURI;

public OutlineViewerInput(IDocument document, LanguageServerWrapper wrapper, @Nullable ITextEditor textEditor) {
this.document = document;
Expand Down Expand Up @@ -167,17 +162,17 @@ protected void initialProcess() {
}

@Override
protected void process(DirtyRegion dirtyRegion) {
protected void process(@NonNullByDefault({}) DirtyRegion dirtyRegion) {
refreshTreeContentFromLS();
}

@Override
protected void reconcilerDocumentChanged(IDocument newDocument) {
protected void reconcilerDocumentChanged(@NonNullByDefault({}) IDocument newDocument) {
// Do nothing
}

@Override
public IReconcilingStrategy getReconcilingStrategy(String contentType) {
public @Nullable IReconcilingStrategy getReconcilingStrategy(@NonNullByDefault({}) String contentType) {
return null;
}
}
Expand Down Expand Up @@ -221,15 +216,15 @@ public void resourceChanged(IResourceChangeEvent event) {
}
}

private TreeViewer viewer;
private volatile Throwable lastError;
private OutlineViewerInput outlineViewerInput;
private TreeViewer viewer = lateNonNull();
private volatile @Nullable Throwable lastError;
private OutlineViewerInput outlineViewerInput = lateNonNull();

private final SymbolsModel symbolsModel = new SymbolsModel();
private volatile CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> symbols;
private volatile @Nullable CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> symbols;
private final boolean refreshOnResourceChanged;
private boolean isQuickOutline;
private IOutlineUpdater outlineUpdater;
private @Nullable IOutlineUpdater outlineUpdater;

public LSSymbolsContentProvider() {
this(false);
Expand All @@ -240,11 +235,11 @@ public LSSymbolsContentProvider(boolean refreshOnResourceChanged) {
}

@Override
public void init(ICommonContentExtensionSite aConfig) {
public void init(@NonNullByDefault({}) ICommonContentExtensionSite aConfig) {
}

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
public void inputChanged(Viewer viewer, @Nullable Object oldInput, @Nullable Object newInput) {
if (outlineUpdater != null) {
outlineUpdater.uninstall();
}
Expand Down Expand Up @@ -276,7 +271,7 @@ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}

private IOutlineUpdater createOutlineUpdater() {
if (refreshOnResourceChanged) {
if (refreshOnResourceChanged && outlineViewerInput.documentFile != null) {
return new ResourceChangeOutlineUpdater(outlineViewerInput.documentFile);
}
final ITextViewer textViewer = LSPEclipseUtils.getTextViewer(outlineViewerInput.textEditor);
Expand All @@ -285,7 +280,7 @@ private IOutlineUpdater createOutlineUpdater() {
}

@Override
public Object[] getElements(Object inputElement) {
public Object[] getElements(@Nullable Object inputElement) {
if (this.symbols != null && !this.symbols.isDone()) {
return new Object[] { new PendingUpdateAdapter() };
}
Expand All @@ -301,7 +296,7 @@ public Object[] getChildren(Object parentElement) {
}

@Override
public Object getParent(Object element) {
public @Nullable Object getParent(Object element) {
return symbolsModel.getParent(element);
}

Expand All @@ -325,7 +320,7 @@ protected void refreshTreeContentFromLS() {
}

final var params = new DocumentSymbolParams(LSPEclipseUtils.toTextDocumentIdentifier(documentURI));
symbols = outlineViewerInput.wrapper.execute(ls -> ls.getTextDocumentService().documentSymbol(params));
final var symbols = this.symbols = outlineViewerInput.wrapper.execute(ls -> ls.getTextDocumentService().documentSymbol(params));
symbols.thenAcceptAsync(response -> {
symbolsModel.update(response);
lastError = null;
Expand Down Expand Up @@ -378,10 +373,10 @@ public void dispose() {
}

@Override
public void restoreState(IMemento aMemento) {
public void restoreState(@NonNullByDefault({}) IMemento aMemento) {
}

@Override
public void saveState(IMemento aMemento) {
public void saveState(@NonNullByDefault({}) IMemento aMemento) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.lsp4e.LanguageServerPlugin;
Expand All @@ -26,7 +27,7 @@ public class OutlineSorter extends ViewerComparator {
protected final IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(LanguageServerPlugin.PLUGIN_ID);

@Override
public int compare(final Viewer viewer, final Object o1, final Object o2) {
public int compare(final @Nullable Viewer viewer, final @Nullable Object o1, final @Nullable Object o2) {
if (!isSortingEnabled())
return 0;

Expand All @@ -42,7 +43,10 @@ public int compare(final Viewer viewer, final Object o1, final Object o2) {
return name1.compareTo(name2);
}

private String getName(Object element) {
private @Nullable String getName(@Nullable Object element) {
if (element == null)
return null;

if (element instanceof Either<?, ?> either) {
element = either.get();
}
Expand All @@ -59,7 +63,7 @@ private String getName(Object element) {
}

@Override
public boolean isSorterProperty(final Object element, final String property) {
public boolean isSorterProperty(final @Nullable Object element, final @Nullable String property) {
return "name".equals(property); //$NON-NLS-1$
}

Expand Down
Loading

0 comments on commit e027b34

Please sign in to comment.