Advertisement
here2share

# tk_quick_indent.py

Feb 14th, 2023
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. # tk_quick_indent.py ZZZ
  2.  
  3. from tkinter import *
  4.  
  5. root = Tk()
  6. root.title('tk Quick-Indent')
  7. root.geometry("1280x640+-10+0")
  8.  
  9. def default_indent(code, tabs=0, indent_char='\t'):
  10.     def tab():
  11.         tabbed_code.append(indent_char * tabs + line)
  12.  
  13.     keywordsA = "def class".split()
  14.     keywordsB = "return continue break yield".split()
  15.     lines = code.split("\n") + ['\n']
  16.     tabbed_code = []
  17.     prev = []
  18.     for i in range(len(lines) - 1):
  19.         line = lines[i].lstrip()
  20.         if '#!t' in line: # custom indent
  21.             tabs = int(line[line.index('#!t') + 3])
  22.             tab()
  23.         elif any(line.startswith(keyword) for keyword in keywordsA):
  24.             tabs = 0
  25.             tab()
  26.             tabs = 1
  27.         else:
  28.             if line.endswith(':'):
  29.                 tab()
  30.                 tabs += 1
  31.             elif any(line.startswith(keyword) for keyword in keywordsB):
  32.                 if lines[i + 1].strip():
  33.                     tab()
  34.                     tabs -= 1
  35.                 else:
  36.                     tabs = 1
  37.                     tab()
  38.                     tabs = 0
  39.             elif not lines[i + 1].strip():
  40.                 tab()
  41.                 tabs = 0
  42.         if prev == tabbed_code:
  43.             tab()
  44.         prev = tabbed_code[:]
  45.     tabbed_code = "\n".join(tabbed_code)
  46.     return tabbed_code
  47.  
  48. frame = Frame(root)
  49. frame.pack(fill=BOTH, expand=1)
  50.  
  51. textbox = Text(frame, height=40, width=140, wrap=WORD)
  52. textbox.pack(fill=BOTH, expand=1)
  53.  
  54. # Add a scrollbar to the Text widget
  55. scrollbar = Scrollbar(frame)
  56. scrollbar.pack(side=RIGHT, fill=Y)
  57. scrollbar.config(command=textbox.yview)
  58. textbox.config(yscrollcommand=scrollbar.set)
  59.  
  60. untabbed_test_code = '''
  61. def complex_operation(a, b, c):
  62. try:
  63. result = a / b
  64. result = result ** c
  65. result = result * (b + c)
  66. return result
  67. except ZeroDivisionError:
  68. return "division by zero"
  69. except TypeError as e:
  70. if "unsupported operand type" in str(e):
  71. return "unsupported operand type for operation"
  72. else:
  73. raise e
  74.  
  75. input_a = 10
  76. input_b = 5
  77. input_c = 2
  78. output = complex_operation(input_a, input_b, input_c)
  79. print("output:", output)
  80.  
  81. def calculate_result(data):
  82. result = 0
  83. for i in range(len(data)):
  84. for j in range(i+1, len(data)):
  85. sub_result = 0
  86. for k in range(j, len(data)):
  87. sub_result += data[k]
  88. if sub_result == 0:
  89. result += 1
  90. return result
  91.  
  92. data = [1, -1, 2, 3, -2, -3, 4, -4]
  93. output = calculate_result(data)
  94. print("output:", output)
  95. '''[1:-1]
  96.  
  97. test_code = default_indent(untabbed_test_code, tabs=0)
  98.  
  99. textbox.insert(INSERT, test_code)
  100.  
  101. line_numbers = ()
  102.  
  103. def select(event):
  104.     global line_numbers
  105.     sel = textbox.tag_ranges("sel")
  106.     if not sel:
  107.         return
  108.     start_line, end_line = map(lambda x: int(str(x).split('.')[0]), sel)
  109.     if start_line % 2 == 0:
  110.         start_line += 1
  111.     if end_line % 2 == 0:
  112.         end_line -= 1
  113.     line_numbers = range(start_line - 1, end_line)
  114.  
  115. def arrowkey_indent(event):
  116.     if line_numbers and event.state == 262148:
  117.         if "Left" in event.keysym:
  118.             def f():
  119.                 if line.startswith('\t'):
  120.                     return line[1:]
  121.         elif "Right" in event.keysym:
  122.             def f():
  123.                 return '\t' + line
  124.         else:
  125.             return
  126.         for line_number in line_numbers:
  127.             line = textbox.get(f"{line_number}.0", f"{line_number}.end")
  128.             line = f()
  129.             try:
  130.                 textbox.replace(f"{line_number}.0", f"{line_number}.end", line)
  131.             except:
  132.                 0
  133.  
  134. textbox.bind("<KeyPress>", arrowkey_indent)
  135. textbox.bind("<ButtonRelease-1>", select)
  136. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement