src/eric7/Preferences/ConfigurationPages/EditorHighlightersPage.py

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

eric ide

mercurial