zardoy / vscode-better-snippets

Create most advanced snippets for VSCode in existence
https://marketplace.visualstudio.com/items?itemName=zardoy.better-snippets
MIT License
6 stars 0 forks source link

Analogous to otherLinks.displayIfNone for typingSnippets #24

Closed AgentRBY closed 1 year ago

AgentRBY commented 2 years ago

Hello 👋

I have this import typing snippet:

{
   "sequence": "import",
   "body": "import { $0 } from '$1'",
   "when":{
      "languages": [...],
      "locations": [ "lineStart" ],
      "otherLines": [
         {
            "line": -1,
            "testRegex": "import(\\s)?({|\\w)|^$",  // If prev line is `import {` or `import Word`, or empty line
            "displayIfNoLine": true
         }
      ]
   }
}

I check to see if the previous line is an import or an empty line. This works fine if there is a string before it, but if there is no string, the snippet doesn't work, even with displayIfNoLine Code_-_Insiders_pB0jiQLNYB

zardoy commented 1 year ago

Sorry, I missed the priority of this issue somehow, and I must admit otherLines is one of the most not tested features of the extension (hope to fix it soon!).

the snippet doesn't work

Not sure why, it should work. I'll look into it tomorrow. I'm also thinking of renaming displayIfNoLine property to something else like acceptIfOutsideFile

zardoy commented 1 year ago

Hey, @AgentRBY just realized via repository search that displayIfNoLine was never implemented. I just pushed a lot of fixes & refactors to make otherLines finally stable. Also remember that you can always use typing snippet with fileStart location.

I also added skipEmptyLines for your case:

{
    "sequence": "import",
    "body": "import { $0 } from '$1'",
    "when": {
        "locations": [
            "lineStart"
        ],
        "otherLines": [
            {
                "line": -1,
                "displayIfNoLine": true,
                "skipEmptyLines": true,
                "testRegex": "^import(\\s)?({|\\w)$", // If prev line is `import {` or `import Word`
            }
        ]
    },
},