2
* Copyright 2014 David Herberth kde@dav1d.de
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) version 3, or any
8
* later version accepted by the membership of KDE e.V. (or its
9
* successor approved by the membership of KDE e.V.), which shall
10
* act as a proxy defined in Section 6 of version 3 of the license.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
23
#include <kpluginfactory.h>
24
#include <kaboutdata.h>
25
#include <ktexteditor/document.h>
26
#include <ktexteditor/range.h>
27
#include <ktexteditor/view.h>
28
#include <ktexteditor/codecompletioninterface.h>
29
#include <ktexteditor/codecompletionmodel.h>
30
#include <ktexteditor/texthintinterface.h>
31
#include <kactioncollection.h>
32
#include <QtCore/QFile>
33
#include <QtCore/QDir>
36
K_PLUGIN_FACTORY_DEFINITION(
37
LumenPluginFactory, registerPlugin<LumenPlugin>("ktexteditor_lumen");
44
"ktexteditor_plugins",
48
KAboutData::License_LGPL_V2,
49
ki18n("© David Herberth"),
50
ki18n("D Autocompletion plugin using DCD as completion server.")
56
LumenPluginView::LumenPluginView(LumenPlugin *plugin, KTextEditor::View *view): QObject(plugin),KXMLGUIClient(view),m_view(view),m_registered(false)
59
m_model = new LumenCompletionModel((QObject*)m_view, m_plugin->dcd());
61
KTextEditor::Document* document = view->document();
63
connect(document, SIGNAL(documentUrlChanged(KTextEditor::Document*)),
64
this, SLOT(urlChanged(KTextEditor::Document*)));
70
void LumenPluginView::registerCompletion()
72
KTextEditor::CodeCompletionInterface *completion =
73
qobject_cast<KTextEditor::CodeCompletionInterface*>(m_view);
75
bool isD = m_view->document()->url().path().endsWith(".d") ||
76
m_view->document()->highlightingMode() == "D";
78
if (isD && !m_registered) {
79
completion->registerCompletionModel(m_model);
81
} else if(!isD && m_registered) {
82
completion->unregisterCompletionModel(m_model);
87
void LumenPluginView::registerTextHints()
89
KTextEditor::TextHintInterface *th =
90
qobject_cast<KTextEditor::TextHintInterface*>(m_view);
91
th->enableTextHints(500);
93
connect(m_view, SIGNAL(needTextHint(const KTextEditor::Cursor&, QString &)),
94
this, SLOT(getTextHint(const KTextEditor::Cursor&, QString &)));
97
void LumenPluginView::getTextHint(const Cursor& cursor, QString& text)
99
KTextEditor::Document* document = m_view->document();
101
KTextEditor::Cursor cursorEnd = document->documentEnd();
102
KTextEditor::Range range0c = KTextEditor::Range(0, 0, cursor.line(), cursor.column());
103
KTextEditor::Range rangece = KTextEditor::Range(cursor.line(), cursor.column(),
104
cursorEnd.line(), cursorEnd.column());
105
QString text0c = document->text(range0c, false);
106
QByteArray utf8 = text0c.toUtf8();
107
int offset = utf8.length();
108
utf8.append(document->text(rangece, false).toUtf8());
110
text = m_plugin->dcd()->doc(utf8, offset).trimmed().replace("\\n", "\n");
113
LumenPluginView::~LumenPluginView()
117
void LumenPluginView::urlChanged(Document* document)
119
registerCompletion();
122
for (KUrl url = document->url(); !url.equals(KUrl("/")); url = url.upUrl()) {
123
url = url.directory();
124
url.addPath(".lumenconfig");
126
QFile file(url.path());
127
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
128
while (!file.atEnd()) {
129
QString path = file.readLine().trimmed();
130
// KUrl doesn really provide this functionallity
131
if (QDir::isRelativePath(path)){
132
path = QDir::cleanPath(
133
url.directory() + QDir::separator() + path
142
if (!paths.isEmpty()) {
143
m_plugin->dcd()->addImportPath(paths);
148
LumenPlugin::LumenPlugin(QObject *parent, const QVariantList &): Plugin(parent)
150
m_dcd = new DCD(9166, "dcd-server", "dcd-client");
151
m_dcd->startServer();
154
LumenPlugin::~LumenPlugin()
160
DCD* LumenPlugin::dcd()
166
void LumenPlugin::addView(KTextEditor::View *view)
168
m_views.insert(view, new LumenPluginView(this, view));
171
void LumenPlugin::removeView(KTextEditor::View *view)
173
delete m_views.take(view);