Page MenuHome

Expose Text Editor Cursor Position in Pixels to Python
AbandonedPublic

Authored by Campbell Barton (campbellbarton) on Dec 18 2014, 3:58 PM.

Details

Summary

This patch intends to expose the text cursor position in blenders text editor to python.
That is useful for smart auto-complete addons, which want to draw their (bgl) Menu at / near the current cursor position.

For example, my own autocompleter addon:

http://blenderartists.org/forum/showthread.php?357266-Addon-%28Another%29-Autocompleter-for-Blenders-Text-Editor

Or the one from @Jacques Lucke (JacquesLucke):

http://blenderartists.org/forum/showthread.php?355858-Addon-Auto-Completion-in-Blenders-Text-Editor

Diff Detail

Event Timeline

Martin Felke (scorpion81) retitled this revision from to Expose Text Editor Cursor Position in Pixels to Python.
Martin Felke (scorpion81) updated this object.

Didn't test it yet, but looking good to me mostly. Just some really picky things

source/blender/editors/space_text/text_draw.c
1516–1518

Should be removed, but maybe it's useful to leave this as a debug option

source/blender/makesdna/DNA_space_types.h
857 ↗(On Diff #3017)

Would prefer something like text_cursor_pos (can be confused with normal cursor otherwise). Also code style: "/*[SPACE][TEXT][SPACE]*/"

source/blender/makesrna/intern/rna_space.c
864–865

Could be copy_v2_v2_int

Martin Felke (scorpion81) edited edge metadata.

adressed the mentioned issues by @Julian Eisel (Severin)

Bastien Montagne (mont29) requested changes to this revision.Dec 28 2014, 5:38 PM
Bastien Montagne (mont29) edited edge metadata.

So, first of all, patch is oudated, since 2014/11/10 sa is no more available in text_scroll_to_cursor. I tried using ar->winy instead, but getting odd values for y, as if offset of about 220 pixels or so.

Another issue I see here is that this value is not updated on scrolling, not sure computing it in text_scroll_to_cursor is right thing to do?

But I’m not a specialist of that editor, think Campbell would be more helpful here.

source/blender/editors/space_text/text_draw.c
1469

To be removed? see comment below.

1510–1520

Do not really see the point to have intermediate vars here, would just do:

	i = txt_get_span(text->lines.first, text->curl);
	st->text_cursor_pos[0] = (text->curc - st->left) * st->cwidth;
	st->text_cursor_pos[1] = sa->winy - ((i - st->top) * st->lheight);

… and remove the debug print in final version too, we do not want that on by default even in debug build. ;)

1512

sa is no more available here in master (since 10/11), ar->winy maybe?

1521

tsst… needless blank lines. :P

source/blender/makesdna/DNA_space_types.h
857 ↗(On Diff #3018)

maybe precise comment? runtime, and in pixels (if I have understood patch correctly ;) ).

source/blender/makesrna/intern/rna_space.c
859–868

See comment below (and needless blank lines again).

2733–2734

This is not what you want to do I think, you want to make this prop read-only, right? Defining a getter func and setting setter to NULL will not make the prop read-only!

You do not need the getter at all here, and should rather define the prop as readonly (i.e. replace RNA_def_property_int_funcs by RNA_def_property_clear_flag(prop, PROP_EDITABLE);).

2735–2736

Add in 'tip' a reference about the unit (pixel)? And no need to define an update for this prop, if it's read-only…

2737

Grrrrr… xD

This revision now requires changes to proceed.Dec 28 2014, 5:38 PM
Bastien Montagne (mont29) requested changes to this revision.Dec 28 2014, 5:40 PM
Bastien Montagne (mont29) edited edge metadata.
Martin Felke (scorpion81) edited edge metadata.

adressed issues of @Bastien Montagne (mont29) and fix for y value, it accumulated some incorrect offset

Julian Eisel (Severin) updated this revision to Diff 3061.
Julian Eisel (Severin) edited edge metadata.

Just some minor edits...

Think it's basically fine, but it doesn't take word wrap into account. This should be fixed before merging.

Feel free to commandeer back Martin

Campbell Barton (campbellbarton) requested changes to this revision.EditedDec 29 2014, 2:35 AM
Campbell Barton (campbellbarton) edited edge metadata.

This is continuously setting cursor location, suggest to either...

  • Set the value when the cursor draws (don't especially like this since the value could become stale, but its better then calculating the position - on the off-chance it may be used).
  • Make the property into an API function, you could even pass in a line+col par, returning a screen-space X,Y value. (this seems generally more useful).
source/blender/editors/space_text/text_draw.c
1539

this is a reasonably expensive function.

Calling it each time on the offchange someone might want to access text_cursor_pos is unnecessary.

source/blender/makesrna/intern/rna_space.c
2752

Suggest to make this a function rather then a property. then the function can do any checks it needs to at the time its run.

This revision now requires changes to proceed.Dec 29 2014, 2:35 AM

ok, got another update, will follow soon :)

Martin Felke (scorpion81) edited edge metadata.

ok, did some bigger refactoring of the patch, trying to follow the suggestions of @Campbell Barton (campbellbarton) and additionally added treatment of wordwrap.

Don't think you need to move these functions about, just expose an editor function, eg ED_text_cursor_to_pixel_space and RNA can call that.

source/blender/blenkernel/BKE_text.h
125 ↗(On Diff #3066)

These are really editor functions, rather not move to BKE, instead, you can just call an editor function from RNA. (ED_text.h)

source/blender/blenkernel/intern/text.c
3049 ↗(On Diff #3066)

line_char can be const

3049 ↗(On Diff #3066)

think it would be good to return true/false based on weather the line_char exists.

3055 ↗(On Diff #3066)

counting the entire list seems unnecessary, just allow BLI_findlink below to fail.

3059 ↗(On Diff #3066)

no need to cast.

source/blender/editors/space_text/text_intern.h
35 ↗(On Diff #3066)

rather not add add headers here. can be included in C files directly, as is done most other parts.

Campbell Barton (campbellbarton) requested changes to this revision.Dec 30 2014, 5:13 AM
Campbell Barton (campbellbarton) edited edge metadata.
Campbell Barton (campbellbarton) added inline comments.
source/blender/makesrna/intern/rna_space.c
865

just finding the first text space is really arbitrary, its even possible st and ar are unrelated.

See how text_scroll_to_cursor__area gets the region from the ScrArea

This revision now requires changes to proceed.Dec 30 2014, 5:13 AM
Martin Felke (scorpion81) edited edge metadata.

made an editor function out of my code, called by RNA and moved stuff back to text_draw.c which i moved out of it before,
again following suggestions of @Campbell Barton (campbellbarton)

Martin Felke (scorpion81) edited edge metadata.

changes to area/space logic, it was necessary to find the correct space and not a random one

Campbell Barton (campbellbarton) requested changes to this revision.Dec 30 2014, 1:17 PM
Campbell Barton (campbellbarton) edited edge metadata.

Attached a test file that prints the x,y location of the current cursor. it shows some problems with line numbers and double pixel size.

This revision now requires changes to proceed.Dec 30 2014, 1:17 PM
Julian Eisel (Severin) edited edge metadata.

Tested again and everything looks fine from here (afaik the diff here is a bit behind the branch I tested with). Think all the issues with wordwrap, double pixel size, line numbers and backend stuff have been addressed!?

@Martin Felke (scorpion81), could you update the diff so it's up to date with the branch?
@Campbell Barton (campbellbarton), @Bastien Montagne (mont29) any concerns remaining?

Martin Felke (scorpion81) edited edge metadata.

updated patch to sync it with branch temp-text_editor_cursor_api