cursorless-dev / cursorless

Don't let the cursor slow you down
https://www.cursorless.org/
MIT License
1.14k stars 80 forks source link

Snippets: Unable to take advantage of variable transformation #2201

Open ScottMorse opened 9 months ago

ScottMorse commented 9 months ago

I have the following snippet:

{
  "arrowFunctionWithOptions": {
    "definitions": [
      {
        "scope": {
          "langIds": [
            "typescript",
            "typescriptreact"
          ]
        },
        "body": [
          "interface ${name/(.*)/${1:/capitalize}/}Options {",
          "\t$options",
          "}",
          "",
          "const $name = ({ $optionFields }: ${name/(.*)/${1:/capitalize}/}Options) => {",
          "\t$body",
          "}"
        ],
        "variables": {
          "name": {
            "formatter": "camelCase"
          }
        }
      }
    ]
  }
}

When using this, I cannot get the capitalization I want for the name variable. If I change every instance of $name to $1, I can get the capitalization by inserting the snippet with no phrase and entering the function name afterwards, and the transformation is applied after I Tab, but I cannot use this in insertion_snippets_single_phrase.csv successfully since arrowFunctionWithOptions.1 doesn't seem to work for the Cursorless identifier.

pokey commented 9 months ago

does this work with regular vscode snippets? we just vendored in their snippet parser

ScottMorse commented 9 months ago

Yes, the transformations work with normal snippets. I'll use a smaller example (the React useState hook) to demonstrate some of the nuances I'm experiencing:

If I use this regular VSCode snippet...

{
  "use state": {
    "scope": "javascript,typescript,javascriptreact,typescriptreact",
    "prefix": "usestate",
    "body": ["const [${1}, set${1/(.*)/${1:/capitalize}/}] = useState(${2});"],
    "description": "useState"
  }
}

... I can get the casing I want by first inserting the snippet, entering my text for variable $1, and then hitting Tab to get to variable $2. The capitalize transform isn't applied until I hit Tab.

If I use this Cursorless snippet...

{
  "useState": {
    "definitions": [
      {
        "scope": {
          "langIds": [
            "javascript",
            "javascriptreact",
            "typescript",
            "typescriptreact"
          ]
        },
        "body": [
          "const [${name}, set${name/(.*)/${1:/capitalize}/}] = useState(${initialValue})"
        ],
        "variables": {
          "name": {
            "formatter": "camelCase"
          }
        }
      }
    ]
  }
}

... I can add useState, useStateTest.name to insertion_snippets_single_phrase.csv and use the snippet without error, but the capitalize transform is not applied whether or not I include the initial phrase or not, so "snippet usestate hello world" will insert const [helloWorld, sethelloWorld] = useState(), and if I use only "snippet usestate" and then enter $name manually, the capitalize transform will not be applied even after I use the Tab key. I also tested this without the formatter configured for $name.

If I try making my Cursorless snippet this...

{
  "useState": {
    "definitions": [
      {
        "scope": {
          "langIds": [
            "javascript",
            "javascriptreact",
            "typescript",
            "typescriptreact"
          ]
        },
        "body": [
          "const [${1}, set${1/(.*)/${1:/capitalize}/}] = useState(${initialValue})"
        ],
        "variables": {
          "1": {
            "formatter": "camelCase"
          }
        }
      }
    ]
  }
}

... I cannot use the initial phrase for variable $1, as it seems that entries such as useState, useState.1 or useState, useState[1], etc. do not work for insertion_snippets_single_phrase.csv. However, I can simply use "snippet usestate" without a phrase and use the Tab key to get a successful capitalize after manually entering $1. This has been my current workaround in general for snippets where I need transformations, using numbered variables and not using the initial phrase (or using the initial phrase for a different variable that doesn't need transformation).

This is purely speculation, but perhaps the issue has to do with VSCode requiring the explicit Tab press to apply the transformation, and perhaps Cursorless cannot currently trigger this with the initial phrase. I'm not sure why in my second code block above, the Cursorless snippet using $name, I cannot get the capitalize even with manual entry of $name plus a Tab press, though.