~ubuntu-branches/ubuntu/trusty/kate/trusty

« back to all changes in this revision

Viewing changes to addons/ktexteditor/lumen/lumen.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell, Rohan Garg, Jonathan Riddell, Philip Muškovac
  • Date: 2014-03-19 10:38:16 UTC
  • mfrom: (1.1.42)
  • Revision ID: package-import@ubuntu.com-20140319103816-f5b5t0m6hnwclzcn
Tags: 4:4.12.90-0ubuntu1
[ Rohan Garg ]
* Update install files

[ Jonathan Riddell ]
* New upstream beta release

[ Philip Muškovac ]
* Override license-problem-json-evil for js_lint.py as the file only
  references the evil file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2014  David Herberth kde@dav1d.de
 
3
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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/>.
 
19
**/
 
20
 
 
21
#include "lumen.h"
 
22
 
 
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>
 
34
 
 
35
 
 
36
K_PLUGIN_FACTORY_DEFINITION(
 
37
    LumenPluginFactory, registerPlugin<LumenPlugin>("ktexteditor_lumen");
 
38
)
 
39
 
 
40
K_EXPORT_PLUGIN(
 
41
    LumenPluginFactory(
 
42
        KAboutData(
 
43
            "ktexteditor_lumen",
 
44
            "ktexteditor_plugins",
 
45
            ki18n("Lumen"),
 
46
            "0.1",
 
47
            ki18n("Lumen"),
 
48
            KAboutData::License_LGPL_V2,
 
49
            ki18n("© David Herberth"),
 
50
            ki18n("D Autocompletion plugin using DCD as completion server.")
 
51
        )
 
52
    )
 
53
)
 
54
 
 
55
 
 
56
LumenPluginView::LumenPluginView(LumenPlugin *plugin, KTextEditor::View *view): QObject(plugin),KXMLGUIClient(view),m_view(view),m_registered(false)
 
57
{
 
58
    m_plugin = plugin;
 
59
    m_model = new LumenCompletionModel((QObject*)m_view, m_plugin->dcd());
 
60
 
 
61
    KTextEditor::Document* document = view->document();
 
62
 
 
63
    connect(document, SIGNAL(documentUrlChanged(KTextEditor::Document*)),
 
64
            this, SLOT(urlChanged(KTextEditor::Document*)));
 
65
 
 
66
    registerCompletion();
 
67
    registerTextHints();
 
68
}
 
69
 
 
70
void LumenPluginView::registerCompletion()
 
71
{
 
72
    KTextEditor::CodeCompletionInterface *completion =
 
73
        qobject_cast<KTextEditor::CodeCompletionInterface*>(m_view);
 
74
 
 
75
    bool isD = m_view->document()->url().path().endsWith(".d") ||
 
76
               m_view->document()->highlightingMode() == "D";
 
77
 
 
78
    if (isD && !m_registered) {
 
79
        completion->registerCompletionModel(m_model);
 
80
        m_registered = true;
 
81
    } else if(!isD && m_registered) {
 
82
        completion->unregisterCompletionModel(m_model);
 
83
        m_registered = false;
 
84
    }
 
85
}
 
86
 
 
87
void LumenPluginView::registerTextHints()
 
88
{
 
89
    KTextEditor::TextHintInterface *th =
 
90
        qobject_cast<KTextEditor::TextHintInterface*>(m_view);
 
91
    th->enableTextHints(500);
 
92
 
 
93
    connect(m_view, SIGNAL(needTextHint(const KTextEditor::Cursor&, QString &)),
 
94
            this, SLOT(getTextHint(const KTextEditor::Cursor&, QString &)));
 
95
}
 
96
 
 
97
void LumenPluginView::getTextHint(const Cursor& cursor, QString& text)
 
98
{
 
99
    KTextEditor::Document* document = m_view->document();
 
100
 
 
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());
 
109
 
 
110
    text = m_plugin->dcd()->doc(utf8, offset).trimmed().replace("\\n", "\n");
 
111
}
 
112
 
 
113
LumenPluginView::~LumenPluginView()
 
114
{
 
115
}
 
116
 
 
117
void LumenPluginView::urlChanged(Document* document)
 
118
{
 
119
    registerCompletion();
 
120
 
 
121
    QStringList paths;
 
122
    for (KUrl url = document->url(); !url.equals(KUrl("/")); url = url.upUrl()) {
 
123
        url = url.directory();
 
124
        url.addPath(".lumenconfig");
 
125
 
 
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
 
134
                    );
 
135
                }
 
136
 
 
137
                paths.append(path);
 
138
            }
 
139
        }
 
140
    }
 
141
 
 
142
    if (!paths.isEmpty()) {
 
143
        m_plugin->dcd()->addImportPath(paths);
 
144
    }
 
145
}
 
146
 
 
147
 
 
148
LumenPlugin::LumenPlugin(QObject *parent, const QVariantList &): Plugin(parent)
 
149
{
 
150
    m_dcd = new DCD(9166, "dcd-server", "dcd-client");
 
151
    m_dcd->startServer();
 
152
}
 
153
 
 
154
LumenPlugin::~LumenPlugin()
 
155
{
 
156
    m_dcd->stopServer();
 
157
    delete m_dcd;
 
158
}
 
159
 
 
160
DCD* LumenPlugin::dcd()
 
161
{
 
162
    return m_dcd;
 
163
}
 
164
 
 
165
 
 
166
void LumenPlugin::addView(KTextEditor::View *view)
 
167
{
 
168
    m_views.insert(view, new LumenPluginView(this, view));
 
169
}
 
170
 
 
171
void LumenPlugin::removeView(KTextEditor::View *view)
 
172
{
 
173
    delete m_views.take(view);
 
174
}
 
175
 
 
176
#include "lumen.moc"