Skip to content
This repository has been archived by the owner on May 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request Unity-Technologies#77 from Unity-Technologies/fixe…
Browse files Browse the repository at this point in the history
…s/indente-4-to-1-line-func

Fixes/indente 4 to 1 line func
  • Loading branch information
hschlichter authored Jan 4, 2018
2 parents 23dac8c + a402ae8 commit 8b7d652
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/combine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4409,7 +4409,7 @@ static void mark_function(chunk_t *pc)
if ( prev->type == CT_ARITH
|| prev->type == CT_ASSIGN
|| prev->type == CT_COMMA
|| prev->type == CT_STRING
|| (prev->type == CT_STRING && prev->parent_type != CT_EXTERN) // fixes issue 1259
|| prev->type == CT_STRING_MULTI
|| prev->type == CT_NUMBER
|| prev->type == CT_NUMBER_FP
Expand Down
29 changes: 29 additions & 0 deletions src/newlines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1783,8 +1783,37 @@ static void newlines_brace_pair(chunk_t *br_open)
}
}

// fix 1247 oneliner function support - converts 4,3,2 liners to oneliner

if ( br_open->parent_type == CT_FUNC_DEF
&& cpd.settings[UO_nl_create_func_def_one_liner].b)
{
chunk_t *br_close = chunk_skip_to_match(br_open, scope_e::ALL);

chunk_t *tmp = chunk_get_prev_ncnl(br_open);

if (((br_close->orig_line - br_open->orig_line) <= 2) && chunk_is_paren_close(tmp))
{
while ( tmp != nullptr
&& (tmp = chunk_get_next(tmp)) != nullptr
&& !chunk_is_closing_brace(tmp)
&& (chunk_get_next(tmp) != nullptr))
{
if (chunk_is_newline(tmp))
{
tmp = chunk_get_prev(tmp);
newline_iarf_pair(tmp, chunk_get_next_ncnl(tmp), AV_REMOVE);
}

chunk_flags_set(br_open, PCF_ONE_LINER);
chunk_flags_set(br_close, PCF_ONE_LINER);
}
}
}


// Make sure we don't break a one-liner

if (!one_liner_nl_ok(br_open))
{
LOG_FMT(LNL1LINE, "%s(%d): a new line may NOT be added\n", __func__, __LINE__);
Expand Down
2 changes: 2 additions & 0 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,8 @@ void register_options(void)
unc_add_option("nl_create_while_one_liner", UO_nl_create_while_one_liner, AT_BOOL,
"Change simple unbraced while statements into a one-liner\n"
"'while (i<5)\\n foo(i++);' => 'while (i<5) foo(i++);'.");
unc_add_option("nl_create_func_def_one_liner", UO_nl_create_func_def_one_liner, AT_BOOL,
"Change simple 4,3,2 liner function def statements into a one-liner\n");
unc_add_option("nl_split_if_one_liner", UO_nl_split_if_one_liner, AT_BOOL,
" Change a one-liner if statement into simple unbraced if\n"
"'if(b) i++;' => 'if(b)\\n i++;'.");
Expand Down
1 change: 1 addition & 0 deletions src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ enum uncrustify_options
UO_nl_create_while_one_liner, // Change simple unbraced while statements into a one-liner
// 'while (i<5)\n foo(i++);' => 'while (i<5) foo(i++);'
// Change that back:
UO_nl_create_func_def_one_liner, // Change simple 4, 3, 2 liner function def statements into a one - liner
UO_nl_split_if_one_liner, // Change a one-liner for statement into simple unbraced for
// 'if(b) i++;' => 'if(b)\n i++;'
UO_nl_split_for_one_liner, // Change a one-liner while statement into simple unbraced while
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/Output/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ Note: Use comments containing ' *INDENT-OFF*' and ' *INDENT-ON*' to disable
processing of parts of the source file (these can be overridden with
enable_processing_cmt and disable_processing_cmt).

There are currently 616 options and minimal documentation.
There are currently 617 options and minimal documentation.
Try UniversalIndentGUI and good luck.

1 change: 1 addition & 0 deletions tests/cli/Output/mini_d_uc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ nl_namespace_two_to_one_liner = false
nl_create_if_one_liner = false
nl_create_for_one_liner = false
nl_create_while_one_liner = false
nl_create_func_def_one_liner = false
nl_split_if_one_liner = false
nl_split_for_one_liner = false
nl_split_while_one_liner = false
Expand Down
3 changes: 3 additions & 0 deletions tests/cli/Output/mini_d_ucwd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,9 @@ nl_create_for_one_liner = false # false/true
# 'while (i<5)\n foo(i++);' => 'while (i<5) foo(i++);'.
nl_create_while_one_liner = false # false/true

# Change simple 4,3,2 liner function def statements into a one-liner
nl_create_func_def_one_liner = false # false/true

# Change a one-liner if statement into simple unbraced if
# 'if(b) i++;' => 'if(b)\n i++;'.
nl_split_if_one_liner = false # false/true
Expand Down
1 change: 1 addition & 0 deletions tests/cli/Output/mini_nd_uc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ nl_namespace_two_to_one_liner = false
nl_create_if_one_liner = false
nl_create_for_one_liner = false
nl_create_while_one_liner = false
nl_create_func_def_one_liner = false
nl_split_if_one_liner = false
nl_split_for_one_liner = false
nl_split_while_one_liner = false
Expand Down
3 changes: 3 additions & 0 deletions tests/cli/Output/mini_nd_ucwd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,9 @@ nl_create_for_one_liner = false # false/true
# 'while (i<5)\n foo(i++);' => 'while (i<5) foo(i++);'.
nl_create_while_one_liner = false # false/true

# Change simple 4,3,2 liner function def statements into a one-liner
nl_create_func_def_one_liner = false # false/true

# Change a one-liner if statement into simple unbraced if
# 'if(b) i++;' => 'if(b)\n i++;'.
nl_split_if_one_liner = false # false/true
Expand Down
4 changes: 4 additions & 0 deletions tests/cli/Output/show_config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,10 @@ nl_create_while_one_liner { False, True }
Change simple unbraced while statements into a one-liner
'while (i<5)\n foo(i++);' => 'while (i<5) foo(i++);'.

nl_create_func_def_one_liner { False, True }
Change simple 4,3,2 liner function def statements into a one-liner


nl_split_if_one_liner { False, True }
Change a one-liner if statement into simple unbraced if
'if(b) i++;' => 'if(b)\n i++;'.
Expand Down
7 changes: 7 additions & 0 deletions tests/config/staging/UNI-11095.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include Uncrustify.Common-Cpp.cfg

# We need to make FAKE_FUNCTION a PROTO_WRAP macro function to help properly tokenize it's parameters.
# Previously parameters with CT_AMP and CT_STAR were tokenized as CT_ARITH. Support for optional parenthesis added in github PR #629.
set PROTO_WRAP FAKE_FUNCTION
nl_create_func_def_one_liner=true
sp_inside_braces=Add
4 changes: 4 additions & 0 deletions tests/config/staging/UNI-2021.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### This file holds rules specific to C#

include Uncrustify.Common-CStyle.cfg
nl_create_func_def_one_liner=true
2 changes: 2 additions & 0 deletions tests/config/staging/Uncrustify.Common-CStyle.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,8 @@ nl_create_for_one_liner=false # { False, True }
nl_create_while_one_liner=false # { False, True }
# Change simple unbraced while statements into a one-liner
# 'while (i<5)\n foo(i++);' => 'while (i<5) foo(i++);'
nl_create_func_def_one_liner=false # { False, True }
# Change simple 4,3,2 liner function def statements into a one-liner
#
#nl_split_if_one_liner { False, True }
# Change a one-liner if statement into simple unbraced if
Expand Down
56 changes: 22 additions & 34 deletions tests/input/staging/UNI-2021.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,29 @@
// Non-indented two-line func+body
// Test code:
// updating the testcase for 1247
// concluded that we need to convert all the 4,3,2 liners to one liner based on the option
int fun ()
{
return 0;
}

int Func()
{ return 0; }
int fun() {return 0; }

// The contents of the method will get aligned with 'int'. Sometimes we want this, sometimes we don't.
int fun() {
return 0;
}

// There is no workaround; would require a new Uncrustify feature.
// Re-indentation of wrapped args leaves hanging previous-line args
// This:
int fun() {
return 0; }

// ```cs
int fun()
{return 0; }

UtilityFunctions.DisplayAwesomeness (position, fullFlagNames.ToArray (),
// Only show selections if we are not multi-editing
GUI.showMixedValue ? new int[] {} : selectedFlags.ToArray (),
Boo.m_Instance.Foo, null);
// ```
// becomes this:
int fun() { return 0;
}

// ```cs
int fun()
{
return 0; }

UtilityFunctions.DisplayAwesomeness(position, fullFlagNames.ToArray(),
// Only show selections if we are not multi-editing
GUI.showMixedValue ? new int[] {} : selectedFlags.ToArray(),
Boo.m_Instance.Foo, null);

// ```

// but we'd rather have this:

// ```cs

UtilityFunctions.DisplayAwesomeness(
position, fullFlagNames.ToArray(),
// Only show selections if we are not multi-editing
GUI.showMixedValue ? new int[] {} : selectedFlags.ToArray(),
Boo.m_Instance.Foo, null);

// ```
int fun()
{ return 0;
}
46 changes: 10 additions & 36 deletions tests/output/staging/10075-UNI-2021.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,17 @@
// Non-indented two-line func+body
// Test code:
// updating the testcase for 1247
// concluded that we need to convert all the 4,3,2 liners to one liner based on the option
int fun() {return 0; }

int Func()
{ return 0; }
int fun() {return 0; }

// The contents of the method will get aligned with 'int'. Sometimes we want this, sometimes we don't.
int fun() {return 0; }

// There is no workaround; would require a new Uncrustify feature.
// Re-indentation of wrapped args leaves hanging previous-line args
// This:
int fun() {return 0; }

// ```cs
int fun() {return 0; }

UtilityFunctions.DisplayAwesomeness(
position, fullFlagNames.ToArray(),
// Only show selections if we are not multi-editing
GUI.showMixedValue ? new int[] {} : selectedFlags.ToArray(),
Boo.m_Instance.Foo, null);
// ```
// becomes this:
int fun() { return 0; }

// ```cs
int fun() {return 0; }

UtilityFunctions.DisplayAwesomeness(
position, fullFlagNames.ToArray(),
// Only show selections if we are not multi-editing
GUI.showMixedValue ? new int[] {} : selectedFlags.ToArray(),
Boo.m_Instance.Foo, null);

// ```

// but we'd rather have this:

// ```cs

UtilityFunctions.DisplayAwesomeness(
position, fullFlagNames.ToArray(),
// Only show selections if we are not multi-editing
GUI.showMixedValue ? new int[] {} : selectedFlags.ToArray(),
Boo.m_Instance.Foo, null);

// ```
int fun() {return 0; }
4 changes: 3 additions & 1 deletion tests/regression.test
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
10071 staging/Uncrustify.Cpp.cfg staging/UNI-1983.cpp
10072 staging/Uncrustify.CSharp.cfg staging/UNI-2007.cs
10073 staging/Uncrustify.CSharp.cfg staging/UNI-2008.cs
10075 staging/UNI-2021.cfg staging/UNI-2021.cs
10076 staging/UNI-1343.cfg staging/UNI-1343.cs
10077 staging/Uncrustify.CSharp.cfg staging/UNI-1919.cs
10078 staging/Uncrustify.CSharp.cfg staging/UNI-3484.cs
Expand All @@ -78,7 +79,8 @@
60005 staging/UNI-Delegate.cfg staging/UNI-2685.cs
60006 staging/UNI-2049.cfg staging/UNI-2049.cpp
60007 staging/Uncrustify.CSharp.cfg staging/UNI-3083.cs
60008 staging/Uncrustify.JamCS.cfg staging/UNI-17253.cs
60008 staging/Uncrustify.JamCS.cfg staging/UNI-17253.cs
60011 staging/UNI-11095.cfg staging/UNI-11095.mm
60012 staging/Uncrustify.CSharp.cfg staging/UNI-12303.cs
60013 staging/UNI-13955.cfg staging/UNI-13955.cs
60015 staging/UNI-Delegate.cfg staging/UNI-14131.cs
Expand Down
3 changes: 0 additions & 3 deletions tests/staging.test
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@
10043 staging/Uncrustify.CSharp.cfg staging/nl-brace.cs
10049 staging/Uncrustify.Cpp.cfg staging/UNI-1336.cpp
10053 staging/Uncrustify.Cpp.cfg staging/UNI-1340.cpp
10056 staging/UNI-1346.cfg staging/UNI-1346.cpp
10057 staging/Uncrustify.Cpp.cfg staging/UNI-1347.cpp
10058 staging/Uncrustify.Cpp.cfg staging/UNI-1348.cpp
10059 staging/UNI-1349.cfg staging/UNI-1349.cpp
10061 staging/Uncrustify.CSharp.cfg staging/UNI-1355.cs
10064 staging/Uncrustify.CSharp.cfg staging/UNI-1443.cs
10068 staging/Uncrustify.CSharp.cfg staging/UNI-1979.cs
10074 staging/Uncrustify.CSharp.cfg staging/UNI-2020.cs
10075 staging/Uncrustify.CSharp.cfg staging/UNI-2021.cs
10076 staging/Uncrustify.CSharp.cfg staging/UNI-1343.cs
60004 staging/Uncrustify.CSharp.cfg staging/UNI-2684.cs
60005 staging/Uncrustify.CSharp.cfg staging/UNI-2685.cs
60006 staging/Uncrustify.Common-CStyle.cfg staging/UNI-2049.cpp
60009 staging/Uncrustify.CSharp.cfg staging/UNI-9917.cs
60011 staging/Uncrustify.Cpp.cfg staging/UNI-11095.mm
60014 staging/Uncrustify.Cpp.cfg staging/UNI-14107.cpp
60018 staging/Uncrustify.CSharp.cfg staging/UNI-18777.cs
60023 staging/Uncrustify.CSharp.cfg staging/UNI-18437.cs
Expand Down

0 comments on commit 8b7d652

Please sign in to comment.