From b82acb31c51ca7b5629766d6438a9451ba8647d6 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Wed, 18 Feb 2015 16:54:26 +0300 Subject: [PATCH] PY-14979 Manually add value of the "NAME" template variable when creating new file for refactoring However PACKAGE_NAME variable for Python templates is still undefined and it seems that better to create such new files via FileTemplateUtil instead somehow. --- python/src/com/jetbrains/python/psi/PyUtil.java | 4 +++- .../after/src/a.py | 0 .../after/src/b.py | 5 +++++ .../before/src/a.py | 2 ++ .../python/refactoring/PyMoveTest.java | 17 +++++++++++++++++ 5 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 python/testData/refactoring/move/templateAttributesExpansionInCreatedDestinationModule/after/src/a.py create mode 100644 python/testData/refactoring/move/templateAttributesExpansionInCreatedDestinationModule/after/src/b.py create mode 100644 python/testData/refactoring/move/templateAttributesExpansionInCreatedDestinationModule/before/src/a.py diff --git a/python/src/com/jetbrains/python/psi/PyUtil.java b/python/src/com/jetbrains/python/psi/PyUtil.java index 3c4b23af6ee3..c094d8dc1ed7 100644 --- a/python/src/com/jetbrains/python/psi/PyUtil.java +++ b/python/src/com/jetbrains/python/psi/PyUtil.java @@ -1365,7 +1365,9 @@ public class PyUtil { final VirtualFile baseDir = project.getBaseDir(); final FileTemplateManager fileTemplateManager = FileTemplateManager.getInstance(project); final FileTemplate template = fileTemplateManager.getInternalTemplate("Python Script"); - final String content = (template != null) ? template.getText(fileTemplateManager.getDefaultProperties(project)) : null; + final Properties properties = fileTemplateManager.getDefaultProperties(); + properties.setProperty("NAME", StringUtil.trimEnd(file.getName(), PyNames.DOT_PY)); + final String content = (template != null) ? template.getText(properties) : null; psi = PyExtractSuperclassHelper.placeFile(project, StringUtil.notNullize( file.getParent(), diff --git a/python/testData/refactoring/move/templateAttributesExpansionInCreatedDestinationModule/after/src/a.py b/python/testData/refactoring/move/templateAttributesExpansionInCreatedDestinationModule/after/src/a.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/python/testData/refactoring/move/templateAttributesExpansionInCreatedDestinationModule/after/src/b.py b/python/testData/refactoring/move/templateAttributesExpansionInCreatedDestinationModule/after/src/b.py new file mode 100644 index 000000000000..d68481e6f67b --- /dev/null +++ b/python/testData/refactoring/move/templateAttributesExpansionInCreatedDestinationModule/after/src/b.py @@ -0,0 +1,5 @@ +NAME = 'b' + + +class C: + pass \ No newline at end of file diff --git a/python/testData/refactoring/move/templateAttributesExpansionInCreatedDestinationModule/before/src/a.py b/python/testData/refactoring/move/templateAttributesExpansionInCreatedDestinationModule/before/src/a.py new file mode 100644 index 000000000000..67b24cac9509 --- /dev/null +++ b/python/testData/refactoring/move/templateAttributesExpansionInCreatedDestinationModule/before/src/a.py @@ -0,0 +1,2 @@ +class C: + pass \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/refactoring/PyMoveTest.java b/python/testSrc/com/jetbrains/python/refactoring/PyMoveTest.java index 6ebfd283ce46..22f4a1fdeab5 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/PyMoveTest.java +++ b/python/testSrc/com/jetbrains/python/refactoring/PyMoveTest.java @@ -15,6 +15,8 @@ */ package com.jetbrains.python.refactoring; +import com.intellij.ide.fileTemplates.FileTemplate; +import com.intellij.ide.fileTemplates.FileTemplateManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.*; @@ -150,6 +152,21 @@ public class PyMoveTest extends PyTestCase { doMoveFileTest("pkg1", "pkg2"); } + // PY-14979 + public void testTemplateAttributesExpansionInCreatedDestinationModule() { + final FileTemplateManager instance = FileTemplateManager.getInstance(myFixture.getProject()); + final FileTemplate template = instance.getInternalTemplate("Python Script"); + assertNotNull(template); + final String oldTemplateContent = template.getText(); + try { + template.setText("NAME = '${NAME}'"); + doMoveSymbolTest("C", "b.py"); + } + finally { + template.setText(oldTemplateContent); + } + } + // PY-7378 public void testMoveNamespacePackage1() { runWithLanguageLevel(LanguageLevel.PYTHON33, new Runnable() {