Search in sources :

Example 31 with Document

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

the class ConvertToJavaProcessor method performRefactoring.

//private static String
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    final GeneratorClassNameProvider classNameProvider = new GeneratorClassNameProvider();
    ExpressionContext context = new ExpressionContext(myProject, myFiles);
    final ClassGenerator classGenerator = new ClassGenerator(classNameProvider, new ClassItemGeneratorImpl(context));
    for (GroovyFile file : myFiles) {
        final PsiClass[] classes = file.getClasses();
        StringBuilder builder = new StringBuilder();
        boolean first = true;
        for (PsiClass aClass : classes) {
            classGenerator.writeTypeDefinition(builder, aClass, true, first);
            first = false;
            builder.append('\n');
        }
        final Document document = PsiDocumentManager.getInstance(myProject).getDocument(file);
        LOG.assertTrue(document != null);
        document.setText(builder.toString());
        PsiDocumentManager.getInstance(myProject).commitDocument(document);
        String fileName = getNewFileName(file);
        PsiElement newFile;
        try {
            newFile = file.setName(fileName);
        } catch (final IncorrectOperationException e) {
            ApplicationManager.getApplication().invokeLater(() -> Messages.showMessageDialog(myProject, e.getMessage(), RefactoringBundle.message("error.title"), Messages.getErrorIcon()));
            return;
        }
        doPostProcessing(newFile);
    }
}
Also used : Document(com.intellij.openapi.editor.Document) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 32 with Document

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

the class FormSourceCodeGenerator method generate.

public void generate(final VirtualFile formFile) {
    myNeedLoadLabelText = false;
    myNeedLoadButtonText = false;
    final Module module = ModuleUtil.findModuleForFile(formFile, myProject);
    if (module == null) {
        return;
    }
    // ensure that new instances of generators are used for every run
    ourContainerLayoutCodeGenerators.clear();
    ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_INTELLIJ, new GridLayoutSourceGenerator());
    ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_GRIDBAG, new GridBagLayoutSourceGenerator());
    ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_BORDER, new BorderLayoutSourceGenerator());
    ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_FLOW, new FlowLayoutSourceGenerator());
    ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_CARD, new CardLayoutSourceGenerator());
    ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_FORM, new FormLayoutSourceGenerator());
    myErrors.clear();
    final PsiPropertiesProvider propertiesProvider = new PsiPropertiesProvider(module);
    final Document doc = FileDocumentManager.getInstance().getDocument(formFile);
    final LwRootContainer rootContainer;
    try {
        rootContainer = Utils.getRootContainer(doc.getText(), propertiesProvider);
    } catch (AlienFormFileException ignored) {
        // ignoring this file
        return;
    } catch (Exception e) {
        myErrors.add(new FormErrorInfo(null, UIDesignerBundle.message("error.cannot.process.form.file", e)));
        return;
    }
    if (rootContainer.getClassToBind() == null) {
        // form skipped - no class to bind
        return;
    }
    ErrorAnalyzer.analyzeErrors(module, formFile, null, rootContainer, null);
    FormEditingUtil.iterate(rootContainer, new FormEditingUtil.ComponentVisitor<LwComponent>() {

        public boolean visit(final LwComponent iComponent) {
            final ErrorInfo errorInfo = ErrorAnalyzer.getErrorForComponent(iComponent);
            if (errorInfo != null) {
                String message;
                if (iComponent.getBinding() != null) {
                    message = UIDesignerBundle.message("error.for.component", iComponent.getBinding(), errorInfo.myDescription);
                } else {
                    message = errorInfo.myDescription;
                }
                myErrors.add(new FormErrorInfo(iComponent.getId(), message));
            }
            return true;
        }
    });
    if (myErrors.size() != 0) {
        return;
    }
    try {
        _generate(rootContainer, module);
    } catch (ClassToBindNotFoundException e) {
    // ignore
    } catch (CodeGenerationException e) {
        myErrors.add(new FormErrorInfo(e.getComponentId(), e.getMessage()));
    } catch (IncorrectOperationException e) {
        myErrors.add(new FormErrorInfo(null, e.getMessage()));
    }
}
Also used : Document(com.intellij.openapi.editor.Document) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module)

Example 33 with Document

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

the class UIFormEditor method getState.

@NotNull
public FileEditorState getState(@NotNull final FileEditorStateLevel ignored) {
    final Document document = FileDocumentManager.getInstance().getCachedDocument(myFile);
    long modificationStamp = document != null ? document.getModificationStamp() : myFile.getModificationStamp();
    final ArrayList<RadComponent> selection = FormEditingUtil.getSelectedComponents(myEditor);
    final String[] ids = new String[selection.size()];
    for (int i = ids.length - 1; i >= 0; i--) {
        ids[i] = selection.get(i).getId();
    }
    return new MyEditorState(modificationStamp, ids);
}
Also used : RadComponent(com.intellij.uiDesigner.radComponents.RadComponent) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull)

Example 34 with Document

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

Example 35 with Document

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

the class PerspectiveFileEditor method setSelectionInTextEditor.

public void setSelectionInTextEditor(final TextEditor textEditor, final DomElement element) {
    if (element != null && element.isValid()) {
        final XmlTag tag = element.getXmlTag();
        if (tag == null)
            return;
        final PsiFile file = tag.getContainingFile();
        if (file == null)
            return;
        final Document document = getDocumentManager().getDocument(file);
        if (document == null || !document.equals(textEditor.getEditor().getDocument()))
            return;
        textEditor.getEditor().getCaretModel().moveToOffset(tag.getTextOffset());
        textEditor.getEditor().getScrollingModel().scrollToCaret(ScrollType.CENTER);
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

Document (com.intellij.openapi.editor.Document)1075 VirtualFile (com.intellij.openapi.vfs.VirtualFile)246 PsiFile (com.intellij.psi.PsiFile)191 Project (com.intellij.openapi.project.Project)164 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 File (java.io.File)32 ArrayList (java.util.ArrayList)32 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)30 List (java.util.List)29