Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code folding #33

Open
tomlin7 opened this issue Jul 25, 2023 · 1 comment
Open

Code folding #33

tomlin7 opened this issue Jul 25, 2023 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@tomlin7
Copy link
Owner

tomlin7 commented Jul 25, 2023

  • 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 tomlin7 added enhancement New feature or request help wanted Extra attention is needed labels Jul 25, 2023
@tomlin7
Copy link
Owner Author

tomlin7 commented Jul 25, 2023

python_YLrMPfK4qw.mp4

@tomlin7 tomlin7 added this to the Update cupcake core milestone Aug 30, 2023
tomlin7 added a commit that referenced this issue Sep 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant