Search in sources :

Example 16 with DocumentWindow

use of com.intellij.injected.editor.DocumentWindow in project intellij-community by JetBrains.

the class DocumentMarkupModel method forDocument.

/**
   * Returns the markup model for the specified project. A document can have multiple markup
   * models for different projects if the file to which it corresponds belongs to multiple projects
   * opened in different IDEA frames at the same time.
   *
   * @param document the document for which the markup model is requested.
   * @param project the project for which the markup model is requested, or null if the default markup
   *                model is requested.
   * @return the markup model instance.
   * @see com.intellij.openapi.editor.Editor#getMarkupModel()
   */
public static MarkupModel forDocument(@NotNull Document document, @Nullable Project project, boolean create) {
    if (document instanceof DocumentWindow) {
        final Document delegate = ((DocumentWindow) document).getDelegate();
        final MarkupModelEx baseMarkupModel = (MarkupModelEx) forDocument(delegate, project, true);
        return new MarkupModelWindow(baseMarkupModel, (DocumentWindow) document);
    }
    if (project == null) {
        MarkupModelEx markupModel = document.getUserData(MARKUP_MODEL_KEY);
        if (create && markupModel == null) {
            MarkupModelEx newModel = new MarkupModelImpl((DocumentEx) document);
            if ((markupModel = ((UserDataHolderEx) document).putUserDataIfAbsent(MARKUP_MODEL_KEY, newModel)) != newModel) {
                newModel.dispose();
            }
        }
        return markupModel;
    }
    final DocumentMarkupModelManager documentMarkupModelManager = project.isDisposed() ? null : DocumentMarkupModelManager.getInstance(project);
    if (documentMarkupModelManager == null || documentMarkupModelManager.isDisposed() || project.isDisposed()) {
        return new EmptyMarkupModel(document);
    }
    ConcurrentMap<Project, MarkupModelImpl> markupModelMap = getMarkupModelMap(document);
    MarkupModelImpl model = markupModelMap.get(project);
    if (create && model == null) {
        MarkupModelImpl newModel = new MarkupModelImpl((DocumentEx) document);
        if ((model = ConcurrencyUtil.cacheOrGet(markupModelMap, project, newModel)) == newModel) {
            documentMarkupModelManager.registerDocument(document);
        } else {
            newModel.dispose();
        }
    }
    return model;
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) Project(com.intellij.openapi.project.Project) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) MarkupModelWindow(com.intellij.injected.editor.MarkupModelWindow) Document(com.intellij.openapi.editor.Document)

Example 17 with DocumentWindow

use of com.intellij.injected.editor.DocumentWindow in project intellij-community by JetBrains.

the class PsiDocumentManagerBase method getLastCommittedDocument.

@NotNull
public DocumentEx getLastCommittedDocument(@NotNull Document document) {
    if (document instanceof FrozenDocument)
        return (DocumentEx) document;
    if (document instanceof DocumentWindow) {
        DocumentWindow window = (DocumentWindow) document;
        Document delegate = window.getDelegate();
        if (delegate instanceof FrozenDocument)
            return (DocumentEx) window;
        if (!window.isValid()) {
            throw new AssertionError("host committed: " + isCommitted(delegate) + ", window=" + window);
        }
        UncommittedInfo info = myUncommittedInfos.get(delegate);
        DocumentWindow answer = info == null ? null : info.myFrozenWindows.get(document);
        if (answer == null)
            answer = freezeWindow(window);
        if (info != null)
            answer = ConcurrencyUtil.cacheOrGet(info.myFrozenWindows, window, answer);
        return (DocumentEx) answer;
    }
    assert document instanceof DocumentImpl;
    UncommittedInfo info = myUncommittedInfos.get(document);
    return info != null ? info.myFrozen : ((DocumentImpl) document).freeze();
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) FrozenDocument(com.intellij.openapi.editor.impl.FrozenDocument) Document(com.intellij.openapi.editor.Document) FrozenDocument(com.intellij.openapi.editor.impl.FrozenDocument) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with DocumentWindow

use of com.intellij.injected.editor.DocumentWindow in project intellij-community by JetBrains.

the class PsiDocumentManagerBase method documentChanged.

@Override
public void documentChanged(DocumentEvent event) {
    if (myStopTrackingDocuments || myProject.isDisposed())
        return;
    final Document document = event.getDocument();
    VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
    boolean isRelevant = virtualFile != null && isRelevant(virtualFile);
    final FileViewProvider viewProvider = getCachedViewProvider(document);
    if (viewProvider == null) {
        handleCommitWithoutPsi(document);
        return;
    }
    boolean inMyProject = viewProvider.getManager() == myPsiManager;
    if (!isRelevant || !inMyProject) {
        clearUncommittedInfo(document);
        return;
    }
    List<PsiFile> files = viewProvider.getAllFiles();
    boolean commitNecessary = files.stream().noneMatch(file -> PsiToDocumentSynchronizer.isInsideAtomicChange(file) || !(file instanceof PsiFileImpl));
    boolean forceCommit = ApplicationManager.getApplication().hasWriteAction(ExternalChangeAction.class) && (SystemProperties.getBooleanProperty("idea.force.commit.on.external.change", false) || ApplicationManager.getApplication().isHeadlessEnvironment() && !ApplicationManager.getApplication().isUnitTestMode());
    // for the huge file (that causes the whole document to be reloaded and 'merge' way takes a while to complete).
    if (event.isWholeTextReplaced() && document.getTextLength() > 100000) {
        document.putUserData(BlockSupport.DO_NOT_REPARSE_INCREMENTALLY, Boolean.TRUE);
    }
    if (commitNecessary) {
        assert !(document instanceof DocumentWindow);
        myUncommittedDocuments.add(document);
        if (forceCommit) {
            commitDocument(document);
        } else if (!((DocumentEx) document).isInBulkUpdate() && myPerformBackgroundCommit) {
            myDocumentCommitProcessor.commitAsynchronously(myProject, document, event, TransactionGuard.getInstance().getContextTransaction());
        }
    } else {
        clearUncommittedInfo(document);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DocumentWindow(com.intellij.injected.editor.DocumentWindow) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) Document(com.intellij.openapi.editor.Document) FrozenDocument(com.intellij.openapi.editor.impl.FrozenDocument)

Example 19 with DocumentWindow

use of com.intellij.injected.editor.DocumentWindow in project intellij-community by JetBrains.

the class FileManagerImpl method getFromInjected.

@Nullable
private FileViewProvider getFromInjected(@NotNull VirtualFile file) {
    if (file instanceof VirtualFileWindow) {
        DocumentWindow document = ((VirtualFileWindow) file).getDocumentWindow();
        PsiFile psiFile = PsiDocumentManager.getInstance(myManager.getProject()).getCachedPsiFile(document);
        if (psiFile == null)
            return null;
        return psiFile.getViewProvider();
    }
    return null;
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with DocumentWindow

use of com.intellij.injected.editor.DocumentWindow in project intellij-community by JetBrains.

the class InjectedSelfElementInfo method hostToInjected.

@Nullable
private static ProperTextRange hostToInjected(boolean psi, Segment hostRange, @Nullable PsiFile injectedFile, @Nullable AffixOffsets affixOffsets) {
    VirtualFile virtualFile = injectedFile == null ? null : injectedFile.getVirtualFile();
    if (virtualFile instanceof VirtualFileWindow) {
        Project project = injectedFile.getProject();
        DocumentWindow documentWindow = ((VirtualFileWindow) virtualFile).getDocumentWindow();
        if (psi) {
            documentWindow = (DocumentWindow) ((PsiDocumentManagerBase) PsiDocumentManager.getInstance(project)).getLastCommittedDocument(documentWindow);
        }
        int start = documentWindow.hostToInjected(hostRange.getStartOffset());
        int end = documentWindow.hostToInjected(hostRange.getEndOffset());
        if (affixOffsets != null) {
            return affixOffsets.expandRangeToAffixes(start, end, InjectedLanguageManager.getInstance(project).getNonEditableFragments(documentWindow));
        }
        return ProperTextRange.create(start, end);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DocumentWindow(com.intellij.injected.editor.DocumentWindow) Project(com.intellij.openapi.project.Project) VirtualFileWindow(com.intellij.injected.editor.VirtualFileWindow) PsiDocumentManagerBase(com.intellij.psi.impl.PsiDocumentManagerBase) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

DocumentWindow (com.intellij.injected.editor.DocumentWindow)42 TextRange (com.intellij.openapi.util.TextRange)18 Document (com.intellij.openapi.editor.Document)17 NotNull (org.jetbrains.annotations.NotNull)11 Nullable (org.jetbrains.annotations.Nullable)9 Project (com.intellij.openapi.project.Project)8 VirtualFileWindow (com.intellij.injected.editor.VirtualFileWindow)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 PsiFile (com.intellij.psi.PsiFile)6 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)5 DocumentWindowImpl (com.intellij.injected.editor.DocumentWindowImpl)4 Language (com.intellij.lang.Language)4 Editor (com.intellij.openapi.editor.Editor)4 Segment (com.intellij.openapi.util.Segment)4 PsiFileImpl (com.intellij.psi.impl.source.PsiFileImpl)4 DaemonProgressIndicator (com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator)3 ASTNode (com.intellij.lang.ASTNode)3 com.intellij.psi (com.intellij.psi)3 PsiElement (com.intellij.psi.PsiElement)3 PsiDocumentManagerBase (com.intellij.psi.impl.PsiDocumentManagerBase)3