summaryrefslogtreecommitdiffstats
path: root/debian/uncrustify-trinity/uncrustify-trinity-0.75.0/src/align_init_brace.cpp
blob: ed1d2d39041c741293d11bfbdefbfb5772399f11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
 * @file align_init_brace.cpp
 *
 * @author  Guy Maurel
 * split from align.cpp
 * @author  Ben Gardner
 * @license GPL v2+
 */

#include "align_init_brace.h"

#include "align_log_al.h"
#include "align_tab_column.h"
#include "align_tools.h"
#include "indent.h"
#include "log_rules.h"

constexpr static auto LCURRENT = LALBR;

using namespace uncrustify;


void align_init_brace(Chunk *start)
{
   LOG_FUNC_ENTRY();

   Chunk *num_token = nullptr;

   cpd.al_cnt       = 0;
   cpd.al_c99_array = false;

   LOG_FMT(LALBR, "%s(%d): start @ orig_line is %zu, orig_col is %zu\n",
           __func__, __LINE__, start->orig_line, start->orig_col);

   Chunk *pc       = start->GetNextNcNnl();
   Chunk *pcSingle = scan_ib_line(pc, true);

   if (  pcSingle == nullptr
      || (  chunk_is_token(pcSingle, CT_BRACE_CLOSE)
         && get_chunk_parent_type(pcSingle) == CT_ASSIGN))
   {
      // single line - nothing to do
      LOG_FMT(LALBR, "%s(%d): single line - nothing to do\n", __func__, __LINE__);
      return;
   }
   LOG_FMT(LALBR, "%s(%d): is not a single line\n", __func__, __LINE__);

   do
   {
      pc = scan_ib_line(pc, false);

      // debug dump the current frame
      LOG_FMT(LALBR, "%s(%d): debug dump after, orig_line is %zu\n",
              __func__, __LINE__, pc->orig_line);
      align_log_al(LALBR, pc->orig_line);

      while (chunk_is_newline(pc))
      {
         pc = pc->GetNext();
      }
   } while (  pc != nullptr
           && pc->IsNotNullChunk()
           && pc->level > start->level);

   // debug dump the current frame
   align_log_al(LALBR, start->orig_line);

   log_rule_B("align_on_tabstop");

   if (  options::align_on_tabstop()
      && cpd.al_cnt >= 1
      && (cpd.al[0].type == CT_ASSIGN))
   {
      cpd.al[0].col = align_tab_column(cpd.al[0].col);
   }
   pc = start->GetNext();
   size_t idx = 0;

   do
   {
      Chunk *tmp;

      if (  idx == 0
         && ((tmp = skip_c99_array(pc)) != nullptr))
      {
         pc = tmp;

         LOG_FMT(LALBR, " -%zu- skipped '[] =' to %s\n",
                 pc->orig_line, get_token_name(pc->type));
         continue;
      }
      Chunk *next = pc;

      if (idx < cpd.al_cnt)
      {
         LOG_FMT(LALBR, " (%zu) check %s vs %s -- ",
                 idx, get_token_name(pc->type), get_token_name(cpd.al[idx].type));

         if (chunk_is_token(pc, cpd.al[idx].type))
         {
            if (  idx == 0
               && cpd.al_c99_array)
            {
               Chunk *prev = pc->GetPrev();

               if (chunk_is_newline(prev))
               {
                  chunk_flags_set(pc, PCF_DONT_INDENT);
               }
            }
            LOG_FMT(LALBR, " [%s] to col %zu\n", pc->Text(), cpd.al[idx].col);

            if (num_token != nullptr)
            {
               int col_diff = pc->column - num_token->column;

               reindent_line(num_token, cpd.al[idx].col - col_diff);
               //LOG_FMT(LSYS, "-= %zu =- NUM indent [%s] col=%d diff=%d\n",
               //        num_token->orig_line,
               //        num_token->Text(), cpd.al[idx - 1].col, col_diff);

               chunk_flags_set(num_token, PCF_WAS_ALIGNED);
               num_token = nullptr;
            }

            // Comma's need to 'fall back' to the previous token
            if (chunk_is_token(pc, CT_COMMA))
            {
               next = pc->GetNext();

               if (!chunk_is_newline(next))
               {
                  //LOG_FMT(LSYS, "-= %zu =- indent [%s] col=%d len=%d\n",
                  //        next->orig_line,
                  //        next->Text(), cpd.al[idx].col, cpd.al[idx].len);

                  log_rule_B("align_number_right");

                  if (  (idx < (cpd.al_cnt - 1))
                     && options::align_number_right()
                     && (  chunk_is_token(next, CT_NUMBER_FP)
                        || chunk_is_token(next, CT_NUMBER)
                        || chunk_is_token(next, CT_POS)
                        || chunk_is_token(next, CT_NEG)))
                  {
                     // Need to wait until the next match to indent numbers
                     num_token = next;
                  }
                  else if (idx < (cpd.al_cnt - 1))
                  {
                     LOG_FMT(LALBR, "%s(%d): idx is %zu, al_cnt is %zu, cpd.al[%zu].col is %zu, cpd.al[%zu].len is %zu\n",
                             __func__, __LINE__, idx, cpd.al_cnt, idx, cpd.al[idx].col, idx, cpd.al[idx].len);
                     reindent_line(next, cpd.al[idx].col + cpd.al[idx].len);
                     chunk_flags_set(next, PCF_WAS_ALIGNED);
                  }
               }
            }
            else
            {
               // first item on the line
               LOG_FMT(LALBR, "%s(%d): idx is %zu, cpd.al[%zu].col is %zu\n",
                       __func__, __LINE__, idx, idx, cpd.al[idx].col);
               reindent_line(pc, cpd.al[idx].col);
               chunk_flags_set(pc, PCF_WAS_ALIGNED);

               // see if we need to right-align a number
               log_rule_B("align_number_right");

               if (  (idx < (cpd.al_cnt - 1))
                  && options::align_number_right())
               {
                  next = pc->GetNext();

                  if (  !chunk_is_newline(next)
                     && (  chunk_is_token(next, CT_NUMBER_FP)
                        || chunk_is_token(next, CT_NUMBER)
                        || chunk_is_token(next, CT_POS)
                        || chunk_is_token(next, CT_NEG)))
                  {
                     // Need to wait until the next match to indent numbers
                     num_token = next;
                  }
               }
            }
            idx++;
         }
         else
         {
            LOG_FMT(LALBR, " no match\n");
         }
      }

      if (  chunk_is_newline(pc)
         || chunk_is_newline(next))
      {
         idx = 0;
      }
      pc = pc->GetNext();
   } while (  pc != nullptr
           && pc->IsNotNullChunk()
           && pc->level > start->level);
} // align_init_brace