2016-11-27 15:13:42 -08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "elementary_config.h"
|
|
|
|
#endif
|
|
|
|
|
2017-01-06 01:38:02 -08:00
|
|
|
#include "regex.h"
|
2016-11-27 15:13:42 -08:00
|
|
|
#include "Elementary.h"
|
|
|
|
|
|
|
|
#include "elm_code_private.h"
|
|
|
|
|
2017-01-06 01:38:02 -08:00
|
|
|
static Eina_Bool
|
|
|
|
elm_code_line_indent_startswith_keyword(Elm_Code_Line *line)
|
|
|
|
{
|
|
|
|
regex_t regex;
|
|
|
|
char *text;
|
2017-02-18 13:41:53 -08:00
|
|
|
int ret;
|
2017-01-06 01:38:02 -08:00
|
|
|
unsigned int textlen;
|
|
|
|
|
|
|
|
text = (char *)elm_code_line_text_get(line, &textlen);
|
|
|
|
text = strndup(text, textlen);
|
|
|
|
|
2017-02-18 13:41:53 -08:00
|
|
|
ret = regcomp(®ex, "^\\s*("
|
|
|
|
"((if|else\\s*if|while|for|switch)\\s*\\(.*\\)\\s*\\{?)|"
|
|
|
|
"((else|do)\\s*\\{?)|"
|
|
|
|
"(case\\s+.+:)|"
|
|
|
|
"(default:)"
|
|
|
|
")\\s*$", REG_EXTENDED | REG_NOSUB);
|
2017-02-19 06:15:29 -08:00
|
|
|
if (ret == 0)
|
|
|
|
ret = regexec(®ex, text, 0, NULL, 0);
|
2017-01-06 01:38:02 -08:00
|
|
|
|
2017-02-18 13:41:53 -08:00
|
|
|
regfree(®ex);
|
2017-01-06 01:38:02 -08:00
|
|
|
free(text);
|
|
|
|
|
2017-02-18 13:41:53 -08:00
|
|
|
return ret == 0;
|
2017-01-06 01:38:02 -08:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:13:42 -08:00
|
|
|
EAPI char *
|
2016-12-03 07:59:40 -08:00
|
|
|
elm_code_line_indent_get(Elm_Code_Line *line)
|
2016-11-27 15:13:42 -08:00
|
|
|
{
|
2016-12-03 07:59:40 -08:00
|
|
|
Elm_Code_Line *prevline;
|
|
|
|
const char *prevtext;
|
|
|
|
unsigned int prevlength, count = 0;
|
|
|
|
char *buf, *ptr;
|
2016-11-29 16:11:15 -08:00
|
|
|
char next, last;
|
2018-03-24 03:39:32 -07:00
|
|
|
const char *indent = "\t";
|
|
|
|
Eina_Bool eflindent = ((Elm_Code *)line->file->parent)->config.indent_style_efl;
|
2016-11-27 15:13:42 -08:00
|
|
|
|
2016-12-03 07:59:40 -08:00
|
|
|
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;
|
2017-01-06 01:38:02 -08:00
|
|
|
buf = malloc((prevlength + 5) * sizeof(char));
|
2016-11-27 15:13:42 -08:00
|
|
|
while (count < prevlength)
|
|
|
|
{
|
|
|
|
if (!_elm_code_text_char_is_whitespace(*ptr))
|
|
|
|
break;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(buf, prevtext, count);
|
|
|
|
buf[count] = '\0';
|
2017-01-06 01:38:02 -08:00
|
|
|
|
2018-03-24 03:39:32 -07:00
|
|
|
if (eflindent)
|
2017-01-06 01:38:02 -08:00
|
|
|
{
|
2018-03-24 03:39:32 -07:00
|
|
|
indent = " ";
|
|
|
|
if (elm_code_line_indent_startswith_keyword(prevline))
|
|
|
|
{
|
|
|
|
strcpy(buf + count, " ");
|
|
|
|
count += 2;
|
|
|
|
}
|
2017-01-06 01:38:02 -08:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:13:42 -08:00
|
|
|
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, "*");
|
|
|
|
}
|
2018-03-24 03:39:32 -07:00
|
|
|
// Simple handling of braces
|
|
|
|
else if (last == '{' || (!eflindent && elm_code_line_indent_startswith_keyword(prevline)))
|
2016-11-29 16:11:15 -08:00
|
|
|
{
|
2018-03-24 03:39:32 -07:00
|
|
|
strcpy(buf + count, indent);
|
2016-11-29 16:11:15 -08:00
|
|
|
}
|
|
|
|
else if (last == '}')
|
|
|
|
{
|
2018-03-24 03:39:32 -07:00
|
|
|
unsigned int offset = strlen(indent) - 1;
|
|
|
|
if (count >= offset)
|
|
|
|
buf[count-offset] = '\0';
|
2016-11-29 16:11:15 -08:00
|
|
|
}
|
2016-11-27 15:13:42 -08:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-01-01 06:29:43 -08:00
|
|
|
EAPI const char *
|
|
|
|
elm_code_line_indent_matching_braces_get(Elm_Code_Line *line, unsigned int *length)
|
2016-12-02 16:55:02 -08:00
|
|
|
{
|
|
|
|
Elm_Code_File *file;
|
|
|
|
int stack, row;
|
2017-01-01 06:29:43 -08:00
|
|
|
unsigned int len_tmp, count = 0;
|
|
|
|
const char *content, *ptr;
|
2016-12-02 16:55:02 -08:00
|
|
|
|
|
|
|
file = line->file;
|
|
|
|
stack = 0;
|
|
|
|
row = line->number - 1;
|
2017-01-01 06:29:43 -08:00
|
|
|
*length = 0;
|
|
|
|
|
2016-12-02 16:55:02 -08:00
|
|
|
while (row > 0)
|
|
|
|
{
|
|
|
|
line = elm_code_file_line_get(file, row);
|
2017-01-01 06:29:43 -08:00
|
|
|
content = elm_code_line_text_get(line, &len_tmp);
|
|
|
|
|
|
|
|
if (memchr(content, '{', len_tmp)) stack--;
|
|
|
|
else if (memchr(content, '}', len_tmp)) stack++;
|
2016-12-02 16:55:02 -08:00
|
|
|
|
|
|
|
if (stack < 0)
|
|
|
|
{
|
2017-01-01 06:29:43 -08:00
|
|
|
if (len_tmp == 0)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
ptr = content;
|
|
|
|
while (count < len_tmp)
|
2016-12-02 16:55:02 -08:00
|
|
|
{
|
|
|
|
if (!_elm_code_text_char_is_whitespace(*ptr))
|
|
|
|
break;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
ptr++;
|
|
|
|
}
|
2017-01-01 06:29:43 -08:00
|
|
|
|
|
|
|
*length = count;
|
|
|
|
return content;
|
2016-12-02 16:55:02 -08:00
|
|
|
}
|
|
|
|
row--;
|
|
|
|
}
|
2017-01-01 06:29:43 -08:00
|
|
|
return "";
|
2016-12-02 16:55:02 -08:00
|
|
|
}
|