How to prevent Uncrustify code formatter from keeping one space in C++ one-liner between { and the rest code?
So, it changes this code
const Foo &GetBar() const { return bar; }
to this
const Foo &GetBar() const {return bar; }
I search for return in default uncrustify config, but found nothing in common with my problem.
I think that this has more to do with brace spacing than with the return, so I'd check the space settings for braces. The first option I would try to play with would be sp_inside_braces (should be "ignore" by default).
Related
I'm looking a for a way to cause trailing return types to always be put on a new line. I noticed clang format will do this with long declarations, but will not if it's short enough. Is there a way to change this?
Ex.
auto foo() -> std::optional<std::string>
{
// ...
}
Becomes
auto foo()
-> std::optional<std::string>
{
// ...
}
Not as far as I am aware (likely because it's a somewhat relatively new feature)
A workaround is to put a comment there:
auto foo() //
-> std::optional<std::string>
{
// ...
}
But seeing as it's not implemented as standard, probably means it's a very uncommon notation, so maybe it's best to stick to the default
Is there a way to change format how netbeans 8.1 generate code? Instead of having
void setSomething1(bool something1)
{
something1_ = something1;
}
bool getSomething1() const
{
return something1_;
}
I would like to have this code
void setSomething1(bool something1) { something1_ = something1; }
bool getSomething1() const { return something1_; }
and also the set/get prefix could change or disappear
void setSomething1(bool something1) { something1_ = something1; }
bool something1() const { return something1_; }
netbeans only allow me to change braces position, same line or new line, but for this short functions i would like to compress it to one line only. With many setters and getters, the code starts to be confusing.
If netbeans itself cant do it, would it be possible via regular expression or some other workaroud?
Other (long body) functions will remain in the format. There it makes the code better to read.
retType name(args)
{
body;
}
This regex will satisfy the first requirement (Demo):
([\w ]+\s+[gs]et\w+\([\w ]*\)\s*(?:const)?)\s+\{\s+(.*)\s+\}
With $1 { $2 } as replacement
Then this one (note the trailing space):
get(\w+)\(
With $1 (trailing space aslo) as replacement
The following code snippets explain my problem well.
What I want:
template<class T>
ostream& operator<<(ostream& out, const vector<T>& v)
{
...
}
int
main()
{
...
}
What I get (notice the over-indentation before the function name):
template<class T>
ostream& operator<<(ostream& out, const vector<T>& v)
{
...
}
int
main()
{
...
}
I have set filetype plugin indent on in my ~/.vimrc.
I have looked at this post but the answer in that looks like learning a new programming language. I am a vim fan, but not a vim expert. Isn't there a simpler solution?
What you are seeing is the effect of cino-t (cinoptions setting t). You need to make sure conniptions contains t0 to make the parameter flush with the left margin
From :h cino-t
cino-t
tN Indent a function return type declaration N characters from the
margin. (default 'shiftwidth').
cino= cino=t0 cino=t7
int int int
func() func() func()
To do this you need to make sure that it is set for the cpp filetype. (cindent is turned on by the default cpp indent file)
I think just adding set cinoptions+=t0 to your vimrc should be sufficient.
Just as I guessed, this had a pretty simple solution! After motivating myself to read the :help 'cinoptions-values', the following configuration was all that's needed to solve this particular problem.
:set cino+=t0
From the help text:
tN Indent a function return type declaration N characters from the
margin. (default 'shiftwidth').
cino= cino=t0 cino=t7
int int int
func() func() func()
You could try these indent options in .vimrc:
" Auto indent
set ai
" Smart indent
set si
" C-Style indent
set cindent
I have a very large source code(>10,000lines) to change, with some function with fn_i_dont_want, I don't need to change, all other functions I must do some changes.So it's very hard to find such functions in such a large source code.
For example:
int foo_i_dont_want()
{
fn_i_dont_want()
}
int foo_i_want1()
{
fn()
fn1()
}
int foo_i_want2()
{
fn()
fn1()
....
}
I want vim to search all function I want, foo_i_want1, foo_i_want2, the condition is I don't want function with function call fn_i_dont_want.
I've tried to use vim regex like
/{\_.\{-}fn_i_dont_want\_.\{-}}
or
/{\_.*fn_i_dont_want\_.*}
But everything fails, it gets worse when there is some function like this:
int foo_i_dont_want()
{
struct bar = {0,0};
fn_i_dont_want();
}
and vim searches for everything in the pair of bracket{}.
So can somebody help me?
Parsing a complex programming language with regular expression is bound to be incomplete and problematic. Instead:
either use the refactoring capabilities of a real IDE, or
if you have to do this in Vim use :substitute with the confirm flag, and manually accept / decline each match position after inspecting it
Edit:
indent -bap foo.cpp
works
Is there any easy way to insert a new line or insert a text before the beginning of every function definition in C++ file?
int main() {
return 0;
}
void foo() {
}
becomes
int main() {
return 0;
}
void foo() {
}
Parsing C++ source code with regexp is guaranteed to fail for some cases (which might not occur
for you depending on your source code/coding style), so some kind of parsing is always a better strategy.
I would have started by looking into the source code of cproto to see if it simply could be changed to add a blank line when it finds a function.
Update: cproto does not handle C++, but genproto does.
In Perl:
while(my $line = <>)
{
$line =~ s/^\}[ \t]*$/}\n/;
print $line;
}
That will also insert something at the end of every namespace (but not struct or class since they have a semi-colon at the end). You could probably get more clever to avoid that, but I suspect that may be overkill.
That also notably will not catch functions that are defined inline in a class declaration. I'm not sure if that's a case that's important in your case.