Repository: twisted/towncrier

Issue:
When using `towncrier create --edit`, use `.md` or `.rst`
Currently when I use `towncrier create --edit`, my `$EDITOR` is opened pointing at a `.txt` file. It would be better if this was actually a `.md` or `.rst` file, as my editor will then automatically enable syntax highlighting.

Discussion:

Patch:
diff --git a/src/towncrier/create.py b/src/towncrier/create.py
index 28903f5..77433fc 100644
--- a/src/towncrier/create.py
+++ b/src/towncrier/create.py
@@ -167,7 +167,7 @@ def __main(
     if edit:
         if content == DEFAULT_CONTENT:
             content = ""
-        content = _get_news_content_from_user(content)
+        content = _get_news_content_from_user(content, extension=filename_ext)
         if not content:
             click.echo("Aborted creating news fragment due to empty message.")
             ctx.exit(1)
@@ -180,14 +180,14 @@ def __main(
     click.echo(f"Created news fragment at {segment_file}")
 
 
-def _get_news_content_from_user(message: str) -> str:
+def _get_news_content_from_user(message: str, extension: str = "") -> str:
     initial_content = """
 # Please write your news content. Lines starting with '#' will be ignored, and
 # an empty message aborts.
 """
     if message:
         initial_content = f"{message}\n{initial_content}"
-    content = click.edit(initial_content)
+    content = click.edit(initial_content, extension=extension or ".txt")
     if content is None:
         return message
     all_lines = content.split("\n")
diff --git a/src/towncrier/newsfragments/594.feature.rst b/src/towncrier/newsfragments/594.feature.rst
new file mode 100644
index 0000000..0f8d129
--- /dev/null
+++ b/src/towncrier/newsfragments/594.feature.rst
@@ -0,0 +1,1 @@
+The temporary file ``towncrier create`` creates now uses the correct ``.rst`` or ``.md`` extension, which may help your editor with with syntax highlighting.


Test patch:
diff --git a/src/towncrier/test/test_create.py b/src/towncrier/test/test_create.py
index e946f20..2ba74af 100644
--- a/src/towncrier/test/test_create.py
+++ b/src/towncrier/test/test_create.py
@@ -63,7 +63,8 @@ class TestCli(TestCase):
             self._test_success(content=content, additional_args=["--edit"])
             mock_edit.assert_called_once_with(
                 "\n# Please write your news content. Lines starting "
-                "with '#' will be ignored, and\n# an empty message aborts.\n"
+                "with '#' will be ignored, and\n# an empty message aborts.\n",
+                extension=".rst",
             )
 
     def test_edit_with_comment(self):
@@ -87,6 +88,56 @@ class TestCli(TestCase):
                 self.assertEqual([], os.listdir("foo/newsfragments"))
                 self.assertEqual(1, result.exit_code)
 
+    def test_edit_markdown_extension(self):
+        """
+        The temporary file extension used when editing is ``.md`` if the main filename
+        also uses that extension.
+        """
+
+        with mock.patch("click.edit") as mock_edit:
+            mock_edit.return_value = "This is line 1"
+            self._test_success(
+                content=["This is line 1"],
+                config=dedent(
+                    """\
+                    [tool.towncrier]
+                    package = "foo"
+                    filename = "README.md"
+                    """
+                ),
+                additional_args=["--edit"],
+            )
+            mock_edit.assert_called_once_with(
+                "\n# Please write your news content. Lines starting "
+                "with '#' will be ignored, and\n# an empty message aborts.\n",
+                extension=".md",
+            )
+
+    def test_edit_unknown_extension(self):
+        """
+        The temporary file extension used when editing is ``.txt`` if it the main
+        filename isn't ``.rst`` or ``.md``.
+        """
+
+        with mock.patch("click.edit") as mock_edit:
+            mock_edit.return_value = "This is line 1"
+            self._test_success(
+                content=["This is line 1"],
+                config=dedent(
+                    """\
+                    [tool.towncrier]
+                    package = "foo"
+                    filename = "README.FIRST"
+                    """
+                ),
+                additional_args=["--edit"],
+            )
+            mock_edit.assert_called_once_with(
+                "\n# Please write your news content. Lines starting "
+                "with '#' will be ignored, and\n# an empty message aborts.\n",
+                extension=".txt",
+            )
+
     def test_content(self):
         """
         When creating a new fragment the content can be passed as a
@@ -132,7 +183,8 @@ class TestCli(TestCase):
             )
             mock_edit.assert_called_once_with(
                 f"{content_line}\n\n# Please write your news content. Lines starting "
-                "with '#' will be ignored, and\n# an empty message aborts.\n"
+                "with '#' will be ignored, and\n# an empty message aborts.\n",
+                extension=".rst",
             )
 
     def test_different_directory(self):