small edits to text editor from writing a python editor extension.

- rename TextLine.line -> body, ConsoleLine.line -> body
- minor speedups when setting the body text, also re-allocate console lines if they are < half the length.
- added option to highlight current line in the text editor.
This commit is contained in:
Campbell Barton 2010-08-11 05:21:43 +00:00
parent 8c39326962
commit d739a1788d
9 changed files with 69 additions and 47 deletions

@ -525,7 +525,7 @@ class Text(bpy_types.ID):
def as_string(self):
"""Return the text as a string."""
return "\n".join(line.line for line in self.lines)
return "\n".join(line.body for line in self.lines)
def from_string(self, string):
"""Replace text with this string."""

@ -131,7 +131,7 @@ def execute(context):
is_multiline = False
try:
line = line_object.line
line = line_object.body
# run the console, "\n" executes a multiline statement
line_exec = line if line.strip() else "\n"
@ -212,13 +212,13 @@ def autocomplete(context):
try:
current_line = sc.history[-1]
line = current_line.line
line = current_line.body
# This function isnt aware of the text editor or being an operator
# just does the autocomp then copy its results back
current_line.line, current_line.current_character, scrollback = \
current_line.body, current_line.current_character, scrollback = \
intellisense.expand(
line=current_line.line,
line=current_line.body,
cursor=current_line.current_character,
namespace=console.locals,
private=bpy.app.debug)
@ -233,7 +233,7 @@ def autocomplete(context):
# Separate automplete output by command prompts
if scrollback != '':
bpy.ops.console.scrollback_append(text=sc.prompt + current_line.line, type='INPUT')
bpy.ops.console.scrollback_append(text=sc.prompt + current_line.body, type='INPUT')
# Now we need to copy back the line from blender back into the
# text editor. This will change when we dont use the text editor

@ -47,7 +47,7 @@ def execute(context):
sc = context.space_data
try:
line = sc.history[-1].line
line = sc.history[-1].body
except:
return {'CANCELLED'}

@ -48,9 +48,9 @@ class TEXT_HT_header(bpy.types.Header):
layout.template_ID(st, "text", new="text.new", unlink="text.unlink")
row = layout.row(align=True)
row.prop(st, "line_numbers", text="")
row.prop(st, "word_wrap", text="")
row.prop(st, "syntax_highlight", text="")
row.prop(st, "show_line_numbers", text="")
row.prop(st, "show_word_wrap", text="")
row.prop(st, "show_syntax_highlight", text="")
if text:
row = layout.row()
@ -81,9 +81,10 @@ class TEXT_PT_properties(bpy.types.Panel):
st = context.space_data
flow = layout.column_flow()
flow.prop(st, "line_numbers")
flow.prop(st, "word_wrap")
flow.prop(st, "syntax_highlight")
flow.prop(st, "show_line_numbers")
flow.prop(st, "show_word_wrap")
flow.prop(st, "show_syntax_highlight")
flow.prop(st, "show_line_highlight")
flow.prop(st, "live_edit")
flow = layout.column_flow()

@ -747,7 +747,6 @@ static int open_invoke(bContext *C, wmOperator *op, wmEvent *event)
}
if (ima==NULL) {
SpaceButs *sbuts= CTX_wm_space_buts(C);
Tex *tex= CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data;
if(tex && tex->type==TEX_IMAGE)
ima= tex->ima;

@ -1101,6 +1101,20 @@ static void draw_cursor(SpaceText *st, ARegion *ar)
}
}
if(st->line_hlight) {
/* TODO, dont draw if hidden */
int x1= st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET;
int x2= x1 + ar->winx;
y= ar->winy-2 - vsell*st->lheight;
glColor4ub(255, 255, 255, 32);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glRecti(x1, y, x2, y-st->lheight+1);
glDisable(GL_BLEND);
}
if(!hidden) {
/* Draw the cursor itself (we draw the sel. cursor as this is the leading edge) */
x= st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET;

@ -303,7 +303,8 @@ typedef struct SpaceText {
int showlinenrs;
int tabnumber;
int showsyntax;
short showsyntax;
short line_hlight;
short overwrite;
short live_edit; /* run python while editing, evil */
float pix_per_line;

@ -567,31 +567,30 @@ static void rna_SpaceProperties_align_set(PointerRNA *ptr, int value)
}
/* Space Console */
static void rna_ConsoleLine_line_get(PointerRNA *ptr, char *value)
static void rna_ConsoleLine_body_get(PointerRNA *ptr, char *value)
{
ConsoleLine *ci= (ConsoleLine*)ptr->data;
strcpy(value, ci->line);
}
static int rna_ConsoleLine_line_length(PointerRNA *ptr)
static int rna_ConsoleLine_body_length(PointerRNA *ptr)
{
ConsoleLine *ci= (ConsoleLine*)ptr->data;
return ci->len;
}
static void rna_ConsoleLine_line_set(PointerRNA *ptr, const char *value)
static void rna_ConsoleLine_body_set(PointerRNA *ptr, const char *value)
{
ConsoleLine *ci= (ConsoleLine*)ptr->data;
int len= strlen(value);
if(len < ci->len_alloc) { /* allocated size is enough? */
strcpy(ci->line, value);
}
else { /* allocate a new strnig */
if((len >= ci->len_alloc) || (len * 2 < ci->len_alloc) ) { /* allocate a new strnig */
MEM_freeN(ci->line);
ci->line= BLI_strdup(value);
ci->len_alloc= len;
ci->line= MEM_mallocN((len + 1) * sizeof(char), "rna_consoleline");
ci->len_alloc= len + 1;
}
memcpy(ci->line, value, len + 1);
ci->len= len;
if(ci->cursor > len) /* clamp the cursor */
@ -1536,31 +1535,28 @@ static void rna_def_space_text(BlenderRNA *brna)
RNA_def_property_update(prop, NC_SPACE|ND_SPACE_TEXT, NULL);
/* display */
prop= RNA_def_property(srna, "syntax_highlight", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "showsyntax", 0);
RNA_def_property_ui_text(prop, "Syntax Highlight", "Syntax highlight for scripting");
RNA_def_property_ui_icon(prop, ICON_SYNTAX_OFF, 1);
RNA_def_property_update(prop, NC_SPACE|ND_SPACE_TEXT, NULL);
prop= RNA_def_property(srna, "word_wrap", PROP_BOOLEAN, PROP_NONE);
prop= RNA_def_property(srna, "show_word_wrap", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "wordwrap", 0);
RNA_def_property_boolean_funcs(prop, NULL, "rna_SpaceTextEditor_word_wrap_set");
RNA_def_property_ui_text(prop, "Word Wrap", "Wrap words if there is not enough horizontal space");
RNA_def_property_ui_icon(prop, ICON_WORDWRAP_OFF, 1);
RNA_def_property_update(prop, NC_SPACE|ND_SPACE_TEXT, NULL);
prop= RNA_def_property(srna, "line_numbers", PROP_BOOLEAN, PROP_NONE);
prop= RNA_def_property(srna, "show_line_numbers", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "showlinenrs", 0);
RNA_def_property_ui_text(prop, "Line Numbers", "Show line numbers next to the text");
RNA_def_property_ui_icon(prop, ICON_LINENUMBERS_OFF, 1);
RNA_def_property_update(prop, NC_SPACE|ND_SPACE_TEXT, NULL);
prop= RNA_def_property(srna, "overwrite", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_ui_text(prop, "Overwrite", "Overwrite characters when typing rather than inserting them");
prop= RNA_def_property(srna, "show_syntax_highlight", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "showsyntax", 0);
RNA_def_property_ui_text(prop, "Syntax Highlight", "Syntax highlight for scripting");
RNA_def_property_ui_icon(prop, ICON_SYNTAX_OFF, 1);
RNA_def_property_update(prop, NC_SPACE|ND_SPACE_TEXT, NULL);
prop= RNA_def_property(srna, "live_edit", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_ui_text(prop, "Live Edit", "Run python while editing");
prop= RNA_def_property(srna, "show_line_highlight", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "line_hlight", 0);
RNA_def_property_ui_text(prop, "Highlight Line", "Highlight the current line");
RNA_def_property_update(prop, NC_SPACE|ND_SPACE_TEXT, NULL);
prop= RNA_def_property(srna, "tab_width", PROP_INT, PROP_NONE);
@ -1575,6 +1571,15 @@ static void rna_def_space_text(BlenderRNA *brna)
RNA_def_property_ui_text(prop, "Font Size", "Font size to use for displaying the text");
RNA_def_property_update(prop, NC_SPACE|ND_SPACE_TEXT, NULL);
/* functionality options */
prop= RNA_def_property(srna, "overwrite", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_ui_text(prop, "Overwrite", "Overwrite characters when typing rather than inserting them");
RNA_def_property_update(prop, NC_SPACE|ND_SPACE_TEXT, NULL);
prop= RNA_def_property(srna, "live_edit", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_ui_text(prop, "Live Edit", "Run python while editing");
RNA_def_property_update(prop, NC_SPACE|ND_SPACE_TEXT, NULL);
/* find */
prop= RNA_def_property(srna, "find_all", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "flags", ST_FIND_ALL);
@ -1925,8 +1930,8 @@ static void rna_def_console_line(BlenderRNA *brna)
RNA_def_struct_ui_text(srna, "Console Input", "Input line for the interactive console");
// XXX using non-inited "prop", uh? RNA_def_property_update(prop, NC_SPACE|ND_SPACE_CONSOLE, NULL);
prop= RNA_def_property(srna, "line", PROP_STRING, PROP_NONE);
RNA_def_property_string_funcs(prop, "rna_ConsoleLine_line_get", "rna_ConsoleLine_line_length", "rna_ConsoleLine_line_set");
prop= RNA_def_property(srna, "body", PROP_STRING, PROP_NONE);
RNA_def_property_string_funcs(prop, "rna_ConsoleLine_body_get", "rna_ConsoleLine_body_length", "rna_ConsoleLine_body_set");
RNA_def_property_ui_text(prop, "Line", "Text in the line");
RNA_def_property_update(prop, NC_SPACE|ND_SPACE_CONSOLE, NULL);

@ -76,7 +76,7 @@ static int rna_Text_modified_get(PointerRNA *ptr)
return text_file_modified(text);
}
static void rna_TextLine_line_get(PointerRNA *ptr, char *value)
static void rna_TextLine_body_get(PointerRNA *ptr, char *value)
{
TextLine *line= (TextLine*)ptr->data;
@ -86,21 +86,23 @@ static void rna_TextLine_line_get(PointerRNA *ptr, char *value)
strcpy(value, "");
}
static int rna_TextLine_line_length(PointerRNA *ptr)
static int rna_TextLine_body_length(PointerRNA *ptr)
{
TextLine *line= (TextLine*)ptr->data;
return line->len;
}
static void rna_TextLine_line_set(PointerRNA *ptr, const char *value)
static void rna_TextLine_body_set(PointerRNA *ptr, const char *value)
{
TextLine *line= (TextLine*)ptr->data;
int len= strlen(value);
if(line->line)
MEM_freeN(line->line);
line->line= BLI_strdup(value);
line->len= strlen(line->line);
line->line= MEM_mallocN((len + 1) * sizeof(char), "rna_text_body");
line->len= len;
memcpy(line->line, value, len + 1);
if(line->format) {
MEM_freeN(line->format);
@ -118,8 +120,8 @@ static void rna_def_text_line(BlenderRNA *brna)
srna = RNA_def_struct(brna, "TextLine", NULL);
RNA_def_struct_ui_text(srna, "Text Line", "Line of text in a Text datablock");
prop= RNA_def_property(srna, "line", PROP_STRING, PROP_NONE);
RNA_def_property_string_funcs(prop, "rna_TextLine_line_get", "rna_TextLine_line_length", "rna_TextLine_line_set");
prop= RNA_def_property(srna, "body", PROP_STRING, PROP_NONE);
RNA_def_property_string_funcs(prop, "rna_TextLine_body_get", "rna_TextLine_body_length", "rna_TextLine_body_set");
RNA_def_property_ui_text(prop, "Line", "Text in the line");
RNA_def_property_update(prop, NC_TEXT|NA_EDITED, NULL);
}