1 /***************************************************************************
2                       bash_parser.cpp  -  description
3                              -------------------
4     begin                : dec 12 2008
5     author               : Daniel Dumitrache
6     email                : daniel.dumitrache@gmail.com
7  ***************************************************************************/
8 /***************************************************************************
9    SPDX-License-Identifier: GPL-2.0-or-later
10 ***************************************************************************/
11 
12 #include "plugin_katesymbolviewer.h"
13 
parseBashSymbols(void)14 void KatePluginSymbolViewerView::parseBashSymbols(void)
15 {
16     if (!m_mainWindow->activeView()) {
17         return;
18     }
19 
20     QString currline;
21 
22     int i;
23     // bool mainprog;
24 
25     QTreeWidgetItem *node = nullptr;
26     QTreeWidgetItem *funcNode = nullptr;
27     QTreeWidgetItem *lastFuncNode = nullptr;
28 
29     QPixmap func(class_xpm);
30 
31     // It is necessary to change names
32     m_func->setText(i18n("Show Functions"));
33 
34     if (m_treeOn->isChecked()) {
35         funcNode = new QTreeWidgetItem(m_symbols, QStringList(i18n("Functions")));
36         funcNode->setIcon(0, QIcon(func));
37 
38         if (m_expandOn->isChecked()) {
39             m_symbols->expandItem(funcNode);
40         }
41 
42         lastFuncNode = funcNode;
43 
44         m_symbols->setRootIsDecorated(1);
45     } else {
46         m_symbols->setRootIsDecorated(0);
47     }
48 
49     KTextEditor::Document *kDoc = m_mainWindow->activeView()->document();
50 
51     for (i = 0; i < kDoc->lines(); i++) {
52         currline = kDoc->line(i);
53         currline = currline.trimmed();
54         currline = currline.simplified();
55 
56         bool comment = false;
57         // qDebug(13000)<<currline<<endl;
58         if (currline.isEmpty()) {
59             continue;
60         }
61         if (currline.at(0) == QLatin1Char('#')) {
62             comment = true;
63         }
64 
65         // mainprog=false;
66         if (!comment && m_func->isChecked()) {
67             QString funcName;
68 
69             // skip line if no function defined
70             // note: function name must match regex: [a-zA-Z0-9-_]+
71             if (!currline.contains(QRegularExpression(QLatin1String("^(function )*[a-zA-Z0-9-_]+ *\\( *\\)")))
72                 && !currline.contains(QRegularExpression(QLatin1String("^function [a-zA-Z0-9-_]+")))) {
73                 continue;
74             }
75 
76             // strip everything unneeded and get the function's name
77             currline.remove(QRegularExpression(QLatin1String("^(function )*")));
78             funcName = currline.split(QRegularExpression(QLatin1String("((\\( *\\))|[^a-zA-Z0-9-_])")))[0].simplified();
79             if (!funcName.size()) {
80                 continue;
81             }
82             funcName.append(QLatin1String("()"));
83 
84             if (m_treeOn->isChecked()) {
85                 node = new QTreeWidgetItem(funcNode, lastFuncNode);
86                 lastFuncNode = node;
87             } else {
88                 node = new QTreeWidgetItem(m_symbols);
89             }
90 
91             node->setText(0, funcName);
92             node->setIcon(0, QIcon(func));
93             node->setText(1, QString::number(i, 10));
94         }
95     } // for i loop
96 }
97