Search in sources :

Example 61 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project Perl5-IDEA by Camelcade.

the class PerlCodeGeneratorImpl method insertCodeAfterElement.

protected void insertCodeAfterElement(PsiElement anchor, String code, Editor editor) {
    ApplicationManager.getApplication().runWriteAction(() -> {
        FileType fileType = anchor.getContainingFile().getFileType();
        final PsiDocumentManager manager = PsiDocumentManager.getInstance(anchor.getProject());
        final Document document = manager.getDocument(anchor.getContainingFile());
        if (code.length() > 0 && document != null) {
            manager.doPostponedOperationsAndUnblockDocument(document);
            PsiFile newFile = PerlElementFactory.createFile(anchor.getProject(), "\n" + code, fileType);
            PsiElement container = anchor.getParent();
            int newOffset = anchor.getTextOffset() + anchor.getTextLength();
            if (newFile.getFirstChild() != null && newFile.getLastChild() != null) {
                container.addRangeAfter(newFile.getFirstChild(), newFile.getLastChild(), anchor);
            }
            manager.commitDocument(document);
            editor.getCaretModel().moveToOffset(newOffset);
            editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
        }
    });
}
Also used : FileType(com.intellij.openapi.fileTypes.FileType) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 62 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project Perl5-IDEA by Camelcade.

the class StringToLastHeredocIntention method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    PsiElement stringElement = element.getParent();
    assert stringElement instanceof PerlString;
    char quoteSymbol = '"';
    if (stringElement instanceof PsiPerlStringSq) {
        quoteSymbol = '\'';
    } else if (stringElement instanceof PsiPerlStringXq) {
        quoteSymbol = '`';
    }
    String contentText = ElementManipulators.getValueText(stringElement);
    List<PsiElement> heredocElements = PerlElementFactory.createHereDocElements(project, quoteSymbol, HEREDOC_MARKER, "");
    PsiFile currentFile = stringElement.getContainingFile();
    int newLineIndex = StringUtil.indexOf(currentFile.getNode().getChars(), "\n", stringElement.getTextRange().getEndOffset());
    if (newLineIndex > 1) {
        PsiElement anchor = currentFile.findElementAt(newLineIndex);
        // fixme we should check here if \n in unbreakable entity - regex, qw, string, something else...
        if (anchor != null && (anchor.getParent() instanceof PerlString || anchor.getParent() instanceof PsiPerlStringList)) {
            return;
        }
    }
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(element.getProject());
    final Document document = manager.getDocument(currentFile);
    if (document == null) {
        return;
    }
    newLineIndex -= stringElement.getTextLength();
    PsiElement heredocOpener = heredocElements.get(0);
    newLineIndex += heredocOpener.getTextLength();
    // replace string with heredoc opener
    stringElement.replace(heredocOpener);
    manager.doPostponedOperationsAndUnblockDocument(document);
    String heredocString = contentText + "\n" + HEREDOC_MARKER + "\n";
    manager.doPostponedOperationsAndUnblockDocument(document);
    document.insertString(newLineIndex + 1, heredocString);
    manager.commitDocument(document);
}
Also used : PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 63 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project Perl5-IDEA by Camelcade.

the class TemplateToolkitStringManipulator method handleContentChange.

@SuppressWarnings("Duplicates")
@Override
public TemplateToolkitString handleContentChange(@NotNull TemplateToolkitString element, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
    final PsiDocumentManager manager = PsiDocumentManager.getInstance(element.getProject());
    final Document document = manager.getDocument(element.getContainingFile());
    if (document != null) {
        TextRange elementRange = element.getTextRange();
        manager.doPostponedOperationsAndUnblockDocument(document);
        document.replaceString(elementRange.getStartOffset() + range.getStartOffset(), elementRange.getStartOffset() + range.getEndOffset(), newContent);
        manager.commitDocument(document);
    }
    return element;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 64 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project idea-handlebars by dmarcotte.

the class HbFormatterTest method doTextTest.

/**
 * This method runs both a full-file reformat on beforeText, and a line-by-line reformat.  Though the tests
 * would output slightly better errors if these were separate tests, enforcing that they are always both run
 * for any test defined is the easiest way to ensure that the line-by-line is not messed up by formatter changes
 *
 * @param beforeText               The text run the formatter on
 * @param textAfter                The expected result after running the formatter
 * @param templateDataLanguageType The templated language of the file
 * @throws IncorrectOperationException
 */
void doTextTest(final String beforeText, String textAfter, LanguageFileType templateDataLanguageType) throws IncorrectOperationException {
    // define action to run "Reformat Code" on the whole "file" defined by beforeText
    FormatRunnableFactory fullFormatRunnableFactory = new FormatRunnableFactory() {

        @Override
        Runnable createFormatRunnable(final PsiFile file) {
            return new Runnable() {

                @Override
                public void run() {
                    try {
                        TextRange rangeToUse = file.getTextRange();
                        CodeStyleManager styleManager = CodeStyleManager.getInstance(getProject());
                        styleManager.reformatText(file, rangeToUse.getStartOffset(), rangeToUse.getEndOffset());
                    } catch (IncorrectOperationException e) {
                        assertTrue(e.getLocalizedMessage(), false);
                    }
                }
            };
        }
    };
    // define action to run "Adjust line indent" on every line in the "file" defined by beforeText
    FormatRunnableFactory lineFormatRunnableFactory = new FormatRunnableFactory() {

        @Override
        Runnable createFormatRunnable(final PsiFile file) {
            return new Runnable() {

                @Override
                public void run() {
                    try {
                        final PsiDocumentManager manager = PsiDocumentManager.getInstance(getProject());
                        final Document document = manager.getDocument(file);
                        assert document != null;
                        for (int lineNum = 0; lineNum < document.getLineCount(); lineNum++) {
                            CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
                            int offset = document.getLineStartOffset(lineNum);
                            // if this breaks at some point, we should
                            @SuppressWarnings("deprecation") boolean // instead of doing the indent directly
                            lineToBeIndented = codeStyleManager.isLineToBeIndented(file, offset);
                            if (lineToBeIndented) {
                                codeStyleManager.adjustLineIndent(file, offset);
                            }
                        }
                    } catch (IncorrectOperationException e) {
                        assertTrue(e.getLocalizedMessage(), false);
                    }
                }
            };
        }
    };
    doFormatterActionTest(fullFormatRunnableFactory, beforeText, textAfter, templateDataLanguageType);
    doFormatterActionTest(lineFormatRunnableFactory, beforeText, textAfter, templateDataLanguageType);
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Document(com.intellij.openapi.editor.Document) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 65 with PsiDocumentManager

use of com.intellij.psi.PsiDocumentManager in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoRecursiveCallMarkerProvider method collectSlowLineMarkers.

@Override
public void collectSlowLineMarkers(@NotNull List<PsiElement> elements, @NotNull Collection<LineMarkerInfo> result) {
    Set<Integer> lines = ContainerUtil.newHashSet();
    for (PsiElement element : elements) {
        if (element instanceof GoCallExpr) {
            PsiElement resolve = GoPsiImplUtil.resolveCall((GoCallExpr) element);
            if (resolve instanceof GoFunctionOrMethodDeclaration) {
                if (isRecursiveCall(element, (GoFunctionOrMethodDeclaration) resolve)) {
                    PsiDocumentManager instance = PsiDocumentManager.getInstance(element.getProject());
                    Document document = instance.getDocument(element.getContainingFile());
                    int textOffset = element.getTextOffset();
                    if (document == null)
                        continue;
                    int lineNumber = document.getLineNumber(textOffset);
                    if (!lines.contains(lineNumber)) {
                        result.add(new RecursiveMethodCallMarkerInfo(element));
                    }
                    lines.add(lineNumber);
                }
            }
        }
    }
}
Also used : GoFunctionOrMethodDeclaration(com.goide.psi.GoFunctionOrMethodDeclaration) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) GoCallExpr(com.goide.psi.GoCallExpr) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

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