I want to create a snippet on Visual Studio Code 1.33.1 that create a C++ class using the file's name.
First, I want to set up the "include guard", the point is to use the file's name, replace every '.' by a '_' and set it all to uppercase (norm):
#ifndef FILE_CLASS_HPP //filename: File.class.hpp
The VSC documentation provides some variable for the file's name, and some Regex to change to all uppercase and replace a character by another.
Point is: I never managed to do both since I know nothing about Regex.
I tried to manually join the Regex but it never worked:
#ifndef ${TM_FILENAME/(.*)/${1:/upcase}/[\\.-]/_/g}
expected result:
#ifndef FILE_CLASS_HPP
actual result:
#ifndef ${TM_FILENAME/(.*)//upcase/[\.-]/_/g}
This should work:
"Filename upcase": {
"prefix": "_uc",
"body": [
"#ifndef ${TM_FILENAME/([^\\.]*)(\\.)*/${1:/upcase}${2:+_}/g}"
],
"description": "Filename uppercase and underscore"
},
([^\\.]*)(\\.)* group1: all characters before a period
group2: the following period
replace with uppercase all the group1's: ${1:/upcase}
replace all the group2s '
's with _
's
The ${2:+_}
is a conditional replacement, so you only add a _
at the end of group1 uppercase if there is a following group2.
The g
global flag is necessary in this case to catch all the occurrences of group1group2's, not just the first.