Skip to content

Commit

Permalink
refact: enable NonNullByDefault for lsp4e.operations package
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Jul 22, 2024
1 parent 0245466 commit 8681cd2
Show file tree
Hide file tree
Showing 78 changed files with 815 additions and 623 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*******************************************************************************/
package org.eclipse.lsp4e.operations.codeactions;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ContextInformation;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
Expand All @@ -29,8 +30,8 @@

public class CodeActionCompletionProposal implements ICompletionProposal {

private CodeAction fcodeAction;
private Command fcommand;
private @Nullable CodeAction fcodeAction;
private @Nullable Command fcommand;
private String fdisplayString;
private final LanguageServerWrapper serverWrapper;

Expand All @@ -39,13 +40,13 @@ public CodeActionCompletionProposal(Either<Command, CodeAction> command, Languag
if (command.isLeft()) {
fcommand = command.getLeft();
fdisplayString = fcommand.getTitle();
} else if (command.isRight()) {
} else {
fcodeAction = command.getRight();
fdisplayString = fcodeAction.getTitle();
}
}

static boolean isCodeActionResolveSupported(ServerCapabilities capabilities) {
static boolean isCodeActionResolveSupported(@Nullable ServerCapabilities capabilities) {
if (capabilities != null) {
Either<Boolean, CodeActionOptions> caProvider = capabilities.getCodeActionProvider();
if (caProvider.isLeft()) {
Expand All @@ -62,6 +63,7 @@ static boolean isCodeActionResolveSupported(ServerCapabilities capabilities) {

@Override
public void apply(IDocument document) {
final var fcodeAction = this.fcodeAction;
if (fcodeAction != null) {
if (isCodeActionResolveSupported(serverWrapper.getServerCapabilities()) && fcodeAction.getEdit() == null) {
// Unresolved code action "edit" property. Resolve it.
Expand All @@ -76,7 +78,7 @@ public void apply(IDocument document) {
}
}

private void apply(CodeAction codeaction) {
private void apply(@Nullable CodeAction codeaction) {
if (codeaction != null) {
if (codeaction.getEdit() != null) {
LSPEclipseUtils.applyWorkspaceEdit(codeaction.getEdit(), codeaction.getTitle());
Expand All @@ -88,13 +90,13 @@ private void apply(CodeAction codeaction) {
}

@Override
public Point getSelection(IDocument document) {
public @Nullable Point getSelection(IDocument document) {
return null;
}

@Override
public String getAdditionalProposalInfo() {
return ""; //$NON-NLS-1$
public @Nullable String getAdditionalProposalInfo() {
return null;
}

@Override
Expand All @@ -103,12 +105,12 @@ public String getDisplayString() {
}

@Override
public Image getImage() {
public @Nullable Image getImage() {
return null;
}

@Override
public IContextInformation getContextInformation() {
public @Nullable IContextInformation getContextInformation() {
return new ContextInformation("some context display string", "some information display string"); //$NON-NLS-1$//$NON-NLS-2$
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.LanguageServerPlugin;
import org.eclipse.lsp4e.LanguageServerWrapper;
Expand Down Expand Up @@ -53,7 +54,7 @@ public String getDescription() {
}

@Override
public Image getImage() {
public @Nullable Image getImage() {
return null;
}

Expand Down Expand Up @@ -127,9 +128,6 @@ private ShowMessageRequestParams reportServerError(LanguageServerDefinition serv

@Override
public IMarker[] findOtherMarkers(IMarker[] markers) {
if (markers == null) {
return new IMarker[0];
}
return Arrays.stream(markers).filter(marker -> {
try {
return codeAction.getDiagnostics()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.lsp4e.LanguageServerWrapper;
import org.eclipse.lsp4e.LanguageServersRegistry;
import org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition;
Expand All @@ -30,9 +30,9 @@

public class CommandMarkerResolution extends WorkbenchMarkerResolution implements IMarkerResolution {

private final @NonNull Command command;
private final Command command;

public CommandMarkerResolution(@NonNull Command command) {
public CommandMarkerResolution(Command command) {
this.command = command;
}

Expand All @@ -57,14 +57,12 @@ public void run(IMarker marker) {
}

LanguageServerWrapper wrapper = LanguageServiceAccessor.getLSWrapper(resource.getProject(), definition);
if (wrapper != null) {
ExecuteCommandOptions provider = wrapper.getServerCapabilities().getExecuteCommandProvider();
if (provider != null && provider.getCommands().contains(command.getCommand())) {
wrapper.execute(ls -> ls.getWorkspaceService()
.executeCommand(new ExecuteCommandParams(command.getCommand(), command.getArguments())));
} else {
CommandExecutor.executeCommandClientSide(command, resource);
}
ExecuteCommandOptions provider = wrapper.getServerCapabilities().getExecuteCommandProvider();
if (provider != null && provider.getCommands().contains(command.getCommand())) {
wrapper.execute(ls -> ls.getWorkspaceService()
.executeCommand(new ExecuteCommandParams(command.getCommand(), command.getArguments())));
} else {
CommandExecutor.executeCommandClientSide(command, resource);
}
}

Expand All @@ -74,7 +72,7 @@ public String getDescription() {
}

@Override
public Image getImage() {
public @Nullable Image getImage() {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.AbstractInformationControlManager;
import org.eclipse.jface.text.ITextHover;
Expand Down Expand Up @@ -200,7 +201,7 @@ private void reinvokeQuickfixProposalsIfNecessary(ITextViewer textViewer) {
}
}

static boolean providesCodeActions(final ServerCapabilities capabilities) {
static boolean providesCodeActions(final @Nullable ServerCapabilities capabilities) {
return capabilities != null && LSPEclipseUtils.hasCapability(capabilities.getCodeActionProvider());
}

Expand All @@ -219,7 +220,7 @@ public boolean hasResolutions(IMarker marker) {
return false;
}

static boolean canPerform(Either<Command, CodeAction> command) {
static boolean canPerform(@Nullable Either<Command, CodeAction> command) {
if (command == null) {
return false;
}
Expand All @@ -241,7 +242,7 @@ static boolean canPerform(Either<Command, CodeAction> command) {
TextDocumentEdit textedit = change.getLeft();
VersionedTextDocumentIdentifier id = textedit.getTextDocument();
URI uri = URI.create(id.getUri());
if (uri != null && LSPEclipseUtils.isReadOnly(uri)) {
if (LSPEclipseUtils.isReadOnly(uri)) {
return false;
}
}
Expand All @@ -251,7 +252,7 @@ static boolean canPerform(Either<Command, CodeAction> command) {
if (changes != null) {
for (java.util.Map.Entry<String, List<TextEdit>> textEdit : changes.entrySet()) {
URI uri = URI.create(textEdit.getKey());
if (uri != null && LSPEclipseUtils.isReadOnly(uri)) {
if (LSPEclipseUtils.isReadOnly(uri)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext;
Expand All @@ -44,8 +44,8 @@
public class LSPCodeActionQuickAssistProcessor implements IQuickAssistProcessor {

// Data necessary for caching proposals
private Object lock = new Object();
private IQuickAssistInvocationContext cachedContext;
private final Object lock = new Object();
private @Nullable IQuickAssistInvocationContext cachedContext;
private List<ICompletionProposal> proposals = Collections.synchronizedList(new ArrayList<>());

private static final ICompletionProposal COMPUTING = new ICompletionProposal() {
Expand All @@ -56,12 +56,12 @@ public void apply(IDocument document) {
}

@Override
public Point getSelection(IDocument document) {
public @Nullable Point getSelection(IDocument document) {
return null;
}

@Override
public String getAdditionalProposalInfo() {
public @Nullable String getAdditionalProposalInfo() {
return null;
}

Expand All @@ -71,19 +71,16 @@ public String getDisplayString() {
}

@Override
public Image getImage() {
public @Nullable Image getImage() {
return JFaceResources.getImage(ProgressInfoItem.class.getPackageName() + ".PROGRESS_DEFAULT"); //$NON-NLS-1$
}

@Override
public IContextInformation getContextInformation() {
public @Nullable IContextInformation getContextInformation() {
return null;
}

};

CompletionProposal[] NO_PROPOSALS = {};

@Override
public String getErrorMessage() {
return "CodeActions not implemented on this Language Server"; //$NON-NLS-1$
Expand All @@ -105,29 +102,30 @@ public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
}

@Override
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
public ICompletionProposal @Nullable [] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
IDocument document = invocationContext.getSourceViewer().getDocument();
if (document == null) {
return NO_PROPOSALS;
return null;
}
LanguageServerDocumentExecutor executor = LanguageServers.forDocument(document).withFilter(LSPCodeActionMarkerResolution::providesCodeActions);
if (!executor.anyMatching()) {
return NO_PROPOSALS;
return null;
}

// If context has changed, i.e. new quick assist invocation rather than old
// proposals computed and calling this method artificially to show proposals in
// the UI
boolean needNewQuery = true;
synchronized (lock) {
needNewQuery = cachedContext == null || invocationContext == null ||
final var cachedContext = this.cachedContext;
needNewQuery = cachedContext == null ||
cachedContext.getClass() != invocationContext.getClass() ||
cachedContext.getSourceViewer() != invocationContext.getSourceViewer() ||
cachedContext.getOffset() != invocationContext.getOffset() ||
cachedContext.getLength() != invocationContext.getLength();
// should also check whether (same) document content changed
if (needNewQuery) {
cachedContext = invocationContext;
this.cachedContext = invocationContext;
}
}

Expand All @@ -138,7 +136,7 @@ public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationC
proposals.clear();
// Start all the servers computing actions - each server will append any code actions to the ongoing list of proposals
// as a side effect of this request
List<CompletableFuture<Void>> futures = executor.computeAll((w, ls) -> ls.getTextDocumentService()
List<CompletableFuture<@Nullable Void>> futures = executor.computeAll((w, ls) -> ls.getTextDocumentService()
.codeAction(params)
.thenAccept(actions -> LanguageServers.streamSafely(actions)
.filter(LSPCodeActionMarkerResolution::canPerform)
Expand All @@ -156,7 +154,7 @@ public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationC
// Server calls didn't complete in time; those that did will have added their results to <code>this.proposals</code> and can be returned
// as an intermediate result; as we're returning control to the UI, we need any stragglers to trigger a refresh when they arrive later on
proposals.add(COMPUTING);
for (CompletableFuture<Void> future : futures) {
for (CompletableFuture<@Nullable Void> future : futures) {
// Refresh will effectively re-enter this method with the same invocationContext and already computed proposals simply to show the proposals in the UI
future.whenComplete((r, t) -> {
if (futures.stream().allMatch(CompletableFuture::isDone)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.lsp4e.operations.codeactions;

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

import java.net.URI;
import java.util.Collections;
import java.util.List;
Expand All @@ -20,7 +22,7 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
Expand Down Expand Up @@ -52,8 +54,8 @@

public class LSPCodeActionsMenu extends ContributionItem implements IWorkbenchContribution {

private IDocument document;
private Range range;
private IDocument document = lateNonNull();
private Range range = lateNonNull();

@Override
public void initialize(IServiceLocator serviceLocator) {
Expand Down Expand Up @@ -81,7 +83,7 @@ public void fill(final Menu menu, int index) {
item.setEnabled(false);

item.setText(Messages.computing);
final @NonNull IDocument document = this.document;
final IDocument document = this.document;
final URI fileUri = LSPEclipseUtils.toUri(document);

final var context = new CodeActionContext(Collections.emptyList());
Expand All @@ -90,10 +92,10 @@ public void fill(final Menu menu, int index) {
params.setRange(this.range);
params.setContext(context);

final @NonNull List<@NonNull CompletableFuture<List<Either<Command, CodeAction>>>> actions
= LanguageServers.forDocument(document).withFilter(LSPCodeActionMarkerResolution::providesCodeActions)
.computeAll((w, ls) -> ls.getTextDocumentService().codeAction(params)
.whenComplete((codeActions, t) -> scheduleMenuUpdate(menu, item, index, document, w, t, codeActions)));
final List<CompletableFuture<@Nullable List<Either<Command, CodeAction>>>> actions = LanguageServers.forDocument(document)
.withFilter(LSPCodeActionMarkerResolution::providesCodeActions)
.computeAll((w, ls) -> ls.getTextDocumentService().codeAction(params).whenComplete(
(codeActions, t) -> scheduleMenuUpdate(menu, item, index, document, w, t, codeActions)));

if (actions.isEmpty()) {
item.setText(Messages.notImplemented);
Expand All @@ -103,10 +105,12 @@ public void fill(final Menu menu, int index) {
super.fill(menu, index);
}

private void scheduleMenuUpdate(final Menu menu, final MenuItem placeHolder, final int index, final IDocument document, final LanguageServerWrapper wrapper, final Throwable ex, final List<Either<Command, CodeAction>> codeActions) {
private void scheduleMenuUpdate(final Menu menu, final MenuItem placeHolder, final int index,
final IDocument document, final LanguageServerWrapper wrapper, final @Nullable Throwable ex,
final @Nullable List<@Nullable Either<Command, CodeAction>> codeActions) {
final var job = new UIJob(menu.getDisplay(), Messages.updateCodeActions_menu) {
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
public IStatus runInUIThread(@Nullable IProgressMonitor monitor) {
if (ex != null) {
final var item = new MenuItem(menu, SWT.NONE, index);
item.setText(String.valueOf(ex.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@NonNullByDefault({ ARRAY_CONTENTS, PARAMETER, RETURN_TYPE, FIELD, TYPE_BOUND, TYPE_ARGUMENT })
package org.eclipse.lsp4e.operations.codeactions;

import static org.eclipse.jdt.annotation.DefaultLocation.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
Loading

0 comments on commit 8681cd2

Please sign in to comment.