mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PY-52162 deprecated implementation of PythonTemplateContextType#isInContext changed to accept TemplateActionContext
Tests for PY-52162 Merge-request: IJ-MR-24510 Merged-by: Daniil Kalinin <Daniil.Kalinin@jetbrains.com> GitOrigin-RevId: 8cb7674b3840424015eeb06981ec16734fe26af2
This commit is contained in:
committed by
intellij-monorepo-bot
parent
143782f5e0
commit
fa259d0727
+9
-8
@@ -2,6 +2,7 @@
|
||||
package com.jetbrains.python.codeInsight.liveTemplates;
|
||||
|
||||
import com.intellij.codeInsight.template.EverywhereContextType;
|
||||
import com.intellij.codeInsight.template.TemplateActionContext;
|
||||
import com.intellij.codeInsight.template.TemplateContextType;
|
||||
import com.intellij.openapi.util.NlsContexts;
|
||||
import com.intellij.patterns.PsiElementPattern;
|
||||
@@ -31,19 +32,19 @@ public abstract class PythonTemplateContextType extends TemplateContextType {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInContext(@NotNull PsiFile file, int offset) {
|
||||
if (isPythonLanguage(file, offset)) {
|
||||
final PsiElement element = file.findElementAt(offset);
|
||||
|
||||
public boolean isInContext(@NotNull TemplateActionContext templateActionContext) {
|
||||
PsiFile file = templateActionContext.getFile();
|
||||
if (isPythonLanguage(file, templateActionContext.getStartOffset())) {
|
||||
final PsiElement element = file.findElementAt(templateActionContext.getStartOffset());
|
||||
if (element != null) {
|
||||
if (isAfterDot(element) || element instanceof PsiComment || isInsideStringLiteral(element) || isInsideParameterList(element)) {
|
||||
return false;
|
||||
if (!templateActionContext.isSurrounding()) {
|
||||
if (isAfterDot(element) || element instanceof PsiComment || isInsideStringLiteral(element) || isInsideParameterList(element)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return isInContext(element);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<selection>'test<caret>'</selection>
|
||||
+38
-11
@@ -15,13 +15,14 @@
|
||||
*/
|
||||
package com.jetbrains.python.codeInsight.liveTemplates;
|
||||
|
||||
import com.intellij.codeInsight.template.TemplateContextType;
|
||||
import com.intellij.codeInsight.template.*;
|
||||
import com.intellij.codeInsight.template.impl.*;
|
||||
import com.intellij.testFramework.fixtures.CodeInsightTestUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.jetbrains.python.fixtures.PyTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PyLiveTemplatesContextTest extends PyTestCase {
|
||||
@@ -32,7 +33,7 @@ public class PyLiveTemplatesContextTest extends PyTestCase {
|
||||
}
|
||||
|
||||
public void testNotPython() {
|
||||
doTest("html");
|
||||
doTest(false, "html");
|
||||
}
|
||||
|
||||
// PY-12212
|
||||
@@ -64,22 +65,48 @@ public class PyLiveTemplatesContextTest extends PyTestCase {
|
||||
doTest(PythonTemplateContextType.Class.class, PythonTemplateContextType.General.class);
|
||||
}
|
||||
|
||||
private void doTest(Class<? extends PythonTemplateContextType> @NotNull ... expectedContextTypes) {
|
||||
doTest("py", expectedContextTypes);
|
||||
// PY-52162
|
||||
public void testSurroundStringLiteral() {
|
||||
doTest(true, "py", PythonTemplateContextType.General.class, PythonTemplateContextType.TopLevel.class);
|
||||
}
|
||||
|
||||
private void doTest(@NotNull String extension, Class<? extends PythonTemplateContextType> @NotNull ... expectedContextTypes) {
|
||||
// PY-52162
|
||||
public void testSurroundTemplateWithStringLiterals() {
|
||||
final TemplateManager templateManager = TemplateManager.getInstance(myFixture.getProject());
|
||||
final Template template = templateManager.createTemplate("pri", "Python", "print($SELECTION$)$END$");
|
||||
|
||||
TemplateContextType context = ContainerUtil.findInstance(TemplateContextType.EP_NAME.getExtensions(), PythonTemplateContextType.General.class);
|
||||
assertNotNull(context);
|
||||
((TemplateImpl)template).getTemplateContext().setEnabled(context, true);
|
||||
|
||||
CodeInsightTestUtil.addTemplate(template, myFixture.getTestRootDisposable());
|
||||
|
||||
myFixture.configureByText("abc.py", "'test'<caret>");
|
||||
myFixture.getEditor().getSelectionModel().setSelection(0, 6);
|
||||
new InvokeTemplateAction((TemplateImpl)template, myFixture.getEditor(), myFixture.getProject(), new HashSet<>()).perform();
|
||||
myFixture.checkResult("print('test')");
|
||||
}
|
||||
|
||||
private void doTest(Class<? extends PythonTemplateContextType> @NotNull ... expectedContextTypes) {
|
||||
doTest(false, "py", expectedContextTypes);
|
||||
}
|
||||
|
||||
private void doTest(boolean isSurrounding,
|
||||
@NotNull String extension,
|
||||
Class<? extends PythonTemplateContextType> @NotNull ... expectedContextTypes) {
|
||||
myFixture.configureByFile(getTestName(true) + "." + extension);
|
||||
|
||||
final List<Class<? extends PythonTemplateContextType>> actualContextTypes = calculateEnabledContextTypes(getRegisteredContextTypes());
|
||||
final List<Class<? extends PythonTemplateContextType>> actualContextTypes =
|
||||
calculateEnabledContextTypes(isSurrounding, getRegisteredContextTypes());
|
||||
assertSameElements(actualContextTypes, expectedContextTypes);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private List<Class<? extends PythonTemplateContextType>> calculateEnabledContextTypes(@NotNull List<PythonTemplateContextType> registeredContextTypes) {
|
||||
private List<Class<? extends PythonTemplateContextType>> calculateEnabledContextTypes(boolean isSurrounding, @NotNull List<PythonTemplateContextType> registeredContextTypes) {
|
||||
TemplateActionContext context = TemplateActionContext.create(myFixture.getFile(), null, myFixture.getCaretOffset(), myFixture.getCaretOffset(), isSurrounding);
|
||||
return registeredContextTypes
|
||||
.stream()
|
||||
.filter(type -> type.isInContext(myFixture.getFile(), myFixture.getCaretOffset()))
|
||||
.filter(type -> type.isInContext(context))
|
||||
.map(PythonTemplateContextType::getClass)
|
||||
.sorted(Comparator.comparing(Class::getSimpleName))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Reference in New Issue
Block a user