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

nvim_win_get_config returns table value for row and col instead of number #24430

Closed
mtbrock opened this issue Jul 22, 2023 · 6 comments
Closed
Labels
api libnvim, Nvim RPC API lua stdlib

Comments

@mtbrock
Copy link

mtbrock commented Jul 22, 2023

Problem

vim.api.nvim_win_get_config returns table values for row and col:

{
  anchor = "NW",
  col = {
    [false] = 10,
    [true] = 3
  },
  external = false,
  focusable = true,
  height = 20,
  relative = "editor",
  row = {
    [false] = 10,
    [true] = 3
  },
  width = 20,
  zindex = 50
}

You have to access row[false] and col[false] to get the real value.

Steps to reproduce

Write to file:

-- config_test.lua
local bid = vim.api.nvim_create_buf(true, false)
local wid = vim.api.nvim_open_win(bid, true, {
    relative = 'editor',
    width = 20,
    height = 20,
    row = 10,
    col = 10,
})

local config = vim.api.nvim_win_get_config(wid)
print(vim.inspect(config))

Run the file:

nvim -u NORC -c 'luafile %' config_test.lua

Expected behavior

I expect row and col to be numbers, not tables.

Neovim version (nvim -v)

0.9.1

Vim (not Nvim) behaves the same?

n/a

Operating system/version

Arch Linux (kernel version 6.4.4-arch1-1)

Terminal name/version

kitty 0.28.1

$TERM environment variable

tmux-256color

Installation

Arch Extra repo

@mtbrock mtbrock added the bug issues reporting wrong behavior label Jul 22, 2023
@clason clason added api libnvim, Nvim RPC API lua stdlib labels Jul 22, 2023
@clason

This comment was marked as off-topic.

@seandewar
Copy link
Member

seandewar commented Jul 22, 2023

The row and col values are fractional, but Lua numbers with integer values are normally converted to Vim script numbers (integers). The [true]=3 field ([vim.type_idx]=vim.types.float) is there to preserve the fact that it should be converted to a float if sent to Vim script.

As a Vim script number would still be accepted as a float value in the API, preserving this information doesn't seem too useful here?

@zeertzjq zeertzjq removed the bug issues reporting wrong behavior label Jul 24, 2023
@zeertzjq
Copy link
Member

zeertzjq commented Jul 24, 2023

I don't think this behavior should be changed. The same behavior also applies to other functions like nvim_eval and nvim_get_var, where it is actually needed to distinguish a Float from a Number, and there is no reason to have a special case for nvim_win_get_config.

See also :h lua-special-tbl

                                                              *lua-special-tbl*
4. Table with `vim.type_idx` key may be a dictionary, a list or floating-point
   value:
   - `{[vim.type_idx]=vim.types.float, [vim.val_idx]=1}` is converted to
     a floating-point 1.0. Note that by default integral Lua numbers are
     converted to |Number|s, non-integral are converted to |Float|s. This
     variant allows integral |Float|s.
   - `{[vim.type_idx]=vim.types.dictionary}` is converted to an empty
     dictionary, `{[vim.type_idx]=vim.types.dictionary, [42]=1, a=2}` is
     converted to a dictionary `{'a': 42}`: non-string keys are ignored.
     Without `vim.type_idx` key tables with keys not fitting in 1., 2. or 3.
     are errors.
   - `{[vim.type_idx]=vim.types.array}` is converted to an empty list. As well
     as `{[vim.type_idx]=vim.types.array, [42]=1}`: integral keys that do not
     form a 1-step sequence from 1 to N are ignored, as well as all
     non-integral keys.

@zeertzjq zeertzjq closed this as not planned Won't fix, can't repro, duplicate, stale Jul 24, 2023
@zeertzjq zeertzjq added the closed:wontfix current behavior is by design, and change is not desired label Jul 24, 2023
@mikesmithgh
Copy link
Sponsor Contributor

Thank you for the comments. It took me a couple times of reading this and the docs to understand what is going on so I'll put my understanding here in case it helps anyone else who comes across this in the future.

Given the result:

  col = {
    [false] = 10,
    [true] = 3
  },

Reading help example :h lua-special-tbl

{[vim.type_idx]=vim.types.float, [vim.val_idx]=1}` is converted to
     a floating-point 1.0. 

This looks confusing at first, but those are just variables.

  • :=vim.type_idx = true
  • :=vim.types.float = 3
  • :=vim.val_idx = false

So, given a lua table with two elements that have the keys true and false, this is recognized as the special table with a value and type.

Back to the example,

  col = {              col = {                    col = {               
    [false] = 10,  ->    [vim.val_idx] = 10,  ->    [vim.val_idx] = 10,  
    [true] = 3     ->    [vim.type_idx] = 3   ->    [vim.type_idx] = vim.types.float   
  },                   },                         },                    

col is a special table, with a value of 10 and a type of float.

@roycrippen4
Copy link

For anyone else ending up here like I did. If you need the row, col values from vim.api.nvim_win_get_config(winnr) you would do the following:

local config = vim.api.nvim_win_get_config(winnr)
local row = config.row[vim.val_idx]
local col = config.col[vim.val_idx]

@zeertzjq
Copy link
Member

zeertzjq commented Feb 8, 2024

Fixed by #27284

@zeertzjq zeertzjq closed this as completed Feb 8, 2024
@zeertzjq zeertzjq removed the closed:wontfix current behavior is by design, and change is not desired label Feb 8, 2024
@neovim neovim locked as resolved and limited conversation to collaborators Feb 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api libnvim, Nvim RPC API lua stdlib
Projects
None yet
Development

No branches or pull requests

6 participants