Editor Help

Your editor should help you format code in a consistent style, as described in CppStyleGuide.

Vim

If you use Vim to edit source files, you can add the following to your ${HOME}/.vimrc to make Vim aware of the ROS formatting standards:

set shiftwidth=2  " Two space indents
set tabstop=2     " Tab key indents two spaces at a time
set expandtab     " Use spaces when the <Tab> key is pressed
set cindent       " Turn on automatic C-code indentation
" Actual formatting rules go here
" once I figure out what they should be...

Vim's auto-indenting feature can wreak havoc when you cut and paste code with the mouse. To temporarily turn off auto-indentation, you can type:

:set paste

do your paste operation, then type:

:set nopaste

to turn auto-indent back on. You can create a shortcut to toggle paste mode by adding the following to ${HOME}/.vimrc:

:set pastetoggle=<F2>

Now you would press <F2> to turn off auto-indentation, do your paste, and press <F2> again to restore auto-indentation.

Vim (Alternate approach)

On ubuntu vim-tiny is installed by default and you need to install the full version of vim, scripts and addons.

sudo aptitude install vim-scripts

Edit your .vimrc file

filetype plugin indent on

If can try the default indent format or you can make the appropriate directories if needed and download the following cpp indent style. There is a reasonable python style installed by default.

cd ~
mkdir .vim
mkdir .vim/indent
cd ~/.vim/indent
curl http://www.vim.org/scripts/download_script.php?src_id=13033 > cpp.vim

The entire buffer can be re-indented by typing gg=G and a section of code you have pasted can be reformatted by moving to the opening { and typing =% the current line can be reindented by typing ==. Blocks of code can be indented or unindented by moving to the opening or closing brace and typing <% to unindent and >% to indent. More then you could ever want to know about indenting can be found here.

Emacs

To get ROS style formatting in Emacs, add the following to your ${HOME}/.emacs file:

(defun ROS-c-mode-hook()
  (setq c-basic-offset 2)
  (setq indent-tabs-mode nil)
  (c-set-offset 'substatement-open 0)
  (c-set-offset 'innamespace 0)
  (c-set-offset 'case-label '+)
  (c-set-offset 'brace-list-open 0)
  (c-set-offset 'brace-list-intro '+)
  (c-set-offset 'brace-list-entry 0)
  (c-set-offset 'member-init-intro 0)
  (c-set-offset 'statement-case-open 0)
  (c-set-offset 'arglist-intro '+)
  (c-set-offset 'arglist-cont-nonempty '+)
  (c-set-offset 'arglist-close '+)
  (c-set-offset 'template-args-cont '+))
(add-hook 'c-mode-common-hook 'ROS-c-mode-hook)

;;; In order to get namespace indentation correct, .h files must be opened in C++ mode
(add-to-list 'auto-mode-alist '("\\.h$" . c++-mode))

Eclipse

  • See Details in IDEs

indent

As per this, the way Indent handles C++ is somewhat broken so an alternate solution is suggested. You can also use the indent program to re-indent an entire file from the command line. To install the indent program on Ubuntu, type:

% sudo apt-get install indent

To invoke the indent program, the command is:

% indent [OPTIONS] [INPUT FILES]

or

% indent [OPTIONS] [INPUT FILE] [-o OUTPUT FILE]

In the first form, indent will make a backup of the input files, the originals are replaced with the re-indented versions. To match the ROS formatting guidelines, the following options can be used:

  • -blin, --brace-indentn Indent braces n spaces.
  • -nce, --dont-cuddle-else Do not cuddle } and else.
  • -ln, --line-lengthn Set maximum line length for non-comment lines to n.
  • -clin, --case-indentationn Case label indent of n spaces.
  • -nut, --no-tabs Use spaces instead of tabs.

As an example, to indent the code in foo.cpp, the command would be:

% indent -bli0 -nce -l120 -cli2 -nut -npsl foo.cpp

Qt Creator

See Details in IDEs (both regular Qt Creator as well as the ROS-Industrial ROS plugin for Qt Creator).

Wiki: EditorHelp (last edited 2018-10-16 18:44:29 by PeterPolidoro)