diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/CreateFileChange.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/CreateFileChange.java index 801c9b04f..8d3a6fb1d 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/CreateFileChange.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/CreateFileChange.java @@ -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; @@ -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; @@ -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(); @@ -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(); diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/DeleteExternalFile.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/DeleteExternalFile.java index f8938b36a..10aaef84d 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/DeleteExternalFile.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/DeleteExternalFile.java @@ -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; } @@ -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) { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/LSPTextChange.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/LSPTextChange.java index fb528058a..1f31b0c11 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/LSPTextChange.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/LSPTextChange.java @@ -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; @@ -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; @@ -47,22 +50,22 @@ @SuppressWarnings("restriction") public class LSPTextChange extends TextChange { - private final @NonNull URI fileUri; + private final URI fileUri; - private Either file; + private @Nullable Either 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; @@ -70,10 +73,10 @@ public LSPTextChange(@NonNull String name, @NonNull URI fileUri, @NonNull String } @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); @@ -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()); } } @@ -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); @@ -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; @@ -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; @@ -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; diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/package-info.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/package-info.java new file mode 100644 index 000000000..eb1143207 --- /dev/null +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/package-info.java @@ -0,0 +1,4 @@ +@NonNullByDefault +package org.eclipse.lsp4e.refactoring; + +import org.eclipse.jdt.annotation.NonNullByDefault;