smjonas / snippet-converter.nvim

Bundle snippets from multiple sources and convert them to your format of choice.
Mozilla Public License 2.0
170 stars 4 forks source link

Fail to convert '\' in snippet from SnipMate to vim-vsnip #16

Closed flotisable closed 1 year ago

flotisable commented 1 year ago

First of all, thanks for the great scripts to convert the snippet to variant format.

I'm trying to convert my snippets from snipmate format to vim-vsnip format. When there is \ in the snippet, which is used to escape $ in the snippet, it disappears in vim-vsnip format. This causes the snippet to be unexpected when being expanded.

Below is an example, note the \$option is conveted to $option snipmate snippet

snippet getopt "process command line options"
    while getopts "${1:h}" option; do

        case \$option in

            ${2:'h') usage; exit;;}${3}
            *)   echo "Warning: Unknown option \$option" > /dev/stderr;;

        esac

    done

This snippet is converted to vim-vsnip snippet

  "getopt": {
    "prefix": "getopt",
    "description": "\"process command line options\"",
    "body": [
      "while getopts \"${1:h}\" option; do",
      "",
      "\tcase $option in",
      "",
      "\t\t${2:'h') usage; exit;;}$3",
      "\t\t*)   echo \"Warning: Unknown option $option\" > /dev/stderr;;",
      "",
      "\tesac",
      "",
      "done",
      ""
    ]

The expected snippet after conversion is below, note the \\ before $option

  "getopt": {
    "prefix": "getopt",
    "description": "\"process command line options\"",
    "body": [
      "while getopts \"${1:h}\" option; do",
      "",
      "\tcase \\$option in",
      "",
      "\t\t${2:'h') usage; exit;;}$3",
      "\t\t*)   echo \"Warning: Unknown option \\$option\" > /dev/stderr;;",
      "",
      "\tesac",
      "",
      "done",
      ""
    ]
smjonas commented 1 year ago

Thanks for reporting! Glad you find this plugin useful :)

flotisable commented 1 year ago

Thanks for your quick response.

The fix resolve the issue in the example. However, it seems the fix breaks a lot of snippet conversion. The example below works as expected before

snippet if "if control flow"
        if ${1:<condition>}
        "
                ${2:${VISUAL}}
        "
        endif

snippet ife "if else control flow"
        if ${1:<condition>}
        "
                ${2}
        "
        else
        "
                ${3}
        "
        endif

snippet func "function definition"
        function ${1:<function name>}(${2:<arguments>})
        "
                ${3:${VISUAL}}
        "
        endfunction

after the fix, it is only converted to the snippet below, note the if and func snippet disappear

{
  "ife": {
    "prefix": "ife",
    "description": "\"if else control flow\"",
    "body": [
      "if ${1:<condition>}",
      "\"",
      "\t$2",
      "\"",
      "else",
      "\"",
      "\t$3",
      "\"",
      "endif",
      ""
    ]
  }
}

If you would like to see more detail, my snippets can be found at https://github.com/flotisable/FlotisableVimSnippets. Clone the repo, and update the snippet-converter.nvim submodule, then run make convert. The converted difference between previous version and the fix can then be seen with git diff