draivin / hsnips

HyperSnips: a powerful snippet engine for VS Code, inspired by vim's UltiSnips
MIT License
148 stars 24 forks source link

Expanding snippets inside snippets deletes tabstops #189

Open Frankwii opened 5 months ago

Frankwii commented 5 months ago

Possibly very related to #157.

When expanding a snippet inside another snippet, all tabstops of the former snippet are deleted. An example:

snippet OOO "Omega" iA
\Omega
endsnippet

snippet II "\int" i
\\int_{$1}${2:f}~d${3:\\mu}
endsnippet

If I write "OOO" inside tabstop $1 of "\int", the snipet "Omega" is expanded correctly, but the tabstops $2 and $3 of "\int" disappear.

This doesn't happen with "native" vscode snippets.

XRay71 commented 5 months ago

Additional note, downgrading VS Code to the December 2023 version fixed this bug for me.

Alex2134556 commented 4 months ago

I am having the same issue, but downgrading did fix it for me.

Oskar-Idland commented 4 months ago

Anyone find another fix to this? I am using linux (arch) and have a hard time downgrading as I got vscode from the AUR

gwesley96 commented 4 months ago

Any update? I originally downgraded to December 2023 but thought I'd retry and this is still an issue. I haven't been able to figure out what part of the VSCode update affected this to think of a workaround. Any ideas for what it may be?

Oskar-Idland commented 4 months ago

Nothing new. Still does not work. Have you @gwesley96 tried any other versions later than the December one? Would be ideal to see which exact version causes issues.

PiotrSokol commented 4 months ago

@Oskar-Idland, do you mean the commit or the release? I can try and figure out the latter.

Oskar-Idland commented 4 months ago

Yes i meant the latest vscode release where expansions works as intended. Hopefully it helps resolve the issue to narrow it down

PiotrSokol commented 4 months ago

Ok, tabstops still work in 1.85.2 but not 1.86.0.

Oskar-Idland commented 4 months ago

I tried seeing the difference on the 1.86 commit, but could not see anything that made sense. Anyone able to try and debug this?

oiiiiiiii commented 3 months ago

Same issue here

Oskar-Idland commented 3 months ago

Experimented more in v.1.87.2 and found the following:

  1. LaTeX Utilities's snippets and the ones built into vscode still works while nested. Only hsnips fails.
  2. I also noticed that you must have the hsnips by OrangeX4 (https://github.com/OrangeX4/hsnips) to be able to open the snippet directory or files or be able to activate any snippets at all. If I only had draivin's (this) version of hsnips I could not even open the snippet file as I got the following error: image
gwesley96 commented 3 months ago

I'm still coming up with nothing. I've looked through all the details and couldn't find anything that on the surface would affect hsnips/tabstops. Has anyone else found anything yet?

Oskar-Idland commented 3 months ago

Anyone know the maintainers (@draivin @mfederczuk) of the extension is on it?

mfederczuk commented 3 months ago

Anyone know the maintainers (@draivin @mfederczuk) of the extension is on it?

Just FYI I'm not a maintainer here.
I've only contributed some code once.

Oskar-Idland commented 3 months ago

For any Arch Linux users reading this, just download the tar ball from 1.85 (other distros has their versions there as well), extract to some directory of choice (I chose ~/VSCode_Old), then just make a symlink from /usr/bin/code to the executable at VSCode-linux-x64/bin/code where ever you extracted it. I renamed the newest binary to code_new just to have it somewhere:

[oskar@archlinux] /usr/bin $ lsd -1 | grep ^code
code ⇒ /home/oskar/VSCode_Old/VSCode-linux-x64/bin/code
code_new

Everything else (settings, snippets etc) remained the same.

Alex2134556 commented 2 months ago

Is anybody able to make the above solution work for MacOS?

Oskar-Idland commented 2 months ago

If you press the link I posted above, you will see the download link for Mac as well as Windows and Linux. This is much simpler for you guys

Alex2134556 commented 2 months ago

Oh okay, it sounds like you merely downgraded your Visual Studio Code (I guess your point of the comment was to make it work for linux). Yes, that's what I have been doing for the time being.

Oskar-Idland commented 2 months ago

Hope this gets fixed. Can't use the python extension (and probably many others) on lower than v1.86

Oskar-Idland commented 2 months ago

Anyone have actual knowledge about the project and it's structure or Typescript? I have neither, but know programming and would love to help sort this out, but it is hard to tell what is wrong, without knowledge of the project

Alex2134556 commented 1 month ago

Could it be possible that this is a vscode issue and raising the issue there would get it fixed or maybe some pointers towards solving the issue?

Oskar-Idland commented 1 month ago

Testing with the regular user snippets, they preserve tabstops if nested

yiktllw commented 4 weeks ago

I found where the problem lies. In VSCode 1.86.0, at line 538 of the file src/vs/workbench/api/browser/mainThreadEditor.ts,

snippetController.insert(template, {
    overwriteBefore: 0, overwriteAfter: 0,
    undoStopBefore: opts.undoStopBefore, undoStopAfter: opts.undoStopAfter,
    clipboardText
});

has been replaced with

const edits: ISnippetEdit[] = ranges.map(range => ({ range: Range.lift(range), template }));
snippetController.apply(edits, {
    overwriteBefore: 0, overwriteAfter: 0,
    undoStopBefore: opts.undoStopBefore, undoStopAfter: opts.undoStopAfter,
    clipboardText
});

Reverting this change should allow the compiled vscode to function properly with hypersnips.

Oskar-Idland commented 4 weeks ago

Great job!! How did you find it? There were soooo many files and changes I gave up looking after a while.

I think the next step should be to find what part of hyprsnips interact with this feature, instead of patching vscode. Nested snippets still work with other snippet programs, so it must be possible.

Oskar-Idland commented 4 weeks ago

Searching through the files, I see no mention of this "snippetController". Do you know in what context it is used ?

Alex2134556 commented 4 weeks ago

@yiktllw Thank you very much for finding the error. Can you detail the process as to how we can achieve the same? I tried cloning the repository and making the change, but I keep running in errors trying to compile the repository.

yiktllw commented 4 weeks ago

@yiktllw Thank you very much for finding the error. Can you detail the process as to how we can achieve the same? I tried cloning the repository and making the change, but I keep running in errors trying to compile the repository.

I tried the latest source code of VSCode(1.90.0). After modifying

const edits: ISnippetEdit[] = ranges.map(range => ({ range: Range.lift(range), template }));
snippetController.apply(edits, {
    overwriteBefore: 0, overwriteAfter: 0,
    undoStopBefore: opts.undoStopBefore, undoStopAfter: opts.undoStopAfter,
    clipboardText
});

to

snippetController.insert(template, {
    overwriteBefore: 0, overwriteAfter: 0,
    undoStopBefore: opts.undoStopBefore, undoStopAfter: opts.undoStopAfter,
    clipboardText
});

I also removed

import { ISnippetEdit } from 'vs/editor/contrib/snippet/browser/snippetSession';

from the beginning of the file. Following these changes, I was able to compile the code successfully.

yiktllw commented 4 weeks ago

Searching through the files, I see no mention of this "snippetController". Do you know in what context it is used ?

It was mentioned at line 530 of the file:

// import { SnippetController2 } from 'vs/editor/contrib/snippet/browser/snippetController2';
const snippetController = SnippetController2.get(this._codeEditor);

I'm not sure about the specific usage of snippetController either.

Oskar-Idland commented 4 weeks ago

yeah I tried looking through any mentioned of everything mentioned above in both hsnips and in the hscope repo. I found nothing.

Alex2134556 commented 4 weeks ago

@yiktllw Okay, I am able to change the line inside the src folder and I was also able to run the yarn and yarn watch commands on the terminal. However, I am not sure how to actually produce the application of vscode. Can you provide some instructions for that?

yiktllw commented 4 weeks ago

yeah I tried looking through any mentioned of everything mentioned above in both hsnips and in the hscope repo. I found nothing.

I misunderstood you. The issue arises when executing editor.insertSnippet() in Hypersnips, and mainThreadEditor.ts is responsible for implementing the insertSnippet() method. So, I checked what updates were made in version 1.86.0 of this file, which led me to the code causing the problem.

yiktllw commented 4 weeks ago

@yiktllw Okay, I am able to change the line inside the src folder and I was also able to run the yarn and yarn watch commands on the terminal. However, I am not sure how to actually produce the application of vscode. Can you provide some instructions for that?

You need to run the yarn and yarn compile commands, and then you will find the executable files in the ./scripts folder. On Windows, it's "code.bat", and on Linux, it's "code.sh".

Alex2134556 commented 4 weeks ago

@yiktllw Sorry for bothering you, but running code.sh opens a VScode application which has no extensions enabled nor can I find any extensions in it. Moreover, the app name is Code - OSS.

Oskar-Idland commented 4 weeks ago

@yiktllw Sorry for bothering you, but running code.sh opens a VScode application which has no extensions enabled nor can I find any extensions in it. Moreover, the app name is Code - OSS.

Have you cloned the right repo?

yiktllw commented 4 weeks ago

@yiktllw Sorry for bothering you, but running code.sh opens a VScode application which has no extensions enabled nor can I find any extensions in it. Moreover, the app name is Code - OSS.

The compiled version of VSCode does not include any extensions and cannot access the marketplace. I'm not sure if this can be avoided. If you need to use extensions, you must download them from the web page marketplace and then select Install from VSIX from the menu in the top right corner of the Extensions view. Therefore, recompiling VSCode is not the best option. A better approach would be to fix the issue within Hypersnips.

Alex2134556 commented 4 weeks ago

@yiktllw Ah I see, so your discovery was more pertaining to having hypersnips patched up instead of providing us an a local solution to fix hypersnips for ourselves (as the compiled version of VScode looks far inferior to the Application VScode). With that said, I think it would be best to open up a new issue with the proposed discovery as this thread already has a lot of messages which the developers might not want to skim through.

Oskar-Idland commented 4 weeks ago

It does not seem like the original developer is interested in this or any other issue. The github profile has not been doing much lately. I even emailed him and have yet to get any response. The best solution, is to just figure out what the problem is, then just fork the project (I do not mind doing that).

On line 87 in hsnips/src/extention.ts we see the use of editor.insertSnippet. I am trying to see what could have went wrong.

FluffLescure commented 3 weeks ago

For any Arch Linux users reading this, just download the tar ball from 1.85 (other distros has their versions there as well), extract to some directory of choice, then just make a symlink from /usr/bin/code to the executable at VSCode-linux-x64/bin/code where ever you extracted it.

I'm sorry for loopingback to this, I tried downgrading this same way and correctly got VSC version 1.85.2 upon checking, but for some reason doesn't fix the problem. I there something dumb I might've overlooked?

Regardless, its great to see people spend time diagnosing this issue on others behalf.

yiktllw commented 3 weeks ago

@FluffLescure You might want to check if there are any conflicts between some plugins and Hypersnips. From what I know, the VSCode Vim plugin can also cause this issue (the VSCode Neovim plugin does not).

Oskar-Idland commented 3 weeks ago

For any Arch Linux users reading this, just download the tar ball from 1.85 (other distros has their versions there as well), extract to some directory of choice, then just make a symlink from /usr/bin/code to the executable at VSCode-linux-x64/bin/code where ever you extracted it.

I'm sorry for loopingback to this, I tried downgrading this same way and correctly got VSC version 1.85.2 upon checking, but for some reason doesn't fix the problem. I there something dumb I might've overlooked?

Regardless, its great to see people spend time diagnosing this issue on others behalf.

When you write code -v in the terminal, do you get the correct version? I use this version as well and can install hnsips through the marketplace inside vscode

FluffLescure commented 3 weeks ago

When you write code -v in the terminal, do you get the correct version?

Yep correct version pops up, VSC wasn't the issue.

@FluffLescure You might want to check if there are any conflicts between some plugins and Hypersnips. From what I know, the VSCode Vim plugin can also cause this issue (the VSCode Neovim plugin does not).

Was using Vim extension rather than neovim, that was my mistake. Quick responses though, thanks guys!

Oskar-Idland commented 3 weeks ago

Okay, and what happens when you try to use the marketplace? Are you able to install hsnips, but it just does not activate? If so, have you checked the snippet file directory to see it is not empty?

FluffLescure commented 3 weeks ago

Marketplace works fine and snippet path was well configured since settings were kept across versions. It was just the Vim extension deleting tabstops for some reason. NVim fixed it with v1.85 of vsc