2 * Copyright 2000-2007 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
.openapi
.editor
.Document
;
19 import com
.intellij
.openapi
.project
.Project
;
20 import com
.intellij
.openapi
.util
.Computable
;
21 import org
.jetbrains
.annotations
.NotNull
;
22 import org
.jetbrains
.annotations
.Nullable
;
24 import java
.util
.EventListener
;
27 * Manages the relationship between documents and PSI trees.
29 public abstract class PsiDocumentManager
{
31 * Returns the document manager instance for the specified project.
33 * @param project the project for which the document manager is requested.
34 * @return the document manager instance.
36 public static PsiDocumentManager
getInstance(@NotNull Project project
) {
37 return project
.getComponent(PsiDocumentManager
.class);
41 * Returns the PSI file for the specified document.
43 * @param document the document for which the PSI file is requested.
44 * @return the PSI file instance.
47 public abstract PsiFile
getPsiFile(@NotNull Document document
);
50 * Returns the cached PSI file for the specified document.
52 * @param document the document for which the PSI file is requested.
53 * @return the PSI file instance, or null if there is currently no cached PSI tree for the file.
56 public abstract PsiFile
getCachedPsiFile(@NotNull Document document
);
59 * Returns the document for the specified PSI file.
61 * @param file the file for which the document is requested.
62 * @return the document instance, or null if the file is binary or has no associated document.
65 public abstract Document
getDocument(@NotNull PsiFile file
);
68 * Returns the cached document for the specified PSI file.
70 * @param file the file for which the document is requested.
71 * @return the document instance, or null if there is currently no cached document for the file.
74 public abstract Document
getCachedDocument(@NotNull PsiFile file
);
77 * Commits (updates the PSI tree for) all modified but not committed documents.
78 * Before a modified document is committed, accessing its PSI may return elements
79 * corresponding to original (unmodified) state of the document.
81 public abstract void commitAllDocuments();
84 * If the document is committed, runs action synchronously, otherwise schedules to execute it right after it has been committed.
88 public abstract void performForCommittedDocument(@NotNull Document document
, @NotNull Runnable action
);
91 * Updates the PSI tree for the specified document.
92 * Before a modified document is committed, accessing its PSI may return elements
93 * corresponding to original (unmodified) state of the document.
95 * @param document the document to commit.
97 public abstract void commitDocument(Document document
);
100 * Returns the list of documents which have been modified but not committed.
102 * @return the list of uncommitted documents.
103 * @see #commitDocument(com.intellij.openapi.editor.Document)
105 public abstract Document
[] getUncommittedDocuments();
108 * Checks if the specified document has been committed.
110 * @param document the document to check.
111 * @return true if the document was modified but not committed, false otherwise
112 * @see #commitDocument(com.intellij.openapi.editor.Document)
114 public abstract boolean isUncommited(Document document
);
117 * Checks if any modified documents have not been committed.
119 * @return true if there are uncommitted documents, false otherwise
121 public abstract boolean hasUncommitedDocuments();
124 * Commits the documents and runs the specified operation, which does not return a value, in a read action.
125 * Can be called from a thread other than the Swing dispatch thread.
127 * @param runnable the operation to execute.
129 public abstract void commitAndRunReadAction(@NotNull Runnable runnable
);
132 * Commits the documents and runs the specified operation, which returns a value, in a read action.
133 * Can be called from a thread other than the Swing dispatch thread.
135 * @param computation the operation to execute.
136 * @return the value returned by the operation.
138 public abstract <T
> T
commitAndRunReadAction(@NotNull Computable
<T
> computation
);
141 * Listener for receiving notifications about creation of {@link Document} and {@link PsiFile} instances.
143 public interface Listener
extends EventListener
{
145 * Called when a document instance is created for a file.
147 * @param document the created document instance.
148 * @param psiFile the file for which the document was created.
149 * @see PsiDocumentManager#getDocument(PsiFile)
151 void documentCreated(Document document
, PsiFile psiFile
);
154 * Called when a file instance is created for a document.
156 * @param file the created file instance.
157 * @param document the document for which the file was created.
158 * @see PsiDocumentManager#getDocument(PsiFile)
160 void fileCreated(PsiFile file
, Document document
);
164 * Adds a listener for receiving notifications about creation of {@link Document} and {@link PsiFile} instances.
166 * @param listener the listener to add.
168 public abstract void addListener(@NotNull Listener listener
);
171 * Removes a listener for receiving notifications about creation of {@link Document} and {@link PsiFile} instances.
173 * @param listener the listener to add.
175 public abstract void removeListener(@NotNull Listener listener
);
177 public abstract boolean isDocumentBlockedByPsi(@NotNull Document doc
);
179 public abstract void doPostponedOperationsAndUnblockDocument(@NotNull Document doc
);