Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSybren A. Stüvel <sybren@blender.org>2019-08-13 16:35:48 +0300
committerSybren A. Stüvel <sybren@blender.org>2019-08-14 17:59:37 +0300
commit05417b22206c479b5ebe8ac8e7dd73ae154c8c96 (patch)
treeaa11344ee7e5fa0bf8c95ce380e347d11a3df16f /source/blender/editors/space_text
parent72eb70f93328e4e19a319aa199f6c46a0c8ab420 (diff)
Text editor: syntax highlighting + line numbers on by default
The most common use of the text editor seems to be for scripting. Having line numbers and syntax highlighting enabled by default seems sensible. Syntax highlighting is now enabled by default, but is automatically disabled when the datablock has a non-highlighted extension. Highlighting is enabled for filenames like: - Text - Text.001 - somefile.py and is automatically disabled when the datablock has an extension for which Blender has no syntax highlighter registered. Reviewers: billreynish, campbellbarton Subscribers: brecht, billreynish Differential Revision: https://developer.blender.org/D5472
Diffstat (limited to 'source/blender/editors/space_text')
-rw-r--r--source/blender/editors/space_text/space_text.c2
-rw-r--r--source/blender/editors/space_text/text_draw.c14
-rw-r--r--source/blender/editors/space_text/text_format.c38
3 files changed, 48 insertions, 6 deletions
diff --git a/source/blender/editors/space_text/space_text.c b/source/blender/editors/space_text/space_text.c
index 8bfb6c87625..c1a3c79b0d8 100644
--- a/source/blender/editors/space_text/space_text.c
+++ b/source/blender/editors/space_text/space_text.c
@@ -63,6 +63,8 @@ static SpaceLink *text_new(const ScrArea *UNUSED(area), const Scene *UNUSED(scen
stext->lheight = 12;
stext->tabnumber = 4;
stext->margin_column = 80;
+ stext->showsyntax = true;
+ stext->showlinenrs = true;
/* header */
ar = MEM_callocN(sizeof(ARegion), "header for text");
diff --git a/source/blender/editors/space_text/text_draw.c b/source/blender/editors/space_text/text_draw.c
index 9dc8dfa93b6..e99bf680077 100644
--- a/source/blender/editors/space_text/text_draw.c
+++ b/source/blender/editors/space_text/text_draw.c
@@ -54,6 +54,7 @@ typedef struct TextDrawContext {
int font_id;
int cwidth;
int lheight_dpi;
+ bool syntax_highlight;
} TextDrawContext;
static void text_draw_context_init(const SpaceText *st, TextDrawContext *tdc)
@@ -61,6 +62,7 @@ static void text_draw_context_init(const SpaceText *st, TextDrawContext *tdc)
tdc->font_id = blf_mono_font;
tdc->cwidth = 0;
tdc->lheight_dpi = st->lheight_dpi;
+ tdc->syntax_highlight = st->showsyntax && ED_text_is_syntax_highlight_supported(st->text);
}
static void text_font_begin(const TextDrawContext *tdc)
@@ -418,7 +420,7 @@ static int text_draw_wrapped(const SpaceText *st,
const char *format,
int skip)
{
- const bool use_syntax = (st->showsyntax && format);
+ const bool use_syntax = (tdc->syntax_highlight && format);
FlattenString fs;
int basex, lines;
int i, wrap, end, max, columns, padding; /* column */
@@ -514,7 +516,7 @@ static void text_draw(const SpaceText *st,
int y,
const char *format)
{
- const bool use_syntax = (st->showsyntax && format);
+ const bool use_syntax = (tdc->syntax_highlight && format);
FlattenString fs;
int columns, size, n, w = 0, padding, amount = 0;
const char *in = NULL;
@@ -1383,8 +1385,8 @@ static void draw_brackets(const SpaceText *st, const TextDrawContext *tdc, ARegi
char ch;
- // showsyntax must be on or else the format string will be null
- if (!text->curl || !st->showsyntax) {
+ // syntax_highlight must be on or else the format string will be null
+ if (!text->curl || !tdc->syntax_highlight) {
return;
}
@@ -1576,7 +1578,7 @@ void draw_text_main(SpaceText *st, ARegion *ar)
tmp = text->lines.first;
lineno = 0;
for (i = 0; i < st->top && tmp; i++) {
- if (st->showsyntax && !tmp->format) {
+ if (tdc.syntax_highlight && !tmp->format) {
tft->format_line(st, tmp, false);
}
@@ -1631,7 +1633,7 @@ void draw_text_main(SpaceText *st, ARegion *ar)
UI_FontThemeColor(tdc.font_id, TH_TEXT);
for (i = 0; y > clip_min_y && i < st->viewlines && tmp; i++, tmp = tmp->next) {
- if (st->showsyntax && !tmp->format) {
+ if (tdc.syntax_highlight && !tmp->format) {
tft->format_line(st, tmp, false);
}
diff --git a/source/blender/editors/space_text/text_format.c b/source/blender/editors/space_text/text_format.c
index 8c102dc009e..48ee30e450f 100644
--- a/source/blender/editors/space_text/text_format.c
+++ b/source/blender/editors/space_text/text_format.c
@@ -25,10 +25,13 @@
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
+#include "BLI_string_utils.h"
#include "DNA_text_types.h"
#include "DNA_space_types.h"
+#include "ED_text.h"
+
#include "text_format.h"
/****************** flatten string **********************/
@@ -224,3 +227,38 @@ TextFormatType *ED_text_format_get(Text *text)
return tft_lb.first;
}
}
+
+bool ED_text_is_syntax_highlight_supported(Text *text)
+{
+ if (text == NULL) {
+ return false;
+ }
+
+ TextFormatType *tft;
+
+ const char *text_ext = BLI_path_extension(text->id.name + 2);
+ if (text_ext == NULL) {
+ /* Extensionless datablocks are considered highlightable as Python. */
+ return true;
+ }
+ text_ext++; /* skip the '.' */
+ if (BLI_string_is_decimal(text_ext)) {
+ /* "Text.001" is treated as extensionless, and thus highlightable. */
+ return true;
+ }
+
+ /* Check all text formats in the static list */
+ for (tft = tft_lb.first; tft; tft = tft->next) {
+ /* All formats should have an ext, but just in case */
+ const char **ext;
+ for (ext = tft->ext; *ext; ext++) {
+ /* If extension matches text name, return the matching tft */
+ if (BLI_strcasecmp(text_ext, *ext) == 0) {
+ return true;
+ }
+ }
+ }
+
+ /* The filename has a non-numerical extension that we could not highlight. */
+ return false;
+}