src/eric7/QScintilla/EditorOutlineModel.py

branch
eric7
changeset 9491
91bcf8b893ee
parent 9482
a2bc06a54d9d
child 9612
93b496cc3c88
equal deleted inserted replaced
9490:77b8d3a635b7 9491:91bcf8b893ee
22 BrowserImportsItem, 22 BrowserImportsItem,
23 BrowserItem, 23 BrowserItem,
24 BrowserMethodItem, 24 BrowserMethodItem,
25 BrowserModel, 25 BrowserModel,
26 ) 26 )
27 from eric7.Utilities import ClassBrowsers
27 28
28 29
29 class EditorOutlineModel(BrowserModel): 30 class EditorOutlineModel(BrowserModel):
30 """ 31 """
31 Class implementing the editor outline model. 32 Class implementing the editor outline model.
32 """ 33 """
33 34
34 SupportedLanguages = ( 35 SupportedLanguages = {
35 "IDL", 36 "IDL": "idl",
36 "JavaScript", 37 "JavaScript": "javascript",
37 "Protocol", 38 "Protocol": "protobuf",
38 "Python3", 39 "Python3": "python",
39 "MicroPython", 40 "MicroPython": "python",
40 "Cython", 41 "Cython": "python",
41 "Ruby", 42 "Ruby": "ruby",
42 ) 43 }
43 44
44 def __init__(self, editor, populate=True): 45 def __init__(self, editor, populate=True):
45 """ 46 """
46 Constructor 47 Constructor
47 48
72 self.__filename = self.__editor.getFileName() 73 self.__filename = self.__editor.getFileName()
73 self.__module = os.path.basename(self.__filename) 74 self.__module = os.path.basename(self.__filename)
74 75
75 language = self.__editor.getLanguage() 76 language = self.__editor.getLanguage()
76 if language in EditorOutlineModel.SupportedLanguages: 77 if language in EditorOutlineModel.SupportedLanguages:
77 if language == "IDL": 78 mod = ClassBrowsers.getClassBrowserModule(
78 from eric7.Utilities.ClassBrowsers import idlclbr # __IGNORE_WARNING__ 79 EditorOutlineModel.SupportedLanguages[language]
79 80 )
80 dictionary = idlclbr.scan( 81 if mod:
82 dictionary = mod.scan(
81 self.__editor.text(), self.__filename, self.__module 83 self.__editor.text(), self.__filename, self.__module
82 ) 84 )
83 idlclbr._modules.clear() 85 mod.clearModulesCache()
84 elif language == "Protocol": 86
85 from eric7.Utilities.ClassBrowsers import ( # __IGNORE_WARNING_I101__ 87 keys = list(dictionary.keys())
86 protoclbr, 88 if len(keys) > 0:
87 ) 89 parentItem = self.rootItem
88 90
89 dictionary = protoclbr.scan( 91 if repopulate:
90 self.__editor.text(), self.__filename, self.__module 92 last = len(keys) - 1
91 ) 93 if "@@Coding@@" in keys and not Preferences.getEditor(
92 protoclbr._modules.clear() 94 "SourceOutlineShowCoding"
93 elif language == "Ruby": 95 ):
94 from eric7.Utilities.ClassBrowsers import rbclbr # __IGNORE_WARNING__ 96 last -= 1
95 97 self.beginInsertRows(QModelIndex(), 0, last)
96 dictionary = rbclbr.scan( 98
97 self.__editor.text(), self.__filename, self.__module 99 for key in keys:
98 ) 100 if key.startswith("@@"):
99 rbclbr._modules.clear() 101 # special treatment done later
100 elif language == "JavaScript": 102 continue
101 from eric7.Utilities.ClassBrowsers import jsclbr # __IGNORE_WARNING__ 103 cl = dictionary[key]
102 104 with contextlib.suppress(AttributeError):
103 dictionary = jsclbr.scan( 105 if cl.module == self.__module:
104 self.__editor.text(), self.__filename, self.__module 106 node = BrowserClassItem(parentItem, cl, self.__filename)
105 ) 107 self._addItem(node, parentItem)
106 jsclbr._modules.clear() 108 if "@@Coding@@" in keys and Preferences.getEditor(
107 elif language in ("Python3", "MicroPython", "Cython"):
108 from eric7.Utilities.ClassBrowsers import pyclbr # __IGNORE_WARNING__
109
110 dictionary = pyclbr.scan(
111 self.__editor.text(), self.__filename, self.__module
112 )
113 pyclbr._modules.clear()
114
115 keys = list(dictionary.keys())
116 if len(keys) > 0:
117 parentItem = self.rootItem
118
119 if repopulate:
120 last = len(keys) - 1
121 if "@@Coding@@" in keys and not Preferences.getEditor(
122 "SourceOutlineShowCoding" 109 "SourceOutlineShowCoding"
123 ): 110 ):
124 last -= 1 111 node = BrowserCodingItem(
125 self.beginInsertRows(QModelIndex(), 0, last) 112 parentItem,
126 113 QCoreApplication.translate(
127 for key in keys: 114 "EditorOutlineModel", "Coding: {0}"
128 if key.startswith("@@"): 115 ).format(dictionary["@@Coding@@"].coding),
129 # special treatment done later 116 dictionary["@@Coding@@"].linenumber,
130 continue 117 )
131 cl = dictionary[key] 118 self._addItem(node, parentItem)
132 with contextlib.suppress(AttributeError): 119 if "@@Globals@@" in keys:
133 if cl.module == self.__module: 120 node = BrowserGlobalsItem(
134 node = BrowserClassItem(parentItem, cl, self.__filename) 121 parentItem,
135 self._addItem(node, parentItem) 122 dictionary["@@Globals@@"].globals,
136 if "@@Coding@@" in keys and Preferences.getEditor( 123 QCoreApplication.translate("EditorOutlineModel", "Globals"),
137 "SourceOutlineShowCoding" 124 )
138 ): 125 self._addItem(node, parentItem)
139 node = BrowserCodingItem( 126 if "@@Import@@" in keys or "@@ImportFrom@@" in keys:
140 parentItem, 127 node = BrowserImportsItem(
141 QCoreApplication.translate( 128 parentItem,
142 "EditorOutlineModel", "Coding: {0}" 129 QCoreApplication.translate("EditorOutlineModel", "Imports"),
143 ).format(dictionary["@@Coding@@"].coding), 130 )
144 dictionary["@@Coding@@"].linenumber, 131 self._addItem(node, parentItem)
145 ) 132 if "@@Import@@" in keys:
146 self._addItem(node, parentItem) 133 for importedModule in (
147 if "@@Globals@@" in keys: 134 dictionary["@@Import@@"].getImports().values()
148 node = BrowserGlobalsItem( 135 ):
149 parentItem, 136 m_node = BrowserImportItem(
150 dictionary["@@Globals@@"].globals, 137 node,
151 QCoreApplication.translate("EditorOutlineModel", "Globals"), 138 importedModule.importedModuleName,
152 ) 139 importedModule.file,
153 self._addItem(node, parentItem) 140 importedModule.linenos,
154 if "@@Import@@" in keys or "@@ImportFrom@@" in keys: 141 )
155 node = BrowserImportsItem( 142 self._addItem(m_node, node)
156 parentItem, 143 for (
157 QCoreApplication.translate("EditorOutlineModel", "Imports"),
158 )
159 self._addItem(node, parentItem)
160 if "@@Import@@" in keys:
161 for importedModule in (
162 dictionary["@@Import@@"].getImports().values()
163 ):
164 m_node = BrowserImportItem(
165 node,
166 importedModule.importedModuleName,
167 importedModule.file,
168 importedModule.linenos,
169 )
170 self._addItem(m_node, node)
171 for (
172 importedName,
173 linenos,
174 ) in importedModule.importedNames.items():
175 mn_node = BrowserImportItem(
176 m_node,
177 importedName, 144 importedName,
178 importedModule.file,
179 linenos, 145 linenos,
180 isModule=False, 146 ) in importedModule.importedNames.items():
181 ) 147 mn_node = BrowserImportItem(
182 self._addItem(mn_node, m_node) 148 m_node,
183 if repopulate: 149 importedName,
184 self.endInsertRows() 150 importedModule.file,
185 151 linenos,
186 self.__populated = True 152 isModule=False,
187 else: 153 )
188 self.clear() 154 self._addItem(mn_node, m_node)
189 self.__populated = False 155 if repopulate:
156 self.endInsertRows()
157
158 self.__populated = True
159 return
160
161 self.clear()
162 self.__populated = False
190 163
191 def isPopulated(self): 164 def isPopulated(self):
192 """ 165 """
193 Public method to check, if the model is populated. 166 Public method to check, if the model is populated.
194 167

eric ide

mercurial