[Bf-blender-cvs] [5d26d6eac28] soc-2020-info-editor: Cleanup: support syntax highlighting directly in TextViewContext (console and info editor)

Mateusz Grzeliński noreply at git.blender.org
Thu Aug 13 21:06:26 CEST 2020


Commit: 5d26d6eac285d92a2aadf1ab4249e354430e93ee
Author: Mateusz Grzeliński
Date:   Thu Aug 13 16:44:25 2020 +0200
Branches: soc-2020-info-editor
https://developer.blender.org/rB5d26d6eac285d92a2aadf1ab4249e354430e93ee

Cleanup: support syntax highlighting directly in TextViewContext (console and info editor)

- remove TextLine from callbacks in favor of simple types
- separate textview_draw_string into dry run version (only for calculating height)
- add custom TextViewContextLine (cast compatible with TextLine)

===================================================================

M	source/blender/editors/space_console/console_draw.c
M	source/blender/editors/space_info/info_draw.c
M	source/blender/editors/space_info/info_draw_clog.c
M	source/blender/editors/space_info/info_draw_report.c
M	source/blender/editors/space_info/info_intern.h
M	source/blender/editors/space_info/textview.c
M	source/blender/editors/space_info/textview.h

===================================================================

diff --git a/source/blender/editors/space_console/console_draw.c b/source/blender/editors/space_console/console_draw.c
index 1c2a2928e5a..0e84f8ed92b 100644
--- a/source/blender/editors/space_console/console_draw.c
+++ b/source/blender/editors/space_console/console_draw.c
@@ -18,7 +18,6 @@
  * \ingroup spconsole
  */
 
-#include <DNA_text_types.h>
 #include <string.h>
 
 #include "BLI_blenlib.h"
@@ -38,37 +37,48 @@
 #include "console_intern.h"
 
 #include "../space_info/textview.h"
-#include "../space_text/text_format.h"
 
-#define TABNUMBER 4
-
-static enum eTextViewContext_LineFlag console_line_draw_data(TextViewContext *tvc,
-                                                             TextLine *text_line,
-                                                             uchar fg[4],
-                                                             uchar UNUSED(bg[4]),
-                                                             int *UNUSED(icon),
-                                                             uchar UNUSED(icon_fg[4]),
-                                                             uchar UNUSED(icon_bg[4]))
+static enum eTextViewContext_LineDrawFlag console_line_draw_data(TextViewContext *tvc,
+                                                                 uchar fg[4],
+                                                                 uchar UNUSED(bg[4]),
+                                                                 int *UNUSED(icon),
+                                                                 uchar UNUSED(icon_fg[4]),
+                                                                 uchar UNUSED(icon_bg[4]))
 {
   const ConsoleLine *cl = tvc->iter;
   int fg_id = TH_TEXT;
 
   switch (cl->type) {
     case CONSOLE_LINE_OUTPUT:
+      ATTR_FALLTHROUGH;
     case CONSOLE_LINE_INPUT: {
-      TextFormatType *py_formatter = ED_text_format_get_by_extension("py");
-      TextLine *text_line_iter = text_line;
-      while (text_line_iter) {
-        py_formatter->format_line(text_line_iter, TABNUMBER, false);
-        text_line_iter = text_line_iter->next;
-      }
-      /* workaround: formatter formats also prompt >>> ... what is not desirable but current
+      /* know issue: formatter formats also input prompt >>> ... what is not desirable but current
        * implementation treats it as keyword, so no big deal */
-      // SpaceConsole *sc = (SpaceConsole *)tvc->arg1;
-      // for (int i = 0; i < text_line->len && i < strlen(sc->prompt); ++i) {
-      //   text_line->format[i] = FMT_TYPE_DEFAULT;
-      // }
-      return TVC_LINE_FG_COMPLEX;
+      /* There is only one line. */
+      if (!cl->prev && !cl->next) {
+        return TVC_LINE_FG_SYNTAX_START | TVC_LINE_FG_SYNTAX_END | TVC_LINE_FG_SYNTAX_PYTHON;
+      }
+      /* First line. */
+      if (!cl->prev) {
+        return TVC_LINE_FG_SYNTAX_START | TVC_LINE_FG_SYNTAX_PYTHON;
+      }
+      /* Last line (user input line) - single >>> */
+      if (!cl->next && cl->prev && cl->prev->type != cl->type) {
+        return TVC_LINE_FG_SYNTAX_START | TVC_LINE_FG_SYNTAX_END | TVC_LINE_FG_SYNTAX_PYTHON;
+      }
+      /* Last line (user input line) - multiline ... */
+      if (!cl->next) {
+        return TVC_LINE_FG_SYNTAX_END | TVC_LINE_FG_SYNTAX_PYTHON;
+      }
+      /* Previous line is not input. */
+      if (cl->prev && cl->prev->type != cl->type) {
+        return TVC_LINE_FG_SYNTAX_START | TVC_LINE_FG_SYNTAX_PYTHON;
+      }
+      /* Next line is not input. */
+      if (cl->next && cl->next->type != cl->type) {
+        return TVC_LINE_FG_SYNTAX_END | TVC_LINE_FG_SYNTAX_PYTHON;
+      }
+      return TVC_LINE_FG_SYNTAX_PYTHON;
     }
     case CONSOLE_LINE_INFO:
       fg_id = TH_CONSOLE_INFO;
@@ -121,33 +131,22 @@ static void console_textview_end(TextViewContext *tvc)
   (void)sc;
 }
 
-/** one step for console is printing one "chunk" at a time. Chunk is line with the same type */
 static int console_textview_step(TextViewContext *tvc)
 {
-  const ConsoleLine *cl_current = tvc->iter;
-  const ConsoleLine *cl_iter = tvc->iter;
-  do {
-    cl_iter = cl_iter->prev;
-  } while (cl_iter && cl_iter->type == cl_current->type);
-  tvc->iter = cl_iter;
-
-  return (tvc->iter != NULL);
+  const ConsoleLine *cl = tvc->iter;
+  return ((tvc->iter = (void *)(cl)->prev) != NULL);
 }
 
-static void console_textview_line_get(TextViewContext *tvc, ListBase *text_lines)
+static void console_textview_line_get(TextViewContext *tvc,
+                                      char **r_line,
+                                      int *r_len,
+                                      bool *owns_memory)
 {
-  const ConsoleLine *cl_current = tvc->iter;
-  const ConsoleLine *cl_iter = tvc->iter;
-  BLI_assert(BLI_listbase_is_empty(text_lines));
-  do {
-    TextLine *text_line = MEM_callocN(sizeof(*text_line), __func__);
-    text_line->line = cl_iter->line;
-    text_line->len = cl_iter->len;
-    BLI_addhead(text_lines, text_line);
-    BLI_assert(cl_iter->line[cl_iter->len] == '\0' &&
-               (cl_iter->len == 0 || cl_iter->line[cl_iter->len - 1] != '\0'));
-    cl_iter = cl_iter->prev;
-  } while (cl_iter && cl_iter->type == cl_current->type);
+  const ConsoleLine *cl = tvc->iter;
+  *r_line = cl->line;
+  *r_len = cl->len;
+  *owns_memory = false;
+  BLI_assert(cl->line[cl->len] == '\0' && (cl->len == 0 || cl->line[cl->len - 1] != '\0'));
 }
 
 static void console_cursor_wrap_offset(
@@ -242,7 +241,7 @@ static int console_textview_main__internal(SpaceConsole *sc,
   tvc.end = console_textview_end;
 
   tvc.step = console_textview_step;
-  tvc.lines_get = console_textview_line_get;
+  tvc.line_get = console_textview_line_get;
   tvc.line_draw_data = console_line_draw_data;
   tvc.draw_cursor = console_textview_draw_cursor;
   tvc.const_colors = console_textview_const_colors;
diff --git a/source/blender/editors/space_info/info_draw.c b/source/blender/editors/space_info/info_draw.c
index 1acb4278bbf..2d33e99cdf4 100644
--- a/source/blender/editors/space_info/info_draw.c
+++ b/source/blender/editors/space_info/info_draw.c
@@ -21,9 +21,6 @@
  * \ingroup spinfo
  */
 
-#include <BLI_blenlib.h>
-#include <DNA_text_types.h>
-#include <MEM_guardedalloc.h>
 #include <CLG_log.h>
 #include <limits.h>
 #include <string.h>
@@ -33,14 +30,9 @@
 #include "DNA_screen_types.h"
 #include "DNA_space_types.h"
 
-#include "BKE_report.h"
-
 #include "UI_interface.h"
-#include "UI_resources.h"
 #include "UI_view2d.h"
 
-#include "../space_text/text_format.h"
-#include "GPU_framebuffer.h"
 #include "info_intern.h"
 #include "textview.h"
 
@@ -70,8 +62,6 @@ static int info_textview_main__internal(const SpaceInfo *sinfo,
                                         void **r_mval_pick_item,
                                         int *r_mval_pick_offset)
 {
-  int ret = 0;
-
   const View2D *v2d = &region->v2d;
 
   TextViewContext tvc = {0};
@@ -82,7 +72,7 @@ static int info_textview_main__internal(const SpaceInfo *sinfo,
   switch (sinfo->view) {
     case INFO_VIEW_CLOG:
       tvc.begin = clog_textview_begin;
-      tvc.lines_get = clog_textview_line_get;
+      tvc.line_get = clog_textview_line_get;
       tvc.line_draw_data = clog_line_draw_data;
       tvc.end = clog_textview_end;
       tvc.step = clog_textview_step;
@@ -90,7 +80,7 @@ static int info_textview_main__internal(const SpaceInfo *sinfo,
       break;
     case INFO_VIEW_REPORTS:
       tvc.begin = report_textview_begin;
-      tvc.lines_get = report_textview_line_get;
+      tvc.line_get = report_textview_line_get;
       tvc.line_draw_data = report_line_draw_data;
       tvc.end = report_textview_end;
       tvc.step = report_textview_step;
@@ -111,9 +101,7 @@ static int info_textview_main__internal(const SpaceInfo *sinfo,
 
   info_textview_draw_rect_calc(region, &tvc.draw_rect, &tvc.draw_rect_outer);
 
-  ret = textview_draw(&tvc, do_draw, mval, r_mval_pick_item, r_mval_pick_offset);
-
-  return ret;
+  return textview_draw(&tvc, do_draw, mval, r_mval_pick_item, r_mval_pick_offset);
 }
 
 void *info_text_pick(const SpaceInfo *sinfo,
diff --git a/source/blender/editors/space_info/info_draw_clog.c b/source/blender/editors/space_info/info_draw_clog.c
index 4fa6d62091a..1ad5fa8698b 100644
--- a/source/blender/editors/space_info/info_draw_clog.c
+++ b/source/blender/editors/space_info/info_draw_clog.c
@@ -185,78 +185,53 @@ int clog_textview_step(struct TextViewContext *tvc)
   return (tvc->iter != NULL);
 }
 
-void clog_textview_line_get(struct TextViewContext *tvc, struct ListBase *text_lines)
+void clog_textview_line_get(struct TextViewContext *tvc,
+                            char **r_line,
+                            int *r_len,
+                            bool *owns_memory)
 {
   const struct CLG_LogRecord *record = tvc->iter;
-  TextLine *text_line = MEM_callocN(sizeof(*text_line), __func__);
-  /* TODO (grzelins) before allocating memory here I must make sure tvc can free it afterwards
-    const SpaceInfo *sinfo = tvc->arg1;
-    const CLG_LogRecordList *records = tvc->arg2;
+  const SpaceInfo *sinfo = tvc->arg1;
 
-    DynStr *dynStr = BLI_dynstr_new();
-    if (sinfo->log_format & INFO_LOG_SHOW_TIMESTAMP) {
-      char timestamp_str[64];
-      const uint64_t timestamp = record->timestamp;
-      snprintf(timestamp_str,
-               sizeof(timestamp_str),
-               "%" PRIu64 ".%03u ",
-               timestamp / 1000,
-               (uint)(timestamp % 1000));
-      BLI_dynstr_appendf(dynStr, "%s", timestamp_str);
-    }
-    if (sinfo->log_format & INFO_LOG_SHOW_LEVEL) {
-      if (record->severity <= CLG_SEVERITY_VERBOSE) {
-        BLI_dynstr_appendf(dynStr, "%s:%u ", clg_severity_as_text(record->severity),
-    record->verbosity);
-      }
-      else {
-        BLI_dynstr_appendf(dynStr, "%s ", clg_severity_as_text(record->severity));
-      }
-    }
-    if (sinfo->log_format & INFO_LOG_SHOW_LOG_TYPE) {
-      BLI_dynstr_appendf(dynStr, "(%s) ", record->type->identifier);
-    }
-    if (sinfo->log_format & INFO_LOG_SHOW_FILE_LINE) {
-      const char *file_line = (sinfo->use_short_file_line) ? BLI_path_basename(record->file_line) :
-                              record->file_line;
-      BLI_dynstr_appendf(dynStr,

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list