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

Handling cases when cursor is not at the end of line #36

Closed
simonmandlik opened this issue Apr 24, 2024 · 3 comments
Closed

Handling cases when cursor is not at the end of line #36

simonmandlik opened this issue Apr 24, 2024 · 3 comments

Comments

@simonmandlik
Copy link
Contributor

Is your feature request related to a problem? Please describe.
When working on #35, I struggled a lot when dealing with cases when the cursor is not at the very end of the line.

Pseudocode examples:

function f(<cursor>)

leads to

function f(
    <cursor>
end)

or for example

if f(<cursor>)

leads to

if f(
    <cursor>
)

I guess this behavior applies to multiple languages, not only julia. For example, try the following test for lua:

test "lua, function", <<~END
-function foo(█)
+function foo(
+  )
END

Maybe I'm missing something, but writing this in TS queries seems quite difficult — I have to always capture the last ")", which leads to many corner cases and queries become large.

Describe the solution you'd like

I have only written some queries and I don't know anything about internals of this plugin. What is the preferred way to deal with that?

What if the following heuristic was used: Whenever on the current line there still is a non-whitespace character after the cursor, do not add an "end"? Is there any case where this would be undesirable?

Thanks!

@simonmandlik simonmandlik changed the title Handling Handling cases when cursor is not at the end of line Apr 24, 2024
@RRethy
Copy link
Owner

RRethy commented Apr 24, 2024

What if the following heuristic was used: Whenever on the current line there still is a non-whitespace character after the cursor, do not add an "end"? Is there any case where this would be undesirable?

Yes it would be undesirable. I don't want to not add an end when it's needed. We have the heuristic that if the text that follows the cursor is alphanumeric, then put it on the line of the cursor, if it's non-alphanumeric then put it on the line of the end. It's especially useful to handling functions defined as arguments.

The code for this is at https://github.com/RRethy/nvim-treesitter-endwise/blob/master/lua/nvim-treesitter/endwise.lua#L81.

This test shows what happens, https://github.com/RRethy/nvim-treesitter-endwise/blob/master/tests/endwise/lua.rb#L185-L189.

@RRethy RRethy closed this as completed Apr 24, 2024
@RRethy
Copy link
Owner

RRethy commented Apr 24, 2024

For what it's worth, there is no perfect solution for this since we don't get a full syntax tree, we have to interpret a broken syntax tree, these are tradeoffs I made a long time ago and honestly aren't going to change.

@simonmandlik
Copy link
Contributor Author

We have the heuristic that if the text that follows the cursor is alphanumeric, then put it on the line of the cursor, if it's non-alphanumeric then put it on the line of the end. It's especially useful to handling functions defined as arguments.

Yes, I have noticed. It works great in cases like

function f(a, <cursor>b)

but not when it is in front of "," or ")".

Sow how about adding these some punctuation mark characters to the regexp here https://github.com/RRethy/nvim-treesitter-endwise/blob/master/lua/nvim-treesitter/endwise.lua#L81?

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

No branches or pull requests

2 participants