Search in sources :

Example 36 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.

the class PyRegexpTest method doTestInjectedText.

@NotNull
private PsiElement doTestInjectedText(@NotNull String text, @NotNull String expected) {
    myFixture.configureByText(PythonFileType.INSTANCE, text);
    final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myFixture.getProject());
    final PsiLanguageInjectionHost host = languageManager.getInjectionHost(getElementAtCaret());
    assertNotNull(host);
    final List<Pair<PsiElement, TextRange>> files = languageManager.getInjectedPsiFiles(host);
    assertNotNull(files);
    assertFalse(files.isEmpty());
    final PsiElement injected = files.get(0).getFirst();
    assertEquals(expected, injected.getText());
    return injected;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager 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 38 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.

the class ImportFromExistingAction method addImportStatement.

private void addImportStatement(ImportCandidateHolder item) {
    final Project project = myTarget.getProject();
    final PyElementGenerator gen = PyElementGenerator.getInstance(project);
    AddImportHelper.ImportPriority priority = AddImportHelper.getImportPriority(myTarget, item.getFile());
    PsiFile file = myTarget.getContainingFile();
    InjectedLanguageManager manager = InjectedLanguageManager.getInstance(project);
    if (manager.isInjectedFragment(file)) {
        file = manager.getTopLevelFile(myTarget);
    }
    // We are trying to import top-level module or package which thus cannot be qualified
    if (isRoot(item.getFile())) {
        if (myImportLocally) {
            AddImportHelper.addLocalImportStatement(myTarget, myName);
        } else {
            AddImportHelper.addImportStatement(file, myName, item.getAsName(), priority, null);
        }
    } else {
        final QualifiedName path = item.getPath();
        final String qualifiedName = path != null ? path.toString() : "";
        if (myUseQualifiedImport) {
            String nameToImport = qualifiedName;
            if (item.getImportable() instanceof PsiFileSystemItem) {
                nameToImport += "." + myName;
            }
            if (myImportLocally) {
                AddImportHelper.addLocalImportStatement(myTarget, nameToImport);
            } else {
                AddImportHelper.addImportStatement(file, nameToImport, item.getAsName(), priority, null);
            }
            myTarget.replace(gen.createExpressionFromText(LanguageLevel.forElement(myTarget), qualifiedName + "." + myName));
        } else {
            if (myImportLocally) {
                AddImportHelper.addLocalFromImportStatement(myTarget, qualifiedName, myName);
            } else {
                // "Update" scenario takes place inside injected fragments, for normal AST addToExistingImport() will be used instead
                AddImportHelper.addOrUpdateFromImportStatement(file, qualifiedName, myName, item.getAsName(), priority, null);
            }
        }
    }
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) Project(com.intellij.openapi.project.Project) QualifiedName(com.intellij.psi.util.QualifiedName) PsiFile(com.intellij.psi.PsiFile) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem)

Example 39 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.

the class PyDocstringTest method doTestIndentation.

private void doTestIndentation(boolean hasErrors) {
    String inputDataFileName = getTestName(true) + ".py";
    myFixture.configureByFile(inputDataFileName);
    final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myFixture.getProject());
    final PsiLanguageInjectionHost host = languageManager.getInjectionHost(myFixture.getElementAtCaret());
    assertNotNull(host);
    final List<Pair<PsiElement, TextRange>> files = languageManager.getInjectedPsiFiles(host);
    assertNotNull(files);
    for (Pair<PsiElement, TextRange> pair : files) {
        assertEquals(hasErrors, PsiTreeUtil.hasErrorElements(pair.getFirst()));
    }
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 40 with InjectedLanguageManager

use of com.intellij.lang.injection.InjectedLanguageManager in project intellij-community by JetBrains.

the class PyTypingTest method doTestNoInjectedText.

private void doTestNoInjectedText(@NotNull String text) {
    myFixture.configureByText(PythonFileType.INSTANCE, text);
    final InjectedLanguageManager languageManager = InjectedLanguageManager.getInstance(myFixture.getProject());
    final PsiLanguageInjectionHost host = languageManager.getInjectionHost(getElementAtCaret());
    assertNull(host);
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost)

Aggregations

InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)41 TextRange (com.intellij.openapi.util.TextRange)12 PsiElement (com.intellij.psi.PsiElement)12 PsiFile (com.intellij.psi.PsiFile)11 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)10 Pair (com.intellij.openapi.util.Pair)7 Nullable (org.jetbrains.annotations.Nullable)7 Document (com.intellij.openapi.editor.Document)5 Project (com.intellij.openapi.project.Project)5 DocumentWindow (com.intellij.injected.editor.DocumentWindow)4 NotNull (org.jetbrains.annotations.NotNull)4 ASTNode (com.intellij.lang.ASTNode)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)2 THashMap (gnu.trove.THashMap)2 THashSet (gnu.trove.THashSet)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 HighlightInfoHolder (com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder)1 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1