cupcake icon indicating copy to clipboard operation
cupcake copied to clipboard

Code folding

Open tomlin7 opened this issue 1 year ago • 1 comments

  • show folding marks (purely based on indentation level increases rn)
class LineNumbers(Canvas):
    def __init__(self, master, text=None, *args, **kwargs):
        super().__init__(master, *args, **kwargs)
        self.config(width=65, bd=0, highlightthickness=0)
        self.text = text
        self.text.bind("<Configure>", self.redraw)

    def get_indentation_level(self, line):
        """Get the indentation level of a given line."""
        return len(line) - len(line.lstrip())

    def redraw(self, *_):
        self.delete(tk.ALL)

        prev_indent = 0
        i = self.text.index("@0,0")
        while True:
            dline = self.text.dlineinfo(i)
            if dline is None:
                break

            y = dline[1]
            linenum = str(i).split(".")[0]

            # Get the text content of the current line
            line_content = self.text.get(f"{linenum}.0", f"{linenum}.end")
            current_indent = self.get_indentation_level(line_content)

            # Determine if the current line has more indentation than the previous line
            if current_indent > prev_indent:
                line_num_with_indent = f"{linenum} +"
            else:
                line_num_with_indent = linenum

            # to highlight the current line
            curline = self.text.dlineinfo(tk.INSERT)
            cur_y = curline[1] if curline else None

            if not cur_y:
                i = self.text.index(f"{i}+1line")
                continue

            self.create_text(40, y, anchor=tk.NE, text=line_num_with_indent, font=("Consolas", 14), tag=i)
            
            # Update the previous indentation level
            prev_indent = current_indent
            i = self.text.index(f"{i}+1line")

image

tomlin7 avatar Jul 25 '23 14:07 tomlin7

https://github.com/billyeatcookies/cupcake/assets/70792552/745bc60d-91f7-49db-860d-f51027a2d849

tomlin7 avatar Jul 25 '23 19:07 tomlin7