1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.testFramework;
import com.intellij.lang.Language;
import com.intellij.openapi.fileTypes.CharsetUtil;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeRegistry;
import com.intellij.openapi.vfs.*;
import com.intellij.util.LocalTimeCounter;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.*;
import java.nio.charset.Charset;
/**
* In-memory implementation of {@link VirtualFile}.
*/
public class LightVirtualFile extends VirtualFile {
private FileType myFileType;
private CharSequence myContent = "";
private String myName = "";
private long myModStamp = LocalTimeCounter.currentTime();
private boolean myIsWritable = true;
private boolean myValid = true;
private Language myLanguage;
private VirtualFile myOriginalFile;
public LightVirtualFile() {
this("");
}
public LightVirtualFile(@NonNls String name) {
this(name, "");
}
public LightVirtualFile(@NonNls String name, CharSequence content) {
this(name, null, content, LocalTimeCounter.currentTime());
}
public LightVirtualFile(final String name, final FileType fileType, final CharSequence text) {
this(name, fileType, text, LocalTimeCounter.currentTime());
}
public LightVirtualFile(VirtualFile original, final CharSequence text, long modificationStamp) {
this(original.getName(), original.getFileType(), text, modificationStamp);
setCharset(original.getCharset());
}
public LightVirtualFile(final String name, final FileType fileType, final CharSequence text, final long modificationStamp) {
this(name, fileType, text, CharsetUtil.extractCharsetFromFileContent(null, null, fileType, text), modificationStamp);
}
public LightVirtualFile(final String name, final FileType fileType, final CharSequence text, Charset charset, final long modificationStamp) {
myName = name;
myFileType = fileType;
setContent(text);
myModStamp = modificationStamp;
setCharset(charset);
}
public LightVirtualFile(final String name, final Language language, final CharSequence text) {
myName = name;
setContent(text);
myModStamp = LocalTimeCounter.currentTime();
setLanguage(language);
}
public Language getLanguage() {
return myLanguage;
}
public void setLanguage(final Language language) {
myLanguage = language;
myFileType = language.getAssociatedFileType();
if (myFileType == null) {
myFileType = FileTypeRegistry.getInstance().getFileTypeByFileName(myName);
}
}
public void setFileType(final FileType fileType) {
myFileType = fileType;
}
private void setContent(CharSequence content) {
//StringUtil.assertValidSeparators(content);
myContent = content;
}
public VirtualFile getOriginalFile() {
return myOriginalFile;
}
public void setOriginalFile(VirtualFile originalFile) {
myOriginalFile = originalFile;
}
private static class MyVirtualFileSystem extends DeprecatedVirtualFileSystem implements NonPhysicalFileSystem{
@NonNls private static final String PROTOCOL = "mock";
private MyVirtualFileSystem() {
startEventPropagation();
}
@Override
@NotNull
public String getProtocol() {
return PROTOCOL;
}
@Override
@Nullable
public VirtualFile findFileByPath(@NotNull String path) {
return null;
}
@Override
public void refresh(boolean asynchronous) {
}
@Override
@Nullable
public VirtualFile refreshAndFindFileByPath(@NotNull String path) {
return null;
}
@Override
public void deleteFile(Object requestor, @NotNull VirtualFile vFile) throws IOException {
}
@Override
public void moveFile(Object requestor, @NotNull VirtualFile vFile, @NotNull VirtualFile newParent) throws IOException {
}
@NotNull
@Override
public VirtualFile copyFile(Object requestor, @NotNull VirtualFile vFile, @NotNull VirtualFile newParent, @NotNull final String copyName) throws IOException {
throw new IOException("Cannot copy files");
}
@Override
public void renameFile(Object requestor, @NotNull VirtualFile vFile, @NotNull String newName) throws IOException {
}
@NotNull
@Override
public VirtualFile createChildFile(Object requestor, @NotNull VirtualFile vDir, @NotNull String fileName) throws IOException {
throw new IOException("Cannot create files");
}
@Override
@NotNull
public VirtualFile createChildDirectory(Object requestor, @NotNull VirtualFile vDir, @NotNull String dirName) throws IOException {
throw new IOException("Cannot create directories");
}
}
private static final MyVirtualFileSystem ourFileSystem = new MyVirtualFileSystem();
@Override
@NotNull
public VirtualFileSystem getFileSystem() {
return ourFileSystem;
}
@Nullable
public FileType getAssignedFileType() {
return myFileType;
}
@NotNull
@Override
public String getPath() {
return "/" + getName();
}
@Override
@NotNull
public String getName() {
return myName;
}
@Override
public boolean isWritable() {
return myIsWritable;
}
@Override
public boolean isDirectory() {
return false;
}
@Override
public boolean isValid() {
return myValid;
}
public void setValid(boolean valid) {
myValid = valid;
}
@Override
public VirtualFile getParent() {
return null;
}
@Override
public VirtualFile[] getChildren() {
return EMPTY_ARRAY;
}
@Override
public InputStream getInputStream() throws IOException {
return VfsUtilCore.byteStreamSkippingBOM(contentsToByteArray(), this);
}
@Override
@NotNull
public OutputStream getOutputStream(Object requestor, final long newModificationStamp, long newTimeStamp) throws IOException {
return VfsUtilCore.outputStreamAddingBOM(new ByteArrayOutputStream() {
@Override
public void close() {
myModStamp = newModificationStamp;
try {
String content = toString(getCharset().name());
setContent(content);
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
},this);
}
@Override
@NotNull
public byte[] contentsToByteArray() throws IOException {
final Charset charset = getCharset();
final String s = getContent().toString();
return charset != null ? s.getBytes(charset.name()) : s.getBytes();
}
@Override
public long getModificationStamp() {
return myModStamp;
}
@Override
public long getTimeStamp() {
return 0; // todo[max] : Add UnsupportedOperationException at better times.
}
@Override
public long getLength() {
try {
return contentsToByteArray().length;
}
catch (IOException e) {
//noinspection CallToPrintStackTrace
e.printStackTrace();
assert false;
return 0;
}
}
@Override
public void refresh(boolean asynchronous, boolean recursive, Runnable postRunnable) {
}
public void setContent(Object requestor, CharSequence content, boolean fireEvent) {
setContent(content);
myModStamp = LocalTimeCounter.currentTime();
}
@Override
public void setWritable(boolean b) {
myIsWritable = b;
}
@Override
public void rename(Object requestor, @NotNull String newName) throws IOException {
myName = newName;
}
public CharSequence getContent() {
return myContent;
}
@Override
public String toString() {
return "LightVirtualFile: " + getPresentableUrl();
}
}
|