1/**
2 * This file is part of the KDE libraries
3 * Copyright (C) 2008 Jakob Petsovits <jpetso@gmx.at>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License version 2 as published by the Free Software Foundation.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#ifndef AUTOBRACE_H
21#define AUTOBRACE_H
22
23#include <ktexteditor/plugin.h>
24#include <ktexteditor/view.h>
25#include <ktexteditor/document.h>
26#include <kpluginfactory.h>
27
28#include <QtCore/QEvent>
29#include <QtCore/QObject>
30#include <QtCore/QHash>
31#include <QtCore/QVariantList>
32
33class AutoBracePlugin
34 : public KTextEditor::Plugin
35{
36 Q_OBJECT
37
38 public:
39 explicit AutoBracePlugin(QObject *parent = 0, const QVariantList &args = QVariantList());
40 virtual ~AutoBracePlugin();
41
42 static AutoBracePlugin *self() { return plugin; }
43
44 void addView (KTextEditor::View *view);
45 void removeView (KTextEditor::View *view);
46
47 void readConfig();
48 void writeConfig();
49
50 virtual void readConfig (KConfig *) {}
51 virtual void writeConfig (KConfig *) {}
52
53 /// Inline Option Get/Setters
54 bool autoBrackets() const { return m_autoBrackets; }
55 void setAutoBrackets(bool y) { m_autoBrackets = y; }
56 bool autoQuotations() const { return m_autoQuotations; }
57 void setAutoQuotations(bool y) { m_autoQuotations = y; }
58 private:
59 static AutoBracePlugin *plugin;
60 QHash<class KTextEditor::View*, class KTextEditor::Document*> m_documents;
61 QHash<class KTextEditor::Document*, class AutoBracePluginDocument*> m_docplugins;
62 bool m_autoBrackets;
63 bool m_autoQuotations;
64};
65
66class AutoBracePluginDocument
67 : public QObject, public KXMLGUIClient
68{
69 Q_OBJECT
70
71 public:
72 explicit AutoBracePluginDocument(KTextEditor::Document *document, const bool& autoBrackets, const bool& autoQuotations);
73 ~AutoBracePluginDocument();
74
75 private Q_SLOTS:
76 void slotTextChanged(KTextEditor::Document *document);
77 void slotTextInserted(KTextEditor::Document *document, const KTextEditor::Range& range);
78 void slotTextRemoved(KTextEditor::Document *document, const KTextEditor::Range& range);
79
80 void connectSlots(KTextEditor::Document* document);
81 void disconnectSlots(KTextEditor::Document* document);
82
83 private:
84 bool isInsertionCandidate(KTextEditor::Document *document, int openingBraceLine);
85
86 Q_SIGNALS:
87 void indent();
88
89 private:
90 void insertAutoBracket(KTextEditor::Document *document,const KTextEditor::Range& range,
91 const QString& brace);
92 const QString previousToken(KTextEditor::Document *document,const KTextEditor::Range& range);
93 const QString nextToken(KTextEditor::Document *document,const KTextEditor::Range& range);
94
95 int m_insertionLine;
96 QString m_indentation;
97 bool m_withSemicolon;
98 KTextEditor::Range m_lastRange;
99 const bool& m_autoBrackets;
100 const bool& m_autoQuotations;
101};
102
103K_PLUGIN_FACTORY_DECLARATION(AutoBracePluginFactory)
104
105#endif // AUTOBRACE_H
106