Search in sources :

Example 6 with BlockSupport

use of com.intellij.psi.text.BlockSupport in project intellij-community by JetBrains.

the class ExpressionUtil method getNames.

@Nullable
public static String[] getNames(final ExpressionContext context) {
    final Project project = context.getProject();
    final int offset = context.getStartOffset();
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    String[] names = null;
    PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
    PsiElement element = file.findElementAt(offset);
    if (element instanceof PsiIdentifier) {
        names = getNamesForIdentifier(project, (PsiIdentifier) element);
    } else {
        final PsiFile fileCopy = (PsiFile) file.copy();
        ApplicationManager.getApplication().runWriteAction(() -> {
            BlockSupport blockSupport = BlockSupport.getInstance(project);
            try {
                blockSupport.reparseRange(fileCopy, offset, offset, "xxx");
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
        });
        PsiElement identifierCopy = fileCopy.findElementAt(offset);
        if (identifierCopy instanceof PsiIdentifier) {
            names = getNamesForIdentifier(project, (PsiIdentifier) identifierCopy);
        }
    }
    return names;
}
Also used : Project(com.intellij.openapi.project.Project) IncorrectOperationException(com.intellij.util.IncorrectOperationException) BlockSupport(com.intellij.psi.text.BlockSupport) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with BlockSupport

use of com.intellij.psi.text.BlockSupport in project intellij-community by JetBrains.

the class DocumentCommitThread method assertAfterCommit.

private void assertAfterCommit(@NotNull Document document, @NotNull final PsiFile file, @NotNull FileElement oldFileNode) {
    if (oldFileNode.getTextLength() != document.getTextLength()) {
        final String documentText = document.getText();
        String fileText = file.getText();
        boolean sameText = Comparing.equal(fileText, documentText);
        LOG.error("commitDocument() left PSI inconsistent: " + DebugUtil.diagnosePsiDocumentInconsistency(file, document) + "; node.length=" + oldFileNode.getTextLength() + "; doc.text" + (sameText ? "==" : "!=") + "file.text" + "; file name:" + file.getName() + "; type:" + file.getFileType() + "; lang:" + file.getLanguage());
        file.putUserData(BlockSupport.DO_NOT_REPARSE_INCREMENTALLY, Boolean.TRUE);
        try {
            BlockSupport blockSupport = BlockSupport.getInstance(file.getProject());
            final DiffLog diffLog = blockSupport.reparseRange(file, file.getNode(), new TextRange(0, documentText.length()), documentText, createProgressIndicator(), oldFileNode.getText());
            doActualPsiChange(file, diffLog);
            if (oldFileNode.getTextLength() != document.getTextLength()) {
                LOG.error("PSI is broken beyond repair in: " + file);
            }
        } finally {
            file.putUserData(BlockSupport.DO_NOT_REPARSE_INCREMENTALLY, null);
        }
    }
}
Also used : BlockSupport(com.intellij.psi.text.BlockSupport) DiffLog(com.intellij.psi.impl.source.text.DiffLog)

Aggregations

BlockSupport (com.intellij.psi.text.BlockSupport)7 TextRange (com.intellij.openapi.util.TextRange)3 Project (com.intellij.openapi.project.Project)2 DiffLog (com.intellij.psi.impl.source.text.DiffLog)2 ASTNode (com.intellij.lang.ASTNode)1 FileASTNode (com.intellij.lang.FileASTNode)1 Disposable (com.intellij.openapi.Disposable)1 com.intellij.openapi.application (com.intellij.openapi.application)1 ApplicationEx (com.intellij.openapi.application.ex.ApplicationEx)1 ServiceManager (com.intellij.openapi.components.ServiceManager)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Document (com.intellij.openapi.editor.Document)1 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 ProgressManager (com.intellij.openapi.progress.ProgressManager)1 StandardProgressIndicatorBase (com.intellij.openapi.progress.util.StandardProgressIndicatorBase)1 com.intellij.openapi.util (com.intellij.openapi.util)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1