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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
Elm_Code_File *file;
|
||||
int stack, row;
|
||||
unsigned int length, count = 0;
|
||||
const char *content;
|
||||
char *buf, *ptr;
|
||||
unsigned int len_tmp, count = 0;
|
||||
const char *content, *ptr;
|
||||
|
||||
file = line->file;
|
||||
stack = 0;
|
||||
row = line->number - 1;
|
||||
*length = 0;
|
||||
|
||||
while (row > 0)
|
||||
{
|
||||
line = elm_code_file_line_get(file, row);
|
||||
content = elm_code_line_text_get(line, &length);
|
||||
if (memchr(content, '{', length)) stack--;
|
||||
else if (memchr(content, '}', length)) stack++;
|
||||
content = elm_code_line_text_get(line, &len_tmp);
|
||||
|
||||
if (memchr(content, '{', len_tmp)) stack--;
|
||||
else if (memchr(content, '}', len_tmp)) stack++;
|
||||
|
||||
if (stack < 0)
|
||||
{
|
||||
if (length == 0) return strdup("");
|
||||
ptr = (char *)content;
|
||||
while (count < length)
|
||||
if (len_tmp == 0)
|
||||
return "";
|
||||
|
||||
ptr = content;
|
||||
while (count < len_tmp)
|
||||
{
|
||||
if (!_elm_code_text_char_is_whitespace(*ptr))
|
||||
break;
|
||||
|
@ -110,12 +114,11 @@ elm_code_line_indent_matching_braces_get(Elm_Code_Line *line)
|
|||
count++;
|
||||
ptr++;
|
||||
}
|
||||
buf = malloc(sizeof(char) * (count + 1));
|
||||
memset(buf, ' ', count);
|
||||
buf[count] = '\0';
|
||||
return buf;
|
||||
|
||||
*length = count;
|
||||
return content;
|
||||
}
|
||||
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_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_Line *line;
|
||||
Elm_Code_Widget_Change_Info *change;
|
||||
unsigned int row, col, position, col_width, curlen, indent, count;
|
||||
char *curtext, *leading;
|
||||
unsigned int row, col, position, col_width, curlen, indent;
|
||||
const char *curtext, *indent_text;
|
||||
|
||||
_elm_code_widget_delete_selection(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] == '}')
|
||||
{
|
||||
curtext = (char *)elm_code_line_text_get(line, &curlen);
|
||||
curtext = elm_code_line_text_get(line, &curlen);
|
||||
|
||||
count = 0;
|
||||
while (count < curlen)
|
||||
if (elm_code_text_is_whitespace(curtext, line->length))
|
||||
{
|
||||
if (*curtext != ' ' && *curtext != '\t')
|
||||
break;
|
||||
|
||||
count++;
|
||||
curtext++;
|
||||
}
|
||||
|
||||
if (count + 1 == col)
|
||||
{
|
||||
leading = elm_code_line_indent_matching_braces_get(line);
|
||||
indent_text = elm_code_line_indent_matching_braces_get(line, &indent);
|
||||
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(
|
||||
widget, line, strlen(leading));
|
||||
elm_obj_code_widget_cursor_position_set(widget, row, indent);
|
||||
if (indent > 0)
|
||||
elm_code_line_text_insert(line, 0, indent_text, indent);
|
||||
|
||||
elm_obj_code_widget_cursor_position_set(widget, row, indent + 1);
|
||||
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 *code;
|
||||
const char *str;
|
||||
unsigned int str_len;
|
||||
|
||||
elm_init(1, NULL);
|
||||
code = elm_code_create();
|
||||
|
@ -100,24 +101,24 @@ START_TEST (elm_code_indent_matching_braces)
|
|||
line = elm_code_file_line_get(file, 1);
|
||||
|
||||
elm_code_file_line_insert(file, 1, " if ()", 8, NULL);
|
||||
str = elm_code_line_indent_matching_braces_get(line);
|
||||
ck_assert_str_eq("", str);
|
||||
str = elm_code_line_indent_matching_braces_get(line, &str_len);
|
||||
ck_assert_strn_eq(str, "", str_len);
|
||||
|
||||
elm_code_file_line_insert(file, 2, " {", 6, NULL);
|
||||
str = elm_code_line_indent_matching_braces_get(line);
|
||||
ck_assert_str_eq(" ", str);
|
||||
str = elm_code_line_indent_matching_braces_get(line, &str_len);
|
||||
ck_assert_strn_eq(str, " ", str_len);
|
||||
|
||||
elm_code_file_line_insert(file, 3, " if (){", 14, NULL);
|
||||
str = elm_code_line_indent_matching_braces_get(line);
|
||||
ck_assert_str_eq(" ", str);
|
||||
str = elm_code_line_indent_matching_braces_get(line, &str_len);
|
||||
ck_assert_strn_eq(str, " ", str_len);
|
||||
|
||||
elm_code_file_line_insert(file, 4, " }", 9, NULL);
|
||||
str = elm_code_line_indent_matching_braces_get(line);
|
||||
ck_assert_str_eq(" ", str);
|
||||
str = elm_code_line_indent_matching_braces_get(line, &str_len);
|
||||
ck_assert_strn_eq(str, " ", str_len);
|
||||
|
||||
elm_code_file_line_insert(file, 5, " }", 6, NULL);
|
||||
str = elm_code_line_indent_matching_braces_get(line);
|
||||
ck_assert_str_eq("", str);
|
||||
str = elm_code_line_indent_matching_braces_get(line, &str_len);
|
||||
ck_assert_strn_eq(str, "", str_len);
|
||||
|
||||
elm_code_free(code);
|
||||
elm_shutdown();
|
||||
|
|
Loading…
Reference in New Issue