forked from enlightenment/efl
elm_code: Add indentation for matching braces
Summary: when type closing brace, it find matching braces and then indent. Test Plan: 1. run elementry_test - Code Editor or Edi. 2. Type some code with braces. 3. Check that closing braces have correct indent. Reviewers: ajwillia.ms Subscribers: cedric, jpeg Differential Revision: https://phab.enlightenment.org/D4444
This commit is contained in:
parent
6e8280a540
commit
ad5bce5cbd
|
@ -70,3 +70,43 @@ elm_code_line_indent_get(const char *prevtext, unsigned int prevlength)
|
|||
return buf;
|
||||
}
|
||||
|
||||
EAPI char *
|
||||
elm_code_line_indent_matching_braces_get(Elm_Code_Line *line)
|
||||
{
|
||||
Elm_Code_File *file;
|
||||
int stack, row;
|
||||
unsigned int length, count = 0;
|
||||
const char *content;
|
||||
char *buf, *ptr;
|
||||
|
||||
file = line->file;
|
||||
stack = 0;
|
||||
row = line->number - 1;
|
||||
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++;
|
||||
|
||||
if (stack < 0)
|
||||
{
|
||||
if (length == 0) return strdup("");
|
||||
ptr = (char *)content;
|
||||
while (count < length)
|
||||
{
|
||||
if (!_elm_code_text_char_is_whitespace(*ptr))
|
||||
break;
|
||||
|
||||
count++;
|
||||
ptr++;
|
||||
}
|
||||
buf = malloc(sizeof(char) * (count + 1));
|
||||
memset(buf, ' ', count);
|
||||
buf[count] = '\0';
|
||||
return buf;
|
||||
}
|
||||
row--;
|
||||
}
|
||||
return strdup("");
|
||||
}
|
||||
|
|
|
@ -22,6 +22,8 @@ extern "C" {
|
|||
|
||||
EAPI char *elm_code_line_indent_get(const char *prevtext, unsigned int prevlength);
|
||||
|
||||
EAPI char *elm_code_line_indent_matching_braces_get(Elm_Code_Line *line);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
|
|
@ -775,7 +775,7 @@ _popup_menu_show(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
|
|||
(pd->hoversel, "Copy", NULL, ELM_ICON_NONE,
|
||||
_popup_menu_copy_cb, obj);
|
||||
if (pd->editable)
|
||||
{
|
||||
{
|
||||
elm_hoversel_item_add
|
||||
(pd->hoversel, "Paste", NULL, ELM_ICON_NONE,
|
||||
_popup_menu_paste_cb, obj);
|
||||
|
@ -1217,7 +1217,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;
|
||||
unsigned int row, col, position, col_width, curlen, indent, count;
|
||||
char *curtext, *leading;
|
||||
|
||||
_elm_code_widget_delete_selection(widget);
|
||||
code = elm_obj_code_widget_code_get(widget);
|
||||
|
@ -1229,6 +1230,33 @@ _elm_code_widget_text_at_cursor_insert_do(Elm_Code_Widget *widget, const char *t
|
|||
row = elm_code_file_lines_get(code->file);
|
||||
line = elm_code_file_line_get(code->file, row);
|
||||
}
|
||||
if (text[0] == '}')
|
||||
{
|
||||
curtext = (char *)elm_code_line_text_get(line, &curlen);
|
||||
|
||||
count = 0;
|
||||
while (count < curlen)
|
||||
{
|
||||
if (!_elm_code_text_char_is_whitespace(*curtext))
|
||||
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_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, indent, row);
|
||||
elm_obj_code_widget_cursor_position_get(widget, &col, &row);
|
||||
free(leading);
|
||||
}
|
||||
}
|
||||
|
||||
position = elm_code_widget_line_text_position_for_column_get(widget, line, col);
|
||||
elm_code_line_text_insert(line, position, text, length);
|
||||
|
|
|
@ -60,9 +60,47 @@ START_TEST (elm_code_indent_simple_braces)
|
|||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST (elm_code_indent_matching_braces)
|
||||
{
|
||||
Elm_Code_File *file;
|
||||
Elm_Code_Line *line;
|
||||
Elm_Code *code;
|
||||
const char *str;
|
||||
unsigned int row;
|
||||
|
||||
elm_init(1, NULL);
|
||||
code = elm_code_create();
|
||||
file = elm_code_file_new(code);
|
||||
|
||||
line = elm_code_file_line_append(file, " if ()", 8, NULL);
|
||||
str = elm_code_line_indent_matching_braces_get(line);
|
||||
ck_assert_str_eq("", str);
|
||||
|
||||
line = elm_code_file_line_append(file, " {", 6, NULL);
|
||||
str = elm_code_line_indent_matching_braces_get(line);
|
||||
ck_assert_str_eq(" ", str);
|
||||
|
||||
line = elm_code_file_line_append(file, " if (){", 14, NULL);
|
||||
str = elm_code_line_indent_matching_braces_get(line;
|
||||
ck_assert_str_eq(" ", str);
|
||||
|
||||
line = elm_code_file_line_append(file, " }", 9, NULL);
|
||||
str = elm_code_line_indent_matching_braces_get(line);
|
||||
ck_assert_str_eq(" ", str);
|
||||
|
||||
line = elm_code_file_line_append(file, " }", 6, NULL);
|
||||
str = elm_code_line_indent_matching_braces_get(line);
|
||||
ck_assert_str_eq("", str);
|
||||
|
||||
elm_code_free(code);
|
||||
elm_shutdown();
|
||||
}
|
||||
END_TEST
|
||||
|
||||
void elm_code_test_indent(TCase *tc)
|
||||
{
|
||||
tcase_add_test(tc, elm_code_indent_whitespace_test);
|
||||
tcase_add_test(tc, elm_code_indent_comments_test);
|
||||
tcase_add_test(tc, elm_code_indent_simple_braces);
|
||||
tcase_add_test(tc, elm_code_indent_matching_braces);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue