Skip to content

Commit

Permalink
Merge pull request #2904 from guy-maurel/IndentMemberFunctionCall
Browse files Browse the repository at this point in the history
Indent the member function call
  • Loading branch information
gmaurel committed Sep 11, 2020
2 parents 36bf34a + 7eeaf75 commit a3ff639
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 3 deletions.
87 changes: 84 additions & 3 deletions src/indent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2691,9 +2691,18 @@ void indent_text(void)

if (chunk_is_newline(chunk_get_prev(pc)))
{
indent_column_set(frm.top().indent);
reindent_line(pc, indent_column);
did_newline = false;
if ( chunk_is_token(pc, CT_MEMBER) // Issue #2890
&& language_is_set(LANG_CPP))
{
// will be done at another place
// look at the comment: XXXXXXXXXXXXXXXXXXXXXXXXXX
}
else
{
indent_column_set(frm.top().indent);
reindent_line(pc, indent_column);
did_newline = false;
}
}
//check for the series of CT_member chunks else pop it.
chunk_t *tmp = chunk_get_next_ncnlnp(pc);
Expand Down Expand Up @@ -3622,6 +3631,78 @@ void indent_text(void)
reindent_line(pc, indent_column);
}
}
else if ( chunk_is_token(pc, CT_MEMBER) // Issue #2890
&& language_is_set(LANG_CPP))
{
// comment name: XXXXXXXXXXXXXXXXXXXXXXXXXX
LOG_FMT(LINDENT, "%s(%d): orig_line is %zu, indent set to %zu, for '%s'\n",
__func__, __LINE__, pc->orig_line, indent_column, pc->text());
const size_t frm_size = frm.size();
LOG_FMT(LINDPC, "%s(%d): frm_size is %zu\n",
__func__, __LINE__, frm_size);
// get pc
LOG_FMT(LINDPC, "%s(%d): text() is '%s', (frm.at(frm_size - 1).pc)->type is %s\n",
__func__, __LINE__, (frm.at(frm_size - 1).pc)->text(), get_token_name((frm.at(frm_size - 1).pc)->type));
// get the token before
const size_t temp_ttidx = frm_size - 2;

if (temp_ttidx == 0)
{
indent_column = 1 + indent_size;
reindent_line(pc, indent_column);
}
else if (temp_ttidx > 0)
{
chunk_t *token_before = frm.at(temp_ttidx).pc;
LOG_FMT(LINDPC, "%s(%d): text() is '%s', token_before->type is %s\n",
__func__, __LINE__, token_before->text(), get_token_name(token_before->type));

size_t vor_col = 0;

if (chunk_is_token(token_before, CT_ASSIGN))
{
chunk_t *before_Assign = frm.at(temp_ttidx - 1).pc;

if (before_Assign == nullptr)
{
indent_column = 1 + indent_size;
}
else
{
vor_col = before_Assign->column;
LOG_FMT(LINDPC, "%s(%d): text() is '%s', before_Assign->type is %s, column is %zu\n",
__func__, __LINE__, before_Assign->text(), get_token_name(before_Assign->type), vor_col);
indent_column = vor_col + 2 * indent_size;
}
}
else if (chunk_is_token(token_before, CT_BRACE_OPEN))
{
vor_col = token_before->column;
LOG_FMT(LINDPC, "%s(%d): text() is '%s', token_before->type is %s, column is %zu\n",
__func__, __LINE__, token_before->text(), get_token_name(token_before->type), vor_col);
indent_column = vor_col + 2 * indent_size;
}
else if (chunk_is_token(token_before, CT_RETURN))
{
chunk_t *before_Return = frm.at(temp_ttidx - 1).pc;
vor_col = before_Return->column;
LOG_FMT(LINDPC, "%s(%d): text() is '%s', before_Return->type is %s, column is %zu\n",
__func__, __LINE__, before_Return->text(), get_token_name(before_Return->type), vor_col);
indent_column = vor_col + 2 * indent_size;
}
else
{
// TO DO
}
reindent_line(pc, indent_column);
}
else
{
LOG_FMT(LINDPC, "%s(%d): temp_ttidx is zero\n",
__func__, __LINE__);
}
reindent_line(pc, indent_column);
}
else
{
LOG_FMT(LINDENT, "%s(%d): orig_line is %zu, indent set to %zu, for '%s'\n",
Expand Down
4 changes: 4 additions & 0 deletions tests/config/Issue_2890.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
indent_with_tabs = 0
indent_member_single = true
use_indent_continue_only_once = true
nl_before_member = force
1 change: 1 addition & 0 deletions tests/cpp.test
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
30126 sp_after_angle-3.cfg cpp/sp_after_angle.cpp
30127 empty.cfg cpp/Issue_2565.cpp
30128 Issue_2873.cfg cpp/Issue_2873.cpp
30129 Issue_2890.cfg cpp/Issue_2890.cpp

30200 bug_1862.cfg cpp/bug_1862.cpp
30201 cmt_indent-1.cfg cpp/cmt_indent.cpp
Expand Down
54 changes: 54 additions & 0 deletions tests/expected/cpp/30129-Issue_2890.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <iostream>
#include <sstream>
#include <string>

struct StringBuilder
{
template <typename T>
StringBuilder& append(const T& thing)
{
ss << thing;
return *this;
}
std::string build()
{
return ss
.str();
}
std::stringstream ss;
};

int main()
{
std::string my_____String = StringBuilder()
.append(7)
.append(" + ")
.append(21)
.append(" = ")
.append(7 + 21)
.build();
std::string my_____String = StringBuilder()
.append(7)
.append(" + ")
.append(21)
.append(" = ")
.append(7 + 21)
.build();

std::cout << my___String << std::endl;
}

void function()
{
auto response = ResponseBuilder_1(1)
.setStatus_1(status)
.finish_1();

ResponseBuilder_2(request)
.setStatus_2(status)
.finish_2();

return ResponseBuilder_3(request)
.setStatus_3(status)
.finish_3();
}
45 changes: 45 additions & 0 deletions tests/input/cpp/Issue_2890.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <iostream>
#include <sstream>
#include <string>

struct StringBuilder
{
template <typename T>
StringBuilder& append(const T& thing)
{
ss << thing;
return *this;
}
std::string build()
{
return ss.str();
}
std::stringstream ss;
};

int main()
{
std::string my_____String = StringBuilder().append(7).append(" + ").append(21).append(" = ").append(7 + 21).build();
std::string my_____String = StringBuilder()
.append(7)
.append(" + ")
.append(21)
.append(" = ")
.append(7 + 21)
.build();

std::cout << my___String << std::endl;
}

void function()
{
auto response = ResponseBuilder_1(1)
.setStatus_1(status)
.finish_1();

ResponseBuilder_2(request)
.setStatus_2(status)
.finish_2();

return ResponseBuilder_3(request).setStatus_3(status).finish_3();
}

0 comments on commit a3ff639

Please sign in to comment.