2016-11-27 15:13:42 -08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "elementary_config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Elementary.h"
|
|
|
|
|
|
|
|
#include "elm_code_private.h"
|
|
|
|
|
|
|
|
EAPI char *
|
|
|
|
elm_code_line_indent_get(const char *prevtext, unsigned int prevlength)
|
|
|
|
{
|
|
|
|
unsigned int count = 0;
|
|
|
|
char *buf, *ptr = (char *)prevtext;
|
2016-11-29 16:11:15 -08:00
|
|
|
char next, last;
|
2016-11-27 15:13:42 -08:00
|
|
|
|
|
|
|
buf = malloc((prevlength + 3) * sizeof(char));
|
|
|
|
while (count < prevlength)
|
|
|
|
{
|
|
|
|
if (!_elm_code_text_char_is_whitespace(*ptr))
|
|
|
|
break;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(buf, prevtext, count);
|
|
|
|
buf[count] = '\0';
|
|
|
|
if (count < prevlength)
|
|
|
|
{
|
|
|
|
next = *ptr;
|
2016-11-29 16:11:15 -08:00
|
|
|
last = prevtext[prevlength - 1];
|
|
|
|
|
|
|
|
// comment handling
|
2016-11-27 15:13:42 -08:00
|
|
|
// TODO this should all be based on comment SCOPE not text matching
|
|
|
|
if (next == '/')
|
|
|
|
{
|
|
|
|
if (count == prevlength - 1)
|
|
|
|
return buf;
|
|
|
|
|
|
|
|
if (*(ptr+1) == '/')
|
|
|
|
strcpy(buf + count, "//");
|
|
|
|
else if (*(ptr+1) == '*')
|
|
|
|
strcpy(buf + count, " * ");
|
|
|
|
}
|
|
|
|
else if (next == '*')
|
|
|
|
{
|
|
|
|
if (count < prevlength - 1 && *(ptr+1) == ' ')
|
|
|
|
strcpy(buf + count, "* ");
|
|
|
|
else if (count < prevlength - 1 && *(ptr+1) == '/')
|
|
|
|
{
|
|
|
|
if (count >= 1)
|
|
|
|
buf[count-1] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(buf + count, "*");
|
|
|
|
}
|
2016-11-29 16:11:15 -08:00
|
|
|
// VERY simple handling of braces
|
|
|
|
else if (last == '{')
|
|
|
|
{
|
|
|
|
strcpy(buf + count, " ");
|
|
|
|
}
|
|
|
|
else if (last == '}')
|
|
|
|
{
|
|
|
|
if (count >= 2)
|
|
|
|
buf[count-2] = '\0';
|
|
|
|
else if (count >= 1)
|
|
|
|
buf[count-1] = '\0';
|
|
|
|
}
|
2016-11-27 15:13:42 -08:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|