Search in sources :

Example 16 with Document

use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.

the class DynamicDialog method doOKAction.

@Override
protected void doOKAction() {
    super.doOKAction();
    mySettings.setContainingClassName((String) myClassComboBox.getSelectedItem());
    mySettings.setStatic(myStaticCheckBox.isSelected());
    GrTypeElement typeElement = getEnteredTypeName();
    if (typeElement == null) {
        mySettings.setType(CommonClassNames.JAVA_LANG_OBJECT);
    } else {
        PsiType type = typeElement.getType();
        if (type instanceof PsiPrimitiveType) {
            type = TypesUtil.boxPrimitiveType(type, typeElement.getManager(), ProjectScope.getAllScope(myProject));
        }
        final String typeQualifiedName = type.getCanonicalText();
        if (typeQualifiedName != null) {
            mySettings.setType(typeQualifiedName);
        } else {
            mySettings.setType(type.getPresentableText());
        }
    }
    final Document document = PsiDocumentManager.getInstance(myProject).getDocument(myContext.getContainingFile());
    CommandProcessor.getInstance().executeCommand(myProject, () -> {
        UndoManager.getInstance(myProject).undoableActionPerformed(new GlobalUndoableAction(document) {

            @Override
            public void undo() throws UnexpectedUndoException {
                final DItemElement itemElement;
                if (mySettings.isMethod()) {
                    final List<ParamInfo> myPairList = mySettings.getParams();
                    final String[] argumentsTypes = QuickfixUtil.getArgumentsTypes(myPairList);
                    itemElement = myDynamicManager.findConcreteDynamicMethod(mySettings.getContainingClassName(), mySettings.getName(), argumentsTypes);
                } else {
                    itemElement = myDynamicManager.findConcreteDynamicProperty(mySettings.getContainingClassName(), mySettings.getName());
                }
                if (itemElement == null) {
                    Messages.showWarningDialog(myProject, GroovyInspectionBundle.message("Cannot.perform.undo.operation"), GroovyInspectionBundle.message("Undo.disable"));
                    return;
                }
                final DClassElement classElement = myDynamicManager.getClassElementByItem(itemElement);
                if (classElement == null) {
                    Messages.showWarningDialog(myProject, GroovyInspectionBundle.message("Cannot.perform.undo.operation"), GroovyInspectionBundle.message("Undo.disable"));
                    return;
                }
                removeElement(itemElement);
                if (classElement.getMethods().isEmpty() && classElement.getProperties().isEmpty()) {
                    myDynamicManager.removeClassElement(classElement);
                }
            }

            @Override
            public void redo() throws UnexpectedUndoException {
                addElement(mySettings);
            }
        });
        addElement(mySettings);
    }, "Add dynamic element", null);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) DClassElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement) UnexpectedUndoException(com.intellij.openapi.command.undo.UnexpectedUndoException) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) List(java.util.List) Document(com.intellij.openapi.editor.Document) DItemElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DItemElement)

Example 17 with Document

use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.

the class GrExecuteCommandAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = e.getProject();
    final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
    final VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
    if (project == null || editor == null || virtualFile == null)
        return;
    FileDocumentManager.getInstance().saveAllDocuments();
    final Document document = editor.getDocument();
    final TextRange selectedRange = EditorUtil.getSelectionInAnyMode(editor);
    final String command = (selectedRange.isEmpty() ? document.getText() : document.getText(selectedRange));
    final GroovyConsole existingConsole = virtualFile.getUserData(GroovyConsole.GROOVY_CONSOLE);
    if (existingConsole == null) {
        GroovyConsole.getOrCreateConsole(project, virtualFile, console -> console.execute(command));
    } else {
        existingConsole.execute(command);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) GroovyConsole(org.jetbrains.plugins.groovy.console.GroovyConsole) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document)

Example 18 with Document

use of com.intellij.openapi.editor.Document 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 19 with Document

use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.

the class YamlKeyCompletionInsertHandler method isCharAtCaret.

public static boolean isCharAtCaret(Editor editor, char ch) {
    final int startOffset = editor.getCaretModel().getOffset();
    final Document document = editor.getDocument();
    return document.getTextLength() > startOffset && document.getCharsSequence().charAt(startOffset) == ch;
}
Also used : Document(com.intellij.openapi.editor.Document) YAMLDocument(org.jetbrains.yaml.psi.YAMLDocument)

Example 20 with Document

use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.

the class XsltDebuggerEditorsProvider method createDocument.

@NotNull
@Override
public Document createDocument(@NotNull Project project, @NotNull String text, @Nullable XSourcePosition sourcePosition, @NotNull EvaluationMode mode) {
    final PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText("XPathExpr." + myFileType.getDefaultExtension(), myFileType, text, LocalTimeCounter.currentTime(), true);
    if (sourcePosition instanceof XsltSourcePosition && ((XsltSourcePosition) sourcePosition).getLocation() instanceof Debugger.StyleFrame) {
        final Debugger.Locatable location = ((XsltSourcePosition) sourcePosition).getLocation();
        final EvalContextProvider context = new EvalContextProvider(((Debugger.StyleFrame) location).getVariables());
        context.attachTo(psiFile);
    } else {
        final PsiElement contextElement = XsltBreakpointHandler.findContextElement(project, sourcePosition);
        if (contextElement != null) {
            final BreakpointContext context = new BreakpointContext(contextElement);
            context.attachTo(psiFile);
        }
    }
    final Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
    assert document != null;
    return document;
}
Also used : Debugger(org.intellij.plugins.xsltDebugger.rt.engine.Debugger) BreakpointContext(org.intellij.plugins.xsltDebugger.BreakpointContext) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Document (com.intellij.openapi.editor.Document)1072 VirtualFile (com.intellij.openapi.vfs.VirtualFile)243 PsiFile (com.intellij.psi.PsiFile)191 Project (com.intellij.openapi.project.Project)163 NotNull (org.jetbrains.annotations.NotNull)138 Nullable (org.jetbrains.annotations.Nullable)129 TextRange (com.intellij.openapi.util.TextRange)122 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)117 PsiElement (com.intellij.psi.PsiElement)107 Editor (com.intellij.openapi.editor.Editor)104 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)56 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)45 IncorrectOperationException (com.intellij.util.IncorrectOperationException)43 IOException (java.io.IOException)42 RangeMarker (com.intellij.openapi.editor.RangeMarker)35 MockVirtualFile (com.intellij.mock.MockVirtualFile)33 ArrayList (java.util.ArrayList)32 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)30 File (java.io.File)30 List (java.util.List)29