[SOLVED] How to get current symbol under the cursor

I am trying to port over the vim-haredoc plugin for the hare programming language

I was wondering if there is a way to get the current symbol under the cursor in kakoune for example if I have

    fmt::println(color::underline(color::red("Hello, world!")))!;

and I have my cursor under the ‘l’ un underline I want to be able to get ‘color::underline’
but If I have the cursor under ‘o’ in color I just want to get ‘color’

same thing for use statements (basically imports)

use::fmt;
use::fmt::println;

I want to get fmt::println If I my cursor is under the println or fmt If under the fmt
I have tried to come up with a regex but that hasn’t worked out well and for shell scripts I am not very good at those only for basic tasks

I have tried selecting the whole line but I want to move the user’s cursor back to its original position instead of selecting it then the cursor is at the end of the line?

How would one solve this using a regex, or some shell scripting?

You could do this using select inner object command(<a-i>).

I found two ways how to achive this. First option is to add : as extra character that may be part of a “word” - pretty much meaning symbol. Though note that this has the side-effect that it will also affect word navigation commands(w, e, b etc). All you need to do is use:

set-option -add buffer extra_word_chars :

After that you can select the current symbol using key sequence:

<a-i>w

Second option is to use the select inner custom object. To do that you can use key sequence:

<a-i>c (^|[^\w:]),[^\w:] <ret>

where (^|[^\w:]) is the object opening regex which matches either beginning of line or any character that isn’t alphanumeric or : and where [^\w:] is object closing regex which matches any character that isn’t alphanumeric or :.

You may add this mapping to the object selection keymap, so that you don’t have to type out the whole regex every time you want to select the symbol:

map -docstring symbol buffer object S c(^|[^\w:]),[^\w:]<ret>

Now you can use key sequence:

<a-i>S

Edit:
I didn’t notice the requirement for difference between namespace selection and namespace member selection. Select inner word doesn’t seem to work for that. Instead you may try this kind of key sequence:

/\w(?=[^\w])<ret> <a-s-/>(^|(?<=[^\w:])\w)<ret>

It works by first searching forwards for regex \w(?=[^\w]) which stands for “any alphanumeric character which is followed by any non-alphanumeric character”. Then it extends selection by searching backwards for regex (^|(?<=[^\w:])\w) which stands for “either beginning of line or any alphanumeric character that is not preceded by either alphanumeric or : character”. Note that I intentionally excluded : when searching forward, so that it doesn’t cross symbol namespace boundary, but allows it to cross it when looking for beginning of symbol.

1 Like

Thanks! This works very well, now I just need to do a little shell scripting to make an info box but that’s easy.