src/eric7/QScintilla/Editor.py

branch
eric7
changeset 9853
080e060a0383
parent 9790
6b14962d9d31
child 9925
a267ac36dd69
equal deleted inserted replaced
9852:b7aef103355a 9853:080e060a0383
225 225
226 self.enableMultiCursorSupport() 226 self.enableMultiCursorSupport()
227 227
228 self.dbs = dbs 228 self.dbs = dbs
229 self.taskViewer = tv 229 self.taskViewer = tv
230 self.setFileName(fn) 230 self.fileName = ""
231 self.vm = vm 231 self.vm = vm
232 self.filetype = filetype 232 self.filetype = filetype
233 self.filetypeByFlag = False 233 self.filetypeByFlag = False
234 self.noName = "" 234 self.noName = ""
235 self.project = ericApp().getObject("Project") 235 self.project = ericApp().getObject("Project")
236 self.setFileName(fn)
236 237
237 # clear some variables 238 # clear some variables
238 self.lastHighlight = None # remember the last highlighted line 239 self.lastHighlight = None # remember the last highlighted line
239 self.lastErrorMarker = None # remember the last error line 240 self.lastErrorMarker = None # remember the last error line
240 self.lastCurrMarker = None # remember the last current line 241 self.lastCurrMarker = None # remember the last current line
618 Public method to set the file name of the current file. 619 Public method to set the file name of the current file.
619 620
620 @param name name of the current file 621 @param name name of the current file
621 @type str 622 @type str
622 """ 623 """
624 renamed = self.fileName != name
625
623 self.fileName = name 626 self.fileName = name
627
628 if renamed:
629 self.vm.setEditorName(self, self.fileName)
624 630
625 if self.fileName: 631 if self.fileName:
626 self.__fileNameExtension = os.path.splitext(self.fileName)[1][1:].lower() 632 self.__fileNameExtension = os.path.splitext(self.fileName)[1][1:].lower()
627 else: 633 else:
628 self.__fileNameExtension = "" 634 self.__fileNameExtension = ""
3254 fn = self.noName 3260 fn = self.noName
3255 res = EricMessageBox.okToClearData( 3261 res = EricMessageBox.okToClearData(
3256 self, 3262 self,
3257 self.tr("File Modified"), 3263 self.tr("File Modified"),
3258 self.tr("<p>The file <b>{0}</b> has unsaved changes.</p>").format(fn), 3264 self.tr("<p>The file <b>{0}</b> has unsaved changes.</p>").format(fn),
3259 self.saveFile if self.isLocalFile() else None, 3265 self.saveFile if not self.isRemoteFile() else None,
3260 ) 3266 )
3261 if res: 3267 if res:
3262 self.vm.setEditorName(self, self.fileName) 3268 self.vm.setEditorName(self, self.fileName)
3263 return res 3269 return res
3264 3270
3526 3532
3527 @param saveas flag indicating a 'save as' action (boolean) 3533 @param saveas flag indicating a 'save as' action (boolean)
3528 @param path directory to save the file in (string) 3534 @param path directory to save the file in (string)
3529 @return flag indicating success (boolean) 3535 @return flag indicating success (boolean)
3530 """ 3536 """
3531 if not saveas and (not self.isModified() or not self.isLocalFile()): 3537 if not saveas and (not self.isModified() or self.isRemoteFile()):
3532 # do nothing if text wasn't changed or is not a local file 3538 # do nothing if text wasn't changed or is a remote file
3533 # TODO: write the file back to the MCU if isDeviceFile()
3534 return False 3539 return False
3540
3541 if self.isDeviceFile():
3542 return self.__saveDeviceFile(saveas=saveas)
3535 3543
3536 newName = None 3544 newName = None
3537 if saveas or self.fileName == "": 3545 if saveas or self.fileName == "":
3538 saveas = True 3546 saveas = True
3539 3547
3606 (boolean) 3614 (boolean)
3607 @return tuple of two values (boolean, string) giving a success 3615 @return tuple of two values (boolean, string) giving a success
3608 indicator and the name of the saved file 3616 indicator and the name of the saved file
3609 """ 3617 """
3610 return self.saveFile(True, path) 3618 return self.saveFile(True, path)
3619
3620 def __saveDeviceFile(self, saveas=False):
3621 """
3622 Private method to save the text to a file on the connected device.
3623
3624 @param saveas flag indicating a 'save as' action (defaults to False)
3625 @type bool (optional)
3626 @return flag indicating success
3627 @rtype bool
3628 """
3629 with contextlib.suppress(KeyError):
3630 mpy = ericApp().getObject("MicroPython")
3631 filemanager = mpy.getFileManager()
3632 if saveas:
3633 fn, ok = QInputDialog.getText(
3634 self,
3635 self.tr("Save File to Device"),
3636 self.tr("Enter the complete device file path:"),
3637 QLineEdit.EchoMode.Normal,
3638 self.fileName,
3639 )
3640 if not ok or not fn:
3641 # aborted
3642 return False
3643 else:
3644 fn = self.fileName
3645 # Convert the file name to device path separators ('/') and ensure,
3646 # intermediate directories exist (creating them if necessary)
3647 fn = fn.replace("\\", "/")
3648 if "/" in fn:
3649 dn = fn.rsplit("/", 1)[0]
3650 filemanager.makedirs(dn.replace("device:", ""))
3651 success = filemanager.writeFile(fn.replace("device:", ""), self.text())
3652 if success:
3653 self.setFileName(fn)
3654 self.setModified(False)
3655 self.resetOnlineChangeTraceInfo()
3656 return success
3657
3658 return False
3611 3659
3612 def handleRenamed(self, fn): 3660 def handleRenamed(self, fn):
3613 """ 3661 """
3614 Public slot to handle the editorRenamed signal. 3662 Public slot to handle the editorRenamed signal.
3615 3663

eric ide

mercurial