Skip to content

Commit

Permalink
refact: enable NonNullByDefault for lsp4e.refactoring package
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Aug 8, 2024
1 parent 56a0829 commit 7034b47
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.LanguageServerPlugin;
import org.eclipse.lsp4e.ui.Messages;
Expand All @@ -51,15 +52,15 @@ public class CreateFileChange extends ResourceChange {

private final URI uri;
private final String fSource;
private String fEncoding;
private @Nullable String fEncoding;
private boolean fExplicitEncoding;
private final long fStampToRestore;

public CreateFileChange(URI uri, String source, String encoding) {
public CreateFileChange(URI uri, String source, @Nullable String encoding) {
this(uri, source, encoding, IResource.NULL_STAMP);
}

public CreateFileChange(URI uri, String source, String encoding, long stampToRestore) {
public CreateFileChange(URI uri, String source, @Nullable String encoding, long stampToRestore) {
Assert.isNotNull(uri, "uri"); //$NON-NLS-1$
Assert.isNotNull(source, "source"); //$NON-NLS-1$
this.uri = uri;
Expand All @@ -81,7 +82,7 @@ public String getName() {
}

@Override
protected IFile getModifiedResource() {
protected @Nullable IFile getModifiedResource() {
return LSPEclipseUtils.getFileHandle(this.uri);
}

Expand All @@ -98,10 +99,10 @@ public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
}

@Override
public Change perform(IProgressMonitor pm) throws CoreException {
public @Nullable Change perform(IProgressMonitor pm) throws CoreException {
pm.beginTask(NLS.bind(Messages.edit_CreateFile, uri), 3);

initializeEncoding();
final var fEncoding = initializeEncoding();

try (InputStream is= new ByteArrayInputStream(fSource.getBytes(fEncoding))) {

Expand Down Expand Up @@ -158,7 +159,7 @@ public Change perform(IProgressMonitor pm) throws CoreException {
return null;
}

private void initializeEncoding() {
private String initializeEncoding() {
if (fEncoding == null) {
fExplicitEncoding= false;
IFile ifile = getModifiedResource();
Expand Down Expand Up @@ -188,5 +189,6 @@ private void initializeEncoding() {
}
}
Assert.isNotNull(fEncoding);
return fEncoding;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
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.lsp4e.LanguageServerPlugin;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;

public class DeleteExternalFile extends Change {

private final @NonNull File file;
private final File file;

public DeleteExternalFile(@NonNull File file) {
public DeleteExternalFile(File file) {
this.file = file;
}

Expand All @@ -48,7 +48,7 @@ public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
}

@Override
public Change perform(IProgressMonitor pm) throws CoreException {
public @Nullable Change perform(IProgressMonitor pm) throws CoreException {
try {
Files.delete(this.file.toPath());
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*******************************************************************************/
package org.eclipse.lsp4e.refactoring;

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

import java.net.URI;

import org.eclipse.core.filebuffers.FileBuffers;
Expand All @@ -26,7 +28,7 @@
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.lsp4e.LSPEclipseUtils;
Expand All @@ -47,22 +49,22 @@
@SuppressWarnings("restriction")
public class LSPTextChange extends TextChange {

private final @NonNull URI fileUri;
private final URI fileUri;

private Either<IFile, IFileStore> file;
private Either<IFile, IFileStore> file = lateNonNull();
private int fAcquireCount;
private ITextFileBuffer fBuffer;
private @NonNull String newText;
private Range range;
private @Nullable ITextFileBuffer fBuffer;
private String newText;
private @Nullable Range range;

public LSPTextChange(@NonNull String name, @NonNull URI fileUri, @NonNull TextEdit textEdit) {
public LSPTextChange(String name, URI fileUri, TextEdit textEdit) {
super(name);
this.fileUri = fileUri;
this.newText = textEdit.getNewText();
this.range = textEdit.getRange();
}

public LSPTextChange(@NonNull String name, @NonNull URI fileUri, @NonNull String newText) {
public LSPTextChange(String name, URI fileUri, String newText) {
super(name);
this.fileUri = fileUri;
this.newText = newText;
Expand All @@ -73,7 +75,7 @@ public LSPTextChange(@NonNull String name, @NonNull URI fileUri, @NonNull String
protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {
fAcquireCount++;
if (fAcquireCount > 1) {
return fBuffer.getDocument();
return castNonNull(this.fBuffer).getDocument();
}

IFile iFile = LSPEclipseUtils.getFileHandle(this.fileUri);
Expand Down Expand Up @@ -106,9 +108,10 @@ protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {
// because that's used by the preview logic to compute the changed document. We do it here rather than in the constructor
// since we need the document to translate line offsets into character offset. Strictly this would not work then
// if the platform called getEdit() prior to this method being traversed, but it seems to be OK in practice.
final IDocument document = fBuffer.getDocument();
final IDocument document = castNonNull(this.fBuffer).getDocument();
int offset = 0;
int length = document.getLength();
final var range = this.range;
if (range != null && getEdit() == null) {
try {
offset = LSPEclipseUtils.toOffset(range.getStart(), document);
Expand All @@ -124,15 +127,15 @@ protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {

@Override
protected void commit(IDocument document, IProgressMonitor pm) throws CoreException {
this.fBuffer.commit(pm, true);
castNonNull(this.fBuffer).commit(pm, true);
}

@Override
protected void releaseDocument(IDocument document, IProgressMonitor pm) throws CoreException {
Assert.isTrue(fAcquireCount > 0);
if (fAcquireCount == 1) {
ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
this.fBuffer.commit(pm, true);
castNonNull(this.fBuffer).commit(pm, true);
if (this.file.isLeft()) {
manager.disconnect(this.file.getLeft().getFullPath(), LocationKind.IFILE, pm);
} else {
Expand All @@ -143,7 +146,7 @@ protected void releaseDocument(IDocument document, IProgressMonitor pm) throws C
}

@Override
protected Change createUndoChange(UndoEdit edit) {
protected @Nullable Change createUndoChange(UndoEdit edit) {
throw new UnsupportedOperationException("Should not be called!"); //$NON-NLS-1$
}

Expand All @@ -158,7 +161,7 @@ public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
}

@Override
public Object getModifiedElement() {
public @Nullable Object getModifiedElement() {
IFile file = LSPEclipseUtils.getFileHandle(this.fileUri);
if (file != null) {
return file;
Expand All @@ -170,7 +173,7 @@ public Object getModifiedElement() {
}

@Override
public Change perform(IProgressMonitor pm) throws CoreException {
public @Nullable Change perform(IProgressMonitor pm) throws CoreException {
pm.beginTask("", 3); //$NON-NLS-1$
IDocument document = null;

Expand All @@ -179,6 +182,7 @@ public Change perform(IProgressMonitor pm) throws CoreException {

int offset = 0;
int length = document.getLength();
final var range = this.range;
if (range != null) {
offset = LSPEclipseUtils.toOffset(range.getStart(), document);
length = LSPEclipseUtils.toOffset(range.getEnd(), document) - offset;
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.refactoring;

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

import org.eclipse.jdt.annotation.NonNullByDefault;

0 comments on commit 7034b47

Please sign in to comment.