Skip to content

Commit 2b89a36

Browse files
committed
use close instead of revert as button name, remember last snippet edited
1 parent 380205c commit 2b89a36

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

__init__.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,14 @@ def __init__(self, parent=None):
102102
self.setWindowModality(Qt.ApplicationModal)
103103
self.title = QLabel(self.tr("Snippet Editor"))
104104
self.saveButton = QPushButton(self.tr("Save"))
105-
self.revertButton = QPushButton(self.tr("Revert"))
105+
self.closeButton = QPushButton(self.tr("Close"))
106106
self.clearHotkeyButton = QPushButton(self.tr("Clear Hotkey"))
107107
self.setWindowTitle(self.title.text())
108108
self.newFolderButton = QPushButton("New Folder")
109109
self.deleteSnippetButton = QPushButton("Delete")
110110
self.newSnippetButton = QPushButton("New Snippet")
111111
self.edit = QPlainTextEdit()
112+
self.edit.setPlaceholderText("python code")
112113
self.resetting = False
113114
self.columns = 3
114115

@@ -118,9 +119,7 @@ def __init__(self, parent=None):
118119
self.currentFileLabel = QLabel()
119120
self.currentFile = ""
120121
self.snippetDescription = QLineEdit()
121-
self.snippetEditsPending = False
122-
123-
self.clearSelection()
122+
self.snippetDescription.setPlaceholderText("optional description")
124123

125124
#Set Editbox Size
126125
font = getMonospaceFont(self)
@@ -158,7 +157,7 @@ def __init__(self, parent=None):
158157
buttons.addWidget(self.clearHotkeyButton)
159158
buttons.addWidget(self.keySequenceEdit)
160159
buttons.addWidget(self.currentHotkeyLabel)
161-
buttons.addWidget(self.revertButton)
160+
buttons.addWidget(self.closeButton)
162161
buttons.addWidget(self.saveButton)
163162

164163
description = QHBoxLayout()
@@ -167,9 +166,8 @@ def __init__(self, parent=None):
167166

168167
vlayoutWidget = QWidget()
169168
vlayout = QVBoxLayout()
170-
vlayout.addWidget(self.currentFileLabel)
171-
vlayout.addWidget(self.edit)
172169
vlayout.addLayout(description)
170+
vlayout.addWidget(self.edit)
173171
vlayout.addLayout(buttons)
174172
vlayoutWidget.setLayout(vlayout)
175173

@@ -193,15 +191,27 @@ def __init__(self, parent=None):
193191

194192
# Add signals
195193
self.saveButton.clicked.connect(self.save)
196-
self.revertButton.clicked.connect(self.loadSnippet)
194+
self.closeButton.clicked.connect(self.close)
197195
self.clearHotkeyButton.clicked.connect(self.clearHotkey)
198196
self.tree.selectionModel().selectionChanged.connect(self.selectFile)
199197
self.newSnippetButton.clicked.connect(self.newFileDialog)
200198
self.deleteSnippetButton.clicked.connect(self.deleteSnippet)
201199
self.newFolderButton.clicked.connect(self.newFolder)
202200

203-
#Read-only until new snippet
204-
self.readOnly(True)
201+
if self.settings.contains("ui/snippeteditor/selected"):
202+
selectedName = self.settings.value("ui/snippeteditor/selected")
203+
self.tree.selectionModel().select(self.files.index(selectedName), QItemSelectionModel.ClearAndSelect | QItemSelectionModel.Rows)
204+
if self.tree.selectionModel().hasSelection():
205+
self.selectFile(self.tree.selectionModel().selection(), None)
206+
self.edit.setFocus()
207+
cursor = self.edit.textCursor()
208+
cursor.setPosition(self.edit.document().characterCount()-1)
209+
self.edit.setTextCursor(cursor)
210+
else:
211+
self.readOnly(True)
212+
else:
213+
self.readOnly(True)
214+
205215

206216
@staticmethod
207217
def registerAllSnippets():
@@ -216,7 +226,7 @@ def registerAllSnippets():
216226
snippetKeys = None
217227
(snippetDescription, snippetKeys, snippetCode) = loadSnippetFromFile(snippet)
218228
if not snippetDescription:
219-
actionText = "Snippets\\" + snippet
229+
actionText = "Snippets\\" + os.path.basename(snippet).rstrip(".py")
220230
else:
221231
actionText = "Snippets\\" + snippetDescription
222232
if snippetCode:
@@ -234,6 +244,7 @@ def clearSelection(self):
234244
self.currentFileLabel.setText("")
235245
self.snippetDescription.setText("")
236246
self.edit.setPlainText("")
247+
self.currentFile = ""
237248

238249
def reject(self):
239250
self.settings.setValue("ui/snippeteditor/geometry", self.saveGeometry())
@@ -252,19 +263,21 @@ def newFolder(self):
252263
if QFileInfo(selection).isDir():
253264
QDir(selection).mkdir(folderName)
254265
else:
255-
QDir(snippetPath).mkdir(folderName)
266+
QDir(snippetPath).mkdir(folderName)
256267

257268
def selectFile(self, new, old):
258269
if (self.resetting):
259270
self.resetting = False
260271
return
261272
newSelection = self.files.filePath(new.indexes()[0])
273+
self.settings.setValue("ui/snippeteditor/selected", newSelection)
262274
if QFileInfo(newSelection).isDir():
263275
self.readOnly(True)
264-
self.clearSelection()
276+
self.tree.clearSelection()
277+
self.currentFile = ""
265278
return
266279

267-
if old.length() > 0:
280+
if old and old.length() > 0:
268281
oldSelection = self.files.filePath(old.indexes()[0])
269282
if not QFileInfo(oldSelection).isDir() and self.snippetChanged():
270283
question = QMessageBox.question(self, self.tr("Discard"), self.tr("Snippet changed. Discard changes?"))
@@ -292,10 +305,12 @@ def newFileDialog(self):
292305
index = self.tree.selectionModel().currentIndex()
293306
selection = self.files.filePath(index)
294307
if QFileInfo(selection).isDir():
295-
open(os.path.join(selection, snippetName), "w").close()
308+
path = os.path.join(selection, snippetName)
296309
else:
297-
open(os.path.join(snippetPath, snippetName), "w").close()
310+
path = os.path.join(snippetPath, snippetName)
298311
self.readOnly(False)
312+
open(path, "w").close()
313+
self.tree.setCurrentIndex(self.files.index(path))
299314
log_debug("Snippet %s created." % snippetName)
300315

301316
def readOnly(self, flag):
@@ -323,8 +338,6 @@ def snippetChanged(self):
323338
if (self.currentFile == "" or QFileInfo(self.currentFile).isDir()):
324339
return False
325340
(snippetDescription, snippetKeys, snippetCode) = loadSnippetFromFile(self.currentFile)
326-
if (not snippetCode):
327-
return False
328341
if snippetKeys == None and not self.keySequenceEdit.keySequence().isEmpty():
329342
return True
330343
if snippetKeys != None and snippetKeys != self.keySequenceEdit.keySequence().toString():

0 commit comments

Comments
 (0)