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

Implement string-inflect-final-position #43

Merged
merged 8 commits into from
Aug 11, 2024

Conversation

johannes-mueller
Copy link
Contributor

Closes #42

This introduces string-inflect-final-position as proposed in #42. Along with this the behavior for (use-region-p) has been improved. When a region is active the inflection operation now does the following:

  • The operation is performed on all symbols in the region.
  • The region remains active after the inflection.
  • As the region remains active the -cycle- operations now make sense.

* New final point behavior when using a region
* The region remains active after the inflection.
  That means the -cycle- operations now actually work with regions.
@akicho8
Copy link
Owner

akicho8 commented Aug 11, 2024

Thank you for the pull request.

I set string-inflection-final-position to 'remain and have been using it for a bit.
However, I noticed an issue when editing a Ruby file. For example:

FOO_BAR
       ^---- cursor position

After calling string-inflection-ruby-style-cycle, the result is:

FooBar

^---- cursor position

The cursor ends up here, which interrupts continuous transformations.

The correct behavior should be:

FooBar
      ^---- cursor position

or

FooBar
     ^---- cursor position

This is the only part that concerns me.

@akicho8
Copy link
Owner

akicho8 commented Aug 11, 2024

I am trying to convert a full file path with underscore notation to a class name.
However, when I execute string-inflection-ruby-style-cycle with the cursor positioned as shown below, nothing happens.

path/to/foo_bar.rb
        ^---- cursor position

I am running this within org-mode. It works correctly in ruby-mode.

When the cursor is at the end of a shortened string and final pos is to
`'remain`, the cursor will be after the inflection one position too far

```
foo_bar foo
       ↕ cursor position
fooBar foo

```

Fix is to not always strictly remain but moving to the end of symbol when
necessary.
@johannes-mueller
Copy link
Contributor Author

FOO_BAR
       ^---- cursor position

After calling string-inflection-ruby-style-cycle, the result is:

FooBar

^---- cursor position

The cursor ends up here, which interrupts continuous transformations.

Thanks for the catch, nice find. The problem is that if the cursor is at the end of the inflected symbol and after the inflection the inflected string is shortened remaining means actually that we end up one char too far.

8ad5b9d fixes this.

@johannes-mueller
Copy link
Contributor Author

I am trying to convert a full file path with underscore notation to a class name. However, when I execute string-inflection-ruby-style-cycle with the cursor positioned as shown below, nothing happens.

path/to/foo_bar.rb
        ^---- cursor position

I am running this within org-mode. It works correctly in ruby-mode.

The problem here is that (symbol-at-point) finds path/to/foo_bar in org-mode and foo_bar in ruby-mode. In the org-mode case path/to/foo_bar neither matches string-inflection-underscore-p nor string-inflection-upcase-p, so the default (string-inflection-underscore is applied)

Thanks for the catch. I will look for a solution.

@akicho8
Copy link
Owner

akicho8 commented Aug 11, 2024

FOO_BAR
       ^---- cursor position

After calling string-inflection-ruby-style-cycle, the result is:

FooBar

^---- cursor position

The cursor ends up here, which interrupts continuous transformations.

Thanks for the catch, nice find. The problem is that if the cursor is at the end of the inflected symbol and after the inflection the inflected string is shortened remaining means actually that we end up one char too far.

8ad5b9d fixes this.

Thank you for the fix. It's working smoothly now.

@johannes-mueller
Copy link
Contributor Author

path/to/foo_bar.rb
        ^---- cursor position

This turns out to be way trickier. We could adjust the string-inflect-*-p functions so that they still match. I have briefly looked into this. While this seems doable, it would significantly complicate the regexes and sometimes even two matches would be required. That would probably introduce a whole bunch of new edge cases.

Moreover, in such a complex symbol there always rises the question, which kind of case "wins". Consider for example

some_path/to/MyProject/foo-bar.txt

Is it undescore, camel case or kebab case? And this can be taken further for programming modes when there is a symbol named someFunction_to_do_someThing.

I don't think we should introduce all that much complexity for these kind of edge cases, but I'd like to suggest a possible feature to work around that.

We could use a region not only to extend the operation space to multiple symbols but also to restrict it to a substring of a symbol

someFunction_to_do_someThing
^----------^
  region

would cycle to some_function_to_do_someThing, wheras

someFunction_to_do_someThing
             ^---^
             region

would cycle to someFunction_ToDo_someThing.

That would also work around the issue of path/to/foo_bar.rb as one could simple mark the foo_bar part.

@akicho8
Copy link
Owner

akicho8 commented Aug 11, 2024

path/to/foo_bar.rb
        ^---- cursor position

This turns out to be way trickier. We could adjust the string-inflect-*-p functions so that they still match. I have briefly looked into this. While this seems doable, it would significantly complicate the regexes and sometimes even two matches would be required. That would probably introduce a whole bunch of new edge cases.

Moreover, in such a complex symbol there always rises the question, which kind of case "wins". Consider for example

some_path/to/MyProject/foo-bar.txt

Is it undescore, camel case or kebab case? And this can be taken further for programming modes when there is a symbol named someFunction_to_do_someThing.

I don't think we should introduce all that much complexity for these kind of edge cases, but I'd like to suggest a possible feature to work around that.

We could use a region not only to extend the operation space to multiple symbols but also to restrict it to a substring of a symbol

someFunction_to_do_someThing
^----------^
  region

would cycle to some_function_to_do_someThing, wheras

someFunction_to_do_someThing
             ^---^
             region

would cycle to someFunction_ToDo_someThing.

That would also work around the issue of path/to/foo_bar.rb as one could simple mark the foo_bar part.

While the range selection might be slightly cumbersome, it also offers the advantage of partial conversion. Therefore, let's proceed with this method for selecting the range.

@johannes-mueller
Copy link
Contributor Author

Implemented the partial conversion behavior in 2612de8.

@akicho8
Copy link
Owner

akicho8 commented Aug 11, 2024

Thank you for the implementation.
The selected portions are being correctly converted.

@akicho8 akicho8 merged commit fa787ae into akicho8:master Aug 11, 2024
@johannes-mueller johannes-mueller deleted the remain-at-point branch August 12, 2024 06:43
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.

Proposal to allow to maintain the cursor position after inflection
2 participants