|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2006 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Editor Highlighter Associations configuration page. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from pygments.lexers import get_all_lexers |
|
13 |
|
14 from PyQt5.QtCore import Qt, pyqtSlot |
|
15 from PyQt5.QtWidgets import QHeaderView, QTreeWidgetItem |
|
16 |
|
17 from .ConfigurationPageBase import ConfigurationPageBase |
|
18 from .Ui_EditorHighlightersPage import Ui_EditorHighlightersPage |
|
19 |
|
20 import Preferences |
|
21 |
|
22 |
|
23 class EditorHighlightersPage(ConfigurationPageBase, Ui_EditorHighlightersPage): |
|
24 """ |
|
25 Class implementing the Editor Highlighter Associations configuration page. |
|
26 """ |
|
27 def __init__(self, lexers): |
|
28 """ |
|
29 Constructor |
|
30 |
|
31 @param lexers reference to the lexers dictionary |
|
32 """ |
|
33 super().__init__() |
|
34 self.setupUi(self) |
|
35 self.setObjectName("EditorHighlightersPage") |
|
36 |
|
37 self.editorLexerList.headerItem().setText( |
|
38 self.editorLexerList.columnCount(), "") |
|
39 header = self.editorLexerList.header() |
|
40 header.setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents) |
|
41 header.setSortIndicator(0, Qt.SortOrder.AscendingOrder) |
|
42 |
|
43 try: |
|
44 self.extsep = os.extsep |
|
45 except AttributeError: |
|
46 self.extsep = "." |
|
47 |
|
48 import QScintilla.Lexers |
|
49 self.extras = ["-----------", self.tr("Alternative")] |
|
50 languages = [''] + sorted(lexers.keys()) + self.extras |
|
51 for lang in languages: |
|
52 self.editorLexerCombo.addItem( |
|
53 QScintilla.Lexers.getLanguageIcon(lang, False), |
|
54 lang) |
|
55 |
|
56 pygmentsLexers = [''] + sorted(lex[0] for lex in get_all_lexers()) |
|
57 self.pygmentsLexerCombo.addItems(pygmentsLexers) |
|
58 |
|
59 # set initial values |
|
60 lexerAssocs = Preferences.getEditorLexerAssocs() |
|
61 for ext in lexerAssocs: |
|
62 QTreeWidgetItem(self.editorLexerList, [ext, lexerAssocs[ext]]) |
|
63 self.editorLexerList.sortByColumn(0, Qt.SortOrder.AscendingOrder) |
|
64 |
|
65 def save(self): |
|
66 """ |
|
67 Public slot to save the Editor Highlighter Associations configuration. |
|
68 """ |
|
69 lexerAssocs = {} |
|
70 for index in range( |
|
71 self.editorLexerList.topLevelItemCount()): |
|
72 itm = self.editorLexerList.topLevelItem(index) |
|
73 lexerAssocs[itm.text(0)] = itm.text(1) |
|
74 Preferences.setEditorLexerAssocs(lexerAssocs) |
|
75 |
|
76 @pyqtSlot() |
|
77 def on_addLexerButton_clicked(self): |
|
78 """ |
|
79 Private slot to add the lexer association displayed to the list. |
|
80 """ |
|
81 ext = self.editorFileExtEdit.text() |
|
82 if ext.startswith(self.extsep): |
|
83 ext.replace(self.extsep, "") |
|
84 lexer = self.editorLexerCombo.currentText() |
|
85 if lexer in self.extras: |
|
86 pygmentsLexer = self.pygmentsLexerCombo.currentText() |
|
87 if not pygmentsLexer: |
|
88 lexer = pygmentsLexer |
|
89 else: |
|
90 lexer = "Pygments|{0}".format(pygmentsLexer) |
|
91 if ext and lexer: |
|
92 itmList = self.editorLexerList.findItems( |
|
93 ext, Qt.MatchFlags(Qt.MatchFlag.MatchExactly), 0) |
|
94 if itmList: |
|
95 index = self.editorLexerList.indexOfTopLevelItem(itmList[0]) |
|
96 itm = self.editorLexerList.takeTopLevelItem(index) |
|
97 # __IGNORE_WARNING__ |
|
98 del itm |
|
99 QTreeWidgetItem(self.editorLexerList, [ext, lexer]) |
|
100 self.editorFileExtEdit.clear() |
|
101 self.editorLexerCombo.setCurrentIndex(0) |
|
102 self.pygmentsLexerCombo.setCurrentIndex(0) |
|
103 self.editorLexerList.sortItems( |
|
104 self.editorLexerList.sortColumn(), |
|
105 self.editorLexerList.header().sortIndicatorOrder()) |
|
106 |
|
107 @pyqtSlot() |
|
108 def on_deleteLexerButton_clicked(self): |
|
109 """ |
|
110 Private slot to delete the currently selected lexer association of the |
|
111 list. |
|
112 """ |
|
113 itmList = self.editorLexerList.selectedItems() |
|
114 if itmList: |
|
115 index = self.editorLexerList.indexOfTopLevelItem(itmList[0]) |
|
116 itm = self.editorLexerList.takeTopLevelItem(index) |
|
117 # __IGNORE_WARNING__ |
|
118 del itm |
|
119 |
|
120 self.editorLexerList.clearSelection() |
|
121 self.editorFileExtEdit.clear() |
|
122 self.editorLexerCombo.setCurrentIndex(0) |
|
123 |
|
124 def on_editorLexerList_itemClicked(self, itm, column): |
|
125 """ |
|
126 Private slot to handle the clicked signal of the lexer association |
|
127 list. |
|
128 |
|
129 @param itm reference to the selecte item (QTreeWidgetItem) |
|
130 @param column column the item was clicked or activated (integer) |
|
131 (ignored) |
|
132 """ |
|
133 if itm is None: |
|
134 self.editorFileExtEdit.clear() |
|
135 self.editorLexerCombo.setCurrentIndex(0) |
|
136 self.pygmentsLexerCombo.setCurrentIndex(0) |
|
137 else: |
|
138 self.editorFileExtEdit.setText(itm.text(0)) |
|
139 lexer = itm.text(1) |
|
140 if lexer.startswith("Pygments|"): |
|
141 pygmentsLexer = lexer.split("|")[1] |
|
142 pygmentsIndex = self.pygmentsLexerCombo.findText(pygmentsLexer) |
|
143 lexer = self.tr("Alternative") |
|
144 else: |
|
145 pygmentsIndex = 0 |
|
146 index = self.editorLexerCombo.findText(lexer) |
|
147 self.editorLexerCombo.setCurrentIndex(index) |
|
148 self.pygmentsLexerCombo.setCurrentIndex(pygmentsIndex) |
|
149 |
|
150 def on_editorLexerList_itemActivated(self, itm, column): |
|
151 """ |
|
152 Private slot to handle the activated signal of the lexer association |
|
153 list. |
|
154 |
|
155 @param itm reference to the selecte item (QTreeWidgetItem) |
|
156 @param column column the item was clicked or activated (integer) |
|
157 (ignored) |
|
158 """ |
|
159 self.on_editorLexerList_itemClicked(itm, column) |
|
160 |
|
161 @pyqtSlot(int) |
|
162 def on_editorLexerCombo_currentIndexChanged(self, index): |
|
163 """ |
|
164 Private slot to handle the selection of a lexer. |
|
165 |
|
166 @param index index of the current item |
|
167 @type int |
|
168 """ |
|
169 text = self.editorLexerCombo.itemText(index) |
|
170 if text in self.extras: |
|
171 self.pygmentsLexerCombo.setEnabled(True) |
|
172 self.pygmentsLabel.setEnabled(True) |
|
173 else: |
|
174 self.pygmentsLexerCombo.setEnabled(False) |
|
175 self.pygmentsLabel.setEnabled(False) |
|
176 |
|
177 |
|
178 def create(dlg): |
|
179 """ |
|
180 Module function to create the configuration page. |
|
181 |
|
182 @param dlg reference to the configuration dialog |
|
183 @return reference to the instantiated page (ConfigurationPageBase) |
|
184 """ |
|
185 page = EditorHighlightersPage(dlg.getLexers()) |
|
186 return page |