Skip to content

Commit 0205552

Browse files
committed
refact: enable NonNullByDefault for lsp4e.refactoring package
1 parent 37449bd commit 0205552

File tree

4 files changed

+65
-45
lines changed

4 files changed

+65
-45
lines changed

org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/CreateFileChange.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333
import org.eclipse.core.runtime.CoreException;
3434
import org.eclipse.core.runtime.IProgressMonitor;
3535
import org.eclipse.core.runtime.IStatus;
36+
import org.eclipse.core.runtime.NullProgressMonitor;
3637
import org.eclipse.core.runtime.Platform;
3738
import org.eclipse.core.runtime.Status;
3839
import org.eclipse.core.runtime.SubMonitor;
3940
import org.eclipse.core.runtime.content.IContentType;
41+
import org.eclipse.jdt.annotation.Nullable;
4042
import org.eclipse.lsp4e.LSPEclipseUtils;
4143
import org.eclipse.lsp4e.LanguageServerPlugin;
4244
import org.eclipse.lsp4e.ui.Messages;
@@ -51,15 +53,15 @@ public class CreateFileChange extends ResourceChange {
5153

5254
private final URI uri;
5355
private final String fSource;
54-
private String fEncoding;
56+
private @Nullable String fEncoding;
5557
private boolean fExplicitEncoding;
5658
private final long fStampToRestore;
5759

58-
public CreateFileChange(URI uri, String source, String encoding) {
60+
public CreateFileChange(URI uri, String source, @Nullable String encoding) {
5961
this(uri, source, encoding, IResource.NULL_STAMP);
6062
}
6163

62-
public CreateFileChange(URI uri, String source, String encoding, long stampToRestore) {
64+
public CreateFileChange(URI uri, String source, @Nullable String encoding, long stampToRestore) {
6365
Assert.isNotNull(uri, "uri"); //$NON-NLS-1$
6466
Assert.isNotNull(source, "source"); //$NON-NLS-1$
6567
this.uri = uri;
@@ -81,12 +83,12 @@ public String getName() {
8183
}
8284

8385
@Override
84-
protected IFile getModifiedResource() {
86+
protected @Nullable IFile getModifiedResource() {
8587
return LSPEclipseUtils.getFileHandle(this.uri);
8688
}
8789

8890
@Override
89-
public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
91+
public RefactoringStatus isValid(@Nullable IProgressMonitor pm) throws CoreException {
9092
final var result= new RefactoringStatus();
9193

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

100102
@Override
101-
public Change perform(IProgressMonitor pm) throws CoreException {
103+
public @Nullable Change perform(@Nullable IProgressMonitor pm) throws CoreException {
104+
if(pm == null) {
105+
pm = new NullProgressMonitor();
106+
}
102107
pm.beginTask(NLS.bind(Messages.edit_CreateFile, uri), 3);
103108

104109
initializeEncoding();

org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/DeleteExternalFile.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
import org.eclipse.core.runtime.IProgressMonitor;
2020
import org.eclipse.core.runtime.IStatus;
2121
import org.eclipse.core.runtime.Status;
22-
import org.eclipse.jdt.annotation.NonNull;
22+
import org.eclipse.jdt.annotation.Nullable;
2323
import org.eclipse.lsp4e.LanguageServerPlugin;
2424
import org.eclipse.ltk.core.refactoring.Change;
2525
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
2626

2727
public class DeleteExternalFile extends Change {
2828

29-
private final @NonNull File file;
29+
private final File file;
3030

31-
public DeleteExternalFile(@NonNull File file) {
31+
public DeleteExternalFile(File file) {
3232
this.file = file;
3333
}
3434

@@ -38,17 +38,17 @@ public String getName() {
3838
}
3939

4040
@Override
41-
public void initializeValidationData(IProgressMonitor pm) {
41+
public void initializeValidationData(@Nullable IProgressMonitor pm) {
4242
// nothing to do yet, comment requested by sonar
4343
}
4444

4545
@Override
46-
public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
46+
public RefactoringStatus isValid(@Nullable IProgressMonitor pm) throws CoreException {
4747
return RefactoringStatus.create(Status.OK_STATUS);
4848
}
4949

5050
@Override
51-
public Change perform(IProgressMonitor pm) throws CoreException {
51+
public @Nullable Change perform(@Nullable IProgressMonitor pm) throws CoreException {
5252
try {
5353
Files.delete(this.file.toPath());
5454
} catch (IOException e) {

org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/LSPTextChange.java

+44-33
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*******************************************************************************/
1212
package org.eclipse.lsp4e.refactoring;
1313

14+
import static org.eclipse.lsp4e.internal.NullSafetyHelper.castNonNull;
15+
1416
import java.net.URI;
1517

1618
import org.eclipse.core.filebuffers.FileBuffers;
@@ -26,7 +28,8 @@
2628
import org.eclipse.core.runtime.NullProgressMonitor;
2729
import org.eclipse.core.runtime.Status;
2830
import org.eclipse.core.runtime.SubMonitor;
29-
import org.eclipse.jdt.annotation.NonNull;
31+
import org.eclipse.jdt.annotation.NonNullByDefault;
32+
import org.eclipse.jdt.annotation.Nullable;
3033
import org.eclipse.jface.text.BadLocationException;
3134
import org.eclipse.jface.text.IDocument;
3235
import org.eclipse.lsp4e.LSPEclipseUtils;
@@ -47,33 +50,33 @@
4750
@SuppressWarnings("restriction")
4851
public class LSPTextChange extends TextChange {
4952

50-
private final @NonNull URI fileUri;
53+
private final URI fileUri;
5154

52-
private Either<IFile, IFileStore> file;
55+
private @Nullable Either<IFile, IFileStore> file;
5356
private int fAcquireCount;
54-
private ITextFileBuffer fBuffer;
55-
private @NonNull String newText;
56-
private Range range;
57+
private @Nullable ITextFileBuffer fBuffer;
58+
private String newText;
59+
private @Nullable Range range;
5760

58-
public LSPTextChange(@NonNull String name, @NonNull URI fileUri, @NonNull TextEdit textEdit) {
61+
public LSPTextChange(String name, URI fileUri, TextEdit textEdit) {
5962
super(name);
6063
this.fileUri = fileUri;
6164
this.newText = textEdit.getNewText();
6265
this.range = textEdit.getRange();
6366
}
6467

65-
public LSPTextChange(@NonNull String name, @NonNull URI fileUri, @NonNull String newText) {
68+
public LSPTextChange(String name, URI fileUri, String newText) {
6669
super(name);
6770
this.fileUri = fileUri;
6871
this.newText = newText;
6972
this.range = null;
7073
}
7174

7275
@Override
73-
protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {
76+
protected IDocument acquireDocument(@Nullable IProgressMonitor pm) throws CoreException {
7477
fAcquireCount++;
7578
if (fAcquireCount > 1) {
76-
return fBuffer.getDocument();
79+
return castNonNull(this.fBuffer).getDocument();
7780
}
7881

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

8690
ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
87-
if (this.file.isLeft()) {
88-
this.fBuffer = manager.getTextFileBuffer(this.file.getLeft().getFullPath(), LocationKind.IFILE);
91+
if (file.isLeft()) {
92+
this.fBuffer = manager.getTextFileBuffer(file.getLeft().getFullPath(), LocationKind.IFILE);
8993
} else {
90-
this.fBuffer = manager.getFileStoreTextFileBuffer(this.file.getRight());
94+
this.fBuffer = manager.getFileStoreTextFileBuffer(file.getRight());
9195
}
9296
if (this.fBuffer != null) {
9397
fAcquireCount++; // allows to mark open editor dirty instead of saving
9498
} else {
95-
if (this.file.isLeft()) {
96-
manager.connect(this.file.getLeft().getFullPath(), LocationKind.IFILE, pm);
97-
this.fBuffer = manager.getTextFileBuffer(this.file.getLeft().getFullPath(), LocationKind.IFILE);
99+
if (file.isLeft()) {
100+
manager.connect(file.getLeft().getFullPath(), LocationKind.IFILE, pm);
101+
this.fBuffer = manager.getTextFileBuffer(file.getLeft().getFullPath(), LocationKind.IFILE);
98102
} else {
99-
manager.connectFileStore(this.file.getRight(), pm);
100-
this.fBuffer = manager.getFileStoreTextFileBuffer(this.file.getRight());
103+
manager.connectFileStore(file.getRight(), pm);
104+
this.fBuffer = manager.getFileStoreTextFileBuffer(file.getRight());
101105
}
102106
}
103107

@@ -106,9 +110,10 @@ protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {
106110
// because that's used by the preview logic to compute the changed document. We do it here rather than in the constructor
107111
// since we need the document to translate line offsets into character offset. Strictly this would not work then
108112
// if the platform called getEdit() prior to this method being traversed, but it seems to be OK in practice.
109-
final IDocument document = fBuffer.getDocument();
113+
final IDocument document = castNonNull(this.fBuffer).getDocument();
110114
int offset = 0;
111115
int length = document.getLength();
116+
final var range = this.range;
112117
if (range != null) {
113118
try {
114119
offset = LSPEclipseUtils.toOffset(range.getStart(), document);
@@ -123,42 +128,43 @@ protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {
123128
}
124129

125130
@Override
126-
protected void commit(IDocument document, IProgressMonitor pm) throws CoreException {
127-
this.fBuffer.commit(pm, true);
131+
protected void commit(@NonNullByDefault({}) IDocument document, @Nullable IProgressMonitor pm) throws CoreException {
132+
castNonNull(this.fBuffer).commit(pm, true);
128133
}
129134

130135
@Override
131-
protected void releaseDocument(IDocument document, IProgressMonitor pm) throws CoreException {
136+
protected void releaseDocument(@NonNullByDefault({}) IDocument document, @Nullable IProgressMonitor pm) throws CoreException {
132137
Assert.isTrue(fAcquireCount > 0);
133138
if (fAcquireCount == 1) {
134139
ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
135-
this.fBuffer.commit(pm, true);
136-
if (this.file.isLeft()) {
137-
manager.disconnect(this.file.getLeft().getFullPath(), LocationKind.IFILE, pm);
140+
castNonNull(this.fBuffer).commit(pm, true);
141+
final var file = castNonNull(this.file);
142+
if (file.isLeft()) {
143+
manager.disconnect(file.getLeft().getFullPath(), LocationKind.IFILE, pm);
138144
} else {
139-
manager.disconnectFileStore(this.file.getRight(), pm);
145+
manager.disconnectFileStore(file.getRight(), pm);
140146
}
141147
}
142148
fAcquireCount--;
143149
}
144150

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

150156
@Override
151-
public void initializeValidationData(IProgressMonitor pm) {
157+
public void initializeValidationData(@Nullable IProgressMonitor pm) {
152158
// nothing to do yet, comment requested by sonar
153159
}
154160

155161
@Override
156-
public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
162+
public RefactoringStatus isValid(@Nullable IProgressMonitor pm) throws CoreException {
157163
return RefactoringStatus.create(Status.OK_STATUS);
158164
}
159165

160166
@Override
161-
public Object getModifiedElement() {
167+
public @Nullable Object getModifiedElement() {
162168
IFile file = LSPEclipseUtils.getFileHandle(this.fileUri);
163169
if (file != null) {
164170
return file;
@@ -170,7 +176,10 @@ public Object getModifiedElement() {
170176
}
171177

172178
@Override
173-
public Change perform(IProgressMonitor pm) throws CoreException {
179+
public Change perform(@Nullable IProgressMonitor pm) throws CoreException {
180+
if(pm == null) {
181+
pm = new NullProgressMonitor();
182+
}
174183
pm.beginTask("", 3); //$NON-NLS-1$
175184
IDocument document = null;
176185

@@ -179,16 +188,18 @@ public Change perform(IProgressMonitor pm) throws CoreException {
179188

180189
int offset = 0;
181190
int length = document.getLength();
191+
final var range = this.range;
182192
if (range != null) {
183193
offset = LSPEclipseUtils.toOffset(range.getStart(), document);
184194
length = LSPEclipseUtils.toOffset(range.getEnd(), document) - offset;
185195
}
186196

187197
final TextChange delegate;
188-
if (this.file.isRight()) {
198+
final var file = castNonNull(this.file);
199+
if (file.isRight()) {
189200
delegate = new DocumentChange("Change in document " + fileUri.getPath(), document); //$NON-NLS-1$
190201
} else {
191-
delegate = new TextFileChange("Change in file " + this.file.getLeft().getName(), this.file.getLeft()) { //$NON-NLS-1$
202+
delegate = new TextFileChange("Change in file " + file.getLeft().getName(), file.getLeft()) { //$NON-NLS-1$
192203
@Override
193204
protected boolean needsSaving() {
194205
return fAcquireCount == 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NonNullByDefault
2+
package org.eclipse.lsp4e.refactoring;
3+
4+
import org.eclipse.jdt.annotation.NonNullByDefault;

0 commit comments

Comments
 (0)