elm_code: indent lines to match whitespace above

We can look at more complex scope based indenting later
This commit is contained in:
Andy Williams 2015-06-07 23:11:25 +01:00
parent 1998483d06
commit 866d22c40b
5 changed files with 52 additions and 2 deletions

View File

@ -1,3 +1,7 @@
2015-06-07 ajwillia.ms (Andy Williams)
* Indent newlines to match whitespace in the previous line
2015-05-31 ajwillia.ms (Andy Williams)
* Add launch configuration so we can run a built binary

View File

@ -244,3 +244,21 @@ elm_code_text_newlinenpos(const char *text, unsigned int length, short *nllen)
return crpos;
}
EAPI unsigned int
elm_code_text_leading_whitespace_length(const char *text, unsigned int length)
{
unsigned int count = 0;
char *ptr = (char *)text;
while (count < length)
{
if (!(*ptr == ' ' || *ptr == '\t'))
break;
count++;
ptr++;
}
return count;
}

View File

@ -54,6 +54,8 @@ EAPI int elm_code_text_strnpos(const char *text, unsigned int length, const char
EAPI int elm_code_text_newlinenpos(const char *text, unsigned int length, short *nllen);
EAPI unsigned int elm_code_text_leading_whitespace_length(const char *text, unsigned int length);
/**
* @}
*/

View File

@ -975,19 +975,26 @@ _elm_code_widget_newline(Elm_Code_Widget *widget)
{
Elm_Code *code;
Elm_Code_Line *line;
unsigned int row, col, position;
unsigned int row, col, position, oldlen, leading;
const char *oldtext;
_elm_code_widget_delete_selection(widget);
eo_do(widget,
code = elm_obj_code_widget_code_get(),
elm_obj_code_widget_cursor_position_get(&col, &row));
line = elm_code_file_line_get(code->file, row);
oldtext = elm_code_line_text_get(line, &oldlen);
position = elm_code_widget_line_text_position_for_column_get(widget, line, col);
elm_code_line_split_at(line, position);
line = elm_code_file_line_get(code->file, row + 1);
leading = elm_code_text_leading_whitespace_length(oldtext, oldlen);
elm_code_line_text_insert(line, 0, oldtext, leading);
eo_do(widget,
elm_obj_code_widget_cursor_position_set(1, row + 1),
elm_obj_code_widget_cursor_position_set(
elm_obj_code_widget_line_text_column_width_to_position(line, leading), row + 1),
// TODO construct and pass a change object
eo_event_callback_call(ELM_CODE_WIDGET_EVENT_CHANGED_USER, NULL));
}

View File

@ -94,6 +94,24 @@ START_TEST (elm_code_text_newline_position_test)
}
END_TEST
START_TEST (elm_code_text_leading_whitespace_test)
{
const char *text;
text = "testing";
ck_assert_int_eq(0, elm_code_text_leading_whitespace_length(text, strlen(text)));
text = " spaces";
ck_assert_int_eq(2, elm_code_text_leading_whitespace_length(text, strlen(text)));
text = "\t\ttabs";
ck_assert_int_eq(2, elm_code_text_leading_whitespace_length(text, strlen(text)));
text = " \t mix";
ck_assert_int_eq(3, elm_code_text_leading_whitespace_length(text, strlen(text)));
}
END_TEST
void elm_code_test_text(TCase *tc)
{
tcase_add_test(tc, elm_code_text_get_test);
@ -101,4 +119,5 @@ void elm_code_test_text(TCase *tc)
tcase_add_test(tc, elm_code_text_contains_test);
tcase_add_test(tc, elm_code_text_strpos_test);
tcase_add_test(tc, elm_code_text_newline_position_test);
tcase_add_test(tc, elm_code_text_leading_whitespace_test);
}