python-black icon indicating copy to clipboard operation
python-black copied to clipboard

In comments, tabs replaced by spaces (plugin only)

Open jaraco opened this issue 2 years ago • 5 comments

I'm editing this file in SublimeText with Format on Save set to Smart. When I save the file, the tab in that line and in the following line get converted to spaces.

diff --git a/project.py b/project.py
index 06bb693..1949f63 100644
--- a/project.py
+++ b/project.py
@@ -2290,8 +2290,8 @@ class Project(object):
             name = self.remote.name
 
         # The output will look like (NB: tabs are separators):
-        # ref: refs/heads/master^IHEAD
-        # 5f6803b100bb3cd0f534e96e88c91373e8ed1c44^IHEAD
+        # ref: refs/heads/master    HEAD
+        # 5f6803b100bb3cd0f534e96e88c91373e8ed1c44  HEAD
         output = self.bare_git.ls_remote(
             "-q", "--symref", "--exit-code", name, "HEAD"

However, when using black 23.7.0 or 23.9.1 on the CLI, the tabs are retained:

 git-repo main @ git-id
83c66ec
 git-repo main @ black --version
black, 23.9.1 (compiled: yes)
Python (CPython) 3.11.5
 git-repo main @ black --check project.py
All done! ✨ 🍰 ✨
1 file would be left unchanged.

If disabling Format on Save, the tabs are retained when saving. If invoking the plugin manually, the tabs are replaced.

I expect the behavior to be consistent with a file system invocation. Why is it not? How can we make it so?

jaraco avatar Oct 02 '23 15:10 jaraco

I think this is an issue with the Sublime Text editor. Could you copy the code below into the editor and see if \t is being replaced with spaces:

    def ResolveRemoteHead(self, name=None):
        """Find out what the default branch (HEAD) points to.
        Normally this points to refs/heads/master, but projects are moving to
        main. Support whatever the server uses rather than hardcoding "master"
        ourselves.
        """
        if name is None:
            name = self.remote.name
        # The output will look like (NB: tabs are separators):
        # ref: refs/heads/master	HEAD
        # 5f6803b100bb3cd0f534e96e88c91373e8ed1c44	HEAD
        output = self.bare_git.ls_remote(
            "-q", "--symref", "--exit-code", name, "HEAD"
        )
        for line in output.splitlines():
            lhs, rhs = line.split("\t", 1)
            if rhs == "HEAD" and lhs.startswith("ref:"):
                return lhs[4:].strip()
        return None

thep0y avatar Oct 06 '23 13:10 thep0y

I pasted it into the editor and the tabs were retained. I then saved the file, and the tabs were retained. I then ran "format the file for black" and the tabs changed to spaces.

https://github.com/thep0y/python-black/assets/308610/e29bcfd4-bc0e-4173-8d4c-c92d09733936

Were you unable to replicate this behavior in your environment?

jaraco avatar Oct 06 '23 14:10 jaraco

The issue of spaces replacing tabs occurs when pasting twice in a new file.

It means Sublime Text may have a bug or is designed deliberately this way.

https://github.com/thep0y/python-black/assets/51874567/3f2edd18-fac9-400c-9c1e-67b227eecb47

thep0y avatar Oct 06 '23 14:10 thep0y

Oh, interesting. I think there's a hint in the gutter when pasting the first time. I see "Detect indentation: setting indentation to 4 spaces". I guess the first paste goes in plain, but thereafter, the editor detects the indentation and switches to spaces and thereafter does replacements. That's really annoying and especially annoying that it affects a plugin's attempt to set content.

jaraco avatar Oct 06 '23 15:10 jaraco

Upstream, the project recommends to set convert_tabs_to_spaces to false while pasting raw content. Can you consider that suggestion for this plugin to address this issue?

jaraco avatar Oct 23 '23 16:10 jaraco