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

Add handling of cursor(+-<num>) for row and column #2

Merged
merged 2 commits into from
Sep 30, 2020

Conversation

anihm136
Copy link
Contributor

Attempt to handle 'cursor[+-] as the argument for row(line) and column. In the screenshot, cursor is in the first row and first column
image

@anihm136
Copy link
Contributor Author

I'm not very familiar with lua, so do let me know if something can be done better. I'm also planning to try out some more of the pending implementations

@@ -94,7 +94,18 @@ function popup.create(what, vim_options)

if vim_options.line then
-- TODO: Need to handle "cursor", "cursor+1", ...
win_opts.row = vim_options.line
if type(vim_options.line) == 'string' then
assert(string.find(vim_options.line,'^cursor') ~= nil, "Invalid option")
Copy link
Member

Choose a reason for hiding this comment

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

You can remove the ~= nil part of the assertion. Just assert(string.find(...), is enough.

win_opts.row = vim_options.line
if type(vim_options.line) == 'string' then
assert(string.find(vim_options.line,'^cursor') ~= nil, "Invalid option")
local line = vim.fn.getcurpos()[2]
Copy link
Member

Choose a reason for hiding this comment

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

In general, I think we should try and use vim.api for these things.

Would something like vim.api.nvim_win_get_cursor(0) work?

assert(string.find(vim_options.line,'^cursor') ~= nil, "Invalid option")
local line = vim.fn.getcurpos()[2]
if (vim_options.line):match "cursor%+(%d+)" then
line = line + tonumber((vim_options.line):match "cursor%+(%d+)")
Copy link
Member

Choose a reason for hiding this comment

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

Can we write a util function that does transformation of cursor+1 -> 1 so that we can re-use it? Or just a local function that we re-use.

@tjdevries
Copy link
Member

Oh! I just remember. nvim_open_win actually accepts a relative parameter. Maybe we can set that to cursor and use that instead? That may make some of it a bit easier to do.

@tjdevries
Copy link
Member

(and btw, would love to see more of the features implemented. Happy to review any PRs that you work on).

Great work!

@anihm136
Copy link
Contributor Author

anihm136 commented Sep 26, 2020

Oh! I just remember. nvim_open_win actually accepts a relative parameter. Maybe we can set that to cursor and use that instead? That may make some of it a bit easier to do.

This actually works better, since the window is created with the cursor at top-left by default. With the old implementation it was top right I think, not sure why

Move common code to get cursore-relative position to a local function.
Also use relative='cursor' for window options instead of computing
relative to editor
@anihm136
Copy link
Contributor Author

@tjdevries I think this is ready, could you take a look?

@tjdevries tjdevries merged commit 732ab8e into nvim-lua:master Sep 30, 2020
@tjdevries
Copy link
Member

Awesome! Thanks :) Let me know if there's something in particular you want to work and/or need help.

@anihm136 anihm136 deleted the cursor_position branch September 30, 2020 03:46
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.

2 participants