1 /***************************************************************************
2                            python_parser.cpp  -  description
3                              -------------------
4     begin                : Apr 2 2003
5     author               : 2003 Massimo Callegari
6     email                : massimocallegari@yahoo.it
7  ***************************************************************************/
8 /***************************************************************************
9  *                                                                         *
10  *   SPDX-License-Identifier: GPL-2.0-or-later
11  *                                                                         *
12  ***************************************************************************/
13 #include "plugin_katesymbolviewer.h"
14 
parsePythonSymbols(void)15 void KatePluginSymbolViewerView::parsePythonSymbols(void)
16 {
17     if (!m_mainWindow->activeView()) {
18         return;
19     }
20 
21     m_macro->setText(i18n("Show Globals"));
22     m_struct->setText(i18n("Show Methods"));
23     m_func->setText(i18n("Show Classes"));
24 
25     QString cl; // Current Line
26     QPixmap cls(class_xpm);
27     QPixmap mtd(method_xpm);
28     QPixmap mcr(macro_xpm);
29 
30     int in_class = 0, state = 0, j;
31     QString name;
32 
33     QTreeWidgetItem *node = nullptr;
34     QTreeWidgetItem *mcrNode = nullptr, *mtdNode = nullptr, *clsNode = nullptr;
35     QTreeWidgetItem *lastMcrNode = nullptr, *lastMtdNode = nullptr, *lastClsNode = nullptr;
36 
37     KTextEditor::Document *kv = m_mainWindow->activeView()->document();
38 
39     // kdDebug(13000)<<"Lines counted :"<<kv->numLines()<<endl;
40     if (m_treeOn->isChecked()) {
41         clsNode = new QTreeWidgetItem(m_symbols, QStringList(i18n("Classes")));
42         mcrNode = new QTreeWidgetItem(m_symbols, QStringList(i18n("Globals")));
43         mcrNode->setIcon(0, QIcon(mcr));
44         clsNode->setIcon(0, QIcon(cls));
45 
46         if (m_expandOn->isChecked()) {
47             m_symbols->expandItem(mcrNode);
48             m_symbols->expandItem(clsNode);
49         }
50         lastClsNode = clsNode;
51         lastMcrNode = mcrNode;
52         mtdNode = clsNode;
53         lastMtdNode = clsNode;
54         m_symbols->setRootIsDecorated(1);
55     } else {
56         m_symbols->setRootIsDecorated(0);
57     }
58 
59     for (int i = 0; i < kv->lines(); i++) {
60         int line = i;
61         cl = kv->line(i);
62         // concatenate continued lines and remove continuation marker
63         if (cl.length() == 0) {
64             continue;
65         }
66         while (cl[cl.length() - 1] == QLatin1Char('\\')) {
67             cl = cl.left(cl.length() - 1);
68             i++;
69             if (i < kv->lines()) {
70                 cl += kv->line(i);
71             } else {
72                 break;
73             }
74         }
75 
76         if (cl.indexOf(QRegularExpression(QLatin1String("^class [a-zA-Z0-9_,\\s\\(\\).]+:"))) >= 0) {
77             in_class = 1;
78         }
79 
80         // if(cl.find( QRegularExpression(QLatin1String("[\\s]+def [a-zA-Z_]+[^#]*:")) ) >= 0) in_class = 2;
81         if (cl.indexOf(QRegularExpression(QLatin1String("^def\\s+[a-zA-Z_]+[^#]*:"))) >= 0) {
82             in_class = 0;
83         }
84 
85         if (cl.indexOf(QLatin1String("def ")) >= 0 || (cl.indexOf(QLatin1String("class ")) >= 0 && in_class == 1)) {
86             if (cl.indexOf(QLatin1String("def ")) >= 0 && in_class == 1) {
87                 in_class = 2;
88             }
89             state = 1;
90             if (cl.indexOf(QLatin1Char(':')) >= 0) {
91                 state = 3; // found in the same line. Done
92             } else if (cl.indexOf(QLatin1Char('(')) >= 0) {
93                 state = 2;
94             }
95 
96             if (state == 2 || state == 3) {
97                 name = cl.left(cl.indexOf(QLatin1Char('(')));
98             }
99         }
100 
101         if (state > 0 && state < 3) {
102             for (j = 0; j < cl.length(); j++) {
103                 if (cl.at(j) == QLatin1Char('(')) {
104                     state = 2;
105                 } else if (cl.at(j) == QLatin1Char(':')) {
106                     state = 3;
107                     break;
108                 }
109 
110                 if (state == 1) {
111                     name += cl.at(j);
112                 }
113             }
114         }
115         if (state == 3) {
116             // qDebug(13000)<<"Function -- Inserted : "<<name<<" at row : "<<i;
117             if (in_class == 1) { // strip off the word "class "
118                 name = name.trimmed().mid(6);
119             } else { // strip off the word "def "
120                 name = name.trimmed().mid(4);
121             }
122 
123             if (m_func->isChecked() && in_class == 1) {
124                 if (m_treeOn->isChecked()) {
125                     node = new QTreeWidgetItem(clsNode, lastClsNode);
126                     if (m_expandOn->isChecked()) {
127                         m_symbols->expandItem(node);
128                     }
129                     lastClsNode = node;
130                     mtdNode = lastClsNode;
131                     lastMtdNode = lastClsNode;
132                 } else {
133                     node = new QTreeWidgetItem(m_symbols);
134                 }
135 
136                 node->setText(0, name);
137                 node->setIcon(0, QIcon(cls));
138                 node->setText(1, QString::number(line, 10));
139             }
140 
141             if (m_struct->isChecked() && in_class == 2) {
142                 if (m_treeOn->isChecked()) {
143                     node = new QTreeWidgetItem(mtdNode, lastMtdNode);
144                     lastMtdNode = node;
145                 } else {
146                     node = new QTreeWidgetItem(m_symbols);
147                 }
148 
149                 node->setText(0, name);
150                 node->setIcon(0, QIcon(mtd));
151                 node->setText(1, QString::number(line, 10));
152             }
153 
154             if (m_macro->isChecked() && in_class == 0) {
155                 if (m_treeOn->isChecked()) {
156                     node = new QTreeWidgetItem(mcrNode, lastMcrNode);
157                     lastMcrNode = node;
158                 } else {
159                     node = new QTreeWidgetItem(m_symbols);
160                 }
161 
162                 node->setText(0, name);
163                 node->setIcon(0, QIcon(mcr));
164                 node->setText(1, QString::number(line, 10));
165             }
166 
167             state = 0;
168             name.clear();
169         }
170     }
171 }
172 
173 // kate: space-indent on; indent-width 2; replace-tabs on;
174