diff -r a8ba35ce81ad -r 2da0139f4f91 eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py --- a/eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py Thu Jan 28 16:36:29 2021 +0100 +++ b/eric6/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py Thu Jan 28 18:29:00 2021 +0100 @@ -7,6 +7,8 @@ Module implementing the Editor Highlighting Styles configuration page. """ +import os + from PyQt5.QtCore import pyqtSlot, Qt, QFileInfo, QFile, QIODevice from PyQt5.QtGui import QPalette, QFont from PyQt5.QtWidgets import ( @@ -28,6 +30,7 @@ NoFontsOption = QFontDialog.FontDialogOptions(0) +# TODO: add capability to export a list of selected highlighter styles class EditorHighlightingStylesPage(ConfigurationPageBase, Ui_EditorHighlightingStylesPage): """ @@ -517,12 +520,12 @@ """ self.__exportStyles(list(self.lexers.values())) - # TODO: do the JSON styles def __exportStyles(self, lexers): """ Private method to export the styles of the given lexers. @param lexers list of lexer objects for which to export the styles + @type list of PreferencesLexer """ from eric6config import getConfig stylesDir = getConfig("ericStylesDir") @@ -531,7 +534,8 @@ self, self.tr("Export Highlighting Styles"), stylesDir, - self.tr("Highlighting styles file (*.e6h)"), + self.tr("Highlighting Styles File (*.ehj);;" + "XML Highlighting Styles File (*.e6h)"), "", E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) @@ -544,22 +548,39 @@ if ex: fn += ex - f = QFile(fn) - if f.open(QIODevice.WriteOnly): - from E5XML.HighlightingStylesWriter import HighlightingStylesWriter - HighlightingStylesWriter(f, lexers).writeXML() - f.close() - else: - E5MessageBox.critical( + if os.path.exists(fn): + ok = E5MessageBox.yesNo( self, self.tr("Export Highlighting Styles"), - self.tr( - """<p>The highlighting styles could not be exported""" - """ to file <b>{0}</b>.</p><p>Reason: {1}</p>""") - .format(fn, f.errorString()) - ) + self.tr("""<p>The highlighting styles file <b>{0}</b> exists""" + """ already. Overwrite it?</p>""").format(fn)) + else: + ok = True + + if ok: + if fn.endswith(".ehj"): + from Preferences.HighlightingStylesFile import ( + HighlightingStylesFile + ) + highlightingStylesFile = HighlightingStylesFile() + highlightingStylesFile.writeFile(fn, lexers) + else: + f = QFile(fn) + if f.open(QIODevice.WriteOnly): + from E5XML.HighlightingStylesWriter import ( + HighlightingStylesWriter + ) + HighlightingStylesWriter(f, lexers).writeXML() + f.close() + else: + E5MessageBox.critical( + self, + self.tr("Export Highlighting Styles"), + self.tr("<p>The highlighting styles file <b>{0}</b>" + " could not be written.</p><p>Reason: {1}</p>") + .format(fn, f.errorString()) + ) - # TODO: do the JSON styles def __importStyles(self, lexers): """ Private method to import the styles of the given lexers. @@ -574,27 +595,41 @@ self, self.tr("Import Highlighting Styles"), stylesDir, - self.tr("Highlighting styles file (*.e6h *.e4h)")) + self.tr("Highlighting Styles File (*.ehj);;" + "XML Highlighting Styles File (*.e6h *.e4h)")) if not fn: return - f = QFile(fn) - if f.open(QIODevice.ReadOnly): - from E5XML.HighlightingStylesReader import HighlightingStylesReader - reader = HighlightingStylesReader(f, lexers) - reader.readXML() - f.close() + if fn.endswith(".ehj"): + # new JSON based file + from Preferences.HighlightingStylesFile import ( + HighlightingStylesFile + ) + highlightingStylesFile = HighlightingStylesFile() + res = highlightingStylesFile.readFile(fn, lexers) + if not res: + return else: - E5MessageBox.critical( - self, - self.tr("Import Highlighting Styles"), - self.tr( - """<p>The highlighting styles could not be read""" - """ from file <b>{0}</b>.</p><p>Reason: {1}</p>""") - .format(fn, f.errorString()) - ) - return + # old XML based file + f = QFile(fn) + if f.open(QIODevice.ReadOnly): + from E5XML.HighlightingStylesReader import ( + HighlightingStylesReader + ) + reader = HighlightingStylesReader(f, lexers) + reader.readXML() + f.close() + else: + E5MessageBox.critical( + self, + self.tr("Import Highlighting Styles"), + self.tr( + "<p>The highlighting styles file <b>{0}</b> could not" + " be read.</p><p>Reason: {1}</p>" + ).format(fn, f.errorString()) + ) + return self.on_lexerLanguageComboBox_activated( self.lexerLanguageComboBox.currentText())