Description
Hi,
I ran into the following issue when working with the following python snippet (some simple plotting code):
{
"Matplotlib 3D surface": {
"prefix": ["surface", "meshgrid", "3d"],
"body": [
"from matplotlib import cm",
"${1:fig} = plt.figure()",
"${2:ax} = $1.add_subplot(111, projection='3d')",
"surf = $2.plot_surface(${3:X}, ${4:Y}, ${5:Z}, cmap=cm.plasma)",
"fig.colorbar(surf)",
"plt.show()",
"$0"
],
"description": "Plot a 3D surface in matplotlib"
}
}
Here are steps to reproduce the issue. I won't provide a minimal .vimrc
here because I think it will be clear, but please let me know if it would help and I can do so.
- Paste the snippet above in some empty
.py
file, e.g. withCTRL+J
in the suggested bindings - Navigate to the third placeholder e.g. with
TAB
in the default bindings - Attempt to type
X
,X[
,X[0
,X[0,
(in sequence). The idea is that I'm trying to index a row of a matrixX
. - After typing the
,
(comma), observe that vsnip exits the snippet context. It is no longer possible to navigate to other placeholders, edit them, etc.
After poking around in the source for a while, I seemed to pinpoint at least roughly where the issue is:
- The call to
Diff.compute
at https://github.com/hrsh7th/vim-vsnip/blob/master/autoload/vsnip/session.vim#L227 behaves somewhat oddly in this setting. The relevant portion of the string being typed, right before the comma is added, looks likeX[0,
, because there is a comma in the snippet following the third placeholder$3
. After the comma is typed, the new buffer content isX[0,,
. Here,Diff.compute
will return the index of the second comma, because what it does is look for the first character between the two strings that differs! On the other hand, the character that was actually inserted is the first comma. - This will then run into an issue when
self.snippet.follow
is called later in this same context. It seems that what ends up triggering a call tovsnip#deactivate
is theis_followable
call at https://github.com/hrsh7th/vim-vsnip/blob/master/autoload/vsnip/snippet.vim#L98. I wasn't able to figure out exactly what the issue is here, due to some confusion about what is going on with thetraverse
function.
I wonder if you can help diagnose the issue here and how it might be fixed. (Sorry, I had hoped to find a solution just by poking around in the code, but feel like I have run into a dead end.) In the meantime, it is possible for me to work around this issue by using the arrows to move around while inputting text and avoiding typing the comma, e.g. the sequence X
, X[
, X[0
, X[0:
, left arrow
, X[0,:
. But this seems like an undesirable interaction and hopefully it can be fixed.