/
MPSPsiRootNode.java
286 lines (245 loc) · 8.38 KB
/
MPSPsiRootNode.java
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
/*
* Copyright 2003-2015 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 jetbrains.mps.idea.core.psi.impl;
import com.intellij.lang.FileASTNode;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiBinaryFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileSystemItem;
import com.intellij.psi.PsiManager;
import com.intellij.psi.SingleRootFileViewProvider;
import com.intellij.psi.search.PsiElementProcessor;
import com.intellij.util.IncorrectOperationException;
import jetbrains.mps.fileTypes.MPSFileTypeFactory;
import jetbrains.mps.ide.icons.IconManager;
import jetbrains.mps.ide.icons.IdeIcons;
import jetbrains.mps.ide.project.ProjectHelper;
import jetbrains.mps.idea.core.projectView.edit.SNodeDeleteProvider;
import jetbrains.mps.openapi.navigation.NavigationSupport;
import jetbrains.mps.smodel.MPSModuleRepository;
import jetbrains.mps.smodel.ModelAccess;
import jetbrains.mps.smodel.SNodePointer;
import jetbrains.mps.util.Computable;
import jetbrains.mps.workbench.nodesFs.MPSNodesVirtualFileSystem;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.mps.openapi.model.SModel;
import org.jetbrains.mps.openapi.model.SNode;
import org.jetbrains.mps.openapi.model.SNodeId;
import org.jetbrains.mps.openapi.model.SNodeReference;
import org.jetbrains.mps.openapi.module.SRepository;
import javax.swing.Icon;
import java.util.Collections;
/**
* User: fyodor
* Date: 3/5/13
*/
public class MPSPsiRootNode extends MPSPsiNodeBase implements PsiFile, PsiBinaryFile, MPSPsiRealNode {
private final FileViewProvider myViewProvider;
private final SNodeId myNodeId;
private String myName;
private MPSPsiModel myModel;
private VirtualFile mySeparateFile;
public MPSPsiRootNode(SNodeId nodeId, String name, MPSPsiModel containingModel, PsiManager manager) {
this(nodeId, name, containingModel, manager, null);
mySeparateFile = null;
}
public MPSPsiRootNode(SNodeId nodeId, String name, MPSPsiModel containingModel, PsiManager manager, @Nullable VirtualFile virtualFile) {
super(manager);
myNodeId = nodeId;
myModel = containingModel;
myName = name;
myViewProvider = new SingleRootFileViewProvider(manager, MPSNodesVirtualFileSystem.getInstance().getFileFor(getSNodeReference()), false);
// myViewProvider = new SingleRootFileViewProvider(manager, virtualFile == null ? new LightVirtualFile() : virtualFile, false);
mySeparateFile = virtualFile;
}
@Override
protected Icon getBaseIcon() {
return ModelAccess.instance().runReadAction(new Computable<Icon>() {
@Override
public Icon compute() {
final SNode node = getSNodeReference().resolve(MPSModuleRepository.getInstance());
if (node == null) return IdeIcons.UNKNOWN_ICON;
return IconManager.getIconFor(node, true);
}
});
}
@Nullable
@Override
protected Icon getElementIcon(@IconFlags int flags) {
return getBaseIcon();
}
@Override
public boolean isValid() {
if (!myModel.isValid() || mySeparateFile != null && !mySeparateFile.isValid()) return false;
final SRepository repository = getProjectRepository();
final Ref<Boolean> result = new Ref<Boolean>(false);
final SNodeReference nodeRef = getSNodeReference();
if (nodeRef == null) return false;
repository.getModelAccess().runReadAction(new Runnable() {
@Override
public void run() {
SNode node = nodeRef.resolve(repository);
result.set(node != null);
}
});
return result.get();
}
@Nullable
@Override
public VirtualFile getVirtualFile() {
return MPSNodesVirtualFileSystem.getInstance().getFileFor(getSNodeReference());
}
@Override
public boolean processChildren(PsiElementProcessor<PsiFileSystemItem> processor) {
return false;
}
@Override
public MPSPsiModel getContainingModel() {
return myModel != null ? myModel : super.getContainingModel();
}
@Nullable
@Override
public PsiDirectory getContainingDirectory() {
return getContainingModel();
}
@Override
public boolean isDirectory() {
return false;
}
@Nullable
@Override
public PsiDirectory getParent() {
if (myViewProvider.getVirtualFile().getFileType() == MPSFileTypeFactory.MPS_ROOT_FILE_TYPE && super.getParent() instanceof MPSPsiModel) {
return ((MPSPsiModel) super.getParent()).getParentDirectory();
}
return (PsiDirectory) super.getParent();
}
@Override
public String toString() {
return "Root: " + myNodeId.toString();
}
@Override
public long getModificationStamp() {
return getVirtualFile().getModificationStamp();
}
@NotNull
@Override
public PsiFile getContainingFile() {
// PsiFileImpl does the same
return this;
}
@NotNull
@Override
public PsiFile getOriginalFile() {
return this;
}
@NotNull
@Override
public FileType getFileType() {
return MPSFileTypeFactory.MPS_NODE_FILE_TYPE;
}
@Override
public String getName() {
return myName;
}
@NotNull
@Override
public PsiFile[] getPsiRoots() {
return new PsiFile[]{this};
}
@NotNull
@Override
public FileViewProvider getViewProvider() {
return myViewProvider;
}
@Override
public FileASTNode getNode() {
return null;
}
@Override
public void navigate(final boolean requestFocus) {
final SRepository repository = getProjectRepository();
repository.getModelAccess().runWriteInEDT(new Runnable() {
@Override
public void run() {
SModel model = myModel.getSModelReference().resolve(repository);
if (model == null) return;
SNode node = model.getNode(myNodeId);
if (node == null) return;
NavigationSupport.getInstance().openNode(ProjectHelper.toMPSProject(getProject()), node, requestFocus, false);
}
});
}
@Override
public boolean isPhysical() {
return true;
}
@Override
public void subtreeChanged() {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void checkSetName(String name) throws IncorrectOperationException {
throw new IncorrectOperationException("Not implemented");
}
@Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
throw new IncorrectOperationException("Not implemented");
}
public SNodeId getNodeId() {
return myNodeId;
}
@Override
public SNodeReference getSNodeReference() {
MPSPsiModel psiModel = getContainingModel();
if (psiModel == null) return null;
return new SNodePointer(psiModel.getSModelReference(), myNodeId);
}
// added for idea search scope to work with our virtual files
// see PsiSearchScopeUtil.isInScope
// The main thing is: return some physical real file that lives well with idea's search scopes
@Override
public PsiElement getContext() {
VirtualFile vFile = mySeparateFile != null ?
mySeparateFile :
myModel.getSourceVirtualFile();
return vFile.isValid() ? PsiManager.getInstance(getProject()).findFile(vFile) : null;
}
@Override
public boolean isWritable() {
return true;
}
@Override
public String getText() {
// implemented to avoid assertion error in PsiDocumentManagerImpl.getDocument(PsiFile)
// document.getTextLength() != file.getTextLength() fails
return myViewProvider.getPsi(getLanguage()).getText();
}
@Override
public void delete() throws IncorrectOperationException {
SNodeDeleteProvider deleteProvider = new SNodeDeleteProvider(
Collections.singletonList(getSNodeReference()),
ProjectHelper.toMPSProject(getProject()));
getProjectRepository().getModelAccess().executeUndoTransparentCommand(deleteProvider);
}
}