elm_code: refactoring of the elm_code_line_indent to work with lines

This commit is contained in:
Andy Williams 2016-12-03 15:59:40 +00:00
parent 84a0f047db
commit 92eacde6e7
4 changed files with 72 additions and 38 deletions

View File

@ -7,12 +7,21 @@
#include "elm_code_private.h"
EAPI char *
elm_code_line_indent_get(const char *prevtext, unsigned int prevlength)
elm_code_line_indent_get(Elm_Code_Line *line)
{
unsigned int count = 0;
char *buf, *ptr = (char *)prevtext;
Elm_Code_Line *prevline;
const char *prevtext;
unsigned int prevlength, count = 0;
char *buf, *ptr;
char next, last;
if (line->number <= 1)
return strdup("");
prevline = elm_code_file_line_get(line->file, line->number - 1);
prevtext = elm_code_line_text_get(prevline, &prevlength);
ptr = (char *)prevtext;
buf = malloc((prevlength + 3) * sizeof(char));
while (count < prevlength)
{

View File

@ -20,7 +20,7 @@ extern "C" {
*
*/
EAPI char *elm_code_line_indent_get(const char *prevtext, unsigned int prevlength);
EAPI char *elm_code_line_indent_get(Elm_Code_Line *line);
EAPI char *elm_code_line_indent_matching_braces_get(Elm_Code_Line *line);

View File

@ -1237,8 +1237,8 @@ _elm_code_widget_text_at_cursor_insert_do(Elm_Code_Widget *widget, const char *t
count = 0;
while (count < curlen)
{
if (!_elm_code_text_char_is_whitespace(*curtext))
break;
if (*curtext != ' ' && *curtext != '\t')
break;
count++;
curtext++;
@ -1338,7 +1338,7 @@ _elm_code_widget_newline(Elm_Code_Widget *widget)
width = elm_code_widget_line_text_column_width_get(widget, line);
line = elm_code_file_line_get(code->file, row + 1);
leading = elm_code_line_indent_get(oldtext, oldlen);
leading = elm_code_line_indent_get(line);
elm_code_line_text_leading_whitespace_strip(line);
elm_code_line_text_insert(line, 0, leading, strlen(leading));
free(oldtext);

View File

@ -8,55 +8,80 @@
#include "Elementary.h"
#include "elm_code_indent.h"
static void
_indent_check(Elm_Code_File *file, const char *prev, const char *expected)
{
Elm_Code_Line *line;
char *str;
elm_code_file_clear(file);
elm_code_file_line_append(file, prev, strlen(prev), NULL);
elm_code_file_line_append(file, "", 0, NULL);
line = elm_code_file_line_get(file, 2);
str = elm_code_line_indent_get(line);
ck_assert_str_eq(expected, str);
free(str);
}
START_TEST (elm_code_indent_whitespace_test)
{
const char *str;
Elm_Code *code;
Elm_Code_File *file;
str = elm_code_line_indent_get("", 0);
ck_assert_str_eq("", str);
str = elm_code_line_indent_get(" ", 2);
ck_assert_str_eq(" ", str);
str = elm_code_line_indent_get("\t", 1);
ck_assert_str_eq("\t", str);
str = elm_code_line_indent_get("\t ", 3);
ck_assert_str_eq("\t ", str);
elm_init(1, NULL);
code = elm_code_create();
file = elm_code_file_new(code);
_indent_check(file, "", "");
_indent_check(file, " ", " ");
_indent_check(file, "\t", "\t");
_indent_check(file, "\t ", "\t ");
elm_shutdown();
}
END_TEST
START_TEST (elm_code_indent_comments_test)
{
const char *str;
Elm_Code *code;
Elm_Code_File *file;
str = elm_code_line_indent_get(" /**", 4);
ck_assert_str_eq(" * ", str);
str = elm_code_line_indent_get(" * ", 4);
ck_assert_str_eq(" * ", str);
str = elm_code_line_indent_get(" */", 4);
ck_assert_str_eq(" ", str);
str = elm_code_line_indent_get("\t//", 3);
ck_assert_str_eq("\t//", str);
elm_init(1, NULL);
code = elm_code_create();
file = elm_code_file_new(code);
_indent_check(file, " /**", " * ");
_indent_check(file, " * ", " * ");
_indent_check(file, " */", " ");
_indent_check(file, "\t//", "\t//");
// test these are not comments
str = elm_code_line_indent_get(" / ", 3);
ck_assert_str_eq(" ", str);
str = elm_code_line_indent_get(" hi//", 5);
ck_assert_str_eq(" ", str);
_indent_check(file, " / ", " ");
_indent_check(file, " hi//", " ");
elm_shutdown();
}
END_TEST
START_TEST (elm_code_indent_simple_braces)
{
const char *str;
Elm_Code *code;
Elm_Code_File *file;
str = elm_code_line_indent_get("if() {", 6);
ck_assert_str_eq(" ", str);
str = elm_code_line_indent_get("}", 1);
ck_assert_str_eq("", str);
elm_init(1, NULL);
code = elm_code_create();
file = elm_code_file_new(code);
str = elm_code_line_indent_get(" {", 3);
ck_assert_str_eq(" ", str);
str = elm_code_line_indent_get(" }", 3);
ck_assert_str_eq("", str);
_indent_check(file, "if() {", " ");
_indent_check(file, "}", "");
_indent_check(file, " {", " ");
_indent_check(file, " }", "");
elm_shutdown();
}
END_TEST