QScintilla/MiniEditor.py

changeset 6105
cbd34d558bd9
parent 6101
b854a825d483
child 6112
cc1fc199643b
equal deleted inserted replaced
6102:a5b9f6a38faf 6105:cbd34d558bd9
89 """ 89 """
90 self.mw.editorActGrp.setEnabled(False) 90 self.mw.editorActGrp.setEnabled(False)
91 self.setCaretWidth(0) 91 self.setCaretWidth(0)
92 92
93 super(MiniScintilla, self).focusOutEvent(event) 93 super(MiniScintilla, self).focusOutEvent(event)
94
95 def removeTrailingWhitespace(self):
96 """
97 Public method to remove trailing whitespace.
98 """
99 searchRE = r"[ \t]+$" # whitespace at the end of a line
100
101 ok = self.findFirstTarget(searchRE, True, False, False, 0, 0)
102 self.beginUndoAction()
103 while ok:
104 self.replaceTarget("")
105 ok = self.findNextTarget()
106 self.endUndoAction()
94 107
95 108
96 class MiniEditor(E5MainWindow): 109 class MiniEditor(E5MainWindow):
97 """ 110 """
98 Class implementing a minimalistic editor for simple editing tasks. 111 Class implementing a minimalistic editor for simple editing tasks.
144 self.__replaceWidget.hide() 157 self.__replaceWidget.hide()
145 158
146 self.lexer_ = None 159 self.lexer_ = None
147 self.apiLanguage = "" 160 self.apiLanguage = ""
148 self.filetype = "" 161 self.filetype = ""
162 self.__curFile = filename
163
164 self.__loadEditorConfig(filename)
149 165
150 self.__createActions() 166 self.__createActions()
151 self.__createMenus() 167 self.__createMenus()
152 self.__createToolBars() 168 self.__createToolBars()
153 self.__createStatusBar() 169 self.__createStatusBar()
2289 Private method to load the given file. 2305 Private method to load the given file.
2290 2306
2291 @param fileName name of the file to load (string) 2307 @param fileName name of the file to load (string)
2292 @param filetype type of the source file (string) 2308 @param filetype type of the source file (string)
2293 """ 2309 """
2310 QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
2311
2294 self.__loadEditorConfig(fileName=fileName) 2312 self.__loadEditorConfig(fileName=fileName)
2295
2296 QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
2297 2313
2298 try: 2314 try:
2299 encoding = self.__getEditorConfig("DefaultEncoding", 2315 encoding = self.__getEditorConfig("DefaultEncoding",
2300 nodefault=True) 2316 nodefault=True)
2301 if encoding: 2317 if encoding:
2311 '<p>Reason: {1}</p>') 2327 '<p>Reason: {1}</p>')
2312 .format(fileName, str(why))) 2328 .format(fileName, str(why)))
2313 QApplication.restoreOverrideCursor() 2329 QApplication.restoreOverrideCursor()
2314 return 2330 return
2315 2331
2332 modified = False
2333
2334 if (not self.__getEditorConfig("TabForIndentation")) and \
2335 Preferences.getEditor("ConvertTabsOnLoad") and \
2336 not (self.lexer_ and
2337 self.lexer_.alwaysKeepTabs()):
2338 txtExpanded = txt.expandtabs(self.__getEditorConfig("TabWidth"))
2339 if txtExpanded != txt:
2340 modified = True
2341 txt = txtExpanded
2342
2316 self.__textEdit.setText(txt) 2343 self.__textEdit.setText(txt)
2317 QApplication.restoreOverrideCursor() 2344 QApplication.restoreOverrideCursor()
2318 2345
2346 self.__textEdit.setModified(modified)
2347 self.setWindowModified(modified)
2348
2319 if filetype is None: 2349 if filetype is None:
2320 self.filetype = "" 2350 self.filetype = ""
2321 else: 2351 else:
2322 self.filetype = filetype 2352 self.filetype = filetype
2323 self.__setCurrentFile(fileName) 2353 self.__setCurrentFile(fileName)
2324 2354
2325 fileEol = self.__textEdit.detectEolString(txt) 2355 self.__textEdit.setModified(modified)
2326 # TODO: editorconfig: end_of_line 2356 self.setWindowModified(modified)
2327 self.__textEdit.setEolModeByEolString(fileEol) 2357
2358 eolMode = self.__getEditorConfig("EOLMode", nodefault=True)
2359 if eolMode is None:
2360 fileEol = self.__textEdit.detectEolString(txt)
2361 self.__textEdit.setEolModeByEolString(fileEol)
2362 else:
2363 self.__textEdit.convertEols(eolMode)
2328 2364
2329 self.__statusBar.showMessage(self.tr("File loaded"), 2000) 2365 self.__statusBar.showMessage(self.tr("File loaded"), 2000)
2330 2366
2331 def __saveFile(self, fileName): 2367 def __saveFile(self, fileName):
2332 """ 2368 """
2354 @param fileName name of the file to be written to 2390 @param fileName name of the file to be written to
2355 @type str 2391 @type str
2356 @return flag indicating success 2392 @return flag indicating success
2357 @rtype bool 2393 @rtype bool
2358 """ 2394 """
2359 # TODO: editorconfig: apply end_of_line, trim_trailing_whitespace, 2395 QApplication.setOverrideCursor(Qt.WaitCursor)
2360 # insert_final_newline 2396
2361 config = self.__loadEditorConfigObject(fileName) 2397 config = self.__loadEditorConfigObject(fileName)
2362 2398
2363 QApplication.setOverrideCursor(Qt.WaitCursor) 2399 eol = self.__getEditorConfig("EOLMode", nodefault=True, config=config)
2400 if eol is not None:
2401 self.__textEdit.convertEols(eol)
2402
2403 if self.__getEditorConfig("StripTrailingWhitespace", config=config):
2404 self.__textEdit.removeTrailingWhitespace()
2405
2364 txt = self.__textEdit.text() 2406 txt = self.__textEdit.text()
2365 # TODO: editorconfig: insert_final_newline 2407
2408 if self.__getEditorConfig("InsertFinalNewline", config=config):
2409 eol = self.__textEdit.getLineSeparator()
2410 if eol:
2411 if len(txt) >= len(eol):
2412 if txt[-len(eol):] != eol:
2413 txt += eol
2414 else:
2415 txt += eol
2416
2417 # now write text to the file
2366 try: 2418 try:
2367 editorConfigEncoding = self.__getEditorConfig( 2419 editorConfigEncoding = self.__getEditorConfig(
2368 "DefaultEncoding", nodefault=True, config=config) 2420 "DefaultEncoding", nodefault=True, config=config)
2369 self.encoding = Utilities.writeEncodedFile( 2421 self.encoding = Utilities.writeEncodedFile(
2370 fileName, txt, self.encoding, 2422 fileName, txt, self.encoding,
2519 linenoMargin = Preferences.getEditor("LinenoMargin") 2571 linenoMargin = Preferences.getEditor("LinenoMargin")
2520 if linenoMargin: 2572 if linenoMargin:
2521 self.__textEdit.setMarginWidth( 2573 self.__textEdit.setMarginWidth(
2522 0, '8' * (len(str(self.__textEdit.lines())) + 1)) 2574 0, '8' * (len(str(self.__textEdit.lines())) + 1))
2523 2575
2524 def __setTextDisplay(self): 2576 def __setTabAndIndent(self):
2525 """ 2577 """
2526 Private method to configure the text display. 2578 Private method to set indentation size and style and tab width.
2527 """ 2579 """
2528 # TODO: editorconfig: move these to separate method called, when 2580 self.__textEdit.setTabWidth(self.__getEditorConfig("TabWidth"))
2529 # EditorConfig has been loaded
2530 # TODO: editorconfig: tab_width
2531 self.__textEdit.setTabWidth(Preferences.getEditor("TabWidth"))
2532 # TODO: editorconfig: indent_size
2533 self.__textEdit.setIndentationWidth( 2581 self.__textEdit.setIndentationWidth(
2534 Preferences.getEditor("IndentWidth")) 2582 self.__getEditorConfig("IndentWidth"))
2535 # TODO: editorconfig: indent_style
2536 if self.lexer_ and self.lexer_.alwaysKeepTabs(): 2583 if self.lexer_ and self.lexer_.alwaysKeepTabs():
2537 self.__textEdit.setIndentationsUseTabs(True) 2584 self.__textEdit.setIndentationsUseTabs(True)
2538 else: 2585 else:
2539 self.__textEdit.setIndentationsUseTabs( 2586 self.__textEdit.setIndentationsUseTabs(
2540 Preferences.getEditor("TabForIndentation")) 2587 self.__getCurrentWord("TabForIndentation"))
2588
2589 def __setTextDisplay(self):
2590 """
2591 Private method to configure the text display.
2592 """
2593 self.__setTabAndIndent()
2594
2541 self.__textEdit.setTabIndents(Preferences.getEditor("TabIndents")) 2595 self.__textEdit.setTabIndents(Preferences.getEditor("TabIndents"))
2542 self.__textEdit.setBackspaceUnindents( 2596 self.__textEdit.setBackspaceUnindents(
2543 Preferences.getEditor("TabIndents")) 2597 Preferences.getEditor("TabIndents"))
2544 self.__textEdit.setIndentationGuides( 2598 self.__textEdit.setIndentationGuides(
2545 Preferences.getEditor("IndentationGuides")) 2599 Preferences.getEditor("IndentationGuides"))
2632 2686
2633 def __setEolMode(self): 2687 def __setEolMode(self):
2634 """ 2688 """
2635 Private method to configure the eol mode of the editor. 2689 Private method to configure the eol mode of the editor.
2636 """ 2690 """
2637 # TODO: editorconfig: end_of_line 2691 eolMode = self.__getEditorConfig("EOLMode")
2638 eolMode = Preferences.getEditor("EOLMode")
2639 eolMode = QsciScintilla.EolMode(eolMode) 2692 eolMode = QsciScintilla.EolMode(eolMode)
2640 self.__textEdit.setEolMode(eolMode) 2693 self.__textEdit.setEolMode(eolMode)
2641 2694
2642 def __setMonospaced(self, on): 2695 def __setMonospaced(self, on):
2643 """ 2696 """
3262 if filetype is None: 3315 if filetype is None:
3263 self.filetype = "" 3316 self.filetype = ""
3264 else: 3317 else:
3265 self.filetype = filetype 3318 self.filetype = filetype
3266 3319
3267 # TODO: editorconfig: end_of_line 3320 eolMode = self.__getEditorConfig("EOLMode", nodefault=True)
3268 fileEol = self.__textEdit.detectEolString(txt) 3321 if eolMode is None:
3269 self.__textEdit.setEolModeByEolString(fileEol) 3322 fileEol = self.__textEdit.detectEolString(txt)
3323 self.__textEdit.setEolModeByEolString(fileEol)
3324 else:
3325 self.__textEdit.convertEols(eolMode)
3270 3326
3271 self.__textEdit.setModified(False) 3327 self.__textEdit.setModified(False)
3328 self.setWindowModified(False)
3272 3329
3273 ####################################################################### 3330 #######################################################################
3274 ## Methods implementing the interface to EditorConfig 3331 ## Methods implementing the interface to EditorConfig
3275 ####################################################################### 3332 #######################################################################
3276 3333
3283 """ 3340 """
3284 if not fileName: 3341 if not fileName:
3285 fileName = self.__curFile 3342 fileName = self.__curFile
3286 3343
3287 self.__editorConfig = self.__loadEditorConfigObject(fileName) 3344 self.__editorConfig = self.__loadEditorConfigObject(fileName)
3345
3346 if fileName:
3347 self.__setTabAndIndent()
3288 3348
3289 def __loadEditorConfigObject(self, fileName): 3349 def __loadEditorConfigObject(self, fileName):
3290 """ 3350 """
3291 Private method to load the EditorConfig properties for the given 3351 Private method to load the EditorConfig properties for the given
3292 file name. 3352 file name.
3351 value = QsciScintilla.EolMac 3411 value = QsciScintilla.EolMac
3352 else: 3412 else:
3353 value = None 3413 value = None
3354 elif option == "DefaultEncoding": 3414 elif option == "DefaultEncoding":
3355 value = config["charset"] 3415 value = config["charset"]
3416 elif option == "InsertFinalNewline":
3417 value = Utilities.toBool(config["insert_final_newline"])
3418 elif option == "StripTrailingWhitespace":
3419 value = Utilities.toBool(config["trim_trailing_whitespace"])
3420 elif option == "TabWidth":
3421 value = int(config["tab_width"])
3422 elif option == "IndentWidth":
3423 value = config["indent_size"]
3424 if value == "tab":
3425 value = self.__getEditorConfig("TabWidth", config=config)
3426 else:
3427 value = int(value)
3428 elif option == "TabForIndentation":
3429 value = config["indent_style"] == "tab"
3356 except KeyError: 3430 except KeyError:
3357 value = None 3431 value = None
3358 3432
3359 if value is None and not nodefault: 3433 if value is None and not nodefault:
3360 # use Preferences in case of error 3434 # use Preferences as default in case of error
3361 value = Preferences.getEditor(option) 3435 value = Preferences.getEditor(option)
3362 3436
3363 return value 3437 return value

eric ide

mercurial