2
{
    "Comment": {
        "prefix": "#",
        "body":  "<!-- ${TM_FILEPATH/([^/]*\/[^/]*)$/$1/} -->"
    }
}

I have set up the about code snippet, the purpose is to add a comment that adds the file's base directory and file name <!-- templates/base.html --> like this but discards the rest of the path. I believe this is originally based on TextMate snippets.

enter image description here

I have tried everything but I can't get it to work, it's probably something silly but I don't see what I'm doing wrong.

Using just TM_FILEPATH
without the regex results in <!-- /Users/johndoe/Sites/blog/blog/templates/base.html -->

I used this https://code.visualstudio.com/docs/editor/userdefinedsnippets to find an example to base my code on. The example is this one:

${TM_FILENAME/(.*)\\..+$/$1/}
  |           |        | |
  |           |        | |-> no options
  |           |        |
  |           |        |-> references the contents of the first
  |           |             capture group
  |           |
  |           |-> regex to capture everything before
  |               the final `.suffix`
  |
  |-> resolves to the filename

Thanks to the ideas of the 2 commenters I was finally able to get it to work.

One commenter put me on track with the double backslashes to catch both Windows and Unix style slashes.

The other commenter suggested the square brackets.

Final result:

{
    "Comment": {
        "prefix": "#",
        "body":  "<!-- ${TM_FILEPATH/.*[\\/](.*[\\/].*)$/$1/} -->",
    }
}
3
  • Thank you that's it Commented Jun 5, 2018 at 6:55
  • But only the square brackets and 2 backslashes per slash were needed that was the trick. Commented Jun 5, 2018 at 7:03
  • Cool. Took me a while. I've added it as an answer. Commented Jun 5, 2018 at 7:09

2 Answers 2

1

Try something like this:

"Comment": {
    "prefix": "#",
    "body":  [

      "<!-- ${TM_FILEPATH/.*\\\\(.*\\\\.*)$$/$1/} -->",

      "<!-- ${TM_DIRECTORY/.*\\\\(.*)$/$1/}/${TM_FILENAME} -->",
    ]
},

Those two lines in the body should be equivalent. That works for the Windows directory style, like :

 c:\Users\Mark\asdf\experimental\src\js\main.js

Since your path.separators are / try something like:

"<!-- ${TM_FILEPATH/.*\/(.*\.*)$/$1/} -->", 
"<!-- ${TM_FILEPATH/.*\\/(.*\\.*)$/$1/} -->",
"<!-- ${TM_FILEPATH/.*\\\/(.*\\\.*)$/$1/} -->",
"<!-- ${TM_FILEPATH/.*\\\\/(.*\\\\.*)$/$1/} -->",

I just don't know how many backslashes you will need (and I can't test it here) for your OS.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for testing it but unfortunately, none of the examples you posted are working for me. I'm on OSX by the way
See edit, you will probably have to use one of he later three options if you are on macOS. That is what I meant by adding backslashes.
I tried by adding backslashes but it didn't help, I'm starting to think this might be a bug on MacOS.
Ok tried again with your suggestions this is the result: <!-- ${TM_FILEPATH/.*/(.**)$//} --> <!-- post_new.html --> <!-- /Users/stofke/Sites/blog/blog/templates/post_new.html --> <!-- /Users/stofke/Sites/blog/blog/templates/post_new.html --> Note it does the output in reverse order. So the last suggestion is the first result and vice versa.
Ok I tried the suggestion of wp78de which added more backslashes and it worked answer is: "body": "<!-- ${TM_FILEPATH/.*[\\/](.*[\\/].*)$/$1/} -->"
|
0

Let's try it with a character class that takes account of both path separator types and helps us to escape properly at the same time:

{
    "Comment": {
        "prefix": "#",
        "body":  [  
            "<!-- ${TM_FILEPATH/.*[\\/](.*[\\/].*)$$/$1/} -->",
        ]
    },
}

1 Comment

The final regex that worked after stripping the redundant backslashes was this: .*[\\/](.*[\\/].*)$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.