When CLion creates a header file it adds include guard strings like this:
#ifndef PROJECTNAME_FILENAME_H
#define PROJECTNAME_FILENAME_H
/* ... code ... */
#endif //PROJECTNAME_FILENAME_H
But I want just FILENAME_H without the PROJECTNAME_ prefix. How to change it in CLion settings?
Bit late to this question, but I've got a slightly more involved solution that'll handle this without the need for manual post-processing regardless of file extension:
Head into your File and Code Templates - The other answers already detail how to do this.
In the File and Code Templates settings page, change to the Includes tab.
Click the + to create a new Include template. Name it something like IncludeGuard and set the Extension to h.
Input the following for the contents. Make sure you don't include any blank lines before or after.
#macro( includeGuard $filename $ext )
#set( $ucfull = ${filename.toUpperCase().replace('-', '_')} )
#set( $extidx = ${ucfull.lastIndexOf(".")} )
#set( $extstart = $extidx + 1 )
#if( $extidx > -1 )
#set( $ucname = ${ucfull.substring(0,$extidx)} )
#set( $ucext = ${ucfull.substring($extstart)} )
#else
#set( $ucname = $!{ucfull} )
#set( $ucext = ${ext.toUpperCase()} )
#end
${ucname}_${ucext}##
#end##
Change back to the Files tab, and find the C Header File or C++ Class Header file depending on which language you're looking to update.
Change the contents of this file template to:
#parse("IncludeGuard.h")##
#set( $blank = "" )
#[[#ifndef]]# #includeGuard(${NAME} "h")${blank}
#[[#define]]# #includeGuard(${NAME} "h")${blank}
// ...
#[[#endif]]# // #includeGuard(${NAME} "h")
If everything works as intended, attempting to create a C Header File using the name test-include-guard or test-include-guard.h should both result in the following:
#ifndef TEST_INCLUDE_GUARD_H
#define TEST_INCLUDE_GUARD_H
// ...
#endif /* TEST_INCLUDE_GUARD_H */
Few notes:
If you need to change the file extension, change the includeGuard(${NAME} "h") parts to use whatever extension you want for the second parameter. The template will attempt to parse the file extension from ${NAME}, but ${NAME} only contains the file extension if you explicitly enter it into the new filename dialog.
The current state of whitespace handling in the Velocity templates used by CLion is a shit show, so you'll need to work around this as I did if you decide to further customize the templates. General guidelines:
If you're experiencing undesired linebreaks, you'll want to try adding a terminating line comment ## to the ends of lines before it.
If you find yourself in the oppostie scenario, (missing an expected linebreak) you can work around this with the #set( $blank = "" ) strategy I utilized above.
Most of the IntelliJ-based IDEs seem to cache the compilation of Include templates the first time they get passed into #parse(). If you make changes to an Include template after this point, you'll usually need to use the File > Invalidate Caches/Restart menu command before the changes propagate.
Settings->Editor->File and Code Templates->Files
change ${INCLUDE_GUARD} into _${NAME}_H_
For example, if your file name is: clion.h, then _${NAME}_H_ is rendered as _clion_H_, because ${NAME} is rendered as the filename (without extension).
File | Settings | Editor | File and Code Templates for Windows and Linux
CLion | Preferences | Editor | File and Code Templates for OS X
#[[#ifndef]]# BASE_${HEADER_FILENAME}
#[[#define]]# BASE_${HEADER_FILENAME}
#[[#endif]]# //BASE_${HEADER_FILENAME}
>
#ifndef BASE_test_h
#define BASE_test_h
#endif //BASE_test_h
select BASE_test_h and press CTRL + SHIFT + U to upper case
According to the latest doc (2019.3, but it may work in earlier versions, too) you can navigate to the Naming Convention tab under Settings / Preferences | Editor | Code Style | C/C++.
There you'll find a field that allows you to easily change the header guard pattern. No need to add custom templates anymore.
Related
CLion has file template to generate C++ Class, which generates source file and header. In my project I have handler classes that have same code part, and i want to generate them by file templates. And templates that i created can't do this:
Set file names for class MyHandlerClass i want my_handler_class.cpp and .hpp
From one class name i want to generate 2 files header and source, don't know how to do that.
I also want to have string like MyClass -> my-class, found function $lowercaseAndDash($NAME) but don't know why its not works
I have template for header:
#pragma once
// includes
namespace handlers {
class ${NAME}: public Parent {
public:
// methods
};
}
I tried to solve first problem by setting the file name like this: #set ($FILE_NAME = "test_class.hpp"). But i don't know hot to set CamelCase to snake, and this don't works.
Also find in docs function to snake case, but its not works for me in file template.
Its impossible to change file name from template. When you create a C++ Class, you set class name and select file name encoding. When you create file from template its saves file with your file name. I found solution this way, enter file_name (in snake case), and with Velocity create CamelCaseName for class name:
#set( $CamelCaseName = "" )
#set( $part = "" )
#foreach($part in $NAME.split("_"))
#set( $CamelCaseName = "${CamelCaseName}$part.substring(0,1).toUpperCase()$part.substring(1).toLowerCase()" )
#end
Its impossible to generate 2 files from one click like it do C++ Class template. I have to templates, and use them both.
This function is for liveTemplates, for FileTemplate i used velocity: #set( $NeededString = $NAME.replaceAll('_', '-') )
To include header file from cpp use #[[#include]]# "${NAME}.hpp"
I have two set commands in my CMake.txt file. I have something like:
set(GUI_SOURCE_FILES
src/UINode/main.cpp
src/UINode/b.cpp
src/UINode/c.cpp
src/UINode/d.cpp
)
set(GUI_HEADER_FILES
src/UINode/b.h
src/UINode/c.h
src/UINode/d.h
)
This works fine and the executable is good. Now I want to have another group of set commands that need to included the above and add new .h and .cpp files. I am not sure if this is possible but I know that I cannot do something like:
set(GUIA_SOURCE_FILES
src/UINode/different_main.cpp
src/UINode/b.cpp
src/UINode/c.cpp
src/UINode/d.cpp
src/anotherpath/e.cpp
)
set(GUIA_HEADER_FILES
src/UINode/b.h
src/UINode/c.h
src/UINode/d.h
src/anotherpath/e.h
)
EDIT 1: Thanks for the reply user2799037! I now have something like:
set(COMMON_SOURCE_FILES
src/UINode/a.cpp
src/UINode/b.cpp
src/UINode/c.cpp
)
set(COMMON_HEADER_FILES
src/UINode/a.h
src/UINode/b.h
src/UINode/c.h
)
I then use them by:
set(GUI_SOURCE_FILES
src/UINode/main_GUI.cpp
${COMMON_SOURCE_FILES}
)
set(GUI_HEADER_FILES
${COMMON_HEADER_FILES}
)
and do
QT4_WRAP_CPP(GUI_HEADER_FILES_HPP ${GUI_HEADER_FILES})
followed by the
rosbuild_add_executable.
For the next part I do:
set(GUIA_SOURCE_FILES
src/commands/main_GUI1.cpp
${COMMON_SOURCE_FILES}
)
set(GUIA_HEADER_FILES
${COMMON_HEADER_FILES}
src/pathplanning/anotherheader.h
)
but I get an error when i do:
QT4_WRAP_CPP(GUIA_HEADER_FILES_HPP ${GUIA_HEADER_FILES})
'CMake Error: Attempt to add a custom rule to output'
I think you want to do something like that
set(COMMON_SOURCE_FILES
src/UINode/b.cpp
src/UINode/c.cpp
src/UINode/d.cpp
)
set(COMMON_HEADER_FILES
src/UINode/b.h
src/UINode/c.h
src/UINode/d.h
)
and use these for you more specific variables:
set(GUI_SOURCE_FILES
${COMMON_SOURCE_FILES }
src/anotherpath/main.cpp
)
set(GUIA_SOURCE_FILES
${COMMON_SOURCE_FILES }
src/anotherpath/different_main.cpp
)
Analog for the headers. With this approach you avoid having two list which can get out of sync.
I'm working on a Word template that the user can access from Sharepoint.
In this template I have made a custom ribbon with custom ui editor.
I want the users to be able to choose a header and a footer.
For this I have already made 2 different headers (1 with fields and 1 without) and saved them in the template.
So when I want to insert a header I can select them like this: Insert --> Header --> scroll all the way down to 'Template' and select one of them. This works perfect. I've recorded a Macro of this process so I am able to use this on my custom ribbon.
the macro looks like this:
Sub Header()
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Application.Templates( _
"http://spf.mysite.be/Shared%20Documents/Template.dotm"). _
BuildingBlockEntries("Header").Insert Where:=Selection.Range, _
RichText:=True
Selection.MoveDown Unit:=wdLine, count:=4
Selection.Delete Unit:=wdCharacter, count:=1
Selection.Delete Unit:=wdCharacter, count:=1
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
The problem:
When I open the template from sharepoint this macro doesn't work anymore.
I think this is because Word changes the linked template. when I go to the developer tab and click on 'Document Template' the linked template is the following: 'C:\Users\xxx\AppData\Local\Temp\TemplateATA-8.dotm' (the 8 changes to a 9 the next time I open the template from SharePoint.)
When i work localy and change the link to the local location, there is no problem.
Can someone please help me?
Thanks
Nina
(I'm using Word 2013, but also older versions of Word have to be able to use the document.)
Problem solved. I changed the link to: Application.Templates( _
ActiveDocument.AttachedTemplate.FullName). _
Now it works perfectly!!
I need to add a field in basic template. Can anyone help me how can i add another field in include/SugarObjects/templates/basic/vardefs.php in upgrade safe manner.
In VardefManager's function addTemplate not like general standards of Sugar it is not requiring the custom paths
include/SugarObjects/VardefManager.php near line 107 SugarCE6.5.5:
if(empty($templates[$template])){
$path = 'include/SugarObjects/templates/' . $template . '/vardefs.php';
if(file_exists($path)){
require($path);
$templates[$template] = $vardefs;
}else{
$path = 'include/SugarObjects/implements/' . $template . '/vardefs.php';
if(file_exists($path)){
require($path);
$templates[$template] = $vardefs;
}
}
}
Really waiting for awesome responses.
Create a file at the path custom/include/SugarObjects/VardefManager.php with the name VardefManager.php and in that file include your mail file it is include/SugarObjects/VardefManager.php.
Here you will create a class with same and and create a function with the name
static function addTemplate
with same the arguments pass in the main file. and override the method here with your custom code (as you want to add some lines of code in that).
This will be upgrade safe and will be workable to you.
My hobby is writing up stuff on a personal wiki site: http://comp-arch.net.
Currently using mediawiki (although I often regret having chosen it, since I need per page access control.)
Often I create pages that define several terms or concepts on the same page. E.g. http://semipublic.comp-arch.net/wiki/Invalidate_before_writing_versus_write_through_is_the_invalidate.
Oftentimes such "A versus B" pages provide the only definitions of A and B. Or at least the only definitions that I have so far gotten around to writing.
Sometimes I will define many more that two topics on the same page.
If I create such an "A vs B" or other paging containing multiple definitions D1, D2, ... DN, I would like to automatically create redirect pages, so that I can say [[A]] or [[B]] or [[D1]] .. [[DN]] in other pages.
At the moment the only way I know of to create such pages is manually. It's hard to keep up.
Furthermore, at the time I create such a page, I would like to provide some page text - typicaly a category.
Here;s another example: variant page names. I often find that I want to create several variants of a page name, all linking to the same place. For example
[[multithreading]],
[[multithreading (MT)]],
[[MT (multithreading)]],
[[MT]]
Please don;t tell me to use piped links. That's NOT what I want!
TWiki has plugins such as
TOPICCREATE automatically create topics or attach files at topic save time
More than that, I remember a twiki plugin, whose name I cannot remember or google up, that included the text of certain subpages within your current opage. You could then edit all of these pages together, and save - and the text would be extracted and distributed as needed. (By the way, if you can remember the name of tghat package, please remind me. It had certain problems, particularly wrt file locking (IIRC it only locked the top file for editing, bot the sub-topics, so you could lose stuff.))
But this last, in combination with parameterized templtes, would be almost everything I need.
Q: does mediawiki have something similar? I can't find it.
I suppose that I can / could should wrote my own robot to perform such actions.
It's possible to do this, although I don't know whether such extensions exist already. If you're not averse to a bit of PHP coding, you could write your own using the ArticleSave and/or ArticleSaveComplete hooks.
Here's an example of an ArticleSaveComplete hook that will create redirects to the page being saved from all section titles on the page:
$wgHooks['ArticleSaveComplete'][] = 'createRedirectsFromSectionTitles';
function createRedirectsFromSectionTitles( &$page, &$user, $text ) {
// do nothing for pages outside the main namespace:
$title = $page->getTitle();
if ( $title->getNamespace() != 0 ) return true;
// extract section titles:
// XXX: this is a very quick and dirty implementation;
// it would be better to call the parser
preg_match_all( '/^(=+)\s*(.*?)\s*\1\s*$/m', $text, $matches );
// create a redirect for each title, unless they exist already:
// (invalid titles and titles outside ns 0 are also skipped)
foreach ( $matches[2] as $section ) {
$nt = Title::newFromText( $section );
if ( !$nt || $nt->getNamespace() != 0 || $nt->exists() ) continue;
$redirPage = WikiPage::factory( $nt );
if ( !$redirPage ) continue; // can't happen; check anyway
// initialize some variables that we can reuse:
if ( !isset( $redirPrefix ) ) {
$redirPrefix = MagicWord::get( 'redirect' )->getSynonym( 0 );
$redirPrefix .= '[[' . $title->getPrefixedText() . '#';
}
if ( !isset( $reason ) ) {
$reason = wfMsgForContent( 'editsummary-auto-redir-to-section' );
}
// create the page (if we can; errors are ignored):
$redirText = $redirPrefix . $section . "]]\n";
$flags = EDIT_NEW | EDIT_MINOR | EDIT_DEFER_UPDATES;
$redirPage->doEdit( $redirText, $reason, $flags, false, $user );
}
return true;
}
Note: Much of this code is based on bits and pieces of the pagemove redirect creating code from Title.php and the double redirect fixer code, as well as the documentation for WikiPage::doEdit(). I have not actually tested this code, but I think it has at least a decent chance of working as is. Note that you'll need to create the MediaWiki:editsummary-auto-redir-to-section page on your wiki to set a meaningful edit summary for the redirect edits.