gpt4 book ai didi

c++ - AStyle 嵌套类格式化

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:19:03 31 4
gpt4 key购买 nike

我的项目中有以下代码:

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 的错误,还是我忘记了任何选项?如果是错误,您有什么建议可以使用 VIM 格式化 C++ 代码?

最佳答案

当然,这是一个错误。现在对 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')

这基本上添加了一个预保存 Hook 。现在,当您使用 C++ 代码保存文件时,Uncrustify 将使用您之前提供的配置文件自动格式化它。

注意:此处介绍的所有内容都经过我的日常测试和使用。

关于c++ - AStyle 嵌套类格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12338531/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com