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 May 22, 2024
1 parent 0a2ab2d commit 5138164
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
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 +53,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,12 +83,12 @@ public String getName() {
}

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

@Override
public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
public RefactoringStatus isValid(@Nullable IProgressMonitor pm) throws CoreException {
final var result= new RefactoringStatus();

IFileInfo jFile = EFS.getStore(this.uri).fetchInfo();
Expand All @@ -98,7 +100,10 @@ public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
}

@Override
public Change perform(IProgressMonitor pm) throws CoreException {
public @Nullable Change perform(@Nullable IProgressMonitor pm) throws CoreException {
if(pm == null) {
pm = new NullProgressMonitor();
}
pm.beginTask(NLS.bind(Messages.edit_CreateFile, uri), 3);

initializeEncoding();
Expand Down
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 @@ -38,17 +38,17 @@ public String getName() {
}

@Override
public void initializeValidationData(IProgressMonitor pm) {
public void initializeValidationData(@Nullable IProgressMonitor pm) {
// nothing to do yet, comment requested by sonar
}

@Override
public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
public RefactoringStatus isValid(@Nullable IProgressMonitor pm) throws CoreException {
return RefactoringStatus.create(Status.OK_STATUS);
}

@Override
public Change perform(IProgressMonitor pm) throws CoreException {
public @Nullable Change perform(@Nullable 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.castNonNull;

import java.net.URI;

import org.eclipse.core.filebuffers.FileBuffers;
Expand All @@ -26,7 +28,8 @@
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.NonNullByDefault;
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,33 +50,33 @@
@SuppressWarnings("restriction")
public class LSPTextChange extends TextChange {

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

private Either<IFile, IFileStore> file;
private @Nullable Either<IFile, IFileStore> file;
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;
this.range = null;
}

@Override
protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {
protected IDocument acquireDocument(@Nullable IProgressMonitor pm) throws CoreException {
fAcquireCount++;
if (fAcquireCount > 1) {
return fBuffer.getDocument();
return castNonNull(this.fBuffer).getDocument();
}

IFile iFile = LSPEclipseUtils.getFileHandle(this.fileUri);
Expand All @@ -82,22 +85,23 @@ protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {
} else {
this.file = Either.forRight(EFS.getStore(this.fileUri));
}
final var file = castNonNull(this.file);

ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
if (this.file.isLeft()) {
this.fBuffer = manager.getTextFileBuffer(this.file.getLeft().getFullPath(), LocationKind.IFILE);
if (file.isLeft()) {
this.fBuffer = manager.getTextFileBuffer(file.getLeft().getFullPath(), LocationKind.IFILE);
} else {
this.fBuffer = manager.getFileStoreTextFileBuffer(this.file.getRight());
this.fBuffer = manager.getFileStoreTextFileBuffer(file.getRight());
}
if (this.fBuffer != null) {
fAcquireCount++; // allows to mark open editor dirty instead of saving
} else {
if (this.file.isLeft()) {
manager.connect(this.file.getLeft().getFullPath(), LocationKind.IFILE, pm);
this.fBuffer = manager.getTextFileBuffer(this.file.getLeft().getFullPath(), LocationKind.IFILE);
if (file.isLeft()) {
manager.connect(file.getLeft().getFullPath(), LocationKind.IFILE, pm);
this.fBuffer = manager.getTextFileBuffer(file.getLeft().getFullPath(), LocationKind.IFILE);
} else {
manager.connectFileStore(this.file.getRight(), pm);
this.fBuffer = manager.getFileStoreTextFileBuffer(this.file.getRight());
manager.connectFileStore(file.getRight(), pm);
this.fBuffer = manager.getFileStoreTextFileBuffer(file.getRight());
}
}

Expand All @@ -106,9 +110,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) {
try {
offset = LSPEclipseUtils.toOffset(range.getStart(), document);
Expand All @@ -123,42 +128,43 @@ protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {
}

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

@Override
protected void releaseDocument(IDocument document, IProgressMonitor pm) throws CoreException {
protected void releaseDocument(@NonNullByDefault({}) IDocument document, @Nullable IProgressMonitor pm) throws CoreException {
Assert.isTrue(fAcquireCount > 0);
if (fAcquireCount == 1) {
ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
this.fBuffer.commit(pm, true);
if (this.file.isLeft()) {
manager.disconnect(this.file.getLeft().getFullPath(), LocationKind.IFILE, pm);
castNonNull(this.fBuffer).commit(pm, true);
final var file = castNonNull(this.file);
if (file.isLeft()) {
manager.disconnect(file.getLeft().getFullPath(), LocationKind.IFILE, pm);
} else {
manager.disconnectFileStore(this.file.getRight(), pm);
manager.disconnectFileStore(file.getRight(), pm);
}
}
fAcquireCount--;
}

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

@Override
public void initializeValidationData(IProgressMonitor pm) {
public void initializeValidationData(@Nullable IProgressMonitor pm) {
// nothing to do yet, comment requested by sonar
}

@Override
public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
public RefactoringStatus isValid(@Nullable IProgressMonitor pm) throws CoreException {
return RefactoringStatus.create(Status.OK_STATUS);
}

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

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

Expand All @@ -179,16 +188,18 @@ 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;
}

final TextChange delegate;
if (this.file.isRight()) {
final var file = castNonNull(this.file);
if (file.isRight()) {
delegate = new DocumentChange("Change in document " + fileUri.getPath(), document); //$NON-NLS-1$
} else {
delegate = new TextFileChange("Change in file " + this.file.getLeft().getName(), this.file.getLeft()) { //$NON-NLS-1$
delegate = new TextFileChange("Change in file " + file.getLeft().getName(), file.getLeft()) { //$NON-NLS-1$
@Override
protected boolean needsSaving() {
return fAcquireCount == 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@NonNullByDefault
package org.eclipse.lsp4e.refactoring;

import org.eclipse.jdt.annotation.NonNullByDefault;

0 comments on commit 5138164

Please sign in to comment.