Page MenuHome

Text Editor: Python API Add read-only RNA Text.selected_text
AbandonedPublic

Authored by Kai Jægersen (kaio) on Dec 8 2019, 11:35 AM.
Tokens
"Like" token, awarded by ckohl_art."Love" token, awarded by wilBr."Love" token, awarded by Samirosman."Love" token, awarded by tintwotin."Love" token, awarded by samytichadou."Love" token, awarded by jc4d.

Details

Summary

This adds the ability to retrieve text selection as string.

Why

Extending the capabilities of blender's text editor through python often requires getting the current selection as string.
Although this can be done (inelegantly) from python, this patch adds a simpler way of doing so.

How

Internal operators use txt_sel_to_buf in text.c for getting selection.
This patch wraps the same function and makes the value accessible from python.

What's being added

Property: Text.selected_text

  • Returns a string concatenation from current selection, separated by a newline char

RNA functions

  • Getter function rna_Text_selected_text_get
  • Length function rna_Text_selected_text_length
Other
  • Property is called selected_text, but open for other suggestions.
  • Only getter and length added. Overwriting selection can be done using Text.write().


Diff Detail

Event Timeline

Campbell Barton (campbellbarton) requested changes to this revision.EditedDec 8 2019, 8:06 PM

Would prefer this be an API function, then we can have a function to set the text too - making it more useful.

Also, using attributes to generating potentially large amounts of data makes writing inefficient code possible, without it being obvious.

def find_empty_parens():
    for i in range(len(text.selected_text) - 1):
        if text.selected_text[i : i + 1] == "()":
            return i
This revision now requires changes to proceed.Dec 8 2019, 8:06 PM

Any chance for getting this in 2.83?

Any chance for getting this in 2.83?

It does the job I intended it for, but it's also weak for the reasons outlined by Campbell.

A set of functions that reads/writes the cursor range is probably more useful, but I'll pass that torch to someone else.