diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/EnableDisableLSJob.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/EnableDisableLSJob.java index c6dbb3518..52b4433dd 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/EnableDisableLSJob.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/EnableDisableLSJob.java @@ -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; @@ -25,17 +26,17 @@ public class EnableDisableLSJob extends Job { private final List serverDefinitions; - private final IEditorReference[] editors; + private final IEditorReference @Nullable [] editors; public EnableDisableLSJob(List 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) { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LSPImages.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LSPImages.java index cd2bc504d..fd7c686ff 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LSPImages.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LSPImages.java @@ -11,6 +11,8 @@ *******************************************************************************/ 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; @@ -18,6 +20,7 @@ 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; @@ -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 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$ @@ -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); } /** @@ -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; } @@ -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; } @@ -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; } @@ -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); @@ -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 diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LanguageServerPreferencePage.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LanguageServerPreferencePage.java index 972203242..d407261eb 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LanguageServerPreferencePage.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LanguageServerPreferencePage.java @@ -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; @@ -47,19 +51,19 @@ public class LanguageServerPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { - private LanguageServersRegistry registry; - private List workingCopy; - private Button removeButton; - private CheckboxTableViewer checkboxViewer; - private TableViewer viewer; + private final LanguageServersRegistry registry = LanguageServersRegistry.getInstance(); + private List workingCopy = lazyNonNull(); + private Button removeButton = lazyNonNull(); + private CheckboxTableViewer checkboxViewer = lazyNonNull(); + private TableViewer viewer = lazyNonNull(); private final SelectionAdapter contentTypeLinkListener; - private List changedDefinitions; + private final List 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$ } @@ -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 @@ -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); @@ -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(); } }); @@ -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(); } }); @@ -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); @@ -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(), @@ -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); @@ -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; } }); @@ -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(); } }); @@ -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; } }); @@ -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(); @@ -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) { @@ -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(); diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LanguageServersView.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LanguageServersView.java index 81f0a08ae..2cac93e07 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LanguageServersView.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LanguageServersView.java @@ -11,6 +11,8 @@ *******************************************************************************/ package org.eclipse.lsp4e.ui; +import static org.eclipse.lsp4e.internal.NullSafetyHelper.*; + import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -22,6 +24,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.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.ColumnLabelProvider; @@ -50,8 +53,8 @@ public class LanguageServersView extends ViewPart { private static final String EMPTY = ""; //$NON-NLS-1$ private static final String NOT_AVAILABLE = "n/a"; //$NON-NLS-1$ - private TableViewer viewer; - private Job viewerRefreshJob; + private TableViewer viewer = lazyNonNull(); + private @Nullable Job viewerRefreshJob; private final Map actionButtons = new HashMap<>(); private final List columnLabelProviders = new ArrayList<>(); @@ -60,14 +63,14 @@ public class LanguageServersView extends ViewPart { private final ViewerComparator tableSorter = new ViewerComparator() { private int compare(int columnIndex, @Nullable final Object e1, @Nullable final Object e2) { - var labelProvider = columnLabelProviders.get(columnIndex); + var labelProvider = castNonNull(columnLabelProviders.get(columnIndex)); return getComparator().compare( // Objects.toString(labelProvider.getText(e1), EMPTY), Objects.toString(labelProvider.getText(e2), EMPTY)); } @Override - public int compare(final Viewer viewer, @Nullable final Object e1, @Nullable final Object e2) { + public int compare(final @NonNullByDefault({}) Viewer viewer, @Nullable final Object e1, @Nullable final Object e2) { int sortResult = compare(tableSortColumn, e1, e2); // use the "Initial Project" column as secondary sort column @@ -105,7 +108,7 @@ public void widgetSelected(@Nullable final SelectionEvent e) { } @Override - public void createPartControl(Composite parent) { + public void createPartControl(@NonNullByDefault({}) Composite parent) { viewer = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER); viewer.setComparator(tableSorter); @@ -115,10 +118,10 @@ public void createPartControl(Composite parent) { createColumn(EMPTY, 26, new ColumnLabelProvider() { @Override - public void update(ViewerCell cell) { + public void update(@NonNullByDefault({}) ViewerCell cell) { final var lsWrapper = (LanguageServerWrapper) cell.getElement(); final var item = (TableItem) cell.getItem(); - final var buttons = actionButtons.computeIfAbsent(lsWrapper, unused -> { + final var buttons = castNonNull(actionButtons.computeIfAbsent(lsWrapper, unused -> { final var toolBar = new ToolBar((Composite) cell.getViewerRow().getControl(), SWT.FLAT); toolBar.setBackground(cell.getBackground()); final var terminateButton = new ToolItem(toolBar, SWT.PUSH); @@ -126,13 +129,13 @@ public void update(ViewerCell cell) { terminateButton.setToolTipText("Terminate this language server"); //$NON-NLS-1$ terminateButton.addSelectionListener(new SelectionAdapter() { @Override - public void widgetSelected(SelectionEvent ev) { + public void widgetSelected(@NonNullByDefault({}) SelectionEvent ev) { lsWrapper.stop(); updateViewerInput(); } }); return toolBar; - }); + })); final var editor = new TableEditor(item.getParent()); editor.setEditor(buttons, item, cell.getColumnIndex()); editor.grabHorizontal = true; @@ -143,7 +146,7 @@ public void widgetSelected(SelectionEvent ev) { createColumn("Initial Project", 150, new ColumnLabelProvider() { //$NON-NLS-1$ @Override - public String getText(Object element) { + public String getText(@NonNullByDefault({}) Object element) { final var lsWrapper = (LanguageServerWrapper) element; final var p = lsWrapper.initialProject; return p == null ? NOT_AVAILABLE : p.getName(); @@ -152,7 +155,7 @@ public String getText(Object element) { createColumn("Name", 150, new ColumnLabelProvider() { //$NON-NLS-1$ @Override - public String getText(Object element) { + public String getText(@NonNullByDefault({}) Object element) { final var lsWrapper = (LanguageServerWrapper) element; return lsWrapper.serverDefinition.label; } @@ -160,7 +163,7 @@ public String getText(Object element) { createColumn("Executable", 100, new ColumnLabelProvider() { //$NON-NLS-1$ @Override - public String getText(Object element) { + public String getText(@NonNullByDefault({}) Object element) { final ProcessHandle ph = ((LanguageServerWrapper) element).getProcessHandle(); final String exe = ph == null ? null : ph.info().command().orElse(null); if (exe == null) @@ -174,7 +177,7 @@ public String getText(Object element) { createColumn("PID", 50, new ColumnLabelProvider() { //$NON-NLS-1$ @Override - public String getText(Object element) { + public String getText(@NonNullByDefault({}) Object element) { final ProcessHandle ph = ((LanguageServerWrapper) element).getProcessHandle(); if (ph == null) return NOT_AVAILABLE; @@ -188,7 +191,7 @@ public String getText(Object element) { createColumn("Command Line", 400, new ColumnLabelProvider() { //$NON-NLS-1$ @Override - public String getText(Object element) { + public String getText(@NonNullByDefault({}) Object element) { final ProcessHandle ph = ((LanguageServerWrapper) element).getProcessHandle(); if (ph == null) return NOT_AVAILABLE; @@ -199,7 +202,7 @@ public String getText(Object element) { createColumn("ID", 150, new ColumnLabelProvider() { //$NON-NLS-1$ @Override - public String getText(Object element) { + public String getText(@NonNullByDefault({}) Object element) { final var lsWrapper = (LanguageServerWrapper) element; return lsWrapper.serverDefinition.id; } @@ -222,9 +225,9 @@ public void dispose() { } private void scheduleRefreshJob() { - viewerRefreshJob = new Job("Refresh Language Server Processes view") { //$NON-NLS-1$ + final var viewerRefreshJob = this.viewerRefreshJob = new Job("Refresh Language Server Processes view") { //$NON-NLS-1$ @Override - protected IStatus run(IProgressMonitor monitor) { + protected IStatus run(@Nullable IProgressMonitor monitor) { if (getSite().getPage().isPartVisible(LanguageServersView.this)) { updateViewerInput(); } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LoggingPreferencePage.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LoggingPreferencePage.java index 62a27f647..77428a471 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LoggingPreferencePage.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LoggingPreferencePage.java @@ -12,6 +12,7 @@ *******************************************************************************/ package org.eclipse.lsp4e.ui; +import static org.eclipse.lsp4e.internal.NullSafetyHelper.lazyNonNull; import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter; import java.util.ArrayList; @@ -19,6 +20,8 @@ import java.util.HashSet; import java.util.Map; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.preference.IPreferenceStore; @@ -62,7 +65,7 @@ private BooleanMapEditingSupport(TableViewer viewer, Map map) } @Override - protected void setValue(Object element, Object value) { + protected void setValue(@NonNullByDefault({}) Object element, @NonNullByDefault({}) Object value) { final var server = (ContentTypeToLanguageServerDefinition) element; map.put(server.getValue().id, (Boolean)value); hasLoggingBeenChanged = true; @@ -70,18 +73,18 @@ protected void setValue(Object element, Object value) { } @Override - protected Object getValue(Object element) { + protected @Nullable Object getValue(@NonNullByDefault({}) Object element) { final var server = (ContentTypeToLanguageServerDefinition) element; return map.get(server.getValue().id); } @Override - protected CellEditor getCellEditor(Object element) { + protected CellEditor getCellEditor(@NonNullByDefault({}) Object element) { return new CheckboxCellEditor(); } @Override - protected boolean canEdit(Object element) { + protected boolean canEdit(@NonNullByDefault({}) Object element) { return true; } } @@ -95,27 +98,27 @@ private BooleanMapLabelProvider(Map map) { } @Override - public String getText(Object element) { + public String getText(@NonNullByDefault({}) Object element) { return map.getOrDefault(((ContentTypeToLanguageServerDefinition) element).getValue().id, false) ? Messages.PreferencePage_enablementCondition_true : Messages.PreferencePage_enablementCondition_false; } } - private TableViewer languageServerViewer; - private TableViewer launchConfigurationViewer; + private TableViewer languageServerViewer = lazyNonNull(); + private TableViewer launchConfigurationViewer = lazyNonNull(); private final Map serverEnableLoggingToFile = new HashMap<>(); private final Map serverEnableLoggingToConsole = new HashMap<>(); private final IPreferenceStore store = LanguageServerPlugin.getDefault().getPreferenceStore(); private boolean hasLoggingBeenChanged = false; @Override - public void init(IWorkbench workbench) { + public void init(@NonNullByDefault({}) IWorkbench workbench) { //nothing to do } @Override - protected Control createContents(Composite parent) { + protected Control createContents(@NonNullByDefault({}) Composite parent) { final var res = new Composite(parent, SWT.NONE); res.setLayout(new GridLayout(1, false)); @@ -136,7 +139,7 @@ private void createStaticServersTable(Composite res) { 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; } }); @@ -156,7 +159,7 @@ private void createLaunchConfigurationServersTable(Composite res) { 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(); } }); diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/Messages.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/Messages.java index 4ad92d8a3..4e50010b8 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/Messages.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/Messages.java @@ -13,8 +13,10 @@ *******************************************************************************/ package org.eclipse.lsp4e.ui; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.osgi.util.NLS; +@NonNullByDefault({}) public final class Messages extends NLS { public static String definitionHyperlinkLabel; diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/NewContentTypeLSPLaunchDialog.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/NewContentTypeLSPLaunchDialog.java index 6239b933c..b95ef5e73 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/NewContentTypeLSPLaunchDialog.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/NewContentTypeLSPLaunchDialog.java @@ -16,6 +16,7 @@ import java.util.Objects; import java.util.Set; +import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; @@ -27,7 +28,8 @@ import org.eclipse.debug.core.ILaunchManager; import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationTreeContentProvider; import org.eclipse.debug.ui.DebugUITools; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.ComboViewer; @@ -59,7 +61,7 @@ public class NewContentTypeLSPLaunchDialog extends Dialog { private static final class ContentTypesLabelProvider extends LabelProvider { @Override - public String getText(Object element) { + public String getText(@NonNullByDefault({}) Object element) { final var contentType = (IContentType) element; return contentType.getName(); } @@ -67,10 +69,13 @@ public String getText(Object element) { private static final class ContentTypesContentProvider implements ITreeContentProvider { - private IContentTypeManager manager; + private @Nullable IContentTypeManager manager; @Override - public Object[] getChildren(Object parentElement) { + public Object[] getChildren(@Nullable Object parentElement) { + final var manager = this.manager; + if(manager == null) + return new Object[0]; final var elements = new ArrayList(); final var baseType = (IContentType) parentElement; IContentType[] contentTypes = manager.getAllContentTypes(); @@ -84,40 +89,39 @@ public Object[] getChildren(Object parentElement) { } @Override - public Object getParent(Object element) { + public @Nullable Object getParent(@Nullable Object element) { + if (element == null) + return null; final var contentType = (IContentType) element; return contentType.getBaseType(); } @Override - public boolean hasChildren(Object element) { + public boolean hasChildren(@Nullable Object element) { return getChildren(element).length > 0; } @Override - public Object[] getElements(Object inputElement) { + public Object[] getElements(@Nullable Object inputElement) { return getChildren(null); } @Override - public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + public void inputChanged(@NonNullByDefault({}) Viewer viewer, @Nullable Object oldInput, @Nullable Object newInput) { manager = (IContentTypeManager) newInput; } } - protected IContentType contentType; - protected ILaunchConfiguration launchConfig; - protected Set launchMode; + protected @Nullable IContentType contentType; + protected @Nullable ILaunchConfiguration launchConfig; + protected Set launchMode = Collections.emptySet(); - // - //// - - protected NewContentTypeLSPLaunchDialog(Shell parentShell) { + protected NewContentTypeLSPLaunchDialog(@NonNullByDefault({}) Shell parentShell) { super(parentShell); } @Override - protected Control createDialogArea(Composite parent) { + protected Control createDialogArea(@NonNullByDefault({}) Composite parent) { final var res = (Composite)super.createDialogArea(parent); res.setLayout(new GridLayout(2, false)); new Label(res, SWT.NONE).setText(Messages.NewContentTypeLSPLaunchDialog_associateContentType); @@ -154,7 +158,7 @@ protected Control createDialogArea(Composite parent) { launchModeViewer.setContentProvider(new ArrayContentProvider()); launchModeViewer.setLabelProvider(new LabelProvider() { @Override - public String getText(Object o) { + public String getText(@NonNullByDefault({}) Object o) { final var res = new StringBuilder(); for (String s : (Set)o) { res.append(s); @@ -180,7 +184,7 @@ public String getText(Object o) { launchModeViewer.addSelectionChangedListener(event -> { ISelection sel = event.getSelection(); if (sel.isEmpty()) { - launchMode = null; + launchMode = Collections.emptySet(); } else if (sel instanceof IStructuredSelection structuredSel) { launchMode = (Set) structuredSel.getFirstElement(); } @@ -190,25 +194,28 @@ public String getText(Object o) { } @Override - protected Control createContents(Composite parent) { + protected Control createContents(@NonNullByDefault({}) Composite parent) { Control res = super.createContents(parent); updateButtons(); return res; } public IContentType getContentType() { - return this.contentType; + Assert.isNotNull(contentType); + return contentType; } public ILaunchConfiguration getLaunchConfiguration() { - return this.launchConfig; + Assert.isNotNull(launchConfig); + return launchConfig; } private void updateButtons() { - getButton(OK).setEnabled(contentType != null && launchConfig != null && launchMode != null); + getButton(OK).setEnabled(contentType != null && launchConfig != null && !launchMode.isEmpty()); } private void updateLaunchModes(ComboViewer launchModeViewer) { + final var launchConfig = this.launchConfig; if (launchConfig == null) { launchModeViewer.setInput(Collections.emptyList()); } else { @@ -232,7 +239,7 @@ private void updateLaunchModes(ComboViewer launchModeViewer) { } if (currentMode == null) { for (Set mode : modes) { - if (mode.size() == 1 && mode.iterator().next().equals(ILaunchManager.RUN_MODE)) { + if (mode.size() == 1 && ILaunchManager.RUN_MODE.equals(mode.iterator().next())) { currentMode = mode; launchModeViewer.setSelection(new StructuredSelection(currentMode)); } @@ -245,7 +252,7 @@ private void updateLaunchModes(ComboViewer launchModeViewer) { updateButtons(); } - public @NonNull Set getLaunchMode() { + public Set getLaunchMode() { return this.launchMode; } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/package-info.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/package-info.java new file mode 100644 index 000000000..848224c19 --- /dev/null +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/package-info.java @@ -0,0 +1,4 @@ +@NonNullByDefault +package org.eclipse.lsp4e.ui; + +import org.eclipse.jdt.annotation.NonNullByDefault; diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/views/package-info.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/views/package-info.java new file mode 100644 index 000000000..16e23e7fd --- /dev/null +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/views/package-info.java @@ -0,0 +1,4 @@ +@NonNullByDefault +package org.eclipse.lsp4e.ui.views; + +import org.eclipse.jdt.annotation.NonNullByDefault;