Skip to content

Commit

Permalink
Merge pull request #89 from Integration-Automation/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JE-Chen authored Jun 15, 2023
2 parents 3071fbd + c504cb8 commit b7b8810
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,6 @@ dmypy.json

# Pyre type checker
.pyre/

# User setting
**/user_setting*
11 changes: 5 additions & 6 deletions stable.toml → dev.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Rename to build stable version
# This is stable version
# Rename to build dev version
# This is dev version
[build-system]
requires = ["setuptools>=61.0"]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "je_editor"
version = "0.0.110"
name = "je_editor_dev"
version = "0.0.117"
authors = [
{ name = "JE-Chen", email = "jechenmailman@gmail.com" },
]
Expand All @@ -29,7 +29,6 @@ classifiers = [
"Operating System :: OS Independent"
]


[project.urls]
Homepage = "https://github.com/JE-Chen/je_editor"
Documentation = "https://je-editor.readthedocs.io/en/latest/"
Expand Down
67 changes: 64 additions & 3 deletions je_editor/pyside_ui/code_editor/code_edit_plaintext.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from typing import Union

from PySide6 import QtGui
from PySide6.QtCore import Qt, QRect
from PySide6.QtGui import QPainter, QColor, QTextCharFormat, QTextFormat, QKeyEvent, QAction, QTextDocument
from PySide6.QtWidgets import QPlainTextEdit, QWidget, QTextEdit
from PySide6.QtGui import QPainter, QColor, QTextCharFormat, QTextFormat, QKeyEvent, QAction, QTextDocument, QTextCursor
from PySide6.QtWidgets import QPlainTextEdit, QWidget, QTextEdit, QCompleter

from je_editor.pyside_ui.complete_list.total_complete_list import complete_list
from je_editor.pyside_ui.search_ui.search_text_box import SearchBox
from je_editor.pyside_ui.syntax.python_syntax import PythonHighlighter

Expand All @@ -14,7 +17,7 @@ class CodeEditor(QPlainTextEdit):
"""
def __init__(self):
super().__init__()
self.line_number = LineNumber(self)
self.line_number: LineNumber = LineNumber(self)
self.blockCountChanged.connect(self.update_line_number_area_width)
self.updateRequest.connect(self.update_line_number_area)
self.update_line_number_area_width(0)
Expand All @@ -32,6 +35,48 @@ def __init__(self):
self.start_search_dialog
)
self.addAction(self.search_action)
# Complete
self.completer: Union[None, QCompleter] = None
self.set_complete()

def set_complete(self, list_to_complete: list = complete_list):
completer = QCompleter(list_to_complete)
completer.activated.connect(self.insert_completion)
completer.setWidget(self)
completer.setWrapAround(False)
completer.setCompletionMode(QCompleter.CompletionMode.PopupCompletion)
completer.setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)
self.completer = completer

def insert_completion(self, completion):
if self.completer.widget() != self:
return
text_cursor = self.textCursor()
extra = len(completion) - len(self.completer.completionPrefix())
text_cursor.movePosition(QTextCursor.MoveOperation.Left)
text_cursor.movePosition(QTextCursor.MoveOperation.EndOfWord)
text_cursor.insertText(completion[-extra:])
self.setTextCursor(text_cursor)

@property
def text_under_cursor(self):
text_cursor = self.textCursor()
text_cursor.select(QTextCursor.SelectionType.WordUnderCursor)
return text_cursor.selectedText()

def focusInEvent(self, e) -> None:
if self.completer:
self.completer.setWidget(self)
QPlainTextEdit.focusInEvent(self, e)

def complete(self):
prefix = self.text_under_cursor
self.completer.setCompletionPrefix(prefix)
popup = self.completer.popup()
cursor_rect = self.cursorRect()
popup.setCurrentIndex(self.completer.completionModel().index(0, 0))
cursor_rect.setWidth(self.completer.popup().rect().size().width())
self.completer.complete(cursor_rect)

def start_search_dialog(self) -> None:
# Search box connect to function
Expand Down Expand Up @@ -126,6 +171,20 @@ def keyPressEvent(self, event) -> None:
:param event: keypress event
:return: None
"""
skip_popup_behavior_list = [
Qt.Key.Key_Enter, Qt.Key.Key_Return, Qt.Key.Key_Up, Qt.Key.Key_Down,
Qt.Key.Key_Tab, Qt.Key.Key_Backtab, Qt.Key.Key_Space
]
need_complete_list = [
Qt.Key.Key_A, Qt.Key.Key_B, Qt.Key.Key_C, Qt.Key.Key_D, Qt.Key.Key_E, Qt.Key.Key_F,
Qt.Key.Key_G, Qt.Key.Key_H, Qt.Key.Key_I, Qt.Key.Key_J, Qt.Key.Key_K, Qt.Key.Key_L,
Qt.Key.Key_M, Qt.Key.Key_N, Qt.Key.Key_O, Qt.Key.Key_P, Qt.Key.Key_Q, Qt.Key.Key_R,
Qt.Key.Key_S, Qt.Key.Key_T, Qt.Key.Key_U, Qt.Key.Key_V, Qt.Key.Key_W, Qt.Key.Key_X,
Qt.Key.Key_Y, Qt.Key.Key_Z, Qt.Key.Key_Backspace
]
if self.completer.popup().isVisible() and event.key() in skip_popup_behavior_list:
event.ignore()
return
key_event = QKeyEvent(event)
if key_event.modifiers() and Qt.Modifier.SHIFT:
key = key_event.key()
Expand All @@ -136,6 +195,8 @@ def keyPressEvent(self, event) -> None:
else:
super().keyPressEvent(event)
self.highlight_current_line()
if event.key() in need_complete_list and self.completer is not None:
self.complete()

def mousePressEvent(self, event) -> None:
# Highlight mouse click line
Expand Down
Empty file.
28 changes: 28 additions & 0 deletions je_editor/pyside_ui/complete_list/total_complete_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
complete_list = [
"False", "None", "True", "and", "as", "assert", "async",
"await", "break", "class", "continue", "def", "del",
"elif", "else", "except", "finally", "for", "from",
"global", "if", "import", "in", "is", "lambda", "nonlocal",
"not", "or", "pass", "raise", "return", "try", "while", "with", "yield",
"abs", "aiter", "all", "any", "anext", "ascii",
"bin", "bool", "breakpoint", "bytearray", "bytes",
"callable", "chr", "classmethod", "compile", "complex",
"delattr", "dict", "dir", "divmod",
"enumerate", "eval", "exec",
"filter", "float", "format", "frozenset",
"getattr", "globals",
"hasattr", "hash", "help", "hex",
"id", "input", "int", "isinstance", "issubclass", "iter",
"len", "list", "locals",
"map", "max", "memoryview", "min",
"next",
"object", "oct", "open", "ord",
"pow", "print", "property",
"range", "repr", "reversed", "round",
"set", "setattr", "slice", "sorted", "staticmethod",
"str", "sum", "super",
"tuple", "type",
"vars",
"zip",
"__import__"
]
36 changes: 18 additions & 18 deletions je_editor/pyside_ui/syntax/python_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from PySide6.QtGui import QTextCharFormat

keywords = [
"False", "None", "True", "and", "as", "assert", "async",
"False", "None", "True", "and", "as", "assert", "async",
"await", "break", "class", "continue", "def", "del",
"elif", "else", "except", "finally", "for", "from",
"elif", "else", "except", "finally", "for", "from",
"global", "if", "import", "in", "is", "lambda", "nonlocal",
"not", "or", "pass", "raise", "return", "try", "while", "with", "yield"
]
Expand Down Expand Up @@ -41,8 +41,8 @@
# Regex pattern

string_rule = [
r"'''[^'\\]*(\\.[^'\\]*)*'''", # 3* Singel
r'"""[^"\\]*(\\.[^"\\]*)*"""', # 3* Double
r"'''^.*(?:[^\\']|\\\\|\\')*.*$'''", # 3* Singel
r'"""^.*(?:[^\\"]|\\\\|\\")*.*$"""', # 3* Double
r"'[^'\\]*(\\.[^'\\]*)*'", # Singel
r'"[^"\\]*(\\.[^"\\]*)*"' # Double
]
Expand All @@ -68,20 +68,6 @@ def __init__(self, parent=None):
pattern = QRegularExpression(rf"\b{word}\b")
self.highlight_rules.append((pattern, text_char_format))

# Highlight numbers
text_char_format = QTextCharFormat()
text_char_format.setForeground(QColor(0, 128, 255))
for rule in number_rule:
pattern = QRegularExpression(rule)
self.highlight_rules.append((pattern, text_char_format))

# Highlight strings
text_char_format = QTextCharFormat()
text_char_format.setForeground(QColor(0, 153, 0))
for rule in string_rule:
pattern = QRegularExpression(rule)
self.highlight_rules.append((pattern, text_char_format))

# Highlight builtins
text_char_format = QTextCharFormat()
text_char_format.setForeground(QColor(0, 255, 255))
Expand All @@ -100,6 +86,20 @@ def __init__(self, parent=None):
pattern = QRegularExpression(r"\bself\b")
self.highlight_rules.append((pattern, text_char_format))

# Highlight numbers
text_char_format = QTextCharFormat()
text_char_format.setForeground(QColor(0, 128, 255))
for rule in number_rule:
pattern = QRegularExpression(rule)
self.highlight_rules.append((pattern, text_char_format))

# Highlight strings
text_char_format = QTextCharFormat()
text_char_format.setForeground(QColor(0, 153, 0))
for rule in string_rule:
pattern = QRegularExpression(rule)
self.highlight_rules.append((pattern, text_char_format))

def highlightBlock(self, text) -> None:
for pattern, format in self.highlight_rules:
match_iterator = pattern.globalMatch(text)
Expand Down
11 changes: 6 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Rename to build dev version
# This is dev version
# Rename to build stable version
# This is stable version
[build-system]
requires = ["setuptools"]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "je_editor_dev"
version = "0.0.115"
name = "je_editor"
version = "0.0.112"
authors = [
{ name = "JE-Chen", email = "jechenmailman@gmail.com" },
]
Expand All @@ -29,6 +29,7 @@ classifiers = [
"Operating System :: OS Independent"
]


[project.urls]
Homepage = "https://github.com/JE-Chen/je_editor"
Documentation = "https://je-editor.readthedocs.io/en/latest/"
Expand Down

0 comments on commit b7b8810

Please sign in to comment.