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

Inlay hints: attempt to index a nil value #81

Closed
monheimx9 opened this issue Aug 5, 2024 · 7 comments · Fixed by #82
Closed

Inlay hints: attempt to index a nil value #81

monheimx9 opened this issue Aug 5, 2024 · 7 comments · Fixed by #82

Comments

@monheimx9
Copy link

Hello tris :^)

Here's my report on an issue after the last plugin update (6772d3a)

Error  20:37:36 msg_show.lua_error Error detected while processing CursorMoved Autocommands for "*":
20:37:36 msg_show Error executing lua callback: ...e/nvim/lazy/precognition.nvim/lua/precognition/utils.lua:77: attempt to index a nil value
stack traceback:
	...e/nvim/lazy/precognition.nvim/lua/precognition/utils.lua:77: in function 'calc_ws_offset'
	...re/nvim/lazy/precognition.nvim/lua/precognition/init.lua:280: in function 'display_marks'
	...re/nvim/lazy/precognition.nvim/lua/precognition/init.lua:316: in function <...re/nvim/lazy/precognition.nvim/lua/precognition/init.lua:304>

It doesn't happen on every lines, I might be wrong but I think the error is triggered when there are more that two hint on the line, and they must be variable type hints, for some reason it's a method hint (i.e what the function is returning) it does not trigger the error

Example 1:
error_example
Example 2:
error_example2

I'm using NVIM v0.10.1
My distribution is LazyVim

And here's my precognition config

return {
  "tris203/precognition.nvim",
  opts = {
    highlightColor = { link = "String" },
    hints = { Caret = { text = "à", prio = 2 } },
  },
}

This might be related, sometimes the length is correctly calculated, sometimes it isn't

Example 1 (Almost got it):
hint_ex1
Example 2 (Got it perfectly):
hint_ex2
Example 3 (Way off):
hint_ex3
Example 4 (Got it perfectly):
hint_ex4

I don't really understand how Neovim plugins are architectured so it's difficult for me to point you in the right direction, yet don't hesitate to ask for more informations if needed

In the meantime, I wish you an excellent day

@tris203
Copy link
Owner

tris203 commented Aug 5, 2024

What language are you writing and which language server are you using for the hints?

Ps, I assume rust

Can you give me a minimal replication file?

@monheimx9
Copy link
Author

And you assumed right

 Language client log: /home/monheim/.local/state/nvim/lsp.log
 Detected filetype:   rust
 
 1 client(s) attached to this buffer: 
 
 Client: rust-analyzer (id: 1, bufnr: [12])
 	filetypes:       rust
 	autostart:       false
 	root directory:  /home/monheim/DocuDeux/dev/rust_proj/qr
 	cmd:             rust-analyzer --log-file /tmp/nvim.monheim/U8oXbw/0-rust-analyzer.log

As for the minimal replication file, I will create an online repo for this project
I was working offline on it, one of my PR hasn't been accepted yet on a certain dependency that I use and that's why I waited before the initial push

I'll keep you in touch tomorrow and give you the entire project so you don't have to deal with missing dependencies

@tris203
Copy link
Owner

tris203 commented Aug 5, 2024

If you can create a really simple reproduction that would make it easier for me.

I don't know if you can create a hello world-esque function that captures the error

@monheimx9
Copy link
Author

Sure I'll try some things and I'll get back to you

@monheimx9
Copy link
Author

I found something:

use std::collections::HashMap;

#[derive(Debug)]
enum Error {
    EmptyField(String),
    MissingField(String),
}

trait EmptyStringValue {
    ///Return an error if the value is empty
    fn try_get<'a>(&'a self, s: &str) -> Result<&'a str, Error>;
    ///Return an error if the value is empty
    fn try_get_owned(&self, s: &str) -> Result<String, Error>;
}

impl EmptyStringValue for HashMap<String, String> {
    fn try_get<'a>(&'a self, s: &str) -> Result<&'a str, Error> {
        if let Some(x) = self.get(s) {
            if !x.is_empty() {
                Ok(x.as_str())
            } else {
                Err(Error::EmptyField(s.into()))
            }
        } else {
            Err(Error::MissingField(s.into()))
        }
    }
    fn try_get_owned(&self, s: &str) -> Result<String, Error> {
        if let Some(x) = self.get(s) {
            if !x.is_empty() {
                Ok(x.into())
            } else {
                Err(Error::EmptyField(s.into()))
            }
        } else {
            Err(Error::MissingField(s.into()))
        }
    }
}

fn main() -> Result<(), Error> {
    println!("Hello, world!");

    let h: HashMap<String, String> = HashMap::new();

    let (a, b, c, d) = (
        h.try_get("a")?,
        h.try_get("b")?,
        h.try_get("c")?,
        h.try_get("d")?,
    );

    Ok(())
}

Error can be triggered on line 46, right on the let (a, b, c, d) = (

error_inlay_hint

@tris203
Copy link
Owner

tris203 commented Aug 5, 2024

Can you test with the branch in the linked PR please @monheimx9

As far as the spacing goes, I know what the issue is, but i need to think about it a little bit more

@monheimx9
Copy link
Author

fastfix
I wasn't able to trigger the error again :^)

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 a pull request may close this issue.

2 participants