Skip to content

Commit

Permalink
refact: enable NonNullByDefault for lsp4e base package
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Aug 8, 2024
1 parent b88bb53 commit f181833
Show file tree
Hide file tree
Showing 43 changed files with 612 additions and 582 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public void setup(IDocument document) {

@Override
public void setup(final IDocument document, IPath location, LocationKind locationKind) {
if (document == null) {
return;
}
// Force document connect
CompletableFuture.runAsync(
() -> PENDING_CONNECTIONS.add(LanguageServers.forDocument(document).collectAll(ls -> CompletableFuture.completedFuture(null))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.lsp4e.LanguageServersRegistry.LaunchConfigurationLanguageServerDefinition;

public class ContentTypeToLSPLaunchConfigEntry extends ContentTypeToLanguageServerDefinition {
Expand All @@ -31,8 +31,8 @@ public class ContentTypeToLSPLaunchConfigEntry extends ContentTypeToLanguageServ
private final ILaunchConfiguration launchConfiguration;
private final Set<String> launchModes;

public ContentTypeToLSPLaunchConfigEntry(@NonNull IContentType contentType, @NonNull ILaunchConfiguration launchConfig,
@NonNull Set<String> launchModes) {
public ContentTypeToLSPLaunchConfigEntry(IContentType contentType, ILaunchConfiguration launchConfig,
Set<String> launchModes) {
super(contentType, new LaunchConfigurationLanguageServerDefinition(launchConfig, launchModes), null);
this.launchConfiguration = launchConfig;
this.launchModes = Collections.unmodifiableSet(launchModes);
Expand Down Expand Up @@ -64,7 +64,7 @@ public Set<String> getLaunchModes() {
return launchModes;
}

static ContentTypeToLSPLaunchConfigEntry readFromPreference(String preferenceEntry) {
static @Nullable ContentTypeToLSPLaunchConfigEntry readFromPreference(String preferenceEntry) {
String[] parts = preferenceEntry.split(":"); //$NON-NLS-1$
if (parts.length != 2) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@
import java.util.AbstractMap.SimpleEntry;

import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition;
import org.eclipse.lsp4e.enablement.EnablementTester;

public class ContentTypeToLanguageServerDefinition extends SimpleEntry<IContentType, LanguageServerDefinition> {

private static final long serialVersionUID = 6002703726009331762L;
private final EnablementTester enablement;
private final @Nullable EnablementTester enablement;

public ContentTypeToLanguageServerDefinition(@NonNull IContentType contentType,
@NonNull LanguageServerDefinition provider,
public ContentTypeToLanguageServerDefinition(IContentType contentType, LanguageServerDefinition provider,
@Nullable EnablementTester enablement) {
super(contentType, provider);
this.enablement = enablement;
}

public boolean isEnabled(URI uri) {
public boolean isEnabled(@Nullable URI uri) {
return isUserEnabled() && isExtensionEnabled(uri);
}

Expand All @@ -48,11 +46,11 @@ public boolean isUserEnabled() {
return true;
}

public boolean isExtensionEnabled(URI uri) {
public boolean isExtensionEnabled(@Nullable URI uri) {
return enablement != null ? enablement.evaluate(uri) : true;
}

public EnablementTester getEnablementCondition() {
public @Nullable EnablementTester getEnablementCondition() {
return enablement;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException;
Expand Down Expand Up @@ -76,20 +75,19 @@

final class DocumentContentSynchronizer implements IDocumentListener {

private final @NonNull LanguageServerWrapper languageServerWrapper;
private final @NonNull IDocument document;
private final @NonNull URI fileUri;
private final LanguageServerWrapper languageServerWrapper;
private final IDocument document;
private final URI fileUri;
private final TextDocumentSyncKind syncKind;

private int version = 0;
private DidChangeTextDocumentParams changeParams;
private @Nullable DidChangeTextDocumentParams changeParams;
private long openSaveStamp;
private IPreferenceStore store;
private IFormatRegionsProvider formatRegionsProvider;
private @Nullable IFormatRegionsProvider formatRegionsProvider;

public DocumentContentSynchronizer(@NonNull LanguageServerWrapper languageServerWrapper,
@NonNull LanguageServer languageServer,
@NonNull IDocument document, TextDocumentSyncKind syncKind) {
public DocumentContentSynchronizer(LanguageServerWrapper languageServerWrapper, LanguageServer languageServer,
IDocument document, @Nullable TextDocumentSyncKind syncKind) {
this.languageServerWrapper = languageServerWrapper;
URI uri = LSPEclipseUtils.toUri(document);
if (uri == null) {
Expand Down Expand Up @@ -127,7 +125,7 @@ public DocumentContentSynchronizer(@NonNull LanguageServerWrapper languageServer
languageId = path.lastSegment();
}
}
if (languageId == null && this.fileUri.getSchemeSpecificPart() != null) {
if (languageId == null && !this.fileUri.getSchemeSpecificPart().isEmpty()) {
String part = this.fileUri.getSchemeSpecificPart();
int lastSeparatorIndex = Math.max(part.lastIndexOf('.'), part.lastIndexOf('/'));
languageId = part.substring(lastSeparatorIndex + 1);
Expand Down Expand Up @@ -180,7 +178,7 @@ public void documentAboutToBeChanged(DocumentEvent event) {
*/
private boolean createChangeEvent(DocumentEvent event) {
Assert.isTrue(changeParams == null);
changeParams = new DidChangeTextDocumentParams(new VersionedTextDocumentIdentifier(),
final var changeParams = this.changeParams = new DidChangeTextDocumentParams(new VersionedTextDocumentIdentifier(),
Collections.singletonList(new TextDocumentContentChangeEvent()));
changeParams.getTextDocument().setUri(fileUri.toASCIIString());

Expand Down Expand Up @@ -220,8 +218,7 @@ private boolean serverSupportsWillSaveWaitUntil() {
if(serverCapabilities != null ) {
Either<TextDocumentSyncKind, TextDocumentSyncOptions> textDocumentSync = serverCapabilities.getTextDocumentSync();
if(textDocumentSync.isRight()) {
TextDocumentSyncOptions saveOptions = textDocumentSync.getRight();
return saveOptions != null && Boolean.TRUE.equals(saveOptions.getWillSaveWaitUntil());
return Boolean.TRUE.equals(textDocumentSync.getRight().getWillSaveWaitUntil());
}
}
return false;
Expand All @@ -238,7 +235,7 @@ private boolean serverSupportsWillSaveWaitUntil() {
*
* @return language server's preference ID to define a timeout for willSaveWaitUntil
*/
private static @NonNull String lsToWillSaveWaitUntilTimeoutKey(String serverId) {
private static String lsToWillSaveWaitUntilTimeoutKey(String serverId) {
return serverId + '.' + WILL_SAVE_WAIT_UNTIL_TIMEOUT__KEY;
}

Expand All @@ -263,7 +260,6 @@ public void documentAboutToBeSaved() {
// Use @link{TextDocumentSaveReason.Manual} as the platform does not give enough information to be accurate
final var params = new WillSaveTextDocumentParams(identifier, TextDocumentSaveReason.Manual);


try {
List<TextEdit> edits = languageServerWrapper.executeImpl(ls -> ls.getTextDocumentService().willSaveWaitUntil(params))
.get(lsToWillSaveWaitUntilTimeout(), TimeUnit.SECONDS);
Expand All @@ -290,7 +286,7 @@ public void documentAboutToBeSaved() {

private void formatDocument() {
var regions = getFormatRegions();
if (regions != null && document != null) {
if (regions != null) {
try {
var textSelection = new MultiTextSelection(document, regions);
var edits = requestFormatting(document, textSelection).get(lsToWillSaveWaitUntilTimeout(), TimeUnit.SECONDS);
Expand All @@ -299,11 +295,12 @@ private void formatDocument() {
edits.apply();
} catch (final ConcurrentModificationException ex) {
ServerMessageHandler.showMessage(Messages.LSPFormatHandler_DiscardedFormat, new MessageParams(MessageType.Error, Messages.LSPFormatHandler_DiscardedFormatResponse));
} catch (BadLocationException e) {
LanguageServerPlugin.logError(e);
}
};
} catch (BadLocationException | InterruptedException | ExecutionException | TimeoutException e) {
}
} catch (InterruptedException e) {
LanguageServerPlugin.logError(e);
Thread.currentThread().interrupt();
} catch (Exception e) {
LanguageServerPlugin.logError(e);
}
}
Expand Down Expand Up @@ -341,14 +338,15 @@ private void formatDocument() {
return null;
}

private CompletableFuture<VersionedEdits> requestFormatting(@NonNull IDocument document, @NonNull ITextSelection textSelection) throws BadLocationException {
private CompletableFuture<@Nullable VersionedEdits> requestFormatting(IDocument document, ITextSelection textSelection) throws BadLocationException {
long modificationStamp = DocumentUtil.getDocumentModificationStamp(document);

FormattingOptions formatOptions = LSPFormatter.getFormatOptions();
TextDocumentIdentifier docId = new TextDocumentIdentifier(fileUri.toString());

final ServerCapabilities capabilities = languageServerWrapper.getServerCapabilities();
if (LSPFormatter.isDocumentRangeFormattingSupported(capabilities)
if (capabilities != null
&& LSPFormatter.isDocumentRangeFormattingSupported(capabilities)
&& !(LSPFormatter.isDocumentFormattingSupported(capabilities) && textSelection.getLength() == 0)) {
var rangeParams = LSPFormatter.getRangeFormattingParams(document, textSelection, formatOptions, docId);
return languageServerWrapper.executeImpl(ls -> ls.getTextDocumentService().rangeFormatting(rangeParams).thenApply(edits -> new VersionedEdits(modificationStamp, edits, document)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.core.resources.IFile;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.lsp4e.ui.UI;
Expand All @@ -23,7 +24,7 @@
public class HasLanguageServerPropertyTester 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 IFile file) {
return LanguageServersRegistry.getInstance().canUseLanguageServer(file);
} else if (receiver instanceof IEditorInput editorInput) {
Expand All @@ -38,7 +39,7 @@ public boolean test(Object receiver, String property, Object[] args, Object expe
return false;
}

private boolean test(ITextViewer viewer) {
private boolean test(@Nullable ITextViewer viewer) {
if (viewer != null) {
IDocument document = viewer.getDocument();
if (document != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Map;

import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.IDocument;
import org.eclipse.lsp4j.Diagnostic;
Expand All @@ -40,6 +39,6 @@ public interface IMarkerAttributeComputer {
* the map with the attributes for the marker, where the
* implementation can add attributes
*/
public void addMarkerAttributesForDiagnostic(@NonNull Diagnostic diagnostic, @Nullable IDocument document,
@NonNull IResource resource, @NonNull Map<String, Object> attributes);
public void addMarkerAttributesForDiagnostic(Diagnostic diagnostic, @Nullable IDocument document,
IResource resource, Map<String, Object> attributes);
}
Loading

0 comments on commit f181833

Please sign in to comment.