--- a/QScintilla/MiniEditor.py Sat Feb 03 10:45:52 2018 +0100 +++ b/QScintilla/MiniEditor.py Sat Feb 03 16:15:24 2018 +0100 @@ -91,6 +91,19 @@ self.setCaretWidth(0) super(MiniScintilla, self).focusOutEvent(event) + + def removeTrailingWhitespace(self): + """ + Public method to remove trailing whitespace. + """ + searchRE = r"[ \t]+$" # whitespace at the end of a line + + ok = self.findFirstTarget(searchRE, True, False, False, 0, 0) + self.beginUndoAction() + while ok: + self.replaceTarget("") + ok = self.findNextTarget() + self.endUndoAction() class MiniEditor(E5MainWindow): @@ -146,6 +159,9 @@ self.lexer_ = None self.apiLanguage = "" self.filetype = "" + self.__curFile = filename + + self.__loadEditorConfig(filename) self.__createActions() self.__createMenus() @@ -2291,9 +2307,9 @@ @param fileName name of the file to load (string) @param filetype type of the source file (string) """ - self.__loadEditorConfig(fileName=fileName) + QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) - QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) + self.__loadEditorConfig(fileName=fileName) try: encoding = self.__getEditorConfig("DefaultEncoding", @@ -2313,18 +2329,38 @@ QApplication.restoreOverrideCursor() return + modified = False + + if (not self.__getEditorConfig("TabForIndentation")) and \ + Preferences.getEditor("ConvertTabsOnLoad") and \ + not (self.lexer_ and + self.lexer_.alwaysKeepTabs()): + txtExpanded = txt.expandtabs(self.__getEditorConfig("TabWidth")) + if txtExpanded != txt: + modified = True + txt = txtExpanded + self.__textEdit.setText(txt) QApplication.restoreOverrideCursor() + self.__textEdit.setModified(modified) + self.setWindowModified(modified) + if filetype is None: self.filetype = "" else: self.filetype = filetype self.__setCurrentFile(fileName) - fileEol = self.__textEdit.detectEolString(txt) - # TODO: editorconfig: end_of_line - self.__textEdit.setEolModeByEolString(fileEol) + self.__textEdit.setModified(modified) + self.setWindowModified(modified) + + eolMode = self.__getEditorConfig("EOLMode", nodefault=True) + if eolMode is None: + fileEol = self.__textEdit.detectEolString(txt) + self.__textEdit.setEolModeByEolString(fileEol) + else: + self.__textEdit.convertEols(eolMode) self.__statusBar.showMessage(self.tr("File loaded"), 2000) @@ -2356,13 +2392,29 @@ @return flag indicating success @rtype bool """ - # TODO: editorconfig: apply end_of_line, trim_trailing_whitespace, - # insert_final_newline + QApplication.setOverrideCursor(Qt.WaitCursor) + config = self.__loadEditorConfigObject(fileName) - QApplication.setOverrideCursor(Qt.WaitCursor) + eol = self.__getEditorConfig("EOLMode", nodefault=True, config=config) + if eol is not None: + self.__textEdit.convertEols(eol) + + if self.__getEditorConfig("StripTrailingWhitespace", config=config): + self.__textEdit.removeTrailingWhitespace() + txt = self.__textEdit.text() - # TODO: editorconfig: insert_final_newline + + if self.__getEditorConfig("InsertFinalNewline", config=config): + eol = self.__textEdit.getLineSeparator() + if eol: + if len(txt) >= len(eol): + if txt[-len(eol):] != eol: + txt += eol + else: + txt += eol + + # now write text to the file try: editorConfigEncoding = self.__getEditorConfig( "DefaultEncoding", nodefault=True, config=config) @@ -2521,23 +2573,25 @@ self.__textEdit.setMarginWidth( 0, '8' * (len(str(self.__textEdit.lines())) + 1)) + def __setTabAndIndent(self): + """ + Private method to set indentation size and style and tab width. + """ + self.__textEdit.setTabWidth(self.__getEditorConfig("TabWidth")) + self.__textEdit.setIndentationWidth( + self.__getEditorConfig("IndentWidth")) + if self.lexer_ and self.lexer_.alwaysKeepTabs(): + self.__textEdit.setIndentationsUseTabs(True) + else: + self.__textEdit.setIndentationsUseTabs( + self.__getCurrentWord("TabForIndentation")) + def __setTextDisplay(self): """ Private method to configure the text display. """ - # TODO: editorconfig: move these to separate method called, when - # EditorConfig has been loaded - # TODO: editorconfig: tab_width - self.__textEdit.setTabWidth(Preferences.getEditor("TabWidth")) - # TODO: editorconfig: indent_size - self.__textEdit.setIndentationWidth( - Preferences.getEditor("IndentWidth")) - # TODO: editorconfig: indent_style - if self.lexer_ and self.lexer_.alwaysKeepTabs(): - self.__textEdit.setIndentationsUseTabs(True) - else: - self.__textEdit.setIndentationsUseTabs( - Preferences.getEditor("TabForIndentation")) + self.__setTabAndIndent() + self.__textEdit.setTabIndents(Preferences.getEditor("TabIndents")) self.__textEdit.setBackspaceUnindents( Preferences.getEditor("TabIndents")) @@ -2634,8 +2688,7 @@ """ Private method to configure the eol mode of the editor. """ - # TODO: editorconfig: end_of_line - eolMode = Preferences.getEditor("EOLMode") + eolMode = self.__getEditorConfig("EOLMode") eolMode = QsciScintilla.EolMode(eolMode) self.__textEdit.setEolMode(eolMode) @@ -3264,11 +3317,15 @@ else: self.filetype = filetype - # TODO: editorconfig: end_of_line - fileEol = self.__textEdit.detectEolString(txt) - self.__textEdit.setEolModeByEolString(fileEol) + eolMode = self.__getEditorConfig("EOLMode", nodefault=True) + if eolMode is None: + fileEol = self.__textEdit.detectEolString(txt) + self.__textEdit.setEolModeByEolString(fileEol) + else: + self.__textEdit.convertEols(eolMode) self.__textEdit.setModified(False) + self.setWindowModified(False) ####################################################################### ## Methods implementing the interface to EditorConfig @@ -3285,6 +3342,9 @@ fileName = self.__curFile self.__editorConfig = self.__loadEditorConfigObject(fileName) + + if fileName: + self.__setTabAndIndent() def __loadEditorConfigObject(self, fileName): """ @@ -3353,11 +3413,25 @@ value = None elif option == "DefaultEncoding": value = config["charset"] + elif option == "InsertFinalNewline": + value = Utilities.toBool(config["insert_final_newline"]) + elif option == "StripTrailingWhitespace": + value = Utilities.toBool(config["trim_trailing_whitespace"]) + elif option == "TabWidth": + value = int(config["tab_width"]) + elif option == "IndentWidth": + value = config["indent_size"] + if value == "tab": + value = self.__getEditorConfig("TabWidth", config=config) + else: + value = int(value) + elif option == "TabForIndentation": + value = config["indent_style"] == "tab" except KeyError: value = None if value is None and not nodefault: - # use Preferences in case of error + # use Preferences as default in case of error value = Preferences.getEditor(option) return value