A样式嵌套类格式
AStyle nested class formatting
我的项目中有以下代码:
class RangeConverter {
private:
struct Converter {
double MinimumInput;
double MaximumInput;
double MinimumOutput;
double MaximumOutput;
template <typename RangeType>
RangeType Convert ( RangeType invalue ) const {
double v = static_cast<double> ( invalue );
if ( v < MinimumInput ) {
v = MinimumInput;
} else if ( v > MaximumInput ) {
v = MaximumInput;
}
double interpolationfactor = ( v - MinimumInput ) / ( MaximumInput - MinimumInput );
return static_cast<RangeType> ( ( interpolationfactor * ( MaximumOutput - MinimumOutput ) ) + MinimumOutput );
}
};
.....
在用AStyle格式化代码后,我得到了以下内容:
class RangeConverter {
private:
struct Converter {
ngeConverter {
private:
struct Converter {
double MinimumInput;
double MaximumInput;
double MinimumOutput;
double MaximumOutput;
template <typename RangeType>
RangeType Convert ( RangeType invalue ) const {
double v = static_cast<double> ( invalue );
if ( v < MinimumInput ) {
v = MinimumInput;
} else if ( v > MaximumInput ) {
v = MaximumInput;
}
double interpolationfactor = ( v - MinimumInput ) / ( MaximumInput - MinimumInput );
return static_cast<RangeType> ( ( interpolationfactor * ( MaximumOutput - MinimumOutput ) ) + MinimumOutput );
}
};
.....
astyle命令:
astyle
--style=java
--indent=force-tab=2
--indent-classes
--indent-switches
--indent-labels
--indent-preprocessor
--indent-col1-comments
--pad-oper
--pad-paren
--delete-empty-lines
--add-brackets
--align-pointer=type
--align-reference=type
这是一个astyle的bug,还是我忘记了任何选择?如果这是一个错误,你可以建议用VIM格式化C++代码吗?
当然,这是一个bug。近来对AStyle的支持不太好。有很多错误一直存在,却从未得到解决。你不应该指望情况在不久的将来会有所改善。我强烈建议切换到Uncrustify。当然,它也有一些问题,但它们并不那么令人讨厌,也不会像AStyle那样破坏代码。它有数百种配置选项,比AStyle灵活得多,所以要耐心,你必须花相当长的时间来调整它以适应你的口味和习惯。
当你完成后,如果你想将它与Vim正确集成,这里是你可以添加到.vimrc
:中的代码
" Restore cursor position, window position, and last search after running a
" command.
function! Preserve(command)
" Save the last search.
let search = @/
" Save the current cursor position.
let cursor_position = getpos('.')
" Save the current window position.
normal! H
let window_position = getpos('.')
call setpos('.', cursor_position)
" Execute the command.
execute a:command
" Restore the last search.
let @/ = search
" Restore the previous window position.
call setpos('.', window_position)
normal! zt
" Restore the previous cursor position.
call setpos('.', cursor_position)
endfunction
" Specify path to your Uncrustify configuration file.
let g:uncrustify_cfg_file_path =
shellescape(fnamemodify('~/.uncrustify.cfg', ':p'))
" Don't forget to add Uncrustify executable to $PATH (on Unix) or
" %PATH% (on Windows) for this command to work.
function! Uncrustify(language)
call Preserve(':silent %!uncrustify'
. ' -q '
. ' -l ' . a:language
. ' -c ' . g:uncrustify_cfg_file_path)
endfunction
现在,您可以将这个函数(Uncrustify
)映射到键的组合,也可以使用我使用的方便技巧。创建一个文件~/.vim/after/ftplugin/cpp.vim
,您可以在其中覆盖任何Vim设置,特别是针对C++的设置,并在其中添加以下行:
autocmd BufWritePre <buffer> :call Uncrustify('cpp')
这基本上增加了一个预保存挂钩。现在,当你用C++代码保存文件时,Uncrustify会利用你之前提供的配置文件自动格式化它。
注意:这里提供的一切都经过了很好的测试,我每天都在使用。
相关文章:
- 嵌套在类中时无法设置成员数据
- 无法访问嵌套类.类的使用无效
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 如何在C++中初始化嵌套类中的2个memeber
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 在C++中搜索嵌套多映射值
- 在C++中将矢量转换为嵌套地图
- C++嵌套if语句,基本货币交换
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 嵌套的匿名命名空间
- 了解嵌套循环打印星号图案
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 嵌套for循环C++的问题(初学者)
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 用C#中的并集模拟C++嵌套结构
- 部分专业化和嵌套模板
- 嵌套While循环不起作用(C++问题)
- C++-模板嵌套类的引用初始化无效
- 嵌套结构字段的 clang 格式规则
- A样式嵌套类格式