[Addon] Auto Completion in Blenders Text Editor

Yes, this is really great !

Lapineige has made some great code for the text editor to comment easilly, maybe you can add something like that.


import bpy
from re import search
from bpy.types import Menu

def changeText(self,context):
    bpy.context.space_data.text = bpy.data.texts[bpy.context.scene.tmp_text]
    
bpy.types.Scene.tmp_text = bpy.props.EnumProperty(items = [(txt.name,txt.name,'','',bpy.data.texts.find(txt.name)) for txt in bpy.data.texts], update=changeText)


class TextEditorPie(Menu):
    # label is displayed at the center of the pie menu.
    bl_label = "Text Editor Pie"
    bl_idname = "text.text_editor_pie"
    
    @classmethod
    def poll(cls, context):
        return context.space_data.type == 'TEXT_EDITOR'
    
    def draw(self, context):
        layout = self.layout

        pie = layout.menu_pie()
        pie.operator('text.unindent',icon='BACK')
        pie.operator('text.better_indent',icon='FORWARD', text='Indent')
        pie.prop(context.space_data, 'show_word_wrap', text='Wrap')
        pie.operator('text.run_script',icon='PLAY')
        pie.operator('text.better_uncomment',icon='VISIBLE_IPO_OFF', text='Uncomment')
        pie.operator('text.better_comment',icon='VISIBLE_IPO_OFF', text='Comment')
        pie.operator('text.select_line',icon='BORDER_RECT')
        pie.menu(menu='text.text_list_menu', text='Choose Text')
        
        
class TextsList(bpy.types.Menu):
    bl_label = "Texts List"
    bl_idname = "text.text_list_menu"   
    
    def draw(self, context):
        layout = self.layout
        layout.props_enum(context.scene,'tmp_text')


class BetterComment(bpy.types.Operator):
    """  """
    bl_idname = "text.better_comment"
    bl_label = "Better Comment"

    def execute(self, context):
        base = context.space_data.text.current_line.body
        bpy.ops.text.comment()
        if context.space_data.text.current_line.body == base:
            context.space_data.text.current_line.body = '#' + context.space_data.text.current_line.body
        return {'FINISHED'}


class BetterUnComment(bpy.types.Operator):
    """  """
    bl_idname = "text.better_uncomment"
    bl_label = "Better Uncomment"

    def execute(self, context):
        base = context.space_data.text.current_line.body
        bpy.ops.text.uncomment()
        if '#' in base:
            if context.space_data.text.current_line.body == base and search(r'[^#]', base).start() > base.index('#'):
                context.space_data.text.current_line.body = base[search(r'[^#]', base).start():]
        return {'FINISHED'}


class BetterIndent(bpy.types.Operator):
    """  """
    bl_idname = "text.better_indent"
    bl_label = "Better Indent"

    def execute(self, context):
        base = context.space_data.text.current_line.body
        bpy.ops.text.indent()
        if search(r'[^ ]', context.space_data.text.current_line.body).start() == search(r'[^ ]', base).start():
            context.space_data.text.current_line.body = ' '*context.space_data.tab_width + base
        return {'FINISHED'}

def register():
    bpy.utils.register_class(TextEditorPie)
    bpy.utils.register_class(TextsList)
    bpy.utils.register_class(BetterComment)
    bpy.utils.register_class(BetterUnComment)
    bpy.utils.register_class(BetterIndent)


def unregister():
    bpy.utils.unregister_class(TextEditorPie)
    bpy.utils.unregister_class(TextsList)
    bpy.utils.unregister_class(BetterComment)
    bpy.utils.unregister_class(BetterUnComment)
    bpy.utils.unregister_class(BetterIndent)


if __name__ == "__main__":
    register()

    bpy.ops.wm.call_menu_pie(name=TextEditorPie.bl_idname)