11
11
*******************************************************************************/
12
12
package org .eclipse .lsp4e .refactoring ;
13
13
14
+ import static org .eclipse .lsp4e .internal .NullSafetyHelper .castNonNull ;
15
+
14
16
import java .net .URI ;
15
17
16
18
import org .eclipse .core .filebuffers .FileBuffers ;
26
28
import org .eclipse .core .runtime .NullProgressMonitor ;
27
29
import org .eclipse .core .runtime .Status ;
28
30
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 ;
30
33
import org .eclipse .jface .text .BadLocationException ;
31
34
import org .eclipse .jface .text .IDocument ;
32
35
import org .eclipse .lsp4e .LSPEclipseUtils ;
47
50
@ SuppressWarnings ("restriction" )
48
51
public class LSPTextChange extends TextChange {
49
52
50
- private final @ NonNull URI fileUri ;
53
+ private final URI fileUri ;
51
54
52
- private Either <IFile , IFileStore > file ;
55
+ private @ Nullable Either <IFile , IFileStore > file ;
53
56
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 ;
57
60
58
- public LSPTextChange (@ NonNull String name , @ NonNull URI fileUri , @ NonNull TextEdit textEdit ) {
61
+ public LSPTextChange (String name , URI fileUri , TextEdit textEdit ) {
59
62
super (name );
60
63
this .fileUri = fileUri ;
61
64
this .newText = textEdit .getNewText ();
62
65
this .range = textEdit .getRange ();
63
66
}
64
67
65
- public LSPTextChange (@ NonNull String name , @ NonNull URI fileUri , @ NonNull String newText ) {
68
+ public LSPTextChange (String name , URI fileUri , String newText ) {
66
69
super (name );
67
70
this .fileUri = fileUri ;
68
71
this .newText = newText ;
69
72
this .range = null ;
70
73
}
71
74
72
75
@ Override
73
- protected IDocument acquireDocument (IProgressMonitor pm ) throws CoreException {
76
+ protected IDocument acquireDocument (@ Nullable IProgressMonitor pm ) throws CoreException {
74
77
fAcquireCount ++;
75
78
if (fAcquireCount > 1 ) {
76
- return fBuffer .getDocument ();
79
+ return castNonNull ( this . fBuffer ) .getDocument ();
77
80
}
78
81
79
82
IFile iFile = LSPEclipseUtils .getFileHandle (this .fileUri );
@@ -82,22 +85,23 @@ protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {
82
85
} else {
83
86
this .file = Either .forRight (EFS .getStore (this .fileUri ));
84
87
}
88
+ final var file = castNonNull (this .file );
85
89
86
90
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 );
89
93
} else {
90
- this .fBuffer = manager .getFileStoreTextFileBuffer (this . file .getRight ());
94
+ this .fBuffer = manager .getFileStoreTextFileBuffer (file .getRight ());
91
95
}
92
96
if (this .fBuffer != null ) {
93
97
fAcquireCount ++; // allows to mark open editor dirty instead of saving
94
98
} 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 );
98
102
} 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 ());
101
105
}
102
106
}
103
107
@@ -106,9 +110,10 @@ protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {
106
110
// because that's used by the preview logic to compute the changed document. We do it here rather than in the constructor
107
111
// since we need the document to translate line offsets into character offset. Strictly this would not work then
108
112
// 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 ();
110
114
int offset = 0 ;
111
115
int length = document .getLength ();
116
+ final var range = this .range ;
112
117
if (range != null ) {
113
118
try {
114
119
offset = LSPEclipseUtils .toOffset (range .getStart (), document );
@@ -123,42 +128,43 @@ protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {
123
128
}
124
129
125
130
@ 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 );
128
133
}
129
134
130
135
@ Override
131
- protected void releaseDocument (IDocument document , IProgressMonitor pm ) throws CoreException {
136
+ protected void releaseDocument (@ NonNullByDefault ({}) IDocument document , @ Nullable IProgressMonitor pm ) throws CoreException {
132
137
Assert .isTrue (fAcquireCount > 0 );
133
138
if (fAcquireCount == 1 ) {
134
139
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 );
138
144
} else {
139
- manager .disconnectFileStore (this . file .getRight (), pm );
145
+ manager .disconnectFileStore (file .getRight (), pm );
140
146
}
141
147
}
142
148
fAcquireCount --;
143
149
}
144
150
145
151
@ Override
146
- protected Change createUndoChange (UndoEdit edit ) {
152
+ protected Change createUndoChange (@ NonNullByDefault ({}) UndoEdit edit ) {
147
153
throw new UnsupportedOperationException ("Should not be called!" ); //$NON-NLS-1$
148
154
}
149
155
150
156
@ Override
151
- public void initializeValidationData (IProgressMonitor pm ) {
157
+ public void initializeValidationData (@ Nullable IProgressMonitor pm ) {
152
158
// nothing to do yet, comment requested by sonar
153
159
}
154
160
155
161
@ Override
156
- public RefactoringStatus isValid (IProgressMonitor pm ) throws CoreException {
162
+ public RefactoringStatus isValid (@ Nullable IProgressMonitor pm ) throws CoreException {
157
163
return RefactoringStatus .create (Status .OK_STATUS );
158
164
}
159
165
160
166
@ Override
161
- public Object getModifiedElement () {
167
+ public @ Nullable Object getModifiedElement () {
162
168
IFile file = LSPEclipseUtils .getFileHandle (this .fileUri );
163
169
if (file != null ) {
164
170
return file ;
@@ -170,7 +176,10 @@ public Object getModifiedElement() {
170
176
}
171
177
172
178
@ 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
+ }
174
183
pm .beginTask ("" , 3 ); //$NON-NLS-1$
175
184
IDocument document = null ;
176
185
@@ -179,16 +188,18 @@ public Change perform(IProgressMonitor pm) throws CoreException {
179
188
180
189
int offset = 0 ;
181
190
int length = document .getLength ();
191
+ final var range = this .range ;
182
192
if (range != null ) {
183
193
offset = LSPEclipseUtils .toOffset (range .getStart (), document );
184
194
length = LSPEclipseUtils .toOffset (range .getEnd (), document ) - offset ;
185
195
}
186
196
187
197
final TextChange delegate ;
188
- if (this .file .isRight ()) {
198
+ final var file = castNonNull (this .file );
199
+ if (file .isRight ()) {
189
200
delegate = new DocumentChange ("Change in document " + fileUri .getPath (), document ); //$NON-NLS-1$
190
201
} 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$
192
203
@ Override
193
204
protected boolean needsSaving () {
194
205
return fAcquireCount == 1 ;
0 commit comments