PY-26060 New version-dependent super method signature macro

Tests for PY-26060

Merge-request: IJ-MR-95531
Merged-by: Daniil Kalinin <Daniil.Kalinin@jetbrains.com>

GitOrigin-RevId: 134f45d43aefc3cf7a3f806e0ea1ea6f768528f4
This commit is contained in:
Daniil Kalinin
2022-10-14 14:02:44 +00:00
committed by intellij-monorepo-bot
parent 323891923c
commit 93e169eea2
8 changed files with 59 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
<templateSet group="Python">
<template name="super" value="super($class$, self).$method$($end$)" description="'super(...)' call" toReformat="false" toShortenFQNames="true">
<variable name="class" expression="pyClassName()" defaultValue="" alwaysStopAt="false" />
<template name="super" value="super($args$).$method$($end$)" description="'super(...)' call" toReformat="false" toShortenFQNames="true">
<variable name="args" expression="pyVerSpecificSuperSignature()" defaultValue="" alwaysStopAt="false" />
<variable name="method" expression="pyFunctionName()" defaultValue="" alwaysStopAt="false" />
<variable name="end" expression="" defaultValue="" alwaysStopAt="true" />
<context>

View File

@@ -215,6 +215,7 @@
<liveTemplateMacro implementation="com.jetbrains.python.codeInsight.liveTemplates.PyClassNameMacro"/>
<liveTemplateMacro implementation="com.jetbrains.python.codeInsight.liveTemplates.PyFunctionNameMacro"/>
<liveTemplateMacro implementation="com.jetbrains.python.codeInsight.liveTemplates.PyIterableVariableMacro"/>
<liveTemplateMacro implementation="com.jetbrains.python.codeInsight.liveTemplates.PyVerSpecificSuperSignatureMacro"/>
<codeInsight.overrideMethod language="Python" implementationClass="com.jetbrains.python.codeInsight.override.PyOverrideMethodsHandler"/>
<codeInsight.implementMethod language="Python"

View File

@@ -0,0 +1,29 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.jetbrains.python.codeInsight.liveTemplates;
import com.intellij.codeInsight.template.*;
import com.jetbrains.python.psi.LanguageLevel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class PyVerSpecificSuperSignatureMacro extends Macro {
@Override
public String getName() {
return "pyVerSpecificSuperSignature";
}
@Override
public @Nullable Result calculateResult(Expression @NotNull [] params, ExpressionContext context) {
if (context.getPsiElementAtStartOffset() != null) {
if (LanguageLevel.forElement(context.getPsiElementAtStartOffset()).isPython2()) {
final Result classNameResult = new PyClassNameMacro().calculateResult(new Expression[]{}, context);
if (classNameResult != null) {
final String className = classNameResult.toString();
final String self = "self";
return new TextResult(className + ", " + self);
}
}
}
return new TextResult("");
}
}

View File

@@ -0,0 +1,3 @@
class Test:
def abc(self):
super<caret>

View File

@@ -0,0 +1,3 @@
class Test:
def abc(self):
super(Test, self).abc(<caret>)

View File

@@ -0,0 +1,3 @@
class Test:
def abc(self):
super<caret>

View File

@@ -0,0 +1,3 @@
class Test:
def abc(self):
super().abc(<caret>)

View File

@@ -18,6 +18,7 @@ package com.jetbrains.python.codeInsight.liveTemplates;
import com.intellij.codeInsight.template.Template;
import com.intellij.codeInsight.template.TemplateManager;
import com.jetbrains.python.fixtures.PyTestCase;
import com.jetbrains.python.psi.LanguageLevel;
public class PyLiveTemplatesExpandingTest extends PyTestCase {
@@ -69,4 +70,18 @@ public class PyLiveTemplatesExpandingTest extends PyTestCase {
public void testMainTopLevel() {
doMultiFileTest();
}
// PY-26060
public void testSuperTemplateWithPython2() {
runWithLanguageLevel(LanguageLevel.PYTHON27, () -> {
doMultiFileTest();
});
}
// PY-26060
public void testSuperTemplateWithPython3() {
runWithLanguageLevel(LanguageLevel.getLatest(), () -> {
doMultiFileTest();
});
}
}