elm_code: refactor auto indent about brace matching
Summary: return value of brace matching function no longer need free(). Test Plan: 1. run elementry_test - Code Editor or Edi. 2. Type some code with braces. 3. Check that closing brace has correct indent. Reviewers: ajwillia.ms Reviewed By: ajwillia.ms Subscribers: cedric, jpeg Differential Revision: https://phab.enlightenment.org/D4540
This commit is contained in:
parent
2b90e193ef
commit
65dc673ae8
|
@ -79,30 +79,34 @@ elm_code_line_indent_get(Elm_Code_Line *line)
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
EAPI char *
|
EAPI const char *
|
||||||
elm_code_line_indent_matching_braces_get(Elm_Code_Line *line)
|
elm_code_line_indent_matching_braces_get(Elm_Code_Line *line, unsigned int *length)
|
||||||
{
|
{
|
||||||
Elm_Code_File *file;
|
Elm_Code_File *file;
|
||||||
int stack, row;
|
int stack, row;
|
||||||
unsigned int length, count = 0;
|
unsigned int len_tmp, count = 0;
|
||||||
const char *content;
|
const char *content, *ptr;
|
||||||
char *buf, *ptr;
|
|
||||||
|
|
||||||
file = line->file;
|
file = line->file;
|
||||||
stack = 0;
|
stack = 0;
|
||||||
row = line->number - 1;
|
row = line->number - 1;
|
||||||
|
*length = 0;
|
||||||
|
|
||||||
while (row > 0)
|
while (row > 0)
|
||||||
{
|
{
|
||||||
line = elm_code_file_line_get(file, row);
|
line = elm_code_file_line_get(file, row);
|
||||||
content = elm_code_line_text_get(line, &length);
|
content = elm_code_line_text_get(line, &len_tmp);
|
||||||
if (memchr(content, '{', length)) stack--;
|
|
||||||
else if (memchr(content, '}', length)) stack++;
|
if (memchr(content, '{', len_tmp)) stack--;
|
||||||
|
else if (memchr(content, '}', len_tmp)) stack++;
|
||||||
|
|
||||||
if (stack < 0)
|
if (stack < 0)
|
||||||
{
|
{
|
||||||
if (length == 0) return strdup("");
|
if (len_tmp == 0)
|
||||||
ptr = (char *)content;
|
return "";
|
||||||
while (count < length)
|
|
||||||
|
ptr = content;
|
||||||
|
while (count < len_tmp)
|
||||||
{
|
{
|
||||||
if (!_elm_code_text_char_is_whitespace(*ptr))
|
if (!_elm_code_text_char_is_whitespace(*ptr))
|
||||||
break;
|
break;
|
||||||
|
@ -110,12 +114,11 @@ elm_code_line_indent_matching_braces_get(Elm_Code_Line *line)
|
||||||
count++;
|
count++;
|
||||||
ptr++;
|
ptr++;
|
||||||
}
|
}
|
||||||
buf = malloc(sizeof(char) * (count + 1));
|
|
||||||
memset(buf, ' ', count);
|
*length = count;
|
||||||
buf[count] = '\0';
|
return content;
|
||||||
return buf;
|
|
||||||
}
|
}
|
||||||
row--;
|
row--;
|
||||||
}
|
}
|
||||||
return strdup("");
|
return "";
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ extern "C" {
|
||||||
|
|
||||||
EAPI char *elm_code_line_indent_get(Elm_Code_Line *line);
|
EAPI char *elm_code_line_indent_get(Elm_Code_Line *line);
|
||||||
|
|
||||||
EAPI char *elm_code_line_indent_matching_braces_get(Elm_Code_Line *line);
|
EAPI const char *elm_code_line_indent_matching_braces_get(Elm_Code_Line *line, unsigned int *length);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
|
|
|
@ -1261,8 +1261,8 @@ _elm_code_widget_text_at_cursor_insert_do(Elm_Code_Widget *widget, const char *t
|
||||||
Elm_Code *code;
|
Elm_Code *code;
|
||||||
Elm_Code_Line *line;
|
Elm_Code_Line *line;
|
||||||
Elm_Code_Widget_Change_Info *change;
|
Elm_Code_Widget_Change_Info *change;
|
||||||
unsigned int row, col, position, col_width, curlen, indent, count;
|
unsigned int row, col, position, col_width, curlen, indent;
|
||||||
char *curtext, *leading;
|
const char *curtext, *indent_text;
|
||||||
|
|
||||||
_elm_code_widget_delete_selection(widget);
|
_elm_code_widget_delete_selection(widget);
|
||||||
code = elm_obj_code_widget_code_get(widget);
|
code = elm_obj_code_widget_code_get(widget);
|
||||||
|
@ -1276,29 +1276,18 @@ _elm_code_widget_text_at_cursor_insert_do(Elm_Code_Widget *widget, const char *t
|
||||||
}
|
}
|
||||||
if (text[0] == '}')
|
if (text[0] == '}')
|
||||||
{
|
{
|
||||||
curtext = (char *)elm_code_line_text_get(line, &curlen);
|
curtext = elm_code_line_text_get(line, &curlen);
|
||||||
|
|
||||||
count = 0;
|
if (elm_code_text_is_whitespace(curtext, line->length))
|
||||||
while (count < curlen)
|
|
||||||
{
|
{
|
||||||
if (*curtext != ' ' && *curtext != '\t')
|
indent_text = elm_code_line_indent_matching_braces_get(line, &indent);
|
||||||
break;
|
|
||||||
|
|
||||||
count++;
|
|
||||||
curtext++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count + 1 == col)
|
|
||||||
{
|
|
||||||
leading = elm_code_line_indent_matching_braces_get(line);
|
|
||||||
elm_code_line_text_leading_whitespace_strip(line);
|
elm_code_line_text_leading_whitespace_strip(line);
|
||||||
elm_code_line_text_insert(line, 0, leading, strlen(leading));
|
|
||||||
|
|
||||||
indent = elm_obj_code_widget_line_text_column_width_to_position(
|
if (indent > 0)
|
||||||
widget, line, strlen(leading));
|
elm_code_line_text_insert(line, 0, indent_text, indent);
|
||||||
elm_obj_code_widget_cursor_position_set(widget, row, indent);
|
|
||||||
|
elm_obj_code_widget_cursor_position_set(widget, row, indent + 1);
|
||||||
elm_obj_code_widget_cursor_position_get(widget, &row, &col);
|
elm_obj_code_widget_cursor_position_get(widget, &row, &col);
|
||||||
free(leading);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -91,6 +91,7 @@ START_TEST (elm_code_indent_matching_braces)
|
||||||
Elm_Code_Line *line;
|
Elm_Code_Line *line;
|
||||||
Elm_Code *code;
|
Elm_Code *code;
|
||||||
const char *str;
|
const char *str;
|
||||||
|
unsigned int str_len;
|
||||||
|
|
||||||
elm_init(1, NULL);
|
elm_init(1, NULL);
|
||||||
code = elm_code_create();
|
code = elm_code_create();
|
||||||
|
@ -100,24 +101,24 @@ START_TEST (elm_code_indent_matching_braces)
|
||||||
line = elm_code_file_line_get(file, 1);
|
line = elm_code_file_line_get(file, 1);
|
||||||
|
|
||||||
elm_code_file_line_insert(file, 1, " if ()", 8, NULL);
|
elm_code_file_line_insert(file, 1, " if ()", 8, NULL);
|
||||||
str = elm_code_line_indent_matching_braces_get(line);
|
str = elm_code_line_indent_matching_braces_get(line, &str_len);
|
||||||
ck_assert_str_eq("", str);
|
ck_assert_strn_eq(str, "", str_len);
|
||||||
|
|
||||||
elm_code_file_line_insert(file, 2, " {", 6, NULL);
|
elm_code_file_line_insert(file, 2, " {", 6, NULL);
|
||||||
str = elm_code_line_indent_matching_braces_get(line);
|
str = elm_code_line_indent_matching_braces_get(line, &str_len);
|
||||||
ck_assert_str_eq(" ", str);
|
ck_assert_strn_eq(str, " ", str_len);
|
||||||
|
|
||||||
elm_code_file_line_insert(file, 3, " if (){", 14, NULL);
|
elm_code_file_line_insert(file, 3, " if (){", 14, NULL);
|
||||||
str = elm_code_line_indent_matching_braces_get(line);
|
str = elm_code_line_indent_matching_braces_get(line, &str_len);
|
||||||
ck_assert_str_eq(" ", str);
|
ck_assert_strn_eq(str, " ", str_len);
|
||||||
|
|
||||||
elm_code_file_line_insert(file, 4, " }", 9, NULL);
|
elm_code_file_line_insert(file, 4, " }", 9, NULL);
|
||||||
str = elm_code_line_indent_matching_braces_get(line);
|
str = elm_code_line_indent_matching_braces_get(line, &str_len);
|
||||||
ck_assert_str_eq(" ", str);
|
ck_assert_strn_eq(str, " ", str_len);
|
||||||
|
|
||||||
elm_code_file_line_insert(file, 5, " }", 6, NULL);
|
elm_code_file_line_insert(file, 5, " }", 6, NULL);
|
||||||
str = elm_code_line_indent_matching_braces_get(line);
|
str = elm_code_line_indent_matching_braces_get(line, &str_len);
|
||||||
ck_assert_str_eq("", str);
|
ck_assert_strn_eq(str, "", str_len);
|
||||||
|
|
||||||
elm_code_free(code);
|
elm_code_free(code);
|
||||||
elm_shutdown();
|
elm_shutdown();
|
||||||
|
|
Loading…
Reference in New Issue