Skip to content

Instantly share code, notes, and snippets.

@niosus
Last active March 1, 2020 00:19
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 niosus/fac5d94860476b2b4747bc96a5fe7ba5 to your computer and use it in GitHub Desktop.
Save niosus/fac5d94860476b2b4747bc96a5fe7ba5 to your computer and use it in GitHub Desktop.
Define guards snippet for Sublime Text 3
<snippet>
<description>#def_guards_formatted</description>
<content><![CDATA[#ifndef ${TM_FILEPATH/(?:^\/*[\w\/]*\/(?:src|include)\/(\w+))|(?:^\/*[\w\/]*\/(\w+)\/(?:\2))|(?:^\/*home\/${TM_FULLNAME}\/(\w+))|(?:\/([\w_]+))|(?:\.(h))/\U$1$2$3$4$5$6_\E/g}
#define ${TM_FILEPATH/(?:^\/*[\w\/]*\/(?:src|include)\/(\w+))|(?:^\/*[\w\/]*\/(\w+)\/(?:\2))|(?:^\/*home\/${TM_FULLNAME}\/(\w+))|(?:\/([\w_]+))|(?:\.(h))/\U$1$2$3$4$5$6_\E/g}
${0:$SELECTION}
#endif // ${TM_FILEPATH/(?:^\/*[\w\/]*\/(?:src|include)\/(\w+))|(?:^\/*[\w\/]*\/(\w+)\/(?:\2))|(?:^\/*home\/${TM_FULLNAME}\/(\w+))|(?:\/([\w_]+))|(?:\.(h))/\U$1$2$3$4$5$6_\E/g}
]]></content>
<tabTrigger>def</tabTrigger>
<scope>source.c, source.c++, source.objc, source.objc++</scope>
</snippet>
@niosus
Copy link
Author

niosus commented Nov 3, 2016

For any file of styles:

  • /home/name/project/src/blah_blah/foo/bar.h - path has src folder
  • /home/name/project/include/blah_blah/foo/bar.h - path has include folder
  • /home/name/project/some_project/some_project/blah_blah/foo/bar.h - path has a folder some_project twice in a row
  • /home/name/blah_blah/foo/bar.h - none of the above

this snippet will generate def guards of form:

#ifndef BLAH_BLAH_FOO_BAR_H_
#define BLAH_BLAH_FOO_BAR_H_
// you selection is copied here
#endif  // BLAH_BLAH_FOO_BAR_H_

It will also copy the code you have selected between the markers.

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