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

Context (?) bug when adding text to a placeholder that is identical to text following the placeholder #256

Open
sdbuch opened this issue Oct 20, 2022 · 1 comment

Comments

@sdbuch
Copy link

sdbuch commented Oct 20, 2022

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.

  1. Paste the snippet above in some empty .py file, e.g. with CTRL+J in the suggested bindings
  2. Navigate to the third placeholder e.g. with TAB in the default bindings
  3. Attempt to type X, X[, X[0, X[0, (in sequence). The idea is that I'm trying to index a row of a matrix X.
  4. 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 like X[0,, because there is a comma in the snippet following the third placeholder $3. After the comma is typed, the new buffer content is X[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 to vsnip#deactivate is the is_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 the traverse 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.

@sdbuch
Copy link
Author

sdbuch commented Oct 20, 2022

Sorry, now that I have actually written it up and thought about it more, I think I see this is the same issue as #254. I'll work around it following your suggestion in the help file for now.

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

No branches or pull requests

1 participant