Search in sources :

Example 71 with TextEditor

use of com.intellij.openapi.fileEditor.TextEditor in project intellij-community by JetBrains.

the class UnresolvedRefCreateFunctionQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PyCallExpression callExpr = myCallExpr.getElement();
    final PyReferenceExpression referenceExpr = myReferenceExpr.getElement();
    if (callExpr == null || !callExpr.isValid() || referenceExpr == null || !referenceExpr.isValid()) {
        return;
    }
    final PyFunctionBuilder functionBuilder = new PyFunctionBuilder(referenceExpr.getText(), callExpr);
    // if function is actually an argument of a call, don't use other arguments of the call to create parameter list of new function
    final PyArgumentList argumentList = callExpr.getArgumentList();
    if (argumentList != null && !PsiTreeUtil.isAncestor(argumentList, referenceExpr, false)) {
        for (PyExpression param : argumentList.getArguments()) {
            if (param instanceof PyKeywordArgument) {
                functionBuilder.parameter(((PyKeywordArgument) param).getKeyword());
            } else if (param instanceof PyReferenceExpression) {
                PyReferenceExpression refex = (PyReferenceExpression) param;
                functionBuilder.parameter(refex.getReferencedName());
            } else {
                functionBuilder.parameter("param");
            }
        }
    } else {
        functionBuilder.parameter("args");
    }
    PyFunction function = functionBuilder.buildFunction(project, LanguageLevel.getDefault());
    final InjectedLanguageManager instance = InjectedLanguageManager.getInstance(project);
    final PsiLanguageInjectionHost host = instance.getInjectionHost(callExpr);
    final PsiElement insertAnchor = host != null ? host : callExpr;
    final PyFunction parentFunction = PsiTreeUtil.getTopmostParentOfType(insertAnchor, PyFunction.class);
    if (parentFunction != null) {
        final PyClass parentClass = PsiTreeUtil.getTopmostParentOfType(parentFunction, PyClass.class);
        if (parentClass != null) {
            final PsiElement parent = parentClass.getParent();
            function = (PyFunction) parent.addBefore(function, parentClass);
        } else {
            final PsiElement parent = parentFunction.getParent();
            function = (PyFunction) parent.addBefore(function, parentFunction);
        }
    } else {
        final PyStatement statement = PsiTreeUtil.getTopmostParentOfType(insertAnchor, PyStatement.class);
        if (statement != null) {
            final PsiElement parent = statement.getParent();
            if (parent != null) {
                function = (PyFunction) parent.addBefore(function, statement);
            }
        }
    }
    function = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(function);
    final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(function);
    ParamHelper.walkDownParamArray(function.getParameterList().getParameters(), new ParamHelper.ParamVisitor() {

        public void visitNamedParameter(PyNamedParameter param, boolean first, boolean last) {
            builder.replaceElement(param, param.getName());
        }
    });
    builder.replaceElement(function.getStatementList(), PyNames.PASS);
    final FileEditor editor = FileEditorManager.getInstance(project).getSelectedEditor(insertAnchor.getContainingFile().getVirtualFile());
    if (!(editor instanceof TextEditor)) {
        return;
    }
    builder.run(((TextEditor) editor).getEditor(), false);
}
Also used : FileEditor(com.intellij.openapi.fileEditor.FileEditor) ParamHelper(com.jetbrains.python.psi.impl.ParamHelper) TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) TextEditor(com.intellij.openapi.fileEditor.TextEditor) PyFunctionBuilder(com.jetbrains.python.psi.impl.PyFunctionBuilder) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiElement(com.intellij.psi.PsiElement)

Example 72 with TextEditor

use of com.intellij.openapi.fileEditor.TextEditor in project intellij-community by JetBrains.

the class DomStructureViewBuilder method createStructureView.

@Override
@NotNull
public StructureView createStructureView(final FileEditor fileEditor, @NotNull final Project project) {
    return new StructureViewComponent(fileEditor, createStructureViewModel(fileEditor instanceof TextEditor ? ((TextEditor) fileEditor).getEditor() : null), project, true) {

        @Override
        public AsyncResult<AbstractTreeNode> expandPathToElement(final Object element) {
            if (element instanceof XmlElement && ((XmlElement) element).isValid()) {
                final XmlElement xmlElement = (XmlElement) element;
                XmlTag tag = PsiTreeUtil.getParentOfType(xmlElement, XmlTag.class, false);
                while (tag != null) {
                    final DomElement domElement = DomManager.getDomManager(xmlElement.getProject()).getDomElement(tag);
                    if (domElement != null) {
                        for (DomElement curElement = domElement; curElement != null; curElement = curElement.getParent()) {
                            if (myDescriptor.fun(curElement) == DomService.StructureViewMode.SHOW) {
                                return super.expandPathToElement(curElement.getXmlElement());
                            }
                        }
                    }
                    tag = PsiTreeUtil.getParentOfType(tag, XmlTag.class, true);
                }
            }
            return super.expandPathToElement(element);
        }
    };
}
Also used : TextEditor(com.intellij.openapi.fileEditor.TextEditor) DomElement(com.intellij.util.xml.DomElement) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) XmlElement(com.intellij.psi.xml.XmlElement) StructureViewComponent(com.intellij.ide.structureView.newStructureView.StructureViewComponent) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 73 with TextEditor

use of com.intellij.openapi.fileEditor.TextEditor in project intellij-plugins by JetBrains.

the class ActionScriptStubsTest method doTest.

private void doTest(@Nullable final ThrowableRunnable<Exception> runnable, String... files) throws Exception {
    Runnable r = runnable != null ? () -> {
        try {
            runnable.run();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } : null;
    doTestFor(true, r, files);
    // the first one was parsed during highlighting
    assertNotParsed(myPsiFiles.subList(1, myPsiFiles.size()));
    // we need to go though files open in editors
    assertNotParsed(ContainerUtil.mapNotNull(FileEditorManager.getInstance(myProject).getOpenFiles(), virtualFile -> {
        if (Comparing.equal(virtualFile, myFile.getVirtualFile())) {
            return null;
        }
        Document document = ((TextEditor) FileEditorManager.getInstance(myProject).getSelectedEditor(virtualFile)).getEditor().getDocument();
        return PsiDocumentManager.getInstance(myProject).getPsiFile(document);
    }));
}
Also used : VfsRootAccess(com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) VfsUtilCore.urlToPath(com.intellij.openapi.vfs.VfsUtilCore.urlToPath) JSTestOption(com.intellij.lang.javascript.JSTestOption) FlexModuleType(com.intellij.lang.javascript.flex.FlexModuleType) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) FlexStylesIndexableSetContributor(com.intellij.javascript.flex.css.FlexStylesIndexableSetContributor) Comparing(com.intellij.openapi.util.Comparing) PsiFile(com.intellij.psi.PsiFile) ModuleType(com.intellij.openapi.module.ModuleType) ActionScriptDaemonAnalyzerTestCase(com.intellij.flex.util.ActionScriptDaemonAnalyzerTestCase) FlexTestUtils(com.intellij.flex.util.FlexTestUtils) LightQuickFixTestCase(com.intellij.codeInsight.daemon.quickFix.LightQuickFixTestCase) TextEditor(com.intellij.openapi.fileEditor.TextEditor) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) ThrowableRunnable(com.intellij.util.ThrowableRunnable) PlatformTestUtil(com.intellij.testFramework.PlatformTestUtil) Collection(java.util.Collection) IOException(java.io.IOException) File(java.io.File) CommandProcessor(com.intellij.openapi.command.CommandProcessor) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) VfsUtilCore.convertFromUrl(com.intellij.openapi.vfs.VfsUtilCore.convertFromUrl) JSTestOptions(com.intellij.lang.javascript.JSTestOptions) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) NotNull(org.jetbrains.annotations.NotNull) TextEditor(com.intellij.openapi.fileEditor.TextEditor) ThrowableRunnable(com.intellij.util.ThrowableRunnable) Document(com.intellij.openapi.editor.Document) IOException(java.io.IOException)

Example 74 with TextEditor

use of com.intellij.openapi.fileEditor.TextEditor in project intellij-plugins by JetBrains.

the class ActionScriptHighlightingTest method setActiveEditor.

private void setActiveEditor(String relativePath) {
    VirtualFile file = myFile.getVirtualFile().findFileByRelativePath(relativePath);
    FileEditor[] editors = FileEditorManager.getInstance(myProject).getEditors(file);
    assertEquals(1, editors.length);
    FileEditor fileEditor = editors[0];
    assertTrue(fileEditor instanceof TextEditor);
    setActiveEditor(((TextEditor) fileEditor).getEditor());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor)

Example 75 with TextEditor

use of com.intellij.openapi.fileEditor.TextEditor in project intellij-plugins by JetBrains.

the class MarkdownActionUtil method findMarkdownTextEditor.

@Nullable
public static Editor findMarkdownTextEditor(AnActionEvent e) {
    final SplitFileEditor splitEditor = findSplitEditor(e);
    if (splitEditor == null) {
        // This fallback is used primarily for testing
        final PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
        if (psiFile != null && psiFile.getLanguage() == MarkdownLanguage.INSTANCE && ApplicationManager.getApplication().isUnitTestMode()) {
            return e.getData(CommonDataKeys.EDITOR);
        } else {
            return null;
        }
    }
    if (!(splitEditor.getMainEditor() instanceof TextEditor)) {
        return null;
    }
    final TextEditor mainEditor = (TextEditor) splitEditor.getMainEditor();
    if (!mainEditor.getComponent().isVisible()) {
        return null;
    }
    return mainEditor.getEditor();
}
Also used : TextEditor(com.intellij.openapi.fileEditor.TextEditor) PsiFile(com.intellij.psi.PsiFile) SplitFileEditor(org.intellij.plugins.markdown.ui.split.SplitFileEditor) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

TextEditor (com.intellij.openapi.fileEditor.TextEditor)75 FileEditor (com.intellij.openapi.fileEditor.FileEditor)52 Editor (com.intellij.openapi.editor.Editor)29 VirtualFile (com.intellij.openapi.vfs.VirtualFile)23 Project (com.intellij.openapi.project.Project)20 Document (com.intellij.openapi.editor.Document)13 Nullable (org.jetbrains.annotations.Nullable)12 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)11 PsiFile (com.intellij.psi.PsiFile)8 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)6 NotNull (org.jetbrains.annotations.NotNull)6 Disposable (com.intellij.openapi.Disposable)3 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)3 EditorNotificationPanel (com.intellij.ui.EditorNotificationPanel)3 LightweightHint (com.intellij.ui.LightweightHint)3 NonNls (org.jetbrains.annotations.NonNls)3 HighlightingPass (com.intellij.codeHighlighting.HighlightingPass)2 TextEditorHighlightingPass (com.intellij.codeHighlighting.TextEditorHighlightingPass)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)2 UndoManager (com.intellij.openapi.command.undo.UndoManager)2