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

refactor(char_classing): use inbuilt vim functions #53

Merged
merged 11 commits into from
Jun 1, 2024

Conversation

josh-nz
Copy link
Contributor

@josh-nz josh-nz commented Jun 1, 2024

This closes #50.

This took me quite a while to follow through. In short, it's hitting the while loop one additional time because of the utils.char_class falling through to the default value of other which is the same class as the initial match. So the resulting offset is one less than it should be, resulting in a column of zero instead of the correct 1.

The docs for vim.fn.strcharpart say it returns empty string on error, which is what is happening here when the index is outside the line. It made the most sense to add a guard for this edge case so I introduced an additional char_classes enum member.

At least, this is how it was working until the simultaneous change to use the FFI in #49. The tests still pass, things still look to work, my issue remains fixed, but this might want a slightly more detailed review due to the FFI changes.

Edit: I also added a missed test for the emoji char_classes member from #49, in commit bf4df53

@josh-nz josh-nz changed the title Bug/big b start of line bug: big b start of line Jun 1, 2024
@willothy
Copy link
Collaborator

willothy commented Jun 1, 2024

Awesome, thanks! Sorry, I maybe should've waited to merge the FFI branch.

@josh-nz
Copy link
Contributor Author

josh-nz commented Jun 1, 2024

No worries, I didn't post the bug until after I had a solution. I should have posted it sooner and mentioned I was looking into it.

I did test the FFI changes on their own without the fix here, and they didn't solve this issue alone.

willothy
willothy previously approved these changes Jun 1, 2024
Copy link
Collaborator

@willothy willothy left a comment

Choose a reason for hiding this comment

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

Nice, this looks good to me! I'll let @tris203 look over it before we merge though :)

@tris203
Copy link
Owner

tris203 commented Jun 1, 2024

I'd have to look at the other code

But can we fix at the other side with a <= or something?

The char class function is basically just the cls function from Nvim/Vim and id like to keep it as close to that as possible

My preference would be to fix the prev_word implementation to exit early and add an assert in char_class that the string isnt empty

If this is possible

https://github.com/neovim/neovim/blob/master/src%2Fnvim%2Ftextobject.c#L273-L306

On my phone, so apologies for formatting

@josh-nz
Copy link
Contributor Author

josh-nz commented Jun 1, 2024

I'll take a look, should be able to move the empty string check I think. I wasn't aware of the Vim cls stuff.

@josh-nz
Copy link
Contributor Author

josh-nz commented Jun 1, 2024

The above changes appear to work for the issue I logged, and all tests pass.

If I add assert(char ~= "") to the char_class function, a number of tests involving whitespace and padding fail. I'm not sure if this is because they were originally passing incorrectly and this change has revealed a source of issues, or if this assertion has legitimately broken things.

Currently this assertion has not been committed.

@josh-nz
Copy link
Contributor Author

josh-nz commented Jun 1, 2024

Upon initial investigation, it looks like there are a number of additional places that would need to handle the empty string case since it is no longer part of the char_class function; every call to this function would need to be guarded against empty string. It looks like there 13 or so calls to this function (outside of tests), which really adds a lot of extra logic everywhere.

I wonder if it's best to disregard adding an empty string assertion in char_class since things look to be working ok without it. It might be more effort to support it than is worthwhile.

@tris203
Copy link
Owner

tris203 commented Jun 1, 2024

I don't know if we need the guard and it might be that the offset ranges in the while loop need to be modified to be non inclusive

@tris203 tris203 changed the title bug: big b start of line fix: empty strings on overlap of char_class Jun 1, 2024
@tris203 tris203 force-pushed the bug/big-b-start-of-line branch from 7e8b682 to a59896b Compare June 1, 2024 12:30
@tris203
Copy link
Owner

tris203 commented Jun 1, 2024

I think we need to make emojis return 3 for this to be complete,

image
as in this example the b is under the p not the cloud

@tris203 tris203 changed the title fix: empty strings on overlap of char_class refactor(char_classing): use inbuilt vim functions for more accuracy Jun 1, 2024
@tris203 tris203 requested a review from willothy June 1, 2024 16:00
@tris203 tris203 dismissed willothy’s stale review June 1, 2024 16:00

large change

@tris203
Copy link
Owner

tris203 commented Jun 1, 2024

this turned into a bigger refactor of how we class chars but this should have much better support for different langs and locales

ffi was short lived, as there is a vim function for it directly

@willothy any thoughts?
@josh-nz thanks for raising the right questions for this!

@tris203 tris203 requested review from willothy and removed request for willothy June 1, 2024 17:46
@tris203 tris203 added this to the 1.0.0 milestone Jun 1, 2024
Copy link
Collaborator

@willothy willothy left a comment

Choose a reason for hiding this comment

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

Just these (other isn't used anywhere anymore), then looks good to me!

lua/precognition/utils.lua Show resolved Hide resolved
tests/precognition/char_spec.lua Show resolved Hide resolved
Copy link
Collaborator

@willothy willothy left a comment

Choose a reason for hiding this comment

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

lgtm! sorry for confusion

@tris203 tris203 changed the title refactor(char_classing): use inbuilt vim functions for more accuracy refactor(char_classing): use inbuilt vim functions Jun 1, 2024
@tris203 tris203 merged commit dbf9a68 into tris203:main Jun 1, 2024
9 checks passed
@josh-nz josh-nz deleted the bug/big-b-start-of-line branch June 2, 2024 01:06
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.

bug: B hint not showing at start of line
3 participants