elm_code: Slightly smarter indent logic

Continue comment blocks where appropriate...
A bunch of refactoring of text/indent code to expand further
This commit is contained in:
Andy Williams 2016-11-27 23:13:42 +00:00
parent 892f4abe37
commit f6486473ed
8 changed files with 145 additions and 62 deletions

View File

@ -276,6 +276,7 @@ includesunstable_HEADERS = \
lib/elementary/elm_code_common.h \
lib/elementary/elm_code_line.h \
lib/elementary/elm_code_text.h \
lib/elementary/elm_code_indent.h \
lib/elementary/elm_code_file.h \
lib/elementary/elm_code_parse.h
includesunstabledir = $(includedir)/elementary-@VMAJ@
@ -553,6 +554,7 @@ lib_elementary_libelementary_la_SOURCES = \
lib/elementary/elm_cnp.c \
lib/elementary/elm_code_line.c \
lib/elementary/elm_code_text.c \
lib/elementary/elm_code_indent.c \
lib/elementary/elm_code_file.c \
lib/elementary/elm_code_parse.c \
lib/elementary/elm_code_widget_selection.c \

View File

@ -6,6 +6,7 @@
#include "elm_code_common.h"
#include "elm_code_line.h"
#include "elm_code_text.h"
#include "elm_code_indent.h"
#include "elm_code_file.h"
#include "elm_code_parse.h"
#include "elm_code_widget.eo.h"

View File

@ -0,0 +1,57 @@
#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;
char next;
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;
// 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, "*");
}
}
return buf;
}

View File

@ -0,0 +1,33 @@
#ifndef ELM_CODE_INDENT_H_
# define ELM_CODE_INDENT_H_
#ifdef __cplusplus
extern "C" {
#endif
/**
* @file
* @brief These routines are used for calculating text indentation of lines in Elm Code.
*/
/**
* @brief Line indent handling functions.
* @defgroup Indent management of indentation of lines
*
* @{
*
* Functions for calculation indentation of lines within elm code.
*
*/
EAPI char *elm_code_line_indent_get(const char *prevtext, unsigned int prevlength);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* ELM_CODE_INDENT_H_ */

View File

@ -24,6 +24,8 @@ extern int _elm_code_lib_log_dom;
#endif
#define DBG(...) EINA_LOG_DOM_DBG(_elm_code_lib_log_dom, __VA_ARGS__)
Eina_Bool _elm_code_text_char_is_whitespace(char c);
/* Private parser callbacks */
void _elm_code_parse_setup();

View File

@ -210,18 +210,55 @@ 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);
}
Eina_Bool
_elm_code_text_char_is_whitespace(char c)
{
return c == ' ' || c == '\t';
}
static 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 void elm_code_line_text_leading_whitespace_strip(Elm_Code_Line *line)
{
unsigned int length = 0;
unsigned int leading;
unsigned int length, count = 0;
const char *content;
char *ptr;
content = elm_code_line_text_get(line, &length);
leading = elm_code_text_leading_whitespace_length(content, length);
if (leading == 0)
if (length == 0)
return;
elm_code_line_text_remove(line, 0, leading);
ptr = (char *)content;
while (count < length)
{
if (!_elm_code_text_char_is_whitespace(*ptr))
break;
count++;
ptr++;
}
elm_code_line_text_remove(line, 0, count);
}
EAPI void elm_code_line_text_trailing_whitespace_strip(Elm_Code_Line *line)
@ -231,7 +268,7 @@ EAPI void elm_code_line_text_trailing_whitespace_strip(Elm_Code_Line *line)
const char *content;
content = elm_code_line_text_get(line, &length);
trailing = elm_code_text_trailing_whitespace_length(content, length);
trailing = _elm_code_text_trailing_whitespace_length(content, length);
if (trailing == 0)
return;
@ -269,58 +306,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)
{
unsigned int count = 0;
char *ptr = (char *)text;
while (count < length)
{
if (!_elm_code_text_char_is_whitespace(*ptr))
break;
count++;
ptr++;
}
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_trailing_whitespace_length(text, length);
leading = _elm_code_text_trailing_whitespace_length(text, length);
return leading == length;
}

View File

@ -56,10 +56,6 @@ EAPI int elm_code_text_strnpos(const char *text, unsigned int length, const char
EAPI int elm_code_text_newlinenpos(const char *text, unsigned int length, short *nllen);
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

@ -1289,8 +1289,8 @@ _elm_code_widget_newline(Elm_Code_Widget *widget)
Elm_Code *code;
Elm_Code_Line *line;
Elm_Code_Widget_Change_Info *change;
unsigned int row, col, position, oldlen, leading, width, indent;
char *oldtext;
unsigned int row, col, position, oldlen, width, indent;
char *oldtext, *leading;
_elm_code_widget_delete_selection(widget);
code = elm_obj_code_widget_code_get(widget);
@ -1310,12 +1310,13 @@ _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_text_leading_whitespace_length(oldtext, oldlen);
leading = elm_code_line_indent_get(oldtext, oldlen);
elm_code_line_text_leading_whitespace_strip(line);
elm_code_line_text_insert(line, 0, oldtext, leading);
elm_code_line_text_insert(line, 0, leading, strlen(leading));
free(oldtext);
indent = elm_obj_code_widget_line_text_column_width_to_position(widget, line, leading);
indent = elm_obj_code_widget_line_text_column_width_to_position(widget, line,
strlen(leading));
elm_obj_code_widget_cursor_position_set(widget, indent, row + 1);
efl_event_callback_legacy_call(widget, ELM_OBJ_CODE_WIDGET_EVENT_CHANGED_USER, NULL);