Simplest example of extmark usage?

Hi everyone! So, I failed to understand how you’d use extmarks. The only thing I know is you can set an extmark to an arbitrary position and it will get updated when the text has changed and you can get its position.

I keep seing it being mentioned as a snippet placeholder or semantic highlighting but I don’t understand how it could be useful for those. If anyone could explain why is that the case and give an example of extmark usage (it could be anything, not necessarily the two I mentioned previously) I’d highly appreciate that.

Thanks!

I use extmarks heavily in octo.nvim to delimit the Issue or PR comments. If the user changes the comment, Im still able to retrieve it by its extmarks. This way I can have multiple comments in the same buffer without mixing them

1 Like

As you mentioned extmarks are updated if the text changes. Snippet plugins can use this to figure out how the placeholders shifted.

For example, if you’ve a snippet like this:

for ${1:target_list} in ${2:expression_list}:
  ${3:pass}

The plugin can add extmarks to the placeholder regions:

for ${1:target_list} in ${2:expression_list}:
    ^^^^^^^^^^^^^^^^    ^^^^^^^^^^^^^^^^^^^^
  ${3:pass}
  ^^^^^^^^^

Now if the user replaces the first placeholder with some text, the second placeholder moves:

for foo in ${2:expression_list}:
    ^^^    ^^^^^^^^^^^^^^^^^^^^
  ${3:pass}
  ^^^^^^^^^

The extmarks shifted too and the plugin can use this to figure out the updated position of the placeholders

There’s another example how they can be used here: Need advice on how separate render the UI on lua plugin development - #2 by mfussenegger

1 Like

nice, I think I understand what it does and how it could be useful. Thanks everyone!