ajwillia-ms pushed a commit to branch master.

http://git.enlightenment.org/tools/edi.git/commit/?id=2297e250bc8b3c7cbda6606ffc47e945af24ff2a

commit 2297e250bc8b3c7cbda6606ffc47e945af24ff2a
Author: Andy Williams <a...@andywilliams.me>
Date:   Sun Sep 20 10:59:52 2015 +0100

    [highlight] Merge highlight tokens on line merge
    
    Instead of blanking colours we append them as with the content.
---
 elm_code/src/lib/elm_code_line.c          | 62 +++++++++++++++++++++++++++++++
 elm_code/src/lib/elm_code_line.h          | 35 +++++++++++++++++
 elm_code/src/lib/widget/elm_code_widget.c | 32 ++++------------
 3 files changed, 104 insertions(+), 25 deletions(-)

diff --git a/elm_code/src/lib/elm_code_line.c b/elm_code/src/lib/elm_code_line.c
index c8b03b7..b2ee714 100644
--- a/elm_code/src/lib/elm_code_line.c
+++ b/elm_code/src/lib/elm_code_line.c
@@ -83,6 +83,68 @@ EAPI void elm_code_line_split_at(Elm_Code_Line *line, 
unsigned int position)
    free(content);
 }
 
+static void
+_elm_code_line_merge_into(Elm_Code_Line *line1, Elm_Code_Line *line2)
+{
+   Eina_List *tokens1, *tokens2;
+   Elm_Code_Token *token;
+   const char *text1, *text2;
+   char *newtext;
+   unsigned int length1, length2;
+
+   text1 = elm_code_line_text_get(line1, &length1);
+   text2 = elm_code_line_text_get(line2, &length2);
+
+   newtext = malloc(sizeof(char) * (length1 + length2 + 1));
+   snprintf(newtext, length1 + 1, "%s", text1);
+   snprintf(newtext + length1, length2 + 1, "%s", text2);
+
+   tokens1 = line1->tokens;
+   line1->tokens = NULL;
+   tokens2 = line2->tokens;
+   line2->tokens = NULL;
+   elm_code_file_line_remove(line2->file, line2->number);
+   elm_code_line_text_set(line1, newtext, length1 + length2);
+
+   EINA_LIST_FREE(tokens1, token)
+     {
+        token->continues = EINA_FALSE;
+        line1->tokens = eina_list_append(line1->tokens, token);
+     }
+   EINA_LIST_FREE(tokens2, token)
+     {
+        token->start += length1;
+        token->end += length1;
+
+        line1->tokens = eina_list_append(line1->tokens, token);
+     }
+
+   elm_code_callback_fire(line1->file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, 
line1);
+   free(newtext);
+}
+
+EAPI void
+elm_code_line_merge_up(Elm_Code_Line *line)
+{
+   Elm_Code_Line *other;
+
+   other = elm_code_file_line_get(line->file, line->number - 1);
+
+   if (other)
+     _elm_code_line_merge_into(other, line);
+}
+
+EAPI void
+elm_code_line_merge_down(Elm_Code_Line *line)
+{
+   Elm_Code_Line *other;
+
+   other = elm_code_file_line_get(line->file, line->number + 1);
+
+   if (other)
+     _elm_code_line_merge_into(line, other);
+}
+
 EAPI void elm_code_line_status_set(Elm_Code_Line *line, Elm_Code_Status_Type 
status)
 {
    if (!line)
diff --git a/elm_code/src/lib/elm_code_line.h b/elm_code/src/lib/elm_code_line.h
index f9492aa..8819689 100644
--- a/elm_code/src/lib/elm_code_line.h
+++ b/elm_code/src/lib/elm_code_line.h
@@ -45,9 +45,44 @@ EAPI void elm_code_line_free(Elm_Code_Line *line);
  * Functions for changing the content of lines in an Elm_Code_File
  */
 
+/**
+ * Split the given line into two at the specified character position.
+ * The additional line will be inserted into the file immediately below the 
specified line.
+ *
+ * @param line The line to split
+ * @param position The character position to split at
+ *
+ * @ingroup Content
+ */
 EAPI void elm_code_line_split_at(Elm_Code_Line *line, unsigned int position);
 
 /**
+ * Merge the specified line with the line above.
+ * The content of the specified line will be added to the end of the previous 
line.
+ * The specified line will then be removed from the file.
+ *
+ * If there is no previous line this method does nothing.
+ *
+ * @param line The line to merge with the previous line.
+ *
+ * @ingroup Content
+ */
+EAPI void elm_code_line_merge_up(Elm_Code_Line *line);
+
+/**
+ * Merge the specified line with the line below.
+ * The content of the specified line will have the contents of the next line 
added to the end.
+ * The next line will then be removed from the file.
+ *
+ * If there is no next line this method does nothing.
+ *
+ * @param line The line to merge with the next line.
+ *
+ * @ingroup Content
+ */
+EAPI void elm_code_line_merge_down(Elm_Code_Line *line);
+
+/**
  * @}
  *
  * @brief Line markup functions.
diff --git a/elm_code/src/lib/widget/elm_code_widget.c 
b/elm_code/src/lib/widget/elm_code_widget.c
index 71867e5..9d2b587 100644
--- a/elm_code/src/lib/widget/elm_code_widget.c
+++ b/elm_code/src/lib/widget/elm_code_widget.c
@@ -1038,12 +1038,8 @@ static void
 _elm_code_widget_backspaceline(Elm_Code_Widget *widget, Eina_Bool nextline)
 {
    Elm_Code *code;
-   Elm_Code_Line *line, *otherline;
-   unsigned int row, col, position;
-
-   const char *text1, *text2;
-   char *newtext;
-   unsigned int length1, length2;
+   Elm_Code_Line *line, *oldline;
+   unsigned int row, col, oldlength, position;
 
    eo_do(widget,
          code = elm_obj_code_widget_code_get(),
@@ -1052,29 +1048,15 @@ _elm_code_widget_backspaceline(Elm_Code_Widget *widget, 
Eina_Bool nextline)
 
    if (nextline)
      {
-        otherline = elm_code_file_line_get(code->file, row + 1);
-        text1 = elm_code_line_text_get(line, &length1);
-        text2 = elm_code_line_text_get(otherline, &length2);
+        elm_code_line_merge_down(line);
      }
    else
      {
-        otherline = elm_code_file_line_get(code->file, row - 1);
-        text1 = elm_code_line_text_get(otherline, &length1);
-        text2 = elm_code_line_text_get(line, &length2);
-     }
-
-   newtext = malloc(sizeof(char) * (length1 + length2 + 1));
-   snprintf(newtext, length1 + 1, "%s", text1);
-   snprintf(newtext + length1, length2 + 1, "%s", text2);
+        oldline = elm_code_file_line_get(code->file, row - 1);
+        elm_code_line_text_get(oldline, &oldlength);
+        elm_code_line_merge_up(line);
 
-// TODO we need to merge tokens from these lines (move this to elm_code_line?)
-   elm_code_file_line_remove(code->file, otherline->number);
-   elm_code_line_text_set(line, newtext, length1 + length2);
-
-   free(newtext);
-   if (!nextline)
-     {
-        position = elm_code_widget_line_text_column_width_to_position(widget, 
line, length1);
+        position = elm_code_widget_line_text_column_width_to_position(widget, 
oldline, oldlength);
 
         eo_do(widget,
               elm_obj_code_widget_cursor_position_set(position, row - 1));

-- 


Reply via email to