mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
PyCodeFragmentBuilder with imports support and tests
This commit is contained in:
@@ -7,6 +7,7 @@ import com.intellij.psi.ResolveResult;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
|
||||
import com.jetbrains.python.psi.*;
|
||||
import com.jetbrains.python.psi.impl.PyImportStatementNavigator;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@@ -31,16 +32,27 @@ public class PyCodeFragmentBuilder extends PyRecursiveElementVisitor {
|
||||
|
||||
@Override
|
||||
public void visitPyTargetExpression(final PyTargetExpression node) {
|
||||
visitDeclaration(node);
|
||||
processDeclaration(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPyNamedParameter(final PyNamedParameter node) {
|
||||
visitDeclaration(node);
|
||||
processDeclaration(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPyReferenceExpression(final PyReferenceExpression element) {
|
||||
// Python PSI makes us to visit qualifier manually
|
||||
final PyExpression qualifier = element.getQualifier();
|
||||
if (qualifier != null){
|
||||
qualifier.accept(this);
|
||||
}
|
||||
// Process import references
|
||||
if (PyImportStatementNavigator.getImportStatementByReference(element) != null){
|
||||
processDeclaration(element);
|
||||
return;
|
||||
}
|
||||
|
||||
final Position position = CodeFragmentUtil.getPosition(element, startOffset, endOffset);
|
||||
final String name = element.getName();
|
||||
|
||||
@@ -68,6 +80,12 @@ public class PyCodeFragmentBuilder extends PyRecursiveElementVisitor {
|
||||
}
|
||||
for (ResolveResult result : element.multiResolve(false)) {
|
||||
final PsiElement declaration = result.getElement();
|
||||
// Handle resolve via import statement
|
||||
if (declaration instanceof PyFile && modifiedInsideMap.containsKey(name)){
|
||||
outElements.add(name);
|
||||
break;
|
||||
}
|
||||
// Ignore declarations out of scope
|
||||
if (declaration == null || !PsiTreeUtil.isAncestor(myOwner, declaration, false)){
|
||||
continue;
|
||||
}
|
||||
@@ -98,13 +116,11 @@ public class PyCodeFragmentBuilder extends PyRecursiveElementVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
private void visitDeclaration(final PyElement element) {
|
||||
private void processDeclaration(final PyElement element) {
|
||||
final Position position = CodeFragmentUtil.getPosition(element, startOffset, endOffset);
|
||||
final String name = element.getName();
|
||||
|
||||
// Collect in variables
|
||||
if (position == Position.INSIDE) {
|
||||
|
||||
// Add modification inside
|
||||
List<PyElement> list = modifiedInsideMap.get(name);
|
||||
if (list == null) {
|
||||
@@ -113,10 +129,5 @@ public class PyCodeFragmentBuilder extends PyRecursiveElementVisitor {
|
||||
}
|
||||
list.add(element);
|
||||
}
|
||||
|
||||
// if name is already in out parameters
|
||||
if (inElements.contains(name)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.jetbrains.python.psi.impl;
|
||||
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.jetbrains.python.psi.PyImportElement;
|
||||
import com.jetbrains.python.psi.PyImportStatement;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author oleg
|
||||
*/
|
||||
public class PyImportStatementNavigator {
|
||||
@Nullable
|
||||
public static PyImportStatement getImportStatementByReference(final PsiElement element){
|
||||
final PyImportStatement statement = PsiTreeUtil.getParentOfType(element, PyImportStatement.class, false);
|
||||
if (statement == null){
|
||||
return null;
|
||||
}
|
||||
for (PyImportElement importElement : statement.getImportElements()) {
|
||||
if (element == importElement.getImportReference()){
|
||||
return statement;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
8
python/testData/codeInsight/codefragment/empty.test
Normal file
8
python/testData/codeInsight/codefragment/empty.test
Normal file
@@ -0,0 +1,8 @@
|
||||
import foo
|
||||
<begin>
|
||||
print("Foo was imported")
|
||||
<end>
|
||||
foo.do_something
|
||||
<result>
|
||||
In:
|
||||
Out:
|
||||
14
python/testData/codeInsight/codefragment/expression.test
Normal file
14
python/testData/codeInsight/codefragment/expression.test
Normal file
@@ -0,0 +1,14 @@
|
||||
a = 12
|
||||
if some_cond:
|
||||
b = 1
|
||||
|
||||
c =
|
||||
<begin>
|
||||
foo(boo(a) + 123 * b)
|
||||
<end>
|
||||
|
||||
<result>
|
||||
In:
|
||||
a
|
||||
b
|
||||
Out:
|
||||
@@ -0,0 +1,9 @@
|
||||
print("start")
|
||||
<begin>
|
||||
print("code")
|
||||
<end>
|
||||
import foo
|
||||
foo.bar
|
||||
<result>
|
||||
In:
|
||||
Out:
|
||||
@@ -0,0 +1,9 @@
|
||||
print("start")
|
||||
import foo
|
||||
<begin>
|
||||
print("code")
|
||||
<end>
|
||||
foo.bar
|
||||
<result>
|
||||
In:
|
||||
Out:
|
||||
@@ -0,0 +1,10 @@
|
||||
print("start")
|
||||
import foo
|
||||
<begin>
|
||||
print(foo)
|
||||
<end>
|
||||
foo.bar
|
||||
<result>
|
||||
In:
|
||||
foo
|
||||
Out:
|
||||
@@ -0,0 +1,9 @@
|
||||
print("start")
|
||||
<begin>
|
||||
import foo
|
||||
<end>
|
||||
foo.bar
|
||||
<result>
|
||||
In:
|
||||
Out:
|
||||
foo
|
||||
9
python/testData/codeInsight/codefragment/out.test
Normal file
9
python/testData/codeInsight/codefragment/out.test
Normal file
@@ -0,0 +1,9 @@
|
||||
print("start")
|
||||
<begin>
|
||||
aaa = 123
|
||||
<end>
|
||||
print(aaa)
|
||||
<result>
|
||||
In:
|
||||
Out:
|
||||
aaa
|
||||
10
python/testData/codeInsight/codefragment/parameters.test
Normal file
10
python/testData/codeInsight/codefragment/parameters.test
Normal file
@@ -0,0 +1,10 @@
|
||||
def plus(dddd, eeeee):
|
||||
<begin>
|
||||
dddd + eeeee * 123
|
||||
<end>
|
||||
|
||||
<result>
|
||||
In:
|
||||
dddd
|
||||
eeeee
|
||||
Out:
|
||||
9
python/testData/codeInsight/codefragment/simple.test
Normal file
9
python/testData/codeInsight/codefragment/simple.test
Normal file
@@ -0,0 +1,9 @@
|
||||
aaa = 123
|
||||
<begin>
|
||||
print(aaa)
|
||||
<end>
|
||||
print("Hello")
|
||||
<result>
|
||||
In:
|
||||
aaa
|
||||
Out:
|
||||
13
python/testData/codeInsight/codefragment/variables.test
Normal file
13
python/testData/codeInsight/codefragment/variables.test
Normal file
@@ -0,0 +1,13 @@
|
||||
aaa = 12
|
||||
<begin>
|
||||
bbb = aaa
|
||||
aaa = bbb + 123
|
||||
<end>
|
||||
bbb
|
||||
aaa
|
||||
<result>
|
||||
In:
|
||||
aaa
|
||||
Out:
|
||||
aaa
|
||||
bbb
|
||||
@@ -0,0 +1,7 @@
|
||||
aaa = 12
|
||||
<begin>
|
||||
aaa = 123
|
||||
<end>
|
||||
<result>
|
||||
In:
|
||||
Out:
|
||||
@@ -0,0 +1,8 @@
|
||||
<begin>
|
||||
aaa = 12
|
||||
<end>
|
||||
aaa = 12
|
||||
aaa
|
||||
<result>
|
||||
In:
|
||||
Out:
|
||||
@@ -0,0 +1,137 @@
|
||||
package com.jetbrains.python.refactoring;
|
||||
|
||||
import com.intellij.codeInsight.codeFragment.CodeFragment;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.jetbrains.python.PythonTestUtil;
|
||||
import com.jetbrains.python.codeInsight.codeFragment.PyCodeFragmentUtil;
|
||||
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
|
||||
import com.jetbrains.python.fixtures.LightMarkedTestCase;
|
||||
import com.jetbrains.python.psi.PyFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* @author oleg
|
||||
*/
|
||||
public class PyCodeFragmentTest extends LightMarkedTestCase {
|
||||
public String getTestDataPath() {
|
||||
return PythonTestUtil.getTestDataPath() + "/codeInsight/codefragment/";
|
||||
}
|
||||
|
||||
final private String BEGIN_MARKER = "<begin>";
|
||||
final private String END_MARKER = "<end>";
|
||||
final private String RESULT_MARKER = "<result>";
|
||||
|
||||
private void doTest(Pair<String, String>... files2Create) throws Exception {
|
||||
final String testName = getTestName(false).toLowerCase();
|
||||
final String fullPath = getTestDataPath() + testName + ".test";
|
||||
|
||||
final VirtualFile vFile = LocalFileSystem.getInstance().findFileByPath(fullPath.replace(File.separatorChar, '/'));
|
||||
String fileText = StringUtil.convertLineSeparators(VfsUtil.loadText(vFile), "\n");
|
||||
|
||||
final int beginMarker = fileText.indexOf(BEGIN_MARKER);
|
||||
final int endMarker = fileText.indexOf(END_MARKER);
|
||||
final int resultMarker = fileText.indexOf(RESULT_MARKER);
|
||||
assertTrue(beginMarker != -1);
|
||||
assertTrue(endMarker != -1);
|
||||
assertTrue(resultMarker != -1);
|
||||
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append(fileText.substring(0, beginMarker));
|
||||
builder.append(fileText.substring(beginMarker + BEGIN_MARKER.length(), endMarker));
|
||||
builder.append((fileText.substring(endMarker + END_MARKER.length(), resultMarker)));
|
||||
|
||||
final String result = fileText.substring(resultMarker + RESULT_MARKER.length());
|
||||
|
||||
// Create additional files
|
||||
for (Pair<String, String> pair : files2Create) {
|
||||
myFixture.addFileToProject(pair.first, pair.second);
|
||||
}
|
||||
|
||||
final PyFile file = (PyFile)myFixture.addFileToProject(testName + ".py", builder.toString());
|
||||
check(file, beginMarker, endMarker, result);
|
||||
}
|
||||
|
||||
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();
|
||||
final CodeFragment fragment = PyCodeFragmentUtil.createCodeFragment((ScopeOwner)context, startElement, endElement);
|
||||
if (fragment.isReturnInstructonInside()) {
|
||||
buffer.append("Return instruction inside found").append("\n");
|
||||
}
|
||||
buffer.append("In:\n");
|
||||
for (String inputVariable : new TreeSet<String>(fragment.getInputVariables())) {
|
||||
buffer.append(inputVariable).append('\n');
|
||||
}
|
||||
buffer.append("Out:\n");
|
||||
for (String outputVariable : new TreeSet<String>(fragment.getOutputVariables())) {
|
||||
buffer.append(outputVariable).append('\n');
|
||||
}
|
||||
|
||||
assertEquals(result.trim(), buffer.toString().trim());
|
||||
}
|
||||
|
||||
public void testImportBefore() throws Exception {
|
||||
doTest(Pair.create("foo.py", ""));
|
||||
}
|
||||
|
||||
public void testImportBeforeUseInside() throws Exception {
|
||||
doTest(Pair.create("foo.py", ""));
|
||||
}
|
||||
|
||||
public void testImportInsideUseAfter() throws Exception {
|
||||
doTest(Pair.create("foo.py", ""));
|
||||
}
|
||||
|
||||
public void testImportAfter() throws Exception {
|
||||
doTest(Pair.create("foo.py", ""));
|
||||
}
|
||||
|
||||
|
||||
public void testSimple() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testEmpty() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testOut() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
|
||||
public void testExpression() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testParameters() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testVariables() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testVariablesEmptyOut() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testVariablesEmptyIn() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user