Skip to content

Instantly share code, notes, and snippets.

@StefanFabian
Last active April 26, 2019 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanFabian/4a33c4c040e7ed7311964269a8b0c2e0 to your computer and use it in GitHub Desktop.
Save StefanFabian/4a33c4c040e7ed7311964269a8b0c2e0 to your computer and use it in GitHub Desktop.
doT C++ Code file template for the VSCode extension File Template Manager
{{
let date = new Date($.DATE);
let date_string = date.getDate().toString().padStart(2, '0') + '.' + (date.getMonth() + 1).toString().padStart(2, '0') + '.' + date.getFullYear();
let paths = $.DIR.split('/');
let index = paths.lastIndexOf('src');
let has_ns = index > 0 && index < paths.length;
let ns = "";
if (has_ns) {
ns = paths[index - 1];
}
let header_dir = ns;
if (index + 1 < paths.length) {
header_dir += '/' + paths.slice(index + 1).join('/');
}
}}
//
// Created by {{= $.AUTHOR }} on {{= date_string }}.
//
#include "{{=header_dir}}/{{=$.NAME}}.h"
{{? has_ns }}
namespace {{=ns}}
{
} // namespace {{=ns}}
{{?}}
@StefanFabian
Copy link
Author

This template may be too specific for your use case but it could be a good starting point.
It assumes a folder structure of
[...]/${PROJECT}/src/.../code_file.cpp
where ${PROJECT} is also the namespace.

Required variables
It requires a variable named AUTHOR to be set in File Templates Manager's settings.
To do so, open the VS Code settings search for "Custom Vars" and edit in the settings.json.
In the settings.json add the following line "templates.customVars": { "AUTHOR": "Your Name" }

Full example:
Creating a code file named some_project/src/some_example_class.cpp will create:

//
// Created by Your Name on 26.04.2019.
//

#include "some_project/some_example_class.h"

namespace some_project
{

} // some_project

You can change the format of the date by changing the date_string variable in the third line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment