Hi,

On Thu, Jan 21, 2021 at 1:50 AM lacygoill <[email protected]> wrote:

> Ideally it would be better if vim supports it by default similar to how
> there are already bunch of helpers for different style of popup.
>
> This would encourage more people to use the feature instead of having a
> custom one.
>
> Oh yes, I completely agree. Even if we can currently achieve something
> with builtin functions, a new helper function would make sense for this use
> case, similar to popup_atcursor()
> <https://vimhelp.org/popup.txt.html#popup_atcursor()>, popup_dialog()
> <https://vimhelp.org/popup.txt.html#popup_dialog()>, popup_menu()
> <https://vimhelp.org/popup.txt.html#popup_menu()>, and
> popup_notification()
> <https://vimhelp.org/popup.txt.html#popup_notification()>.
>

Agree. It makes sense to add a helper function for the virtual text. But
how will it handle
the listener callback (shown below) that updates the left padding of the
popup? If this is
not internally managed, then every plugin needs to add support for this.

Thanks,
Yegappan


> ------------------------------
>
> Unfortunately, it seems to be ignored (contrary to line which works as
> expected). It could be a Vim bug. I'll make more tests; if it is a bug,
> I'll report it.
>
> I was wrong, it's not ignored. But the code made it look like so, because
> it wrongly used pos: 'topright', which changed how col was interpreted.
> Once it's removed, the popups are correctly moved when we insert text
> before a text property. But not when we insert text afterward. For that, I
> think we need a listener callback to update the padding and mask options
> of the popups.
>
> Here is an updated version:
>
> vim9
>
>
> var lines: list<string> =<< trim END
>
>     I met a traveller from an antique land,
>
>     Who said—“Two vast and trunkless legs of stone
>
>     Stand in the desert. . . . Near them, on the sand,
>
>     Half sunk a shattered visage lies, whose frown,
>
>     And wrinkled lip, and sneer of cold command,
>
>     Tell that its sculptor well those passions read
>
>     Which yet survive, stamped on these lifeless things,
>
>     The hand that mocked them, and the heart that fed;
>
>     And on the pedestal, these words appear:
>
>     My name is Ozymandias, King of Kings;
>
>     Look on my Works, ye Mighty, and despair!
>
>     Nothing beside remains. Round the decay
>
>     Of that colossal Wreck, boundless and bare
>
>     The lone and level sands stretch far away.”
>
> END
> setline(1, lines)
>
>
> var lnum2popup_id: dict<dict<number>> = {}
>
>
> def AddVirtualText(props: dict<any>): number
>
>     var lnum: number = props.lnum
>
>     var col: number = props.col
>
>     var length: number = props.length
>
>     var text: string = props.text
>
>     var highlight_text: string = has_key(props, 'highlight_text')
>
>         ? props.highlight_text
>
>         : 'Normal'
>
>     var highlight_virtualtext: string = has_key(props, 
> 'highlight_virtualtext')
>
>         ? props.highlight_virtualtext
>
>         : 'Normal'
>
>     if has_key(props, 'highlight_virtualtext')
>
>         highlight_virtualtext = props.highlight_virtualtext
>
>     endif
>
>
>
>     var buf: number = bufnr('%')
>
>
>
>     # We need to make sure that only  1 listener is added per buffer.
>
>     # Otherwise,  there's the  risk  that  when you  insert  text  after a  
> text
>
>     # property, *all* popups are moved.
>
>     if !lnum2popup_id->has_key(buf)
>
>         listener_add(UpdatePadding, buf)
>
>     endif
>
>
>
>     if prop_type_list({bufnr: buf})->index('virtualText' .. lnum) == -1
>
>         prop_type_add('virtualText' .. lnum, {
>
>             bufnr: buf,
>
>             highlight: highlight_text
>
>             })
>
>     endif
>
>     if has_key(lnum2popup_id, buf) && has_key(lnum2popup_id[buf], lnum)
>
>         popup_close(lnum2popup_id[buf][lnum])
>
>     endif
>
>     prop_add(lnum, col, {
>
>         type: 'virtualText' .. lnum,
>
>         length: length,
>
>         bufnr: buf,
>
>         })
>
>
>
>     var left_padding: number = col([lnum, '$']) - length - col + 1
>
>
>
>     var popup_id: number = popup_create(text, {
>
>         line: -1,
>
>         padding: [0, 0, 0, left_padding],
>
>         mask: [[1, left_padding, 1, 1]],
>
>         textprop: 'virtualText' .. lnum,
>
>         highlight: highlight_virtualtext,
>
>         fixed: true,
>
>         wrap: false,
>
>         zindex: 50 - 1,
>
>         })
>
>     if !has_key(lnum2popup_id, buf)
>
>         extend(lnum2popup_id, {[buf->string()]: {}})
>
>     endif
>
>     extend(lnum2popup_id[buf], {[lnum->string()]: popup_id})
>
>
>
>     return popup_id
> enddef
>
>
> def UpdatePadding(
>
>     buffer: number,
>
>     start: number,
>
>     ...l: any
>
>     )
>
>     if start > line('$')
>
>         return
>
>     endif
>
>     var prop_list: list<dict<any>> = start->prop_list()
>
>     var i: number = prop_list->match('virtualText')
>
>     if i == -1
>
>         return
>
>     endif
>
>     var textprop: dict<any> = prop_list[i]
>
>     var left_padding: number = col([start, '$']) - textprop.length - 
> textprop.col + 1
>
>
>
>     if !has_key(lnum2popup_id, buffer) || !has_key(lnum2popup_id[buffer], 
> start)
>
>         return
>
>     endif
>
>     var popup_id: number = lnum2popup_id[buffer][start]
>
>     popup_setoptions(popup_id, {
>
>         padding: [0, 0, 0, left_padding],
>
>         mask: [[1, left_padding, 1, 1]]
>
>         })
> enddef
>
>
> var shattered_pos: list<number> = searchpos('shattered', 'n')
> AddVirtualText({
>
>     lnum: shattered_pos[0],
>
>     col: shattered_pos[1],
>
>     length: strchars('shattered'),
>
>     text: 'broken into many pieces',
>
>     highlight_text: 'Search',
>
>     highlight_virtualtext: 'MoreMsg',
>
>     })
>
>
> var sneer_pos: list<number> = searchpos('sneer', 'n')
> AddVirtualText({
>
>     lnum: sneer_pos[0],
>
>     col: sneer_pos[1],
>
>     length: strchars('sneer'),
>
>     text: 'a contemptuous or mocking smile, remark, or tone',
>
>     highlight_text: 'Search',
>
>     highlight_virtualtext: 'MoreMsg',
>
>     })
>
>
> var ozymandias_pos: list<number> = searchpos('Ozymandias', 'n')
> AddVirtualText({
>
>     lnum: ozymandias_pos[0],
>
>     col: ozymandias_pos[1],
>
>     length: strchars('Ozymandias'),
>
>     text: 'Greek name for Ramesses II, pharaoh of Egypt',
>
>     highlight_text: 'Search',
>
>     highlight_virtualtext: 'MoreMsg',
>
>     })
>
>
> var wreck_pos: list<number> = searchpos('Wreck', 'n')
> AddVirtualText({
>
>     lnum: wreck_pos[0],
>
>     col: wreck_pos[1],
>
>     length: strchars('Wreck'),
>
>     text: 'something that has been badly damaged or destroyed',
>
>     highlight_text: 'Search',
>
>     highlight_virtualtext: 'MoreMsg',
>
>     })
>
> [image: gif]
> <https://user-images.githubusercontent.com/8505073/105333437-03cfa180-5bd6-11eb-9de0-d7d6de2ff3c3.gif>
> ------------------------------
>
> If I find other issues, I'll try to fix them in this gist
> <https://gist.github.com/lacygoill/09da3dddea83e83bc15e6d9a9044bc95>.
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/vim/vim/issues/7553#issuecomment-764514812>, or
> unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ACY5DGDAGJWY4LXHV7BIIE3S272FDANCNFSM4VJQ2U6Q>
> .
>
> --
> --
> You received this message from the "vim_dev" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups
> "vim_dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/vim_dev/vim/vim/issues/7553/764514812%40github.com
> <https://groups.google.com/d/msgid/vim_dev/vim/vim/issues/7553/764514812%40github.com?utm_medium=email&utm_source=footer>
> .
>

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/CAAW7x7mFSkBEVMqrp8FfEc-PAWYScpV8sBBijCpGhcTwYdUwBg%40mail.gmail.com.

Raspunde prin e-mail lui