Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Improve renamed_tree method (Editor) #21696

Merged
merged 1 commit into from
Jan 21, 2024

Conversation

juliangilbey
Copy link
Contributor

Description of Changes

  • Wrote at least one-line docstrings (for any new functions)
  • Added unit test(s) covering the changes (if testable)
  • Included a screenshot or animation (if affecting the UI, see Licecap)

This follows the discussion in PR #21692 and fixes the issue identified there. Turns out that count=1 is not needed as the regex is anchored to the start of the string.

Affirmation

By submitting this Pull Request or typing my (user)name below,
I affirm the Developer Certificate of Origin
with respect to all commits and content included in this PR,
and understand I am releasing the same under Spyder's MIT (Expat) license.

I certify the above statement is true and correct: @juliangilbey

@juliangilbey
Copy link
Contributor Author

Ah, but this PR does assume that renamed_tree will always be called with absolute paths rather than relative paths. What if that is not the case? Could it ever be called with something like renamed_tree("test/dir1", "test/dir2") with relative paths?

@juliangilbey
Copy link
Contributor Author

Ah, but this PR does assume that renamed_tree will always be called with absolute paths rather than relative paths. What if that is not the case? Could it ever be called with something like renamed_tree("test/dir1", "test/dir2") with relative paths?

Looking through the code more carefully, I see that this should never be the case. So it turns out that this patch is probably not needed, but it does no harm. In fact, assuming that source and dest are always absolute paths, as are the filenames returned by self.get_filenames(), the whole function could be significantly simplified to:

    def renamed_tree(self, source, dest):
        """Directory was renamed in file explorer or in project explorer."""
        for fname in self.get_filenames():
            if fname.startswith(source):
                source_re = "^" + re.escape(source)
                dest_quoted = dest.replace("\\", r"\\")
                new_filename = re.sub(source_re, dest_quoted, fname)
                self.renamed(source=fname, dest=new_filename)

But I wouldn't suggest doing this, in case there are cases I've not spotted where this assumption is invalid. But if this patch is not applied, the test test_renamed_tree will need to be adapted to test Windows and POSIX platforms differently. This might not be such a silly thing to do, anyway, as the method is called with filenames that look different on these two platforms ("/test/dir" versus "C:\\test\\dir").

@ccordoba12 ccordoba12 changed the title Fix renamed_tree on Windows PR: Fix renamed_tree method on Windows (Editor) Jan 19, 2024
@ccordoba12 ccordoba12 added this to the v5.5.1 milestone Jan 19, 2024
@ccordoba12 ccordoba12 changed the title PR: Fix renamed_tree method on Windows (Editor) PR: Improve renamed_tree method on Windows (Editor) Jan 19, 2024
@ccordoba12 ccordoba12 changed the title PR: Improve renamed_tree method on Windows (Editor) PR: Improve renamed_tree method (Editor) Jan 19, 2024
@ccordoba12
Copy link
Member

ccordoba12 commented Jan 19, 2024

@juliangilbey, could you merge with 5.x or rebase on top of it to check if our test related to renamed_tree passes here after the changes you did to it in PR #21692? Thanks!

Copy link
Member

@ccordoba12 ccordoba12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @juliangilbey!

@ccordoba12 ccordoba12 merged commit 256de5f into spyder-ide:5.x Jan 21, 2024
22 checks passed
ccordoba12 added a commit that referenced this pull request Jan 21, 2024
@juliangilbey
Copy link
Contributor Author

Oh, I hadn't even had a chance to check if all the tests had passed!! Thanks for merging.

@juliangilbey juliangilbey deleted the fix-renamed_tree branch January 21, 2024 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants