Search in sources :

Example 1 with IpnbPyFragment

use of org.jetbrains.plugins.ipnb.psi.IpnbPyFragment in project intellij-community by JetBrains.

the class IpnbPep8ProblemSuppressor method isProblemSuppressed.

@Override
public boolean isProblemSuppressed(@NotNull Pep8ExternalAnnotator.Problem problem, @NotNull PsiFile file, @Nullable PsiElement targetElement) {
    if (file instanceof IpnbPyFragment) {
        // Ignore warnings about missing new line at the end of file
        if (problem.getCode().equals("W292")) {
            return true;
        }
        // Ignore warnings about imports not at the top of file if there are magic commands before
        if (problem.getCode().equals("E402") && targetElement != null) {
            final PyImportStatementBase importStatement = PsiTreeUtil.getParentOfType(targetElement, PyImportStatementBase.class);
            if (importStatement != null && importStatement.getParent() == file) {
                boolean containsMagicCommandsBefore = false;
                PsiElement prev = PyPsiUtils.getPrevNonCommentSibling(importStatement, true);
                while (prev != null) {
                    if (prev instanceof PyEmptyExpression && prev.getText().startsWith("%")) {
                        containsMagicCommandsBefore = true;
                    } else if (!(isModuleLevelDocstring(prev, (PyFile) file) || prev instanceof PyImportStatementBase)) {
                        return false;
                    }
                    prev = PyPsiUtils.getPrevNonCommentSibling(prev, true);
                }
                return containsMagicCommandsBefore;
            }
        }
    }
    return false;
}
Also used : PsiElement(com.intellij.psi.PsiElement) IpnbPyFragment(org.jetbrains.plugins.ipnb.psi.IpnbPyFragment)

Example 2 with IpnbPyFragment

use of org.jetbrains.plugins.ipnb.psi.IpnbPyFragment in project intellij-community by JetBrains.

the class IpnbEditorUtil method createPythonCodeEditor.

public static Editor createPythonCodeEditor(@NotNull final Project project, @NotNull final IpnbCodeSourcePanel codeSourcePanel) {
    final EditorFactory editorFactory = EditorFactory.getInstance();
    assert editorFactory != null;
    final String text = codeSourcePanel.getCell().getSourceAsString().trim();
    final Module module = ProjectRootManagerEx.getInstanceEx(project).getFileIndex().getModuleForFile(codeSourcePanel.getIpnbCodePanel().getFileEditor().getVirtualFile());
    final IpnbPyFragment fragment = new IpnbPyFragment(project, text, true, codeSourcePanel);
    fragment.putUserData(ModuleUtilCore.KEY_MODULE, module);
    final Document document = PsiDocumentManager.getInstance(project).getDocument(fragment);
    assert document != null;
    EditorEx editor = (EditorEx) editorFactory.createEditor(document, project, fragment.getVirtualFile(), false);
    setupEditor(editor);
    return editor;
}
Also used : EditorFactory(com.intellij.openapi.editor.EditorFactory) EditorEx(com.intellij.openapi.editor.ex.EditorEx) Module(com.intellij.openapi.module.Module) Document(com.intellij.openapi.editor.Document) IpnbPyFragment(org.jetbrains.plugins.ipnb.psi.IpnbPyFragment)

Aggregations

IpnbPyFragment (org.jetbrains.plugins.ipnb.psi.IpnbPyFragment)2 Document (com.intellij.openapi.editor.Document)1 EditorFactory (com.intellij.openapi.editor.EditorFactory)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 Module (com.intellij.openapi.module.Module)1 PsiElement (com.intellij.psi.PsiElement)1