Skip to content

Commit

Permalink
Merge pull request uncrustify#4373 from guy-maurel/trackingForNewline
Browse files Browse the repository at this point in the history
test the nl_ options
  • Loading branch information
gmaurel authored Sep 19, 2024
2 parents 66e8808 + 9a74033 commit 81fd172
Show file tree
Hide file tree
Showing 16 changed files with 253 additions and 133 deletions.
4 changes: 4 additions & 0 deletions src/newlines/brace_pair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ using namespace uncrustify;
* void foo() {
* }
*/


void newlines_brace_pair(Chunk *br_open)
{
LOG_FUNC_ENTRY();
Expand Down Expand Up @@ -313,6 +315,7 @@ void newlines_brace_pair(Chunk *br_open)
{
// Braces belong to a function definition
log_rule_B("nl_collapse_empty_body_functions");
log_ruleNL("nl_collapse_empty_body_functions", br_open);

if (options::nl_collapse_empty_body_functions())
{
Expand All @@ -323,6 +326,7 @@ void newlines_brace_pair(Chunk *br_open)
else
{
log_rule_B("nl_collapse_empty_body");
log_ruleNL("nl_collapse_empty_body", br_open);

if (options::nl_collapse_empty_body())
{
Expand Down
4 changes: 3 additions & 1 deletion src/newlines/cleanup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void newlines_cleanup_braces(bool first)
if (pc->GetLevel() == pc->GetBraceLevel())
{
log_rule_B("nl_class_brace");
log_ruleNL("nl_class_brace", pc); // this is still a beta test
log_ruleNL("nl_class_brace", pc);
newlines_do_else(pc->GetPrevNnl(), options::nl_class_brace());
}
break;
Expand Down Expand Up @@ -470,6 +470,7 @@ void newlines_cleanup_braces(bool first)
&& options::nl_type_brace_init_lst_open() == IARF_IGNORE
&& options::nl_type_brace_init_lst_close() == IARF_IGNORE))
{
log_ruleNL("nl_type_brace_init_lst_close", pc);
newlines_brace_pair(pc);
}

Expand Down Expand Up @@ -556,6 +557,7 @@ void newlines_cleanup_braces(bool first)
&& options::nl_type_brace_init_lst_close() != IARF_IGNORE)
{
// Handle unnamed temporary direct-list-initialization
log_ruleNL("nl_after_brace_close", pc);
newline_iarf_pair(pc->GetPrevNnl(), pc,
options::nl_type_brace_init_lst_close(), true);
}
Expand Down
2 changes: 2 additions & 0 deletions src/newlines/eat_start_end.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ void newlines_eat_start_end()
chunk.CopyAndAddBefore(pc);
LOG_FMT(LNEWLINE, "%s(%d): %zu:%zu add newline before '%s'\n",
__func__, __LINE__, pc->GetOrigLine(), pc->GetOrigCol(), pc->Text());
Chunk *prev = pc->GetPrev();
log_ruleNL("nl_start_of_file_min", prev);
MARK_CHANGE();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/newlines/one_liner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ bool one_liner_nl_ok(Chunk *pc)
return(false);
}
log_rule_B("nl_func_leave_one_liners");
log_ruleNL("nl_func_leave_one_liners", pc);

if ( options::nl_func_leave_one_liners()
&& ( pc->GetParentType() == CT_FUNC_DEF
Expand Down
13 changes: 13 additions & 0 deletions src/nl_func_leave_one_liners.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Uncrustify: where do the Newlines options work</title>
</head>
<body lang="en-US">
<p>
</p>
<pre>
int foo() {<a title="nl_func_leave_one_liners: 2&#010;nl_type_brace_init_lst_close: 1"><font color="red">M</font></a>return 0; }
</pre>
</body>
</html>
213 changes: 115 additions & 98 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,109 @@ bool compareTrack(TrackNumber t1, TrackNumber t2)
}


void DecodeTrackingData(Chunk *pc)
{
if (pc->GetTrackingData() == nullptr)
{
return;
}
// insert <here> the HTML code for the tracking
LOG_FMT(LGUY, "%s(%d): Text is %s, orig_line is %zu, column is %zu\n",
__func__, __LINE__, pc->Text(), pc->GetOrigLine(), pc->GetColumn());
LOG_FMT(LGUY, " Tracking info are: \n");
LOG_FMT(LGUY, " number of track(s) %zu\n", pc->GetTrackingData()->size());
// is sorting necessary?
size_t many = pc->GetTrackingData()->size();

if (many > 1) // there is only one track
{
#ifdef EXTRA_LOG
LOG_FMT(LGUY, " tracking before\n");

// protocol before sort
for (size_t track = 0; track < pc->GetTrackingData()->size(); track++)
{
const TrackList *A = pc->GetTrackingData();
const TrackNumber B = A->at(track);
size_t Bfirst = B.first;
char *Bsecond = B.second;

LOG_FMT(LGUY, " %zu, tracking number is %zu\n", track, Bfirst);
LOG_FMT(LGUY, " %zu, rule is %s\n", track, Bsecond);
}
#endif

if (options::debug_sort_the_tracks())
{
TrackList *A1 = pc->TrackingData();
sort(A1->begin(), A1->end(), compareTrack);
}
}
else
{
// sorting is not necessary
}
#ifdef EXTRA_LOG
LOG_FMT(LGUY, " tracking after\n");

// protocol ( after sort )
for (size_t track = 0; track < pc->GetTrackingData()->size(); track++)
{
const TrackList *A = pc->GetTrackingData();
const TrackNumber B = A->at(track);
size_t Bfirst = B.first;
char *Bsecond = B.second;

LOG_FMT(LGUY, " %zu, tracking number is %zu\n", track, Bfirst);
LOG_FMT(LGUY, " %zu, rule is %s\n", track, Bsecond);
}
#endif
char *old_one = nullptr;
bool first_text = true;
char tempText[80];

add_text("<a title=\"");

for (size_t track = 0; track < pc->GetTrackingData()->size(); track++)
{
const TrackList *A = pc->GetTrackingData();
const TrackNumber B = A->at(track);
size_t Bfirst = B.first;
char *Bsecond = B.second;

if ( old_one == nullptr
|| strcmp(old_one, Bsecond) != 0)
{
// first time this option
if (old_one != nullptr)
{
// newline
add_text("&#010;");
add_text(Bsecond);
add_text(": ");
}
old_one = Bsecond;

if (first_text)
{
snprintf(tempText, sizeof(tempText), "%s", Bsecond);
add_text(tempText);
add_text(": ");
first_text = false;
}
}
else
{
add_text(", ");
}
snprintf(tempText, sizeof(tempText), "%zu", Bfirst);
add_text(tempText);
} // for (size_t track = 0; track < pc->GetTrackingData()->size(); track++)

add_text("\"><font color=\"red\">M</font></a>");
} // DecodeTrackingData(


void output_text(FILE *pfile)
{
// tracking_is_on is "false" if we have the standard output
Expand Down Expand Up @@ -693,7 +796,15 @@ void output_text(FILE *pfile)
add_text("<html>\n");
add_text("<head>\n");
add_text(" <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>\n");
add_text(" <title>Uncrustify: where do the Spaces options work</title>\n");

if (cpd.html_type == tracking_type_e::TT_SPACE)
{
add_text(" <title>Uncrustify: where do the Spaces options work</title>\n");
}
else if (cpd.html_type == tracking_type_e::TT_NEWLINE)
{
add_text(" <title>Uncrustify: where do the Newlines options work</title>\n");
}
add_text("</head>\n");
add_text("<body lang=\"en-US\">\n");
add_text("<p>\n");
Expand Down Expand Up @@ -727,6 +838,8 @@ void output_text(FILE *pfile)

if (pc->Is(CT_NEWLINE))
{
DecodeTrackingData(pc);

for (size_t cnt = 0; cnt < pc->GetNlCount(); cnt++)
{
if ( cnt > 0
Expand Down Expand Up @@ -988,104 +1101,8 @@ void output_text(FILE *pfile)
{
add_text(pc->GetStr(), false, pc->Is(CT_STRING));
}

// insert <here> the HTML code for the tracking
if (pc->GetTrackingData() != nullptr)
{
LOG_FMT(LGUY, "%s(%d): Text is %s, orig_line is %zu, column is %zu\n",
__func__, __LINE__, pc->Text(), pc->GetOrigLine(), pc->GetColumn());
LOG_FMT(LGUY, " Tracking info are: \n");
LOG_FMT(LGUY, " number of track(s) %zu\n", pc->GetTrackingData()->size());
// is sorting necessary?
size_t many = pc->GetTrackingData()->size();

if (many > 1) // there is only one track
{
#ifdef EXTRA_LOG
LOG_FMT(LGUY, " tracking before\n");

// protocol before sort
for (size_t track = 0; track < pc->GetTrackingData()->size(); track++)
{
const TrackList *A = pc->GetTrackingData();
const TrackNumber B = A->at(track);
size_t Bfirst = B.first;
char *Bsecond = B.second;

LOG_FMT(LGUY, " %zu, tracking number is %zu\n", track, Bfirst);
LOG_FMT(LGUY, " %zu, rule is %s\n", track, Bsecond);
}
#endif

if (options::debug_sort_the_tracks())
{
TrackList *A1 = pc->TrackingData();
sort(A1->begin(), A1->end(), compareTrack);
}
}
else
{
// sorting is not necessary
}
#ifdef EXTRA_LOG
LOG_FMT(LGUY, " tracking after\n");

// protocol ( after sort )
for (size_t track = 0; track < pc->GetTrackingData()->size(); track++)
{
const TrackList *A = pc->GetTrackingData();
const TrackNumber B = A->at(track);
size_t Bfirst = B.first;
char *Bsecond = B.second;

LOG_FMT(LGUY, " %zu, tracking number is %zu\n", track, Bfirst);
LOG_FMT(LGUY, " %zu, rule is %s\n", track, Bsecond);
}
#endif
char *old_one = nullptr;
bool first_text = true;
char tempText[80];

add_text("<a title=\"");

for (size_t track = 0; track < pc->GetTrackingData()->size(); track++)
{
const TrackList *A = pc->GetTrackingData();
const TrackNumber B = A->at(track);
size_t Bfirst = B.first;
char *Bsecond = B.second;

if ( old_one == nullptr
|| strcmp(old_one, Bsecond) != 0)
{
// first time this option
if (old_one != nullptr)
{
// newline
add_text("&#010;");
add_text(Bsecond);
add_text(": ");
}
old_one = Bsecond;

if (first_text)
{
snprintf(tempText, sizeof(tempText), "%s", Bsecond);
add_text(tempText);
add_text(": ");
first_text = false;
}
}
else
{
add_text(", ");
}
snprintf(tempText, sizeof(tempText), "%zu", Bfirst);
add_text(tempText);
} // for (size_t track = 0; track < pc->GetTrackingData()->size(); track++)

add_text("\"><font color=\"red\">M</font></a>");
}
DecodeTrackingData(pc);
}
else // standard output
{
Expand Down
5 changes: 5 additions & 0 deletions src/uncrustify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2417,8 +2417,13 @@ void uncrustify_file(const file_mem &fm, FILE *pfout, const char *parsed_file,
__func__, cpd.html_file, strerror(errno), errno);
exit(EX_IOERR);
}
else
{
LOG_FMT(LDATA, "tracking is opened = %s\n", (cpd.html_file != nullptr) ? cpd.html_file : "null");
}
output_text(t_file);
fclose(t_file);
LOG_FMT(LDATA, "tracking is closed = %s\n", (cpd.html_file != nullptr) ? cpd.html_file : "null");
exit(EX_OK);
}

Expand Down
Loading

0 comments on commit 81fd172

Please sign in to comment.