Logo Search packages:      
Sourcecode: uncrustify version File versions

static void newlines_if_for_while_switch_post_blank_lines ( chunk_t start,
argval_t  nl_opt 
) [static]

Add or remove extra newline after end of the block started in chunk. Doesn't do anything if close brace after it Interesting issue is that at this point, nls can be before or after vbraces VBraces will stay VBraces, conversion to real ones should have already happened "if (...)\ncode\ncode" or "if (...)\ncode\n\ncode"

Definition at line 425 of file newlines.cpp.

References chunk_get_next_ncnl(), chunk_get_next_nnl(), chunk_get_next_nvb(), chunk_get_next_type(), chunk_get_prev_nvb(), chunk_t::flags, chunk_t::level, chunk_t::nl_count, remove_next_newlines(), and chunk_t::type.

Referenced by newlines_insert_blank_lines().

{
   chunk_t *pc;
   chunk_t *next;
   chunk_t *prev;
   bool    have_pre_vbrace_nl = false;
   int     nl_count;

   if ((nl_opt == AV_IGNORE) ||
       (((start->flags & PCF_IN_PREPROC) != 0) &&
        !cpd.settings[UO_nl_define_macro].b))
   {
      return;
   }

   /* first find ending brace */
   if ((pc = get_closing_brace(start)) == NULL)
   {
      return;
   }

   /* if we're dealing with an if, we actually want to add or remove blank lines after any elses */
   if (start->type == CT_IF)
   {
      while (true)
      {
         next = chunk_get_next_ncnl(pc);
         if (next != NULL && (next->type == CT_ELSE || next->type == CT_ELSEIF))
         {
            /* point to the closing brace of the else */
            if ((pc = get_closing_brace(next)) == NULL)
            {
               return;
            }
         }
         else
         {
            break;
         }
      }
   }

   /* if we're dealing with a do/while, we actually want to add or remove blank lines after while and its condition */
   if (start->type == CT_DO)
   {
      /* point to the next semicolon */
      if ((pc = chunk_get_next_type(pc, CT_SEMICOLON, start->level)) == NULL)
      {
         return;
      }
   }

   bool isVBrace = pc->type == CT_VBRACE_CLOSE;
   if ((prev = chunk_get_prev_nvb(pc)) == NULL)
   {
      return;
   }

   have_pre_vbrace_nl = isVBrace && chunk_is_newline(prev);
   if (nl_opt & AV_REMOVE)
   {
      /* if vbrace, have to check before and after */
      /* if chunk before vbrace, remove any nls after vbrace */
      if (have_pre_vbrace_nl)
      {
         prev->nl_count = 1;
         remove_next_newlines(pc);
      }
      else if (chunk_is_newline(next = chunk_get_next_nvb(pc)))/* otherwise just deal with nls after brace */
      {
         next->nl_count = 1;
         remove_next_newlines(next);
      }
   }

   /* may have a nl before and after vbrace */
   /* don't do anything with it if the next non nl chunk is a closing brace */
   if (nl_opt & AV_ADD)
   {
      if ((next = chunk_get_next_nnl(pc)) == NULL)
      {
         return;
      }

      if (next->type != CT_BRACE_CLOSE)
      {
         /* if vbrace, have to check before and after */
         /* if chunk before vbrace, check its count */
         nl_count = have_pre_vbrace_nl ? prev->nl_count : 0;
         if (chunk_is_newline(next = chunk_get_next_nvb(pc)))
         {
            nl_count += next->nl_count;
         }
         /* if we have no newlines, add one and make it double */
         if (nl_count == 0)
         {
            if ((next = newline_add_after(pc)) == NULL)
            {
               return;
            }

            next->nl_count = 2;
         }
         else if (nl_count == 1)/* if we don't have enough newlines */
         {
            /* if we have one before vbrace, need to add one after */
            if (have_pre_vbrace_nl)
            {
               next = newline_add_after(pc);
            }
            else
            {
               /* make nl after double */
               next->nl_count = 2;
            }
         }
      }
   }
}


Generated by  Doxygen 1.6.0   Back to index