mirror of
https://github.com/neovim/neovim.git
synced 2025-10-15 06:16:08 +00:00
refactor: collapse statements in single assignments
Problem: Variables are often assigned multiple places in common patterns. Solution: Replace these common patterns with different patterns that reduce the number of assignments. Use `MAX` and `MIN`: ```c if (x < y) { x = y; } // --> x = MAX(x, y); ``` ```c if (x > y) { x = y; } // --> x = MIN(x, y); ``` Use ternary: ```c int a; if (cond) { a = b; } els { a = c; } // --> int a = cond ? b : c; ```
This commit is contained in:

committed by
Lewis Russell

parent
1b5a394ffd
commit
d1bd3d643e
@@ -390,13 +390,8 @@ static bool do_incsearch_highlighting(int firstc, int *search_delim, incsearch_s
|
||||
parse_cmd_address(&ea, &dummy, true);
|
||||
if (ea.addr_count > 0) {
|
||||
// Allow for reverse match.
|
||||
if (ea.line2 < ea.line1) {
|
||||
search_first_line = ea.line2;
|
||||
search_last_line = ea.line1;
|
||||
} else {
|
||||
search_first_line = ea.line1;
|
||||
search_last_line = ea.line2;
|
||||
}
|
||||
search_first_line = MIN(ea.line1, ea.line1);
|
||||
search_last_line = MAX(ea.line2, ea.line1);
|
||||
} else if (cmd[0] == 's' && cmd[1] != 'o') {
|
||||
// :s defaults to the current line
|
||||
search_first_line = curwin->w_cursor.lnum;
|
||||
@@ -1034,11 +1029,7 @@ static int command_line_handle_ctrl_bsl(CommandLineState *s)
|
||||
|
||||
// Restore the cursor or use the position set with
|
||||
// set_cmdline_pos().
|
||||
if (new_cmdpos > ccline.cmdlen) {
|
||||
ccline.cmdpos = ccline.cmdlen;
|
||||
} else {
|
||||
ccline.cmdpos = new_cmdpos;
|
||||
}
|
||||
ccline.cmdpos = MIN(ccline.cmdlen, new_cmdpos);
|
||||
|
||||
KeyTyped = false; // Don't do p_wc completion.
|
||||
redrawcmd();
|
||||
@@ -1658,11 +1649,7 @@ static int command_line_insert_reg(CommandLineState *s)
|
||||
KeyTyped = false; // Don't do p_wc completion.
|
||||
if (new_cmdpos >= 0) {
|
||||
// set_cmdline_pos() was used
|
||||
if (new_cmdpos > ccline.cmdlen) {
|
||||
ccline.cmdpos = ccline.cmdlen;
|
||||
} else {
|
||||
ccline.cmdpos = new_cmdpos;
|
||||
}
|
||||
ccline.cmdpos = MIN(ccline.cmdlen, new_cmdpos);
|
||||
}
|
||||
}
|
||||
new_cmdpos = save_new_cmdpos;
|
||||
@@ -3546,10 +3533,6 @@ void unputcmdline(void)
|
||||
// called afterwards.
|
||||
void put_on_cmdline(const char *str, int len, bool redraw)
|
||||
{
|
||||
int i;
|
||||
int m;
|
||||
int c;
|
||||
|
||||
if (len < 0) {
|
||||
len = (int)strlen(str);
|
||||
}
|
||||
@@ -3563,7 +3546,8 @@ void put_on_cmdline(const char *str, int len, bool redraw)
|
||||
ccline.cmdlen += len;
|
||||
} else {
|
||||
// Count nr of characters in the new string.
|
||||
m = 0;
|
||||
int m = 0;
|
||||
int i;
|
||||
for (i = 0; i < len; i += utfc_ptr2len(str + i)) {
|
||||
m++;
|
||||
}
|
||||
@@ -3587,8 +3571,8 @@ void put_on_cmdline(const char *str, int len, bool redraw)
|
||||
{
|
||||
// When the inserted text starts with a composing character,
|
||||
// backup to the character before it. There could be two of them.
|
||||
i = 0;
|
||||
c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
|
||||
int i = 0;
|
||||
int c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
|
||||
while (ccline.cmdpos > 0 && utf_iscomposing(c)) {
|
||||
i = utf_head_off(ccline.cmdbuff, ccline.cmdbuff + ccline.cmdpos - 1) + 1;
|
||||
ccline.cmdpos -= i;
|
||||
@@ -3619,7 +3603,7 @@ void put_on_cmdline(const char *str, int len, bool redraw)
|
||||
|
||||
if (redraw && !cmd_silent) {
|
||||
msg_no_more = true;
|
||||
i = cmdline_row;
|
||||
int i = cmdline_row;
|
||||
cursorcmd();
|
||||
draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
|
||||
// Avoid clearing the rest of the line too often.
|
||||
@@ -3628,6 +3612,7 @@ void put_on_cmdline(const char *str, int len, bool redraw)
|
||||
}
|
||||
msg_no_more = false;
|
||||
}
|
||||
int m;
|
||||
if (KeyTyped) {
|
||||
m = Columns * Rows;
|
||||
if (m < 0) { // overflow, Columns or Rows at weird value
|
||||
@@ -3636,8 +3621,8 @@ void put_on_cmdline(const char *str, int len, bool redraw)
|
||||
} else {
|
||||
m = MAXCOL;
|
||||
}
|
||||
for (i = 0; i < len; i++) {
|
||||
c = cmdline_charsize(ccline.cmdpos);
|
||||
for (int i = 0; i < len; i++) {
|
||||
int c = cmdline_charsize(ccline.cmdpos);
|
||||
// count ">" for a double-wide char that doesn't fit.
|
||||
correct_screencol(ccline.cmdpos, c, &ccline.cmdspos);
|
||||
// Stop cursor at the end of the screen, but do increment the
|
||||
@@ -3647,9 +3632,7 @@ void put_on_cmdline(const char *str, int len, bool redraw)
|
||||
ccline.cmdspos += c;
|
||||
}
|
||||
c = utfc_ptr2len(ccline.cmdbuff + ccline.cmdpos) - 1;
|
||||
if (c > len - i - 1) {
|
||||
c = len - i - 1;
|
||||
}
|
||||
c = MIN(c, len - i - 1);
|
||||
ccline.cmdpos += c;
|
||||
i += c;
|
||||
ccline.cmdpos++;
|
||||
@@ -3886,17 +3869,13 @@ void cursorcmd(void)
|
||||
}
|
||||
|
||||
if (ui_has(kUICmdline)) {
|
||||
if (ccline.redraw_state < kCmdRedrawPos) {
|
||||
ccline.redraw_state = kCmdRedrawPos;
|
||||
}
|
||||
ccline.redraw_state = MAX(ccline.redraw_state, kCmdRedrawPos);
|
||||
return;
|
||||
}
|
||||
|
||||
msg_row = cmdline_row + (ccline.cmdspos / Columns);
|
||||
msg_col = ccline.cmdspos % Columns;
|
||||
if (msg_row >= Rows) {
|
||||
msg_row = Rows - 1;
|
||||
}
|
||||
msg_row = MIN(msg_row, Rows - 1);
|
||||
|
||||
msg_cursor_goto(msg_row, msg_col);
|
||||
}
|
||||
@@ -4192,11 +4171,7 @@ static int set_cmdline_pos(int pos)
|
||||
|
||||
// The position is not set directly but after CTRL-\ e or CTRL-R = has
|
||||
// changed the command line.
|
||||
if (pos < 0) {
|
||||
new_cmdpos = 0;
|
||||
} else {
|
||||
new_cmdpos = pos;
|
||||
}
|
||||
new_cmdpos = MAX(0, pos);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user