[elm_code] trim all trailing whitespace on save

Rather than just blank lines remove all trailing whitespace from lines
This commit is contained in:
Andy Williams 2015-08-29 12:05:56 +01:00
parent 23b3d3ffba
commit 0101b988d5
5 changed files with 75 additions and 10 deletions

View File

@ -1,6 +1,6 @@
2015-08-27 ajwillia.ms (Andy Williams)
* Trim lines that are purely whitespace during save
* Trim trailing whitespace from lines during save
2015-07-17 ajwillia.ms (Andy Williams)

View File

@ -178,12 +178,9 @@ EAPI void elm_code_file_save(Elm_Code_File *file)
EINA_LIST_FOREACH(file->lines, item, line_item)
{
elm_code_line_text_trailing_whitespace_strip(line_item);
content = elm_code_line_text_get(line_item, &length);
if (elm_code_text_is_whitespace(content, length))
{
length = 0;
elm_code_line_text_set(line_item, "", 0);
}
fwrite(content, sizeof(char), length, out);
fwrite(crchars, sizeof(char), crlength, out);
}

View File

@ -24,19 +24,22 @@ EAPI void
elm_code_line_text_set(Elm_Code_Line *line, const char *chars, unsigned int length)
{
Elm_Code_File *file;
char *newtext;
char *newtext, *oldtext = NULL;
if (!line)
return;
if (line->modified)
free(line->modified);
oldtext = line->modified;
newtext = malloc(sizeof(char) * length);
strncpy(newtext, chars, length);
line->modified = newtext;
line->length = length;
if (oldtext)
free(oldtext);
file = line->file;
if (file->parent)
{
@ -207,6 +210,20 @@ elm_code_line_text_remove(Elm_Code_Line *line, unsigned int position, int length
elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, line);
}
EAPI void elm_code_line_text_trailing_whitespace_strip(Elm_Code_Line *line)
{
unsigned int length, trailing;
const char *content;
content = elm_code_line_text_get(line, &length);
trailing = elm_code_text_trailing_whitespace_length(content, length);
if (trailing == 0)
return;
length -= trailing;;
elm_code_line_text_set(line, content, length);
}
/* generic text functions */
EAPI int
@ -238,6 +255,12 @@ elm_code_text_newlinenpos(const char *text, unsigned int length, short *nllen)
return crpos;
}
static Eina_Bool
_elm_code_text_char_is_whitespace(char c)
{
return c == ' ' || c == '\t';
}
EAPI unsigned int
elm_code_text_leading_whitespace_length(const char *text, unsigned int length)
{
@ -246,7 +269,7 @@ elm_code_text_leading_whitespace_length(const char *text, unsigned int length)
while (count < length)
{
if (!(*ptr == ' ' || *ptr == '\t'))
if (!_elm_code_text_char_is_whitespace(*ptr))
break;
count++;
@ -256,12 +279,34 @@ elm_code_text_leading_whitespace_length(const char *text, unsigned int length)
return count;
}
EAPI unsigned int
elm_code_text_trailing_whitespace_length(const char *text, unsigned int length)
{
unsigned int count = 0;
char *ptr;
if (length == 0)
return 0;
ptr = (char *)text + length - 1;
while (count < length)
{
if (!_elm_code_text_char_is_whitespace(*ptr))
break;
count++;
ptr--;
}
return count;
}
EAPI unsigned int
elm_code_text_is_whitespace(const char *text, unsigned int length)
{
unsigned int leading;
leading = elm_code_text_leading_whitespace_length(text, length);
leading = elm_code_text_trailing_whitespace_length(text, length);
return leading == length;
}

View File

@ -36,6 +36,8 @@ EAPI void elm_code_line_text_insert(Elm_Code_Line *line, unsigned int position,
EAPI void elm_code_line_text_remove(Elm_Code_Line *line, unsigned int position, int length);
EAPI void elm_code_line_text_trailing_whitespace_strip(Elm_Code_Line *line);
/**
* @}
*
@ -54,6 +56,8 @@ EAPI int elm_code_text_newlinenpos(const char *text, unsigned int length, short
EAPI unsigned int elm_code_text_leading_whitespace_length(const char *text, unsigned int length);
EAPI unsigned int elm_code_text_trailing_whitespace_length(const char *text, unsigned int length);
EAPI unsigned int elm_code_text_is_whitespace(const char *text, unsigned int length);
/**

View File

@ -112,6 +112,24 @@ START_TEST (elm_code_text_leading_whitespace_test)
}
END_TEST
START_TEST (elm_code_text_trailing_whitespace_test)
{
const char *text;
text = "testing";
ck_assert_int_eq(0, elm_code_text_trailing_whitespace_length(text, strlen(text)));
text = "spaces ";
ck_assert_int_eq(2, elm_code_text_trailing_whitespace_length(text, strlen(text)));
text = "tabs\t\t";
ck_assert_int_eq(2, elm_code_text_trailing_whitespace_length(text, strlen(text)));
text = "mix \t ";
ck_assert_int_eq(3, elm_code_text_trailing_whitespace_length(text, strlen(text)));
}
END_TEST
START_TEST (elm_code_text_is_whitespace_test)
{
const char *text;
@ -135,5 +153,6 @@ void elm_code_test_text(TCase *tc)
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);
tcase_add_test(tc, elm_code_text_trailing_whitespace_test);
tcase_add_test(tc, elm_code_text_is_whitespace_test);
}