eric6/QScintilla/EditorOutlineModel.py

Mon, 12 Oct 2020 19:28:42 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 12 Oct 2020 19:28:42 +0200
changeset 7781
607a6098cb44
parent 7757
1f9f35f9be6d
child 7923
91e843545d9a
permissions
-rw-r--r--

Performed some more code cleanup.

7685
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2020 Detlev Offenbach <detlev@die-offenbachs.de>
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the editor outline model.
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import os
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 from PyQt5.QtCore import QCoreApplication, QModelIndex
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 from UI.BrowserModel import (
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 BrowserModel, BrowserItem, BrowserClassItem, BrowserCodingItem,
7690
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
16 BrowserGlobalsItem, BrowserImportsItem, BrowserImportItem,
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
17 BrowserClassAttributesItem, BrowserMethodItem
7685
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 )
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
7757
1f9f35f9be6d File Browser, Project Source Browser, Editor Outline: added option to suppress the source code encoding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7697
diff changeset
20 import Preferences
1f9f35f9be6d File Browser, Project Source Browser, Editor Outline: added option to suppress the source code encoding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7697
diff changeset
21
7685
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 class EditorOutlineModel(BrowserModel):
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 """
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 Class implementing the editor outline model.
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 SupportedLanguages = (
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 "IDL", "JavaScript", "Protocol", "Python3", "MicroPython", "Ruby",
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 )
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
7690
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
31 def __init__(self, editor, populate=True):
7685
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 """
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 Constructor
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 @param editor reference to the editor containing the source text
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 @type Editor
7690
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
37 @param populate flag indicating to populate the outline
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
38 @type bool
7685
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 """
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 super(EditorOutlineModel, self).__init__(nopopulate=True)
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 self.__editor = editor
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 self.__populated = False
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 rootData = QCoreApplication.translate("EditorOutlineModel", "Name")
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 self.rootItem = BrowserItem(None, rootData)
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48
7690
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
49 if populate:
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
50 self.__populateModel()
7685
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 def __populateModel(self, repopulate=False):
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 """
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 Private slot to populate the model.
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 @param repopulate flag indicating a repopulation
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 @type bool
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 """
7686
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
59 self.__filename = self.__editor.getFileName()
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
60 self.__module = os.path.basename(self.__filename)
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
61
7685
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 language = self.__editor.getLanguage()
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 if language in EditorOutlineModel.SupportedLanguages:
7690
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
64 if language == "IDL":
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
65 from Utilities.ClassBrowsers import idlclbr
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
66 dictionary = idlclbr.scan(
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
67 self.__editor.text(), self.__filename, self.__module)
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
68 idlclbr._modules.clear()
7697
c981a807aab1 EditorOutlineModel: fixed an issue causing ProtoBuf files not being outlined.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7690
diff changeset
69 elif language == "Protocol":
7690
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
70 from Utilities.ClassBrowsers import protoclbr
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
71 dictionary = protoclbr.scan(
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
72 self.__editor.text(), self.__filename, self.__module)
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
73 protoclbr._modules.clear()
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
74 elif language == "Ruby":
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
75 from Utilities.ClassBrowsers import rbclbr
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
76 dictionary = rbclbr.scan(
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
77 self.__editor.text(), self.__filename, self.__module)
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
78 rbclbr._modules.clear()
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
79 elif language == "JavaScript":
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
80 from Utilities.ClassBrowsers import jsclbr
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
81 dictionary = jsclbr.scan(
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
82 self.__editor.text(), self.__filename, self.__module)
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
83 jsclbr._modules.clear()
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
84 elif language in ("Python3", "MicroPython"):
7685
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 from Utilities.ClassBrowsers import pyclbr
7690
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
86 dictionary = pyclbr.scan(
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
87 self.__editor.text(), self.__filename, self.__module)
7685
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 pyclbr._modules.clear()
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 keys = list(dictionary.keys())
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 if len(keys) > 0:
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 parentItem = self.rootItem
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 if repopulate:
7757
1f9f35f9be6d File Browser, Project Source Browser, Editor Outline: added option to suppress the source code encoding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7697
diff changeset
95 last = len(keys) - 1
1f9f35f9be6d File Browser, Project Source Browser, Editor Outline: added option to suppress the source code encoding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7697
diff changeset
96 if (
1f9f35f9be6d File Browser, Project Source Browser, Editor Outline: added option to suppress the source code encoding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7697
diff changeset
97 "@@Coding@@" in keys and
1f9f35f9be6d File Browser, Project Source Browser, Editor Outline: added option to suppress the source code encoding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7697
diff changeset
98 not Preferences.getEditor("SourceOutlineShowCoding")
1f9f35f9be6d File Browser, Project Source Browser, Editor Outline: added option to suppress the source code encoding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7697
diff changeset
99 ):
1f9f35f9be6d File Browser, Project Source Browser, Editor Outline: added option to suppress the source code encoding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7697
diff changeset
100 last -= 1
1f9f35f9be6d File Browser, Project Source Browser, Editor Outline: added option to suppress the source code encoding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7697
diff changeset
101 self.beginInsertRows(QModelIndex(), 0, last)
7685
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 for key in keys:
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 if key.startswith("@@"):
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 # special treatment done later
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 continue
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 cl = dictionary[key]
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 try:
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 if cl.module == self.__module:
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 node = BrowserClassItem(
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 parentItem, cl, self.__filename)
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 self._addItem(node, parentItem)
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 except AttributeError:
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 pass
7757
1f9f35f9be6d File Browser, Project Source Browser, Editor Outline: added option to suppress the source code encoding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7697
diff changeset
115 if (
1f9f35f9be6d File Browser, Project Source Browser, Editor Outline: added option to suppress the source code encoding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7697
diff changeset
116 "@@Coding@@" in keys and
1f9f35f9be6d File Browser, Project Source Browser, Editor Outline: added option to suppress the source code encoding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7697
diff changeset
117 Preferences.getEditor("SourceOutlineShowCoding")
1f9f35f9be6d File Browser, Project Source Browser, Editor Outline: added option to suppress the source code encoding line.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7697
diff changeset
118 ):
7685
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 node = BrowserCodingItem(
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 parentItem,
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 QCoreApplication.translate(
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 "EditorOutlineModel", "Coding: {0}")
7690
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
123 .format(dictionary["@@Coding@@"].coding),
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
124 dictionary["@@Coding@@"].linenumber)
7685
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 self._addItem(node, parentItem)
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 if "@@Globals@@" in keys:
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 node = BrowserGlobalsItem(
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 parentItem,
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 dictionary["@@Globals@@"].globals,
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 QCoreApplication.translate(
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 "EditorOutlineModel", "Globals"))
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 self._addItem(node, parentItem)
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 if "@@Import@@" in keys or "@@ImportFrom@@" in keys:
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 node = BrowserImportsItem(
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 parentItem,
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 QCoreApplication.translate(
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 "EditorOutlineModel", "Imports"))
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 self._addItem(node, parentItem)
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 if "@@Import@@" in keys:
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 for importedModule in (
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 dictionary["@@Import@@"].getImports().values()
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 ):
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 m_node = BrowserImportItem(
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 node,
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 importedModule.importedModuleName,
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 importedModule.file,
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 importedModule.linenos)
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 self._addItem(m_node, node)
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 for importedName, linenos in (
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 importedModule.importedNames.items()
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 ):
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 mn_node = BrowserImportItem(
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 m_node,
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 importedName,
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 importedModule.file,
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 linenos,
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 isModule=False)
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 self._addItem(mn_node, m_node)
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 if repopulate:
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 self.endInsertRows()
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 self.__populated = True
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 else:
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 self.clear()
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 self.__populated = False
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 def isPopulated(self):
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 """
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 Public method to check, if the model is populated.
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 @return flag indicating a populated model
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 @rtype bool
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 """
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 return self.__populated
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 def repopulate(self):
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 """
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 Public slot to repopulate the model.
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 """
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 self.clear()
0b6e8c0d6403 Started implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 self.__populateModel(repopulate=True)
7686
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
182
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
183 def editor(self):
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
184 """
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
185 Public method to retrieve a reference to the editor.
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
186
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
187 @return reference to the editor
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
188 @rtype Editor
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
189 """
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
190 return self.__editor
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
191
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
192 def fileName(self):
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
193 """
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
194 Public method to retrieve the file name of the editor.
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
195
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
196 @return file name of the editor
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
197 @rtype str
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
198 """
379d402162ca Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7685
diff changeset
199 return self.__filename
7690
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
200
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
201 def itemIndexByLine(self, lineno):
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
202 """
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
203 Public method to find an item's index given a line number.
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
204
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
205 @param lineno one based line number of the item
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
206 @type int
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
207 @return index of the item found
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
208 @rtype QModelIndex
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
209 """
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
210 def findItem(lineno, parent):
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
211 """
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
212 Function to iteratively search for an item containing the given
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
213 line.
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
214
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
215 @param lineno one based line number of the item
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
216 @type int
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
217 @param parent reference to the parent item
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
218 @type BrowserItem
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
219 @return found item or None
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
220 @rtype BrowserItem
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
221 """
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
222 if not parent.isPopulated():
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
223 if parent.isLazyPopulated():
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
224 self.populateItem(parent)
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
225 else:
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
226 return None
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
227 for child in parent.children():
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
228 if isinstance(child, BrowserClassAttributesItem):
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
229 itm = findItem(lineno, child)
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
230 if itm is not None:
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
231 return itm
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
232 elif isinstance(child, (BrowserClassItem, BrowserMethodItem)):
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
233 start, end = child.boundaries()
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
234 if end == -1:
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
235 end = 1000000 # assume end of file
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
236 if start <= lineno <= end:
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
237 itm = findItem(lineno, child)
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
238 if itm is not None:
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
239 return itm
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
240 else:
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
241 return child
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
242 elif hasattr(child, "linenos"):
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
243 if lineno in child.linenos():
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
244 return child
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
245 elif hasattr(child, "lineno"):
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
246 if lineno == child.lineno():
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
247 return child
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
248 else:
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
249 return None
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
250
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
251 if self.__populated:
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
252 for rootChild in self.rootItem.children():
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
253 itm = None
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
254 if isinstance(rootChild, BrowserClassItem):
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
255 start, end = rootChild.boundaries()
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
256 if end == -1:
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
257 end = 1000000 # assume end of file
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
258 if start <= lineno <= end:
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
259 itm = findItem(lineno, rootChild)
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
260 if itm is None:
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
261 itm = rootChild
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
262 elif isinstance(rootChild,
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
263 (BrowserImportsItem, BrowserGlobalsItem)):
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
264 itm = findItem(lineno, rootChild)
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
265 elif (
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
266 isinstance(rootChild, BrowserCodingItem) and
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
267 lineno == rootChild.lineno()
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
268 ):
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
269 itm = rootChild
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
270 if itm is not None:
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
271 return self.createIndex(itm.row(), 0, itm)
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
272 else:
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
273 return QModelIndex()
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
274
a59680062837 Continued implementing the editor outline widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7686
diff changeset
275 return QModelIndex()

eric ide

mercurial