Saturday, March 25, 2023

A simple Python Code for Un/Commenting multiple lines with PyQt

For everyone's benefits I am sharing my code snippet. As part of my on going personal Python IDE project, I need to find a way to somehow automate certain repetetive tasks that a python developer encounters when using an ordinary text editor like me. I encounter this scenario a lot wherein I have to comment out/uncomment several lines of codes. Or as a bonus, indenting/unindenting several lines of codes. I have prepared a simple python program to address this issue by using the following python program. I will update my actual project using this code :

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from PyQt5.QtWidgets import QTextEdit, QPushButton, QVBoxLayout, QApplication, QWidget, QShortcut, QMenu, QAction
from PyQt5.QtGui import QTextCursor, QKeySequence
from PyQt5.QtCore import Qt

app = QApplication([])

# Create a QTextEdit widget
text_edit = QTextEdit()

# Set the text of the widget
text_edit.setPlainText("This is a test text")

# Create a context menu for the text edit
context_menu = QMenu(text_edit)
add_hash_action = QAction("Add '#' to selected lines", context_menu)
remove_hash_action = QAction("Remove '#' from selected lines", context_menu)
add_spaces_action = QAction("Add 4 leading spaces to selected lines", context_menu)
remove_spaces_action = QAction("Remove 4 leading spaces from selected lines", context_menu)

# Define the function to be called when F3 is pressed
def insert_spaces():
    # Get the current cursor position
    cursor_position = text_edit.textCursor().position()
    cursor = text_edit.textCursor()
    selected_text = cursor.selectedText()
    # Get the text before the cursor position
    text_before_cursor = text_edit.toPlainText()[:cursor_position]
    
    # Split the text into lines
    lines = selected_text.split("\u2029")
    print(lines)
    # Add 4 spaces at the beginning of each line

    lines = ["    " + line if not line.startswith("#") else line for line in lines]
    # Join the lines and set the new text
    cursor.insertText("\n".join(lines))


# Define the function to be called when Shift+F3 is pressed
def remove_spaces():
    # Get the current cursor position and selected text
    cursor = text_edit.textCursor()
    selected_text = cursor.selectedText()

    # Split the selected text into lines
    lines = selected_text.split("\u2029")

    # Remove a 4 SPACES from the beginning of each line
    lines = [line[4:] if line.startswith("    ") else line for line in lines]

    # Replace the selected text with the modified text
    cursor.insertText("\n".join(lines))

# Define the function to add hash at the beginning of each selected line
def add_hash():
    # Get the current cursor position and selected text
    cursor = text_edit.textCursor()
    selected_text = cursor.selectedText()

    # Split the selected text into lines
    lines = selected_text.split("\u2029")
    print(lines)
    # Add a hash at the beginning of each line
    lines = ["#" + line if not line.startswith("#") else line for line in lines]

    # Replace the selected text with the modified text
    cursor.insertText("\n".join(lines))

# Define the function to remove hash from the beginning of each selected line
def remove_hash():
    # Get the current cursor position and selected text
    cursor = text_edit.textCursor()
    selected_text = cursor.selectedText()

    # Split the selected text into lines
    lines = selected_text.split("\u2029")

    # Remove a hash from the beginning of each line
    lines = [line[1:] if line.startswith("#") else line for line in lines]

    # Replace the selected text with the modified text
    cursor.insertText("\n".join(lines))
# Create a shortcut for the F3 key
shortcut_insert = QShortcut(QKeySequence("F3"), text_edit)
shortcut_insert.activated.connect(insert_spaces)

# Create a shortcut for the Shift+F3 key
shortcut_remove = QShortcut(QKeySequence("Shift+F3"), text_edit)
shortcut_remove.activated.connect(remove_spaces)

# Connect the add_spaces_action to the insert_spaces function
add_spaces_action.triggered.connect(insert_spaces)

# Connect the remove_spaces_action to the remove_spaces function
remove_spaces_action.triggered.connect(remove_spaces)

# Add the add_spaces_action and remove_spaces_action to the context menu
context_menu.addAction(add_spaces_action)
context_menu.addAction(remove_spaces_action)

# Set the context menu policy of the text edit
text_edit.setContextMenuPolicy(Qt.CustomContextMenu)
text_edit.customContextMenuRequested.connect(lambda event: context_menu.exec_(text_edit.mapToGlobal(event)))

# Create a shortcut for the F2 key to add hash
shortcut_add_hash = QShortcut(QKeySequence("F2"), text_edit)
shortcut_add_hash.activated.connect(add_hash)

# Create a shortcut for the Shift+F2 key to remove hash
shortcut_remove_hash = QShortcut(QKeySequence("Shift+F2"), text_edit)
shortcut_remove_hash.activated.connect(remove_hash)

# Connect the add_hash_action to the add_hash function
add_hash_action.triggered.connect(add_hash)

# Connect the remove_hash_action to the remove_hash function
remove_hash_action.triggered.connect(remove_hash)

# Add the add_hash_action and remove_hash_action to the context menu
context_menu.addAction(add_hash_action)
context_menu.addAction(remove_hash_action)

# Set the context menu policy of the text edit
text_edit.setContextMenuPolicy(Qt.CustomContextMenu)
text_edit.customContextMenuRequested.connect(lambda event: context_menu.exec_(text_edit.mapToGlobal(event)))

# Create a QPushButton widget
button = QPushButton("Print Selected Text")

# Define the function to be called when the button is clicked
def print_selected_text():
    selected_text = text_edit.textCursor().selectedText()
    print(f"Selected text: {selected_text}")

# Connect the button's clicked signal to the print_selected_text function
button.clicked.connect(print_selected_text)

# Create a QVBoxLayout to organize the widgets vertically
layout = QVBoxLayout()

# Add the widgets to the layout
layout.addWidget(text_edit)
layout.addWidget(button)

# Create a QWidget to hold the layout
widget = QWidget()

# Set the layout of the QWidget
widget.setLayout(layout)

# Show the QWidget
widget.show()

app.exec_()

I am also thinking of adding a feature wherein if I double-click a method, the cursor will go to the definition of that method and invent a way to make the cursor go back to its original position.

No comments:

Post a Comment