Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions cc3d/twedit5/EditorWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def __init__(self, _editor, _editorWindow):
def handleChangedText(self):

self.editorWindow.updateTextSizeLabel()
self.editorWindow.scheduleAutoSave(self.editor)

def handleModificationChanged(self, m):

Expand Down Expand Up @@ -454,6 +455,11 @@ def __init__(self, _startFileListener=True):
self.defaultEditor = None

self.textChangedHandlers = {}
self.autoSavePendingEditors = set()
self.autoSaveTimer = QTimer(self)
self.autoSaveTimer.setSingleShot(True)
self.autoSaveTimer.setInterval(self.configuration.setting("AutoSaveInterval"))
self.autoSaveTimer.timeout.connect(self.performAutoSave)

# class variables used in searches

Expand Down Expand Up @@ -1429,6 +1435,8 @@ def closeEvent(self, event):
openFilesToRestore = [{}, {}]

self.deactivateChangeSensing = True
self.autoSaveTimer.stop()
self.autoSavePendingEditors.clear()

# determining index of the current tab

Expand Down Expand Up @@ -4327,6 +4335,40 @@ def configureEnableQuickTextDecoding(self, _flag):

pass

def configureAutoPairCharacters(self, _flag):

"""

fcn handling AutoPairCharacters configuration change

"""

self.configuration.setSetting("AutoPairCharacters", _flag)

def configureEnableAutoSave(self, _flag):

"""

fcn handling EnableAutoSave configuration change

"""

self.configuration.setSetting("EnableAutoSave", _flag)
if not _flag:
self.autoSaveTimer.stop()
self.autoSavePendingEditors.clear()

def configureAutoSaveInterval(self, _value):

"""

fcn handling AutoSaveInterval configuration change

"""

self.configuration.setSetting("AutoSaveInterval", _value)
self.autoSaveTimer.setInterval(_value)

def configureAutocompletionThreshold(self, _value):

"""
Expand Down Expand Up @@ -5157,6 +5199,54 @@ def saveAll(self):

currentEditor.setFocus(Qt.MouseFocusReason)

def scheduleAutoSave(self, editor):

"""

schedules autosave for a changed editor

"""

if self.deactivateChangeSensing:
return

if not self.configuration.setting("EnableAutoSave"):
return

if editor is None or editor.isReadOnly():
return

file_name = self.getEditorFileName(editor)
if not file_name:
return

self.autoSavePendingEditors.add(editor)
self.autoSaveTimer.start(self.configuration.setting("AutoSaveInterval"))

def performAutoSave(self):

"""

saves modified named documents after the configured autosave delay

"""

if self.deactivateChangeSensing:
return

pending_editors = list(self.autoSavePendingEditors)
self.autoSavePendingEditors.clear()

for editor in pending_editors:
if editor is None or editor.isReadOnly() or not editor.isModified():
continue

file_name = self.getEditorFileName(editor)
if not file_name:
continue

self.saveFile(file_name, editor)

def about(self):

"""
Expand Down
67 changes: 67 additions & 0 deletions cc3d/twedit5/QsciScintillaCustom.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

class QsciScintillaCustom(QsciScintilla):

AUTO_PAIR_CHARS = {
"'": "'",
'"': '"',
"(": ")",
"[": "]",
"{": "}"
}

def __init__(self, parent=None, _panel=None):

super(QsciScintillaCustom, self).__init__(parent)
Expand Down Expand Up @@ -88,9 +96,68 @@ def keyPressEvent(self, event):
elif event.modifiers() & Qt.ControlModifier and event.modifiers() & Qt.ShiftModifier:
self.handleScintillaDefaultShortcut('Ctrl+Shift', event)

elif self.handle_auto_pair_characters(event):
event.accept()

else:
super(QsciScintillaCustom, self).keyPressEvent(event)

def handle_auto_pair_characters(self, event):
"""
Inserts matching closing characters or wraps selected text when enabled.

:param event: keyboard event
:return: True when this method handled the event
"""
typed_text = event.text()
if len(typed_text) != 1:
return False

if not self.__auto_pair_characters_enabled():
return False

if event.modifiers() & (Qt.ControlModifier | Qt.AltModifier | Qt.MetaModifier):
return False

if typed_text in self.AUTO_PAIR_CHARS:
self.__insert_auto_pair(typed_text, self.AUTO_PAIR_CHARS[typed_text])
return True

if typed_text in self.AUTO_PAIR_CHARS.values() and self.__current_char() == typed_text:
line, index = self.getCursorPosition()
self.setCursorPosition(line, index + 1)
return True

return False

def __auto_pair_characters_enabled(self):
try:
return self.editorWindow.configuration.setting("AutoPairCharacters")
except AttributeError:
return True

def __insert_auto_pair(self, opening_char, closing_char):
if self.hasSelectedText():
selected_text = self.selectedText()
self.beginUndoAction()
self.replaceSelectedText(opening_char + selected_text + closing_char)
self.endUndoAction()
return

line, index = self.getCursorPosition()
self.beginUndoAction()
self.insert(opening_char + closing_char)
self.endUndoAction()
self.setCursorPosition(line, index + 1)

def __current_char(self):
line, index = self.getCursorPosition()
line_text = self.text(line)
if index < len(line_text):
return line_text[index]

return ''

def focusInEvent(self, event):
editor_tab = 0

Expand Down
Loading