Search in sources :

Example 1 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project buck by facebook.

the class DependenciesOptimizer method optimzeDeps.

public static void optimzeDeps(@NotNull PsiFile file) {
    final PropertyVisitor visitor = new PropertyVisitor();
    file.accept(new BuckVisitor() {

        @Override
        public void visitElement(PsiElement node) {
            node.acceptChildren(this);
            node.accept(visitor);
        }
    });
    // Commit modifications.
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject());
    manager.doPostponedOperationsAndUnblockDocument(manager.getDocument(file));
}
Also used : BuckVisitor(com.facebook.buck.intellij.ideabuck.lang.psi.BuckVisitor) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 2 with PsiDocumentManager

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

the class XsltBreakpointHandler method getActualLineNumber.

public static int getActualLineNumber(Project project, @Nullable XSourcePosition position) {
    if (position == null) {
        return -1;
    }
    final PsiElement element = findContextElement(project, position);
    if (element == null) {
        return -1;
    }
    if (element instanceof XmlToken) {
        final IElementType tokenType = ((XmlToken) element).getTokenType();
        if (tokenType == XmlTokenType.XML_START_TAG_START || tokenType == XmlTokenType.XML_NAME) {
            final PsiManager psiManager = PsiManager.getInstance(project);
            final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
            final PsiFile psiFile = psiManager.findFile(position.getFile());
            if (psiFile == null) {
                return -1;
            }
            final Document document = documentManager.getDocument(psiFile);
            if (document == null) {
                return -1;
            }
            if (document.getLineNumber(element.getTextRange().getStartOffset()) == position.getLine()) {
                final XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, false);
                if (tag != null) {
                    final ASTNode node = tag.getNode();
                    assert node != null;
                    // TODO: re-check if/when Xalan is supported
                    final ASTNode end = XmlChildRole.START_TAG_END_FINDER.findChild(node);
                    if (end != null) {
                        return document.getLineNumber(end.getTextRange().getEndOffset()) + 1;
                    } else {
                        final ASTNode end2 = XmlChildRole.EMPTY_TAG_END_FINDER.findChild(node);
                        if (end2 != null) {
                            return document.getLineNumber(end2.getTextRange().getEndOffset()) + 1;
                        }
                    }
                }
            }
        }
    }
    return -1;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 3 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project kotlin by JetBrains.

the class AbstractFormatterTest method doTextTest.

public void doTextTest(final Action action, final String text, File fileAfter, String extension) throws IncorrectOperationException {
    final PsiFile file = createFile("A" + extension, text);
    if (myLineRange != null) {
        DocumentImpl document = new DocumentImpl(text);
        myTextRange = new TextRange(document.getLineStartOffset(myLineRange.getStartOffset()), document.getLineEndOffset(myLineRange.getEndOffset()));
    }
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
    final Document document = manager.getDocument(file);
    CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {

        @Override
        public void run() {
            ApplicationManager.getApplication().runWriteAction(new Runnable() {

                @Override
                public void run() {
                    document.replaceString(0, document.getTextLength(), text);
                    manager.commitDocument(document);
                    try {
                        TextRange rangeToUse = myTextRange;
                        if (rangeToUse == null) {
                            rangeToUse = file.getTextRange();
                        }
                        ACTIONS.get(action).run(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
                    } catch (IncorrectOperationException e) {
                        assertTrue(e.getLocalizedMessage(), false);
                    }
                }
            });
        }
    }, "", "");
    if (document == null) {
        fail("Don't expect the document to be null");
        return;
    }
    KotlinTestUtils.assertEqualsToFile(fileAfter, document.getText());
    manager.commitDocument(document);
    KotlinTestUtils.assertEqualsToFile(fileAfter, file.getText());
}
Also used : PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Document(com.intellij.openapi.editor.Document) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 4 with PsiDocumentManager

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

the class DomUIFactoryImpl method createDomHighlighter.

@Override
public BackgroundEditorHighlighter createDomHighlighter(final Project project, final PerspectiveFileEditor editor, final DomElement element) {
    return new BackgroundEditorHighlighter() {

        @Override
        @NotNull
        public HighlightingPass[] createPassesForEditor() {
            if (!element.isValid())
                return HighlightingPass.EMPTY_ARRAY;
            final XmlFile psiFile = DomUtil.getFile(element);
            final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
            final Document document = psiDocumentManager.getDocument(psiFile);
            if (document == null)
                return HighlightingPass.EMPTY_ARRAY;
            editor.commit();
            GeneralHighlightingPass ghp = new GeneralHighlightingPass(project, psiFile, document, 0, document.getTextLength(), true, new ProperTextRange(0, document.getTextLength()), null, new DefaultHighlightInfoProcessor());
            LocalInspectionsPass lip = new LocalInspectionsPass(psiFile, document, 0, document.getTextLength(), LocalInspectionsPass.EMPTY_PRIORITY_RANGE, true, new DefaultHighlightInfoProcessor());
            return new HighlightingPass[] { ghp, lip };
        }

        @Override
        @NotNull
        public HighlightingPass[] createPassesForVisibleArea() {
            return createPassesForEditor();
        }
    };
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) LocalInspectionsPass(com.intellij.codeInsight.daemon.impl.LocalInspectionsPass) ProperTextRange(com.intellij.openapi.util.ProperTextRange) BackgroundEditorHighlighter(com.intellij.codeHighlighting.BackgroundEditorHighlighter) Document(com.intellij.openapi.editor.Document) DefaultHighlightInfoProcessor(com.intellij.codeInsight.daemon.impl.DefaultHighlightInfoProcessor) HighlightingPass(com.intellij.codeHighlighting.HighlightingPass) GeneralHighlightingPass(com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) GeneralHighlightingPass(com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass)

Example 5 with PsiDocumentManager

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

the class DefaultGenerateElementProvider method generate.

@SuppressWarnings({ "unchecked" })
@Nullable
public T generate(@Nullable final DomElement parent, final Editor editor) {
    if (parent == null) {
        return null;
    }
    final List<? extends DomCollectionChildDescription> list = parent.getGenericInfo().getCollectionChildrenDescriptions();
    for (DomCollectionChildDescription childDescription : list) {
        if (ReflectionUtil.getRawType(childDescription.getType()).isAssignableFrom(myChildElementClass)) {
            XmlTag parentTag = parent.getXmlTag();
            if (editor != null && parentTag != null) {
                int offset = editor.getCaretModel().getOffset();
                PsiFile file = parentTag.getContainingFile();
                PsiElement psiElement = file.findElementAt(offset);
                if (psiElement instanceof PsiWhiteSpace && PsiTreeUtil.isAncestor(parentTag, psiElement, false)) {
                    Document document = editor.getDocument();
                    XmlTag childTag = parentTag.createChildTag(childDescription.getXmlElementName(), null, null, true);
                    String text = childTag.getText();
                    document.insertString(offset, text);
                    Project project = editor.getProject();
                    assert project != null;
                    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
                    documentManager.commitDocument(document);
                    PsiElement element = file.findElementAt(offset + 1);
                    T domElement = DomUtil.findDomElement(element, myChildElementClass);
                    if (domElement != null)
                        return domElement;
                    document.deleteString(offset, offset + text.length());
                    documentManager.commitDocument(document);
                }
            }
            int index = getCollectionIndex(parent, childDescription, editor);
            return index < 0 ? (T) childDescription.addValue(parent, myChildElementClass) : (T) childDescription.addValue(parent, myChildElementClass, index);
        }
    }
    return null;
}
Also used : Project(com.intellij.openapi.project.Project) DomCollectionChildDescription(com.intellij.util.xml.reflect.DomCollectionChildDescription) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiDocumentManager (com.intellij.psi.PsiDocumentManager)140 Document (com.intellij.openapi.editor.Document)111 PsiFile (com.intellij.psi.PsiFile)100 VirtualFile (com.intellij.openapi.vfs.VirtualFile)51 ResourceItem (com.android.ide.common.res2.ResourceItem)26 PsiElement (com.intellij.psi.PsiElement)24 Project (com.intellij.openapi.project.Project)22 TextRange (com.intellij.openapi.util.TextRange)13 NotNull (org.jetbrains.annotations.NotNull)12 Nullable (org.jetbrains.annotations.Nullable)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 Editor (com.intellij.openapi.editor.Editor)7 XmlFile (com.intellij.psi.xml.XmlFile)6 FileType (com.intellij.openapi.fileTypes.FileType)5 XmlTag (com.intellij.psi.xml.XmlTag)5 ASTNode (com.intellij.lang.ASTNode)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)2 EditorEx (com.intellij.openapi.editor.ex.EditorEx)2