Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Add preserve case option for buffer replace. References #165. #938

Merged
merged 3 commits into from
Mar 23, 2021

Conversation

paradoxxxzero
Copy link
Contributor

Description of the Change

This PR adds a preserveCaseOnReplace option (off by default) which keeps the replaced text case. For exemple, when replacing test with case:
test Test TEST will be replaced with: case Case CASE.

This is made by swapping the replacement right before putting it in the buffer in buffer-search.js. The preserveCase algorithm is trivial and defined in project/util.coffee.

This only works in buffer search. The project replace is done in atom core and should be addressed in a separate ticket.

Alternate Designs

One could have added a button for this option, but a plugin configuration was chosen since it concerns only replacing (not searching) and it might not be toggled very often.

Benefits

Huge benefits arise when replacing variables name that are in different case, for instance replacing in one pass client, getClient and MAX_CLIENT with person, getPerson, MAX_PERSON.

Possible Drawbacks

This adds a boolean check for each replace, low performance impact though.

Applicable Issues

#165

replacePattern;
const replacedText = this.editor.getTextInBufferRange(bufferRange)
let replacementText = findRegex ? replacedText.replace(findRegex, replacePattern) : replacePattern;
replacementText = preserveCaseOnReplace ? Util.preserveCase(replacementText, replacedText): replacementText
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice

else if reference is reference.toUpperCase()
text.toUpperCase()
else
text
Copy link
Contributor

@bmperrea bmperrea Aug 30, 2017

Choose a reason for hiding this comment

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

This seems like a difficult feature to design. You have implemented preservation of All CAPS, and of Proper case. I would have expected lower case to be preserved as well. It might be worth thinking through a few more cases, such as strings with chars other than spaces and letters (e.g. one could imaging replacing snake_case and SNAKE_CASE at the same time, one being recognizably lower case and the other upper). camelCase might be interesting too if both the find and replace had that form, although maybe now I'm getting too fancy. I would prefer to open this discussion a little bit, though, such as with an issue for this feature.

Copy link
Contributor

Choose a reason for hiding this comment

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

You have support for

  • Title Case
  • UPPER CASE

I suggest you add

  • lower case
  • Sentence case

These are the main ones in Wikipedia/Letter_case, and ones that can be recovered from lower case. Then the user can set the default (what they put in the replace box) to whatever mixed case they want otherwise. I realize not supporting "Sentence case" was by design, but I would expect it to keep that case. Thanks!

`);
});
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

well done

else if reference is reference.toUpperCase()
text.toUpperCase()
else
text
Copy link
Contributor

Choose a reason for hiding this comment

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

You have support for

  • Title Case
  • UPPER CASE

I suggest you add

  • lower case
  • Sentence case

These are the main ones in Wikipedia/Letter_case, and ones that can be recovered from lower case. Then the user can set the default (what they put in the replace box) to whatever mixed case they want otherwise. I realize not supporting "Sentence case" was by design, but I would expect it to keep that case. Thanks!

Distinguish between titleize and capitalize for lower case and Sentence
case support.
Change them to return strict cases. (Don't keep other uppercase
letters.)
Add a clearer test.
Copy link
Contributor

@bmperrea bmperrea left a comment

Choose a reason for hiding this comment

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

Thanks - very well done on the tests!

@paradoxxxzero
Copy link
Contributor Author

paradoxxxzero commented Aug 31, 2017

First, thanks for the code review!
Ok so now when replacing aAa bBb by xxX yYy, it goes like this:

aaa bbb -> xxx yyy  # lower case
Aaa bbb -> Xxx yyy  # Sentence case
aaa Bbb -> xxX yYy  # ? -> default
Aaa Bbb -> Xxx Yyy  # Title Case
AAA BBB -> XXX YYY  # UPPER CASE
aaA bbb -> xxX yYy  # ? -> default
aaa bbB -> xxX yYy  # ? -> default
aaA bbB -> xxX yYy  # ? -> default

Is that what you wanted?

Edit: Took too long to type this comment! Thanks for the approval.

@paradoxxxzero
Copy link
Contributor Author

Is there anything else that I could do to make it hit a release?

@Le-Stagiaire
Copy link

Le-Stagiaire commented Dec 13, 2017

Aaaah that's what I was looking for ! Hope to see it in the next release.
Shouldn't it be merged already though ?

@jedwards1211
Copy link
Contributor

jedwards1211 commented Aug 14, 2018

I made a PR that supports a wider range of cases like snake case, camel case, and kebab case, using spaces, dashes, underscores, or camel casing as word boundaries during conversion. Could we close this in favor of it? #1041

Mine also adds a toggle button because there really needs to be a toggle button. Multiple other editors use a toggle for preserve case, like WebStorm and Sublime.

@darangi
Copy link
Contributor

darangi commented Mar 23, 2021

Thanks @paradoxxxzero

@darangi darangi merged commit 65bf3e6 into atom:master Mar 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants