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

feat(extras): Insert repeat node when duplicated keys occur #665

Merged
merged 4 commits into from
Dec 1, 2022
Merged

Conversation

uyha
Copy link
Contributor

@uyha uyha commented Nov 19, 2022

Experimental implementation for #465

@uyha uyha changed the title Insert repeat node when duplicated keys occur feat(extras): Insert repeat node when duplicated keys occur Nov 20, 2022
@L3MON4D3
Copy link
Owner

Nice so far 👍
I'd like to see the access to node.pos wrapped in a function, like node.get_jump_indx. (I think this should make clear that the jump-index may not be changed)

@uyha
Copy link
Contributor Author

uyha commented Nov 20, 2022

Hmmm, isn't it better to stick with pos for now and check if pos is nil or not to decide between copy3 and rp? or do you have in mind that get_jump_index can be used in other use cases?

@L3MON4D3
Copy link
Owner

Hmmm, isn't it better to stick with pos for now and check if pos is nil or not to decide between copy3 and rp?

Oh, yeah that's what I also had in mind, I only think we should wrap the access to pos in a function (so the nodes implement a new function node:get_jump_indx() which returns node.pos)
Both because pos isn't very informative (could be the jump-index, or the position in the buffer, or the position of the node in its parent), and because it feels like a function is the nicer interface.

do you have in mind that get_jump_index can be used in other use cases?

Not really, but I'd like the extras to not rely on any internals of luasnip, only public API.

@uyha
Copy link
Contributor Author

uyha commented Nov 20, 2022

I added the function, can you have another look?

@L3MON4D3
Copy link
Owner

Yup 👍
There's one issue with the tests: they expect numbers (for simplicity, and because it used to be pretty generic), but now we need to make it work with nodes (or objects that pretend to be nodes, eg. implement exactly the functionality we need in interpolate).
I think the best way to fix that is just mocking the nodes, and adding a __tostring-metamethod to still get the expected strings from table.concat.

Aside from that, just docs are left, and some tests for the new functionality.
Are you familliar with neovim's test-framework?

@uyha
Copy link
Contributor Author

uyha commented Nov 20, 2022

I am not familiar with it, but I will take a look.

@uyha
Copy link
Contributor Author

uyha commented Nov 24, 2022

so, i am getting to mocking the nodes, but i am not sure how to actually do it since it requires the node definition to be global and I am not sure how to proceed with that.

@L3MON4D3
Copy link
Owner

Oh, I think it's enought to only mock the functions we actually need (so just get_jump_indx, and I think that can just always return nil).
Since those tests are mainly about making sure the interpolation is correct, we only need to simulate nodes so far that it actually works, but no further.

For actual tests of the functionality, I'd say we can test it in a real setting, I feel like that's easiest to write tests for too (use screen:snapshot_util() to generate a capture of the tested neovim-instance, and test it via screen:expect (bunch of examples for that in tests/integration))

@uyha
Copy link
Contributor Author

uyha commented Nov 25, 2022

but the problem i have here is not knowing how to pass a mock object to the works call, I tried putting

local FakeNode = {}
function fake_node() end

at the beginning of fmt_spec.lua and have a call like

works(“”, “{a}{a}”, “{a = fake_node()}”, “”, “{repeat_duplicates = true}”)

I know that it shouldn’t work but for the reason of the test case being wrong, here the test case fails due to fake_node is not visible to the caller.

@L3MON4D3
Copy link
Owner

Ahhh, you can run code in the tested neovim-instance via exec_lua, defining fakeNode with that should work

@uyha
Copy link
Contributor Author

uyha commented Nov 26, 2022

I added a test, it is messy but I want to see if the direction is correct first. Also, it is a bit weird since tests like this won't tell if there repeat is being used or not.

@L3MON4D3
Copy link
Owner

That test looks good👍👍
Can you make it generic, so the existing tests can be re-used? (ie put part of the new test into the works-function)

Also, it is a bit weird since tests like this won't tell if there repeat is being used or not

Yeah, it's unfortunate that the current tests for just the interpolation rely on this function handling objects other than nodes :/
The actual new tests for this feature should probably be integration-tests (so create a snippet with repeated nodes, expand it, and check that everything works as expexted), at least that's how I like to test most features.

@uyha
Copy link
Contributor Author

uyha commented Nov 27, 2022

I generalize the works function, but there is a problem. Since interpolate's result will have a repeat node which doesn't have a __tostring, so it can't be stringified which leads to the test failing. Any idea how i should proceed?

@uyha
Copy link
Contributor Author

uyha commented Nov 27, 2022

The previous code works because I have a bug in the mock table btw

@L3MON4D3
Copy link
Owner

I generalize the works function, but there is a problem. Since interpolate's result will have a repeat node which doesn't have a __tostring, so it can't be stringified which leads to the test failing. Any idea how i should proceed?

Turn of repeat-insertion for those tests? That should be fine as long as we check it elsewhere

@uyha
Copy link
Contributor Author

uyha commented Nov 27, 2022

I am not sure what you mean by turning off repeat-insert here

@L3MON4D3
Copy link
Owner

Ah, repeat_duplicates to false

@uyha
Copy link
Contributor Author

uyha commented Nov 27, 2022

oh, ok

@L3MON4D3
Copy link
Owner

I took the liberty of implementing my idea of handling the existing tests (figured explaining would take longer than quickly handling it myself 😅) WDYT?

@uyha
Copy link
Contributor Author

uyha commented Nov 27, 2022

I was trying to make it as general as possible since I am not sure how you want it, but this is simpler in my opinion. So, I should move on to create the integration test, right?

@L3MON4D3
Copy link
Owner

Ah, I might've been a bit misleading then, I only wanted to re-use those tests (but they're also more readable now, with the explicit argument-names, so that's nice :D)

Umm yeah, integration tests sound good👍👍

@L3MON4D3
Copy link
Owner

Oh great idea to forward TEST_FILE 👍

@uyha
Copy link
Contributor Author

uyha commented Nov 28, 2022

yeah, it was annoying having to run the whole test set everytime

@uyha
Copy link
Contributor Author

uyha commented Nov 29, 2022

I added 2 tests, 1 one for fmt and 1 for rep, both of them are failing but I'm not sure why since testing on real nvim instance works fine

@L3MON4D3
Copy link
Owner

Ah, screen:expect is very particular about the text it receives :/
Try formatting the text there like in the passing tests (especially whitespace!)

@uyha
Copy link
Contributor Author

uyha commented Nov 30, 2022

that’s not really the problem, the problem is the repeating text doesn’t appear at all in the actual portion of the screen 😅

@uyha
Copy link
Contributor Author

uyha commented Nov 30, 2022

Expected:
  |*asdf^ repeat asdf                                  |
  |{0:~                                                 }|
  |{2:-- INSERT --}                                      |
Actual:
  |*asdf^ repeat                                       |
  |{0:~                                                 }|
  |{2:-- INSERT --}                                      |

@L3MON4D3
Copy link
Owner

Ahhh xD
Okay that's probably due to updateevents being different, try jumping after feeding the text (jump always causes an update)

@uyha
Copy link
Contributor Author

uyha commented Nov 30, 2022

eh, how do you do a jump in this case 😅?

@L3MON4D3
Copy link
Owner

Oh, manually, binding some keys to jump is not necessary: exec_lua(ls.jump())

@uyha uyha marked this pull request as ready for review November 30, 2022 20:40
@uyha
Copy link
Contributor Author

uyha commented Nov 30, 2022

I think it is done now, could you have another look?

DOC.md Show resolved Hide resolved
lua/luasnip/extras/fmt.lua Outdated Show resolved Hide resolved
@L3MON4D3
Copy link
Owner

L3MON4D3 commented Dec 1, 2022

Nice nice nice, this is it, great work!! :D
Would you squash all of this into two commits, one for the fmt-stuff, one for the makefile?

@uyha
Copy link
Contributor Author

uyha commented Dec 1, 2022

sure

@uyha
Copy link
Contributor Author

uyha commented Dec 1, 2022

can you have a look again?

@uyha
Copy link
Contributor Author

uyha commented Dec 1, 2022

welp, looks like either github action's internet is down or neovim-ppa is down

@L3MON4D3
Copy link
Owner

L3MON4D3 commented Dec 1, 2022

off and on again did it 👍

@uyha
Copy link
Contributor Author

uyha commented Dec 1, 2022

hmmm, it seems like i messed up the commits, i will fix it

@L3MON4D3
Copy link
Owner

L3MON4D3 commented Dec 1, 2022

Ah my bad, I just noticed that get_jump_index should be officially exported -.-
Best add a subsection Api (like in Snippets below) here

@uyha uyha requested a review from L3MON4D3 December 1, 2022 19:45
@uyha
Copy link
Contributor Author

uyha commented Dec 1, 2022

everything should be good now, could you have another look?

@L3MON4D3
Copy link
Owner

L3MON4D3 commented Dec 1, 2022

Ohh right, just Api doesn't work, then that tag is duplicated.. Could you change the titles to Snippet-Api and Node-Api?

lua/luasnip/extras/fmt.lua Outdated Show resolved Hide resolved
tests/unit/fmt_spec.lua Outdated Show resolved Hide resolved
uyha and others added 3 commits December 1, 2022 22:30
…ously occured node instead of a copy of it

don't have implementation rely on tests/handle tests specially.

adapt existing tests.

Provide objects on which `get_jump_index` can be called, as opposed to
flat numbers.
@uyha
Copy link
Contributor Author

uyha commented Dec 1, 2022

looks like everything is in order

@L3MON4D3
Copy link
Owner

L3MON4D3 commented Dec 1, 2022

I agree 👍👍
Nice work, thank you very much for sticking through all of my demands 😅

@L3MON4D3 L3MON4D3 merged commit bc9ba28 into L3MON4D3:master Dec 1, 2022
@uyha
Copy link
Contributor Author

uyha commented Dec 2, 2022

not a problem, thank you very much for the plugin

@uyha uyha deleted the fmt branch December 2, 2022 08:06
@uyha uyha mentioned this pull request Dec 2, 2022
@danielo515
Copy link

This is awesome. Thanks

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.

3 participants