2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.psi
;
18 import com
.intellij
.lang
.Language
;
19 import com
.intellij
.openapi
.editor
.Document
;
20 import com
.intellij
.openapi
.fileEditor
.FileDocumentManager
;
21 import com
.intellij
.openapi
.util
.UserDataHolderBase
;
22 import com
.intellij
.openapi
.vfs
.VirtualFile
;
23 import com
.intellij
.psi
.impl
.PsiManagerEx
;
24 import com
.intellij
.psi
.impl
.SharedPsiElementImplUtil
;
25 import com
.intellij
.psi
.impl
.source
.DummyHolder
;
26 import com
.intellij
.psi
.impl
.source
.PsiFileImpl
;
27 import com
.intellij
.psi
.impl
.source
.tree
.LeafElement
;
28 import com
.intellij
.testFramework
.LightVirtualFile
;
29 import com
.intellij
.util
.LocalTimeCounter
;
30 import org
.jetbrains
.annotations
.NotNull
;
31 import org
.jetbrains
.annotations
.Nullable
;
33 import java
.util
.Collections
;
34 import java
.util
.List
;
37 public class DummyHolderViewProvider
extends UserDataHolderBase
implements FileViewProvider
{
38 private DummyHolder myHolder
;
39 private final PsiManager myManager
;
40 private final long myModificationStamp
;
41 private final LightVirtualFile myLightVirtualFile
= new LightVirtualFile("DummyHolder");
43 public DummyHolderViewProvider(final PsiManager manager
) {
45 myModificationStamp
= LocalTimeCounter
.currentTime();
49 public PsiManager
getManager() {
54 public Document
getDocument() {
55 return FileDocumentManager
.getInstance().getDocument(getVirtualFile());
59 public CharSequence
getContents() {
60 return myHolder
!= null ? myHolder
.getNode().getText() : "";
64 public VirtualFile
getVirtualFile() {
65 return myLightVirtualFile
;
69 public Language
getBaseLanguage() {
70 return myHolder
.getLanguage();
74 public Set
<Language
> getLanguages() {
75 return Collections
.singleton(getBaseLanguage());
78 public PsiFile
getPsi(@NotNull Language target
) {
79 ((PsiManagerEx
)myManager
).getFileManager().setViewProvider(getVirtualFile(), this);
80 return target
== getBaseLanguage() ? myHolder
: null;
84 public List
<PsiFile
> getAllFiles() {
85 return Collections
.singletonList(getPsi(getBaseLanguage()));
88 public void beforeContentsSynchronized() {}
90 public void contentsSynchronized() {}
92 public boolean isEventSystemEnabled() {
96 public boolean isPhysical() {
100 public long getModificationStamp() {
101 return myModificationStamp
;
104 public boolean supportsIncrementalReparse(final Language rootLanguage
) {
108 public void rootChanged(PsiFile psiFile
) {
111 public void setDummyHolder(final DummyHolder dummyHolder
) {
112 myHolder
= dummyHolder
;
113 //myLightVirtualFile.setContent(null, getContents(), false);
116 public FileViewProvider
clone(){
117 throw new RuntimeException("Clone is not supported for DummyHolderProviders. Use DummyHolder clone directly.");
120 public PsiReference
findReferenceAt(final int offset
) {
121 return SharedPsiElementImplUtil
.findReferenceAt(getPsi(getBaseLanguage()), offset
);
125 public PsiElement
findElementAt(final int offset
, final Language language
) {
126 return language
== getBaseLanguage() ?
findElementAt(offset
) : null;
130 public PsiElement
findElementAt(int offset
, Class
<?
extends Language
> lang
) {
131 if (!lang
.isAssignableFrom(getBaseLanguage().getClass())) return null;
132 return findElementAt(offset
);
135 public PsiReference
findReferenceAt(final int offsetInElement
, @NotNull final Language language
) {
136 return language
== getBaseLanguage() ?
findReferenceAt(offsetInElement
) : null;
139 public boolean isLockedByPsiOperations() {
144 public FileViewProvider
createCopy(final LightVirtualFile copy
) {
145 throw new RuntimeException("Clone is not supported for DummyHolderProviders. Use DummyHolder clone directly.");
148 public PsiElement
findElementAt(final int offset
) {
149 final LeafElement element
= ((PsiFileImpl
)getPsi(getBaseLanguage())).calcTreeElement().findLeafElementAt(offset
);
150 return element
!= null ? element
.getPsi() : null;