Skip to content

Instantly share code, notes, and snippets.

@mbasaglia
Last active January 3, 2017 09:27
Show Gist options
  • Save mbasaglia/075f5389e89a4dc839526a391277bf82 to your computer and use it in GitHub Desktop.
Save mbasaglia/075f5389e89a4dc839526a391277bf82 to your computer and use it in GitHub Desktop.
Kdevelop C++ file setup snippet
function find_components(header)
{
var url = document.url();
var components = url.split("/");
var end = header ? components.length : components.length-1;
components = components.slice(3, end);
var cut = -1;
for ( cut = components.length-1; cut >= 0; cut-- )
if ( components[cut] == "src" || components[cut] == "include" )
break;
var result = components.slice(cut+1);
if ( header && cut > 0 )
result.unshift(components[cut-1]);
return result;
}
function namespaces()
{
var result = "";
var components = find_components(false);
for ( var i = 0; i < components.length; i++ )
result += "namespace " + components[i] + "{\n";
result += "\n\n\n";
for ( var i = components.length-1; i >= 0; i-- )
result += "} // namespace " + components[i] + "\n";
return result;
}
function body_cpp()
{
var header_name = document.fileName().replace(".cpp", ".hpp");
return "#include \"" + header_name + "\"\n" + namespaces();
}
function body_hpp()
{
var guard_name = find_components(true).join("_")
.replace(".", "_").replace("-", "_")
.toUpperCase();
return "#ifndef " + guard_name + "\n#define " + guard_name +
"\n\n\n" + namespaces() + "#endif // " + guard_name + "\n";
}
function body()
{
try{
var filename = document.fileName();
if ( filename.slice(-4) == ".cpp" )
return body_cpp();
return body_hpp();
}catch(e){ return e; }
}
/**
* \file
*
* \author Mattia Basaglia
*
* \copyright Copyright (C) %{year} Mattia Basaglia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
%{fn`body`}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment