draivin / hsnips

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

Expanding snippets inside snippets deletes tabstops #189

Open Frankwii opened 10 months ago

Frankwii commented 10 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 9 months ago

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

Alex2134556 commented 9 months ago

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

Oskar-Idland commented 9 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 9 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 9 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 9 months ago

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

Oskar-Idland commented 9 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 8 months ago

Ok, tabstops still work in 1.85.2 but not 1.86.0.

Oskar-Idland commented 8 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 8 months ago

Same issue here

Oskar-Idland commented 8 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 8 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 8 months ago

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

mfederczuk commented 8 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 8 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 7 months ago

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

Oskar-Idland commented 7 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 7 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 7 months ago

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

Oskar-Idland commented 7 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 6 months 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 6 months ago

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

yiktllw commented 5 months 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 5 months 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 5 months ago

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

Alex2134556 commented 5 months 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 5 months 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 5 months 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 5 months 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 5 months 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 5 months 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 5 months 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 5 months 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 5 months 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 5 months 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 5 months 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 5 months 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 5 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, 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 5 months 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 5 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, 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 5 months 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 5 months 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 5 months 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

superle3 commented 3 months ago

@Oskar-Idland I tried latex utilities at 1.89.1 and 1.92.2 but they also deleted tabstops.
One could configure the build step of something like vscodium to change those lines back. Then you will still have the marketplace and an experience like the normal vscode. But some things will not work.

I don't know if its worth it to find a work around when vscode is probably gonna fix the bug of vscode.TextEditor.insertSnippet

Oskar-Idland commented 3 months ago

@superle3 I am not sure this is a bug, but just a refactoring. Latex Workshop no longer works on 1.85, so this makes this a lot more work to fit into a regular workflow. I think the only way is to actually sit down, learn this codebase and patch the API calls that do not work. I tried contacting the original developer, but he won't reply. I can try giving him a ping tho.

Oskar-Idland commented 3 months ago

@draivin Hey man, I know this is a FOSS project so I apologize for asking for your time. This is a suuuper useful project which I know many people use. It makes working on latex go from a chore, to a breeze. Please, if you are able, help us patch it up. We know what API calls that are problematic, but we do not have your knowlegde of the codebase. Please, update the code, or teach us how to do it our selves. I think many people (myself included) would be happy to fork the repo and continue the work, but a little help getting started would be amazing!

Unpredictability commented 2 months ago

After some inspection of the source code, i think the problem does not orginate from HyperSnips. hsnip use vscode.editor.insertSnippets() from the vscode api, i tried calling this function two times, the second time i called it the editor will just simpliy discard the tabstops from the first call. tested on vscode 1.93 (August 2024). At this moment, there is not way to retain previous tabstops when calling insertSnippets(). This is an issue with VSCode API itself.

XRay71 commented 2 months ago

After some inspection of the source code, i think the problem does not orginate from HyperSnips. hsnip use vscode.editor.insertSnippets() from the vscode api, i tried calling this function two times, the second time i called it the editor will just simpliy discard the tabstops from the first call. tested on vscode 1.93 (August 2024). At this moment, there is not way to retain previous tabstops when calling insertSnippets(). This is an issue with VSCode API itself.

Would it be possible to also test it on the last known working version (v1.85 November 2023) and see if it discards the tabstops? I'm unfamiliar with how the API works or how to use it.

yiktllw commented 2 months ago

After some inspection of the source code, i think the problem does not orginate from HyperSnips. hsnip use vscode.editor.insertSnippets() from the vscode api, i tried calling this function two times, the second time i called it the editor will just simpliy discard the tabstops from the first call. tested on vscode 1.93 (August 2024). At this moment, there is not way to retain previous tabstops when calling insertSnippets(). This is an issue with VSCode API itself.

I agree with you. In fact, I've reported this bug to VSCode: https://github.com/microsoft/vscode/issues/214757. As mentioned in the issue, this bug is caused by https://github.com/microsoft/vscode/commit/9f5cb9537a691881d67ab12e874dd225449b74d1.

Oskar-Idland commented 2 months ago

After some inspection of the source code, i think the problem does not orginate from HyperSnips. hsnip use vscode.editor.insertSnippets() from the vscode api, i tried calling this function two times, the second time i called it the editor will just simpliy discard the tabstops from the first call. tested on vscode 1.93 (August 2024). At this moment, there is not way to retain previous tabstops when calling insertSnippets(). This is an issue with VSCode API itself.

Great job! I hope everyone here interacts with issue 214757. It will increase the odds of it being looked at.