[elm_code] trim all trailing whitespace on save
Rather than just blank lines remove all trailing whitespace from lines
This commit is contained in:
parent
23b3d3ffba
commit
0101b988d5
|
@ -1,6 +1,6 @@
|
||||||
2015-08-27 ajwillia.ms (Andy Williams)
|
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)
|
2015-07-17 ajwillia.ms (Andy Williams)
|
||||||
|
|
||||||
|
|
|
@ -178,12 +178,9 @@ EAPI void elm_code_file_save(Elm_Code_File *file)
|
||||||
|
|
||||||
EINA_LIST_FOREACH(file->lines, item, line_item)
|
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);
|
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(content, sizeof(char), length, out);
|
||||||
fwrite(crchars, sizeof(char), crlength, out);
|
fwrite(crchars, sizeof(char), crlength, out);
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,19 +24,22 @@ EAPI void
|
||||||
elm_code_line_text_set(Elm_Code_Line *line, const char *chars, unsigned int length)
|
elm_code_line_text_set(Elm_Code_Line *line, const char *chars, unsigned int length)
|
||||||
{
|
{
|
||||||
Elm_Code_File *file;
|
Elm_Code_File *file;
|
||||||
char *newtext;
|
char *newtext, *oldtext = NULL;
|
||||||
|
|
||||||
if (!line)
|
if (!line)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (line->modified)
|
if (line->modified)
|
||||||
free(line->modified);
|
oldtext = line->modified;
|
||||||
|
|
||||||
newtext = malloc(sizeof(char) * length);
|
newtext = malloc(sizeof(char) * length);
|
||||||
strncpy(newtext, chars, length);
|
strncpy(newtext, chars, length);
|
||||||
line->modified = newtext;
|
line->modified = newtext;
|
||||||
line->length = length;
|
line->length = length;
|
||||||
|
|
||||||
|
if (oldtext)
|
||||||
|
free(oldtext);
|
||||||
|
|
||||||
file = line->file;
|
file = line->file;
|
||||||
if (file->parent)
|
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);
|
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 */
|
/* generic text functions */
|
||||||
|
|
||||||
EAPI int
|
EAPI int
|
||||||
|
@ -238,6 +255,12 @@ elm_code_text_newlinenpos(const char *text, unsigned int length, short *nllen)
|
||||||
return crpos;
|
return crpos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Eina_Bool
|
||||||
|
_elm_code_text_char_is_whitespace(char c)
|
||||||
|
{
|
||||||
|
return c == ' ' || c == '\t';
|
||||||
|
}
|
||||||
|
|
||||||
EAPI unsigned int
|
EAPI unsigned int
|
||||||
elm_code_text_leading_whitespace_length(const char *text, unsigned int length)
|
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)
|
while (count < length)
|
||||||
{
|
{
|
||||||
if (!(*ptr == ' ' || *ptr == '\t'))
|
if (!_elm_code_text_char_is_whitespace(*ptr))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
count++;
|
count++;
|
||||||
|
@ -256,12 +279,34 @@ elm_code_text_leading_whitespace_length(const char *text, unsigned int length)
|
||||||
return count;
|
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
|
EAPI unsigned int
|
||||||
elm_code_text_is_whitespace(const char *text, unsigned int length)
|
elm_code_text_is_whitespace(const char *text, unsigned int length)
|
||||||
{
|
{
|
||||||
unsigned int leading;
|
unsigned int leading;
|
||||||
|
|
||||||
leading = elm_code_text_leading_whitespace_length(text, length);
|
leading = elm_code_text_trailing_whitespace_length(text, length);
|
||||||
|
|
||||||
return leading == length;
|
return leading == length;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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_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_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);
|
EAPI unsigned int elm_code_text_is_whitespace(const char *text, unsigned int length);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -112,6 +112,24 @@ START_TEST (elm_code_text_leading_whitespace_test)
|
||||||
}
|
}
|
||||||
END_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)
|
START_TEST (elm_code_text_is_whitespace_test)
|
||||||
{
|
{
|
||||||
const char *text;
|
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_strpos_test);
|
||||||
tcase_add_test(tc, elm_code_text_newline_position_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_leading_whitespace_test);
|
||||||
|
tcase_add_test(tc, elm_code_text_trailing_whitespace_test);
|
||||||
tcase_add_test(tc, elm_code_text_is_whitespace_test);
|
tcase_add_test(tc, elm_code_text_is_whitespace_test);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue