Skip to content

Commit

Permalink
refact: enable NonNullByDefault for lsp4e.ui package
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed May 22, 2024
1 parent f168eac commit 562fd0c
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.lsp4e.ContentTypeToLanguageServerDefinition;
import org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition;
import org.eclipse.lsp4e.LanguageServiceAccessor;
Expand All @@ -25,17 +26,17 @@
public class EnableDisableLSJob extends Job {

private final List<ContentTypeToLanguageServerDefinition> serverDefinitions;
private final IEditorReference[] editors;
private final IEditorReference @Nullable [] editors;

public EnableDisableLSJob(List<ContentTypeToLanguageServerDefinition> serverDefinitions,
IEditorReference[] editors) {
IEditorReference @Nullable [] editors) {
super(Messages.enableDisableLSJob);
this.serverDefinitions = serverDefinitions;
this.editors = editors;
}

@Override
protected IStatus run(IProgressMonitor monitor) {
protected IStatus run(@Nullable IProgressMonitor monitor) {
for (ContentTypeToLanguageServerDefinition changedDefinition : serverDefinitions) {
LanguageServerDefinition serverDefinition = changedDefinition.getValue();
if (serverDefinition != null) {
Expand Down
20 changes: 12 additions & 8 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LSPImages.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
*******************************************************************************/
package org.eclipse.lsp4e.ui;

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

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.lsp4e.LSPEclipseUtils;
Expand All @@ -38,7 +41,7 @@ private LSPImages() {
// this class shouldn't be instantiated
}

private static ImageRegistry imageRegistry;
private static @Nullable ImageRegistry imageRegistry;
private static final Map<java.awt.Color, Image> colorToImageCache = new HashMap<>();
private static final String ICONS_PATH = "$nl$/icons/full/"; //$NON-NLS-1$
private static final String OBJECT = ICONS_PATH + "obj16/"; // basic colors - size 16x16 //$NON-NLS-1$
Expand Down Expand Up @@ -123,7 +126,7 @@ private static final void declareRegistryImage(String key, String path) {
desc = ImageDescriptor.createFromURL(url);
}
}
imageRegistry.put(key, desc);
castNonNull(imageRegistry).put(key, desc);
}

/**
Expand All @@ -141,8 +144,9 @@ public static ImageDescriptor getImageDescriptor(String key) {
}

public static ImageRegistry getImageRegistry() {
ImageRegistry imageRegistry = LSPImages.imageRegistry;
if (imageRegistry == null) {
imageRegistry = LanguageServerPlugin.getDefault().getImageRegistry();
imageRegistry = LSPImages.imageRegistry = LanguageServerPlugin.getDefault().getImageRegistry();
}
return imageRegistry;
}
Expand All @@ -151,7 +155,7 @@ public static ImageRegistry getImageRegistry() {
* @param imageId See static IMG_* fields of {@link ISharedImages}
* @return the workbench's shared image for the , or null if not found
*/
public static Image getSharedImage(String imageId) {
public static @Nullable Image getSharedImage(@Nullable String imageId) {
if(imageId == null) {
return null;
}
Expand All @@ -162,14 +166,14 @@ public static Image getSharedImage(String imageId) {
* @param imageId See static IMG_* fields of {@link ISharedImages}
* @return the workbench's shared image descriptor for the workbench, or null if not found
*/
public static ImageDescriptor getSharedImageDescriptor(String imageId) {
public static @Nullable ImageDescriptor getSharedImageDescriptor(@Nullable String imageId) {
if(imageId == null) {
return null;
}
return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(imageId);
}

public static Image imageFromSymbolKind(SymbolKind kind) {
public static @Nullable Image imageFromSymbolKind(@Nullable SymbolKind kind) {
if (kind == null) {
return EMPTY_IMAGE;
}
Expand Down Expand Up @@ -199,7 +203,7 @@ public static Image imageFromSymbolKind(SymbolKind kind) {
};
}

public static Image imageFromCompletionItem(CompletionItem completionItem) {
public static @Nullable Image imageFromCompletionItem(CompletionItem completionItem) {
return switch (completionItem.getKind()) {
case Text -> getImage(IMG_TEXT);
case Method -> getImage(IMG_METHOD);
Expand All @@ -226,7 +230,7 @@ public static Image imageFromCompletionItem(CompletionItem completionItem) {
};
}

private static Image getImageForColor(CompletionItem completionItem) {
private static @Nullable Image getImageForColor(CompletionItem completionItem) {
String hexValue = null;

// TODO most probably can be extended for more cases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
*******************************************************************************/
package org.eclipse.lsp4e.ui;

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

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.layout.GridDataFactory;
Expand Down Expand Up @@ -47,19 +51,19 @@

public class LanguageServerPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

private LanguageServersRegistry registry;
private List<ContentTypeToLSPLaunchConfigEntry> workingCopy;
private Button removeButton;
private CheckboxTableViewer checkboxViewer;
private TableViewer viewer;
private final LanguageServersRegistry registry = LanguageServersRegistry.getInstance();
private List<ContentTypeToLSPLaunchConfigEntry> workingCopy = lazyNonNull();
private Button removeButton = lazyNonNull();
private CheckboxTableViewer checkboxViewer = lazyNonNull();
private TableViewer viewer = lazyNonNull();
private final SelectionAdapter contentTypeLinkListener;
private List<ContentTypeToLanguageServerDefinition> changedDefinitions;
private final List<ContentTypeToLanguageServerDefinition> changedDefinitions = new ArrayList<>();

public LanguageServerPreferencePage() {

contentTypeLinkListener = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
public void widgetSelected(@NonNullByDefault({}) SelectionEvent e) {
if (getContainer() instanceof IWorkbenchPreferenceContainer preferenceContainer) {
preferenceContainer.openPage("org.eclipse.ui.preferencePages.ContentTypes", null); //$NON-NLS-1$
}
Expand All @@ -68,9 +72,7 @@ public void widgetSelected(SelectionEvent e) {
}

@Override
public void init(IWorkbench workbench) {
this.changedDefinitions = new ArrayList<>();
this.registry = LanguageServersRegistry.getInstance();
public void init(@NonNullByDefault({}) IWorkbench workbench) {
}

@Override
Expand All @@ -82,7 +84,7 @@ public void setVisible(boolean visible) {
}

@Override
protected Control createContents(Composite parent) {
protected Control createContents(@NonNullByDefault({}) Composite parent) {
final var res = new Composite(parent, SWT.NONE);
res.setLayout(new GridLayout(2, false));
final var intro = new Link(res, SWT.WRAP);
Expand All @@ -106,7 +108,7 @@ protected Control createContents(Composite parent) {
contentTypeColumn.getColumn().setWidth(200);
contentTypeColumn.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
public String getText(@NonNullByDefault({}) Object element) {
return ((ContentTypeToLanguageServerDefinition)element).getKey().getName();
}
});
Expand All @@ -115,7 +117,7 @@ public String getText(Object element) {
launchConfigColumn.getColumn().setWidth(300);
launchConfigColumn.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
public String getText(@NonNullByDefault({}) Object element) {
return ((ContentTypeToLSPLaunchConfigEntry)element).getLaunchConfiguration().getName();
}
});
Expand All @@ -124,7 +126,7 @@ public String getText(Object element) {
launchModeColumn.getColumn().setWidth(100);
launchModeColumn.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
public String getText(@NonNullByDefault({}) Object element) {
final var res = new StringBuilder();
for (String s : ((ContentTypeToLSPLaunchConfigEntry)element).getLaunchModes()) {
res.append(s);
Expand All @@ -145,7 +147,7 @@ public String getText(Object element) {
addButton.setText(Messages.PreferencesPage_Add);
addButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
public void widgetSelected(@NonNullByDefault({}) SelectionEvent e) {
NewContentTypeLSPLaunchDialog dialog = new NewContentTypeLSPLaunchDialog(getShell());
if (dialog.open() == IDialogConstants.OK_ID) {
workingCopy.add(new ContentTypeToLSPLaunchConfigEntry(dialog.getContentType(),
Expand All @@ -160,7 +162,7 @@ public void widgetSelected(SelectionEvent e) {
removeButton.setText(Messages.PreferencesPage_Remove);
removeButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
public void widgetSelected(@NonNullByDefault({}) SelectionEvent e) {
ISelection sel = viewer.getSelection();
if (!sel.isEmpty() && sel instanceof IStructuredSelection structuredSel) {
structuredSel.forEach(workingCopy::remove);
Expand Down Expand Up @@ -188,7 +190,7 @@ private void createStaticServersTable(Composite res) {
enablementColumn.getColumn().setWidth(70);
enablementColumn.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
public @Nullable String getText(@NonNullByDefault({}) Object element) {
return null;
}
});
Expand All @@ -198,7 +200,7 @@ public String getText(Object element) {
contentTypeColumn.getColumn().setWidth(200);
contentTypeColumn.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
public String getText(@NonNullByDefault({}) Object element) {
return ((ContentTypeToLanguageServerDefinition)element).getKey().getName();
}
});
Expand All @@ -208,7 +210,7 @@ public String getText(Object element) {
launchConfigColumn.getColumn().setWidth(300);
launchConfigColumn.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
public String getText(@NonNullByDefault({}) Object element) {
return ((ContentTypeToLanguageServerDefinition)element).getValue().label;
}
});
Expand All @@ -223,7 +225,7 @@ public String getText(Object element) {
conditionColumn.getColumn().setWidth(150);
conditionColumn.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
public String getText(@NonNullByDefault({}) Object element) {
EnablementTester tester = ((ContentTypeToLanguageServerDefinition) element)
.getEnablementCondition();

Expand All @@ -239,7 +241,7 @@ public String getText(Object element) {
}

@Override
public Color getBackground(Object element) {
public @Nullable Color getBackground(@NonNullByDefault({}) Object element) {
EnablementTester tester = ((ContentTypeToLanguageServerDefinition) element)
.getEnablementCondition();
if (tester == null) {
Expand Down Expand Up @@ -280,7 +282,7 @@ public boolean performOk() {
return super.performOk();
}

private IEditorReference[] getEditors() {
private IEditorReference @Nullable [] getEditors() {
var page = UI.getActivePage();
if (page != null) {
return page.getEditorReferences();
Expand Down
Loading

0 comments on commit 562fd0c

Please sign in to comment.