use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyBlockEvaluatorTest method testFunction.
public void testFunction() {
PyBlockEvaluator eval = new PyBlockEvaluator();
PyFile file = (PyFile) PsiFileFactory.getInstance(myFixture.getProject()).createFileFromText("a.py", PythonFileType.INSTANCE, "def foo(): return 'a'");
PyFunction foo = file.findTopLevelFunction("foo");
eval.evaluate(foo);
assertEquals("a", eval.getReturnValue());
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyHierarchyNodeDescriptor method update.
@Override
public boolean update() {
boolean changes = super.update();
final CompositeAppearance oldText = myHighlightedText;
myHighlightedText = new CompositeAppearance();
NavigatablePsiElement element = (NavigatablePsiElement) getPsiElement();
if (element == null) {
final String invalidPrefix = IdeBundle.message("node.hierarchy.invalid");
if (!myHighlightedText.getText().startsWith(invalidPrefix)) {
myHighlightedText.getBeginning().addText(invalidPrefix, HierarchyNodeDescriptor.getInvalidPrefixAttributes());
}
return true;
}
final ItemPresentation presentation = element.getPresentation();
if (presentation != null) {
if (element instanceof PyFunction) {
final PyClass cls = ((PyFunction) element).getContainingClass();
if (cls != null) {
myHighlightedText.getEnding().addText(cls.getName() + ".");
}
}
myHighlightedText.getEnding().addText(presentation.getPresentableText());
myHighlightedText.getEnding().addText(" " + presentation.getLocationString(), HierarchyNodeDescriptor.getPackageNameAttributes());
}
myName = myHighlightedText.getText();
if (!Comparing.equal(myHighlightedText, oldText)) {
changes = true;
}
return changes;
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PythonDocumentationMap method transformPattern.
@Nullable
private static String transformPattern(@NotNull String urlPattern, QualifiedName moduleQName, @Nullable PsiNamedElement element, String pyVersion) {
Map<String, String> macros = new HashMap<>();
macros.put("element.name", element == null ? null : element.getName());
PyClass pyClass = element == null ? null : PsiTreeUtil.getParentOfType(element, PyClass.class, false);
macros.put("class.name", pyClass == null ? null : pyClass.getName());
if (element != null) {
StringBuilder qName = new StringBuilder(moduleQName.toString()).append(".");
if (element instanceof PyFunction && ((PyFunction) element).getContainingClass() != null) {
qName.append(((PyFunction) element).getContainingClass().getName()).append(".");
}
qName.append(element.getName());
macros.put("element.qname", qName.toString());
} else {
macros.put("element.qname", "");
}
macros.put("function.name", element instanceof PyFunction ? element.getName() : "");
macros.put("module.name", moduleQName.toString());
macros.put("python.version", pyVersion);
final String pattern = transformPattern(urlPattern, macros);
if (pattern == null) {
return rootForPattern(urlPattern);
}
return pattern;
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyQualifiedNameProvider method qualifiedNameToElement.
@Nullable
@Override
public PsiElement qualifiedNameToElement(String fqn, Project project) {
PyClass aClass = PyClassNameIndex.findClass(fqn, project);
if (aClass != null) {
return aClass;
}
final Collection<PyFunction> functions = PyFunctionNameIndex.find(fqn, project);
if (!functions.isEmpty()) {
return ContainerUtil.getFirstItem(functions);
}
final int sharpIdx = fqn.indexOf("#");
if (sharpIdx > -1) {
final String className = StringUtil.getPackageName(fqn, '#');
aClass = PyClassNameIndex.findClass(className, project);
if (aClass != null) {
final String memberName = StringUtil.getShortName(fqn, '#');
final PyClass nestedClass = aClass.findNestedClass(memberName, false);
if (nestedClass != null)
return nestedClass;
final PyFunction methodByName = aClass.findMethodByName(memberName, false, null);
if (methodByName != null)
return methodByName;
}
}
return null;
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyParameterListFixer method doApply.
@Override
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyParameterList parameters) throws IncorrectOperationException {
final PsiElement lBrace = PyPsiUtils.getChildByFilter(parameters, PyTokenTypes.OPEN_BRACES, 0);
final PsiElement rBrace = PyPsiUtils.getChildByFilter(parameters, PyTokenTypes.CLOSE_BRACES, 0);
final PyFunction pyFunction = as(parameters.getParent(), PyFunction.class);
if (pyFunction != null && !PyFunctionFixer.isFakeFunction(pyFunction) && (lBrace == null || rBrace == null)) {
final Document document = editor.getDocument();
if (lBrace == null) {
final String textToInsert = pyFunction.getNameNode() == null ? " (" : "(";
document.insertString(parameters.getTextOffset(), textToInsert);
} else if (parameters.getParameters().length == 0) {
document.insertString(lBrace.getTextRange().getEndOffset(), ")");
} else {
document.insertString(parameters.getTextRange().getEndOffset(), ")");
}
}
}
Aggregations