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

Do not select the extension when refactoring file names #3552

Merged
merged 4 commits into from
May 27, 2024

Conversation

jojo2357
Copy link
Contributor

Fix #3551

So. The simplest solution was to simply make a custom Rename Processor that would gobble up ALL PsiFiles and automatically null the editor. This has the side effect of making every file refactor not select the extension by default, which covers my issue, but may cause problems?

I tested, every \input will have this behavior (which IMO is just another manifestation of this issue), refactoring commands from \newcommand is unaffected.

Attempting to refactor a package does not work.

Let me know if we need to go ahead and actually implement more logic. If anything, this opens the door to smarter refactoring?

\includegraphics{IM<caret>G.jpg}

If I had a dollar for everytime i shoulda hit the spacebar but didnt, id have two dollars
@PHPirates PHPirates changed the title The simplest solution Do not select the extension when refactoring file names May 17, 2024
Copy link
Collaborator

@PHPirates PHPirates left a comment

Choose a reason for hiding this comment

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

Good find! I think to be on the safe side, we should indeed do some smarter refactoring to only not select the text after the dot if we know the command is referencing a file name, so something like this:

    override fun createRenameDialog(project: Project, element: PsiElement, nameSuggestionContext: PsiElement?, editor: Editor?): RenameDialog {
        return object : PsiFileRenameDialog(project, element, nameSuggestionContext, editor) {
            override fun createNewNameComponent() {
                super.createNewNameComponent()
                // if psi element references a file name:
                nameSuggestionsField.selectNameWithoutExtension()
            }
        }
    }

@jojo2357
Copy link
Contributor Author

override fun canProcessElement(element: PsiElement): Boolean {
    return when (element) {
        is PsiFile -> true
        else -> true
    }
}

but it only runs on Files, and is only a rename refactorer, what other cases are there?

@PHPirates
Copy link
Collaborator

I don't understand that code snippet, it's just return true?
In general, even though it might not have any (known) side effects at the moment, it might change in the future so I would prefer the more restricted code of only setting the editor to null if we are renaming a file name and not something else.

@jojo2357
Copy link
Contributor Author

Oops, dont know how i missed that. ofc the else should be false 😑

@PHPirates
Copy link
Collaborator

Ah okay, then yes if you set editor to null only if the psi element is a PsiFile, if that works then I can agree to that.

@jojo2357
Copy link
Contributor Author

I checked and it still works with the else branch being false.

Ready to go

@PHPirates PHPirates added this to the Next milestone May 27, 2024
Copy link
Collaborator

@PHPirates PHPirates left a comment

Choose a reason for hiding this comment

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

Thanks! I just added a comment to explain

@PHPirates PHPirates enabled auto-merge May 27, 2024 18:46
@PHPirates PHPirates merged commit 16d6418 into Hannah-Sten:master May 27, 2024
9 checks passed
@jojo2357 jojo2357 deleted the refactoring-name branch May 27, 2024 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactoring Image Name can delete extension
2 participants