Search in sources :

Example 1 with CodeFragment

use of com.intellij.codeInsight.codeFragment.CodeFragment in project intellij-community by JetBrains.

the class PyCodeFragmentTest method check.

private void check(final PyFile myFile, final int beginMarker, final int endMarker, final String result) {
    final PsiElement startElement = myFile.findElementAt(beginMarker);
    final PsiElement endElement = myFile.findElementAt(endMarker - BEGIN_MARKER.length());
    PsiElement context = PsiTreeUtil.findCommonParent(startElement, endElement);
    if (!(context instanceof ScopeOwner)) {
        context = PsiTreeUtil.getParentOfType(context, ScopeOwner.class);
    }
    final StringBuffer buffer = new StringBuffer();
    try {
        final CodeFragment fragment = PyCodeFragmentUtil.createCodeFragment((ScopeOwner) context, startElement, endElement);
        if (fragment.isReturnInstructionInside()) {
            buffer.append("Return instruction inside found").append("\n");
        }
        buffer.append("In:\n");
        for (String inputVariable : new TreeSet<>(fragment.getInputVariables())) {
            buffer.append(inputVariable).append('\n');
        }
        buffer.append("Out:\n");
        for (String outputVariable : new TreeSet<>(fragment.getOutputVariables())) {
            buffer.append(outputVariable).append('\n');
        }
    } catch (CannotCreateCodeFragmentException e) {
        assertEquals(result.trim(), e.getMessage());
        return;
    }
    assertEquals(result.trim(), buffer.toString().trim());
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) CannotCreateCodeFragmentException(com.intellij.codeInsight.codeFragment.CannotCreateCodeFragmentException) TreeSet(java.util.TreeSet) CodeFragment(com.intellij.codeInsight.codeFragment.CodeFragment) PsiElement(com.intellij.psi.PsiElement)

Aggregations

CannotCreateCodeFragmentException (com.intellij.codeInsight.codeFragment.CannotCreateCodeFragmentException)1 CodeFragment (com.intellij.codeInsight.codeFragment.CodeFragment)1 PsiElement (com.intellij.psi.PsiElement)1 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)1 TreeSet (java.util.TreeSet)1