[RFC PATCH 1/1] SymbolView: Add a plain-text-parser
loh.tar
loh.tar at googlemail.com
Sun Jul 22 14:49:51 BST 2018
With this you have something like a table of contents when reading/edit
a plain text file like a README
- Split and sort katesymbolviewerplugin_PART_SRCS in CMakeLists
---
addons/symbolviewer/CMakeLists.txt | 16 +-
addons/symbolviewer/plaintext_parser.cpp | 156 ++++++++++++++++++
.../symbolviewer/plugin_katesymbolviewer.cpp | 2 +
addons/symbolviewer/plugin_katesymbolviewer.h | 2 +-
4 files changed, 173 insertions(+), 3 deletions(-)
create mode 100644 addons/symbolviewer/plaintext_parser.cpp
diff --git a/addons/symbolviewer/CMakeLists.txt b/addons/symbolviewer/CMakeLists.txt
index a7176bec4..293e98f26 100644
--- a/addons/symbolviewer/CMakeLists.txt
+++ b/addons/symbolviewer/CMakeLists.txt
@@ -1,7 +1,19 @@
add_definitions(-DTRANSLATION_DOMAIN=\"katesymbolviewer\")
########### next target ###############
-set(katesymbolviewerplugin_PART_SRCS cpp_parser.cpp tcl_parser.cpp fortran_parser.cpp perl_parser.cpp
-php_parser.cpp xslt_parser.cpp ruby_parser.cpp python_parser.cpp bash_parser.cpp ecma_parser.cpp plugin_katesymbolviewer.cpp )
+set(katesymbolviewerplugin_PART_SRCS
+ plugin_katesymbolviewer.cpp
+ bash_parser.cpp
+ cpp_parser.cpp
+ ecma_parser.cpp
+ fortran_parser.cpp
+ perl_parser.cpp
+ php_parser.cpp
+ plaintext_parser.cpp
+ python_parser.cpp
+ ruby_parser.cpp
+ tcl_parser.cpp
+ xslt_parser.cpp
+)
# resource for ui file and stuff
qt5_add_resources(katesymbolviewerplugin_PART_SRCS plugin.qrc)
diff --git a/addons/symbolviewer/plaintext_parser.cpp b/addons/symbolviewer/plaintext_parser.cpp
new file mode 100644
index 000000000..15ff40131
--- /dev/null
+++ b/addons/symbolviewer/plaintext_parser.cpp
@@ -0,0 +1,156 @@
+/***************************************************************************
+ plaintext_parser.cpp - description
+ -------------------
+ begin : Jul 2018
+ author : loh.tar
+ email : loh.tar at googlemail.com
+ ***************************************************************************/
+/***************************************************************************
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of
+ the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+***************************************************************************/
+
+#include "plugin_katesymbolviewer.h"
+
+void KatePluginSymbolViewerView::parsePlainTextSymbols(void)
+{
+ if (!m_mainWindow->activeView())
+ return;
+
+ // Rename menu actions
+ m_struct->setText(i18n("Show Sections"));
+ m_func->setText(i18n("Show Header"));
+ m_macro->setText(i18n("Show Paragraphs"));
+
+ // We should also rename the header caption, but that is then not restored
+ // after a switch to an other document -> Needs a patch e.g. in parseSymbols()
+ //m_symbols->headerItem()->setText(0, i18n("Sections"));
+
+ enum nodeType {
+ RootNode,
+ SectNode,
+ HeadNode,
+ ParaNode
+ };
+
+ QTreeWidgetItem *rootNode = nullptr;
+ QTreeWidgetItem *parentNode = nullptr;
+
+ if (m_plugin->treeOn) {
+ rootNode = new QTreeWidgetItem(m_symbols, QStringList(i18n("Document")), RootNode);
+ rootNode->setIcon(0, QIcon(QPixmap(const_cast<const char**>(class_xpm))));
+
+ parentNode = rootNode;
+ m_symbols->setRootIsDecorated(1);
+
+ } else {
+ m_symbols->setRootIsDecorated(0);
+ }
+
+ QString currLine;
+ QString prevLine;
+ QString paraText;
+ int paraLineNumber;
+ bool newParagraph = false;
+
+ KTextEditor::Document *kDoc = m_mainWindow->activeView()->document();
+
+ // Run the loop one line more as usually needed...
+ for (int i = 0; i < kDoc->lines() + 1; i++) {
+ // ...to ensure a last paragraph is added properly
+ if (i < kDoc->lines()) {
+ currLine = kDoc->line(i);
+ currLine = currLine.trimmed();
+ currLine = currLine.simplified();
+ currLine.truncate(80); // Limit the size to some acceptable length
+
+ if (currLine.isEmpty()) {
+ if (!paraText.isEmpty()) {
+ newParagraph = true;
+ }
+ continue;
+ }
+
+ } else {
+ if (paraText.isEmpty()) {
+ break;
+ }
+ newParagraph = true;
+ }
+
+ QTreeWidgetItem *node = nullptr;
+ QString nodeText;
+ int nodeLineNumber;
+ const char** nodePixmap = const_cast<const char**>(class_xpm) ;
+
+ if (m_struct->isChecked() && currLine.contains(QRegExp(QLatin1String("^[=]+$")))) {
+ if (m_plugin->treeOn) {
+ node = new QTreeWidgetItem(rootNode, SectNode);
+ parentNode = node;
+ } else {
+ node = new QTreeWidgetItem(m_symbols, SectNode);
+ }
+ nodeText = prevLine;
+ nodeLineNumber = i - 1;
+ nodePixmap = const_cast<const char**>(struct_xpm);
+
+ } else if (m_func->isChecked() && currLine.contains(QRegExp(QLatin1String("^[-]+$")))) {
+ if (m_plugin->treeOn) {
+ while (parentNode->type() >= HeadNode) {
+ parentNode = parentNode->parent();
+ }
+ node = new QTreeWidgetItem(parentNode, HeadNode);
+ parentNode = node;
+ } else {
+ node = new QTreeWidgetItem(m_symbols, HeadNode);
+ }
+ nodeText = prevLine;
+ nodeLineNumber = i - 1;
+ nodePixmap = const_cast<const char**>(method_xpm);
+
+ } else if (m_macro->isChecked()) {
+ if (newParagraph) {
+ if (m_plugin->treeOn) {
+ node = new QTreeWidgetItem(parentNode, ParaNode);
+ } else {
+ node = new QTreeWidgetItem(m_symbols, ParaNode);
+ }
+ nodeText = paraText;
+ nodeLineNumber = paraLineNumber;
+ nodePixmap = const_cast<const char**>(macro_xpm);
+ newParagraph = false;
+ paraText.clear();
+ }
+
+ if (paraText.isEmpty()) {
+ paraText = currLine;
+ paraLineNumber = i;
+ }
+ }
+
+ if (node) {
+ node->setText(0, nodeText);
+ node->setIcon(0, QIcon(QPixmap(nodePixmap)));
+ node->setText(1, QString::number(nodeLineNumber, 10));
+ if (node->type() < ParaNode) {
+ paraText.clear();
+ }
+
+ if (m_plugin->expandedOn) {
+ m_symbols->expandItem(node->parent());
+ }
+ }
+
+ prevLine = currLine;
+ }
+}
diff --git a/addons/symbolviewer/plugin_katesymbolviewer.cpp b/addons/symbolviewer/plugin_katesymbolviewer.cpp
index 1d90a48b6..ffbd195c1 100644
--- a/addons/symbolviewer/plugin_katesymbolviewer.cpp
+++ b/addons/symbolviewer/plugin_katesymbolviewer.cpp
@@ -350,6 +350,8 @@ void KatePluginSymbolViewerView::parseSymbols(void)
parseXsltSymbols();
else if (hlModeName == QLatin1String("Bash"))
parseBashSymbols();
+ else if (hlModeName == QLatin1String("Normal"))
+ parsePlainTextSymbols();
else if (hlModeName == QLatin1String("ActionScript 2.0") ||
hlModeName == QLatin1String("JavaScript") ||
hlModeName == QLatin1String("QML"))
diff --git a/addons/symbolviewer/plugin_katesymbolviewer.h b/addons/symbolviewer/plugin_katesymbolviewer.h
index 1e4e34c47..e131a1b0b 100644
--- a/addons/symbolviewer/plugin_katesymbolviewer.h
+++ b/addons/symbolviewer/plugin_katesymbolviewer.h
@@ -136,8 +136,8 @@ class KatePluginSymbolViewerView : public QObject, public KXMLGUIClient
void parseXsltSymbols(void);
void parsePhpSymbols(void);
void parseBashSymbols(void);
+ void parsePlainTextSymbols(void);
void parseEcmaSymbols(void);
-
};
class KatePluginSymbolViewer : public KTextEditor::Plugin
--
2.18.0
More information about the KWrite-Devel
mailing list