[Addon] Auto Completion in Blenders Text Editor

From my experience, the devs are more than happy to add some python api access when people ask nicely - wouldn’t hurt to ask on the bf-committers mailing list anyway :slight_smile:

Really great update !

@gregzaal: yeah, I will ask there :slight_smile:

@pitiwazou: glad you like it

A lot of things like current line/ character and selection are saved in the text(id) object from bpy.data.texts.

There is a file named sphinx_doc_gen.py available which generates the .rst files for sphinx. These might be easier to parse than html and might be used for external text editors.

I couldn’t find the info about the return type of a function using bl_rna.

This is cool!

@pink vertex: Yes, I know that these things are saved there. I use this all the time. The problem is that I couldn’t found a good way to set the cursor position. (getting it is simple (as long as I don’t need it in pixels, but this is another story))

I had to find out myself how the data is stored in bl_rna. It was just a lot of trial and error until it worked :slight_smile:
I think this is pretty reliable now. (and much easier than parsing a .rst or .html file)

Is this operator suitable?

heyyy that looks good. Thank you a lot. I will try this out in a few moments. It’s always so easy… :smiley:

Do you also have a suggestion for getting the the cursor position in pixels? (I need this for drawing the auto complete box in the right place)

[Edit:]
I tested this now. The cursor_set operator seams to set the cursor position based on pixel values. This isn’t exactly what I want but there are many other operators I can use now. I don’t know why I didn’t had the idea to look at the text operators^^

Looks like I have to move the cursor step by step to set it to the right position (line, character_index). This isn’t perfect but way better than what I have now

Tested the operator binding it to a key and it behaves erratic. However you can also use jump and move to navigate.

Finding the position in pixels seems to be tough. The only informations i found are font_size, top and visible_lines. The text editor shows only lines fully visible and scrolls line-by-line.

Yes, I just found that. thanks for linking me there :slight_smile:

This is exactly the way I do this since the first day: https://github.com/JacquesLucke/script_auto_complete/blob/master/text_editor_utils.py#L13-L29

Just a minute ago I looked at one of the links, Blender in Ipython notebook looks superb!
I am on Windows and there the get-line if I remember correctly, is not available …

Maybe I have to install Virtual Box and a Linux variant?

Thanks for the info in any case
Greets
Peter

Hi PKHG,

Withn Mathias alias Panzi, we have made several try on linuxMint but the ipython release is so update that the script of Panzi don’t run.
His Script run with restriction of release and would be modified in few months…

On windows I have try it but no possible! Weird. I have used Anaconda bundle with ipython but the python of Blender is internal.
You must install ipython as Package-module but is difficult. I have stop with winodws.

More easy with linux. Panzi is too on linux.

Wait and see…change are coming…
Bye bye.
Spirou4D

Problem is probably on Windows, Blender user MSVC 13 and Python.org MSVC 8 ? … (Ipython?!) …
Will see when it gets together

May have to use Linux in a Virtual Box … (next year … )

Ciao

@ Jacques Lucke

Hi, your autocompleter looks really Awesome !!! Much better than my humble attempt :slight_smile: Did you add jedi already inside your autocompleter now ? if yes, wow thats really fast !!!
else… great work from you as well :slight_smile:

And regarding the cursor location in text editor, i made a small C Patch (back in 2012) to expose the location of cursor to python. Its located in my addon zip file (caret_xy.patch) … But the disadvantage is you would need to have a special build then… or ask one of the coredevs whether its suitable for master inclusion.

Edit: I opened a patch task for my cursor position patch here: https://developer.blender.org/T42934 :slight_smile: (before that i updated my patch of course )

@scorpion81:
Thanks a lot. No, this is without jedi atm. I use a much simpler (and less perfect) approach, but it works well for fast scripting of small scripts and that is my target. For longer or multi-file addons I still recommend to use another editor.
This here should be very specialized for Blender Script development. Also to allow artists to customize the ui or write simple operators much easier. :slight_smile:

Thanks that you published this patch!! Hopefully it gets accepted for master :slight_smile:

Btw I had a look at the source code and text.current_character gives the current position in bytes. If you use utf8 chars larger than a byte, ä,ö,ü for instance the value does not match the current position in chars. (The rna value maps straight to the dna value)

This is probably the reason for being readonly. So you can’t set a invalid byte position.

There are some functions available to deal with utf8 chars (used by the move operator) and current_character might be wrapped with a setter and getter.

The documentation for the source code is scarce so i don’t feel very comfortable editing it. You might hint them at rna_text.c, text.c.

I think it would be possible to follow the example of current_line_index and add a wrapper using RNA_def_property_int_funcs and add the update but i am not sure.

@pink vertex: ah okay. I already had some trouble with ä, ö and ü. Thanks for your explanation :slight_smile: and that you looked at the code

Using the move operator works quite good now :slight_smile:

Beside some internal stuff I started to add a more advanced feature:

Basicly I can define some patterns which can be evaluated by the addon as you see in the .gif

I would like to hear if someone has other ideas for those dynamic snippets. :slight_smile:

Great update! The snippets remind me of zen coding (html). They could be really useful for bpy.props, you could feed it a name and have the cursor sitting at the description, just add a default value and done!

Also, any chance of adding support for autocompleting our own code (things defined in the script). Maybe using sphinx tags to get more data?

@Januz: the addon can autocomplete all words which already exist in the code. It’s quite hard to get better autocompletion like with Intellisense, because python is very dynamically typed… So its very hard to find out the type of a given variable name…
There are other frameworks which can do this (Jedi py) but I don’t know how well this works.