[rkward-cvs] SF.net SVN: rkward: [2109] branches/KDE4_port
tfry at users.sourceforge.net
tfry at users.sourceforge.net
Mon Oct 22 23:03:27 UTC 2007
Revision: 2109
http://rkward.svn.sourceforge.net/rkward/?rev=2109&view=rev
Author: tfry
Date: 2007-10-22 16:03:27 -0700 (Mon, 22 Oct 2007)
Log Message:
-----------
Make code completion work in RKCommandEditorWindow
Modified Paths:
--------------
branches/KDE4_port/TODO_KDE4
branches/KDE4_port/rkward/windows/rkcommandeditorwindow.cpp
branches/KDE4_port/rkward/windows/rkcommandeditorwindow.h
Modified: branches/KDE4_port/TODO_KDE4
===================================================================
--- branches/KDE4_port/TODO_KDE4 2007-10-22 20:58:59 UTC (rev 2108)
+++ branches/KDE4_port/TODO_KDE4 2007-10-22 23:03:27 UTC (rev 2109)
@@ -26,9 +26,10 @@
- grep sources for "KDE4"
- remove all the passing around of KGlobal::config() as a parameter
- the whole idea of having a single mutex for everything is flawed. We should use several specialized ones
+ - all i18n with arguments calls need fixing (grep for all i18n with '%')
rkcommandeditorwindow
- - code completion will be broken. Fix after porting.
+ - polish code completion (e.g. hide window, when there are no completions)
- does the popup-menu still work? See SVN rev. 962.
- placement of menu options?
Modified: branches/KDE4_port/rkward/windows/rkcommandeditorwindow.cpp
===================================================================
--- branches/KDE4_port/rkward/windows/rkcommandeditorwindow.cpp 2007-10-22 20:58:59 UTC (rev 2108)
+++ branches/KDE4_port/rkward/windows/rkcommandeditorwindow.cpp 2007-10-22 23:03:27 UTC (rev 2109)
@@ -23,6 +23,7 @@
#include <ktexteditor/configinterface.h>
#include <ktexteditor/sessionconfiginterface.h>
+#include <ktexteditor/codecompletioninterface.h>
#include <ktexteditor/editorchooser.h>
#include <qlayout.h>
@@ -91,19 +92,20 @@
setIcon (SmallIcon ("source"));
- connect (m_doc, SIGNAL (fileNameChanged ()), this, SLOT (updateCaption ()));
- connect (m_doc, SIGNAL (modifiedChanged ()), this, SLOT (updateCaption ())); // of course most of the time this causes a redundant call to updateCaption. Not if a modification is undone, however.
- connect (m_doc, SIGNAL (textChanged ()), this, SLOT (tryCompletionProxy ()));
- connect (m_view, SIGNAL (filterInsertString (KTextEditor::CompletionEntry *, QString *)), this, SLOT (fixCompletion (KTextEditor::CompletionEntry *, QString *)));
+ connect (m_doc, SIGNAL (documentUrlChanged (KTextEditor::Document*)), this, SLOT (updateCaption (KTextEditor::Document*)));
+ connect (m_doc, SIGNAL (modifiedChanged (KTextEditor::Document*)), this, SLOT (updateCaption (KTextEditor::Document*))); // of course most of the time this causes a redundant call to updateCaption. Not if a modification is undone, however.
+ connect (m_doc, SIGNAL (textChanged (KTextEditor::Document*)), this, SLOT (tryCompletionProxy (KTextEditor::Document*)));
//KDE4 connect (m_view, SIGNAL (gotFocus (Kate::View *)), this, SLOT (setPopupMenu (Kate::View *)));
completion_timer = new QTimer (this);
connect (completion_timer, SIGNAL (timeout ()), this, SLOT (tryCompletion()));
if (use_r_highlighting) {
setRHighlighting ();
+ completion_model = new RKCodeCompletionModel (m_view);
hinter = new RKFunctionArgHinter (this, m_view);
} else {
hinter = 0;
+ completion_model = 0;
}
updateCaption (); // initialize
@@ -149,7 +151,7 @@
void RKCommandEditorWindow::closeEvent (QCloseEvent *e) {
if (isModified ()) {
- int status = KMessageBox::warningYesNo (this, i18n ("The document \"%1\" has been modified. Close it anyway?").arg (caption ()), i18n ("File not saved"));
+ int status = KMessageBox::warningYesNo (this, i18n ("The document \"%1\" has been modified. Close it anyway?", caption ()), i18n ("File not saved"));
if (status != KMessageBox::Yes) {
e->ignore ();
@@ -216,7 +218,7 @@
m_doc->setText (text);
}
-void RKCommandEditorWindow::updateCaption () {
+void RKCommandEditorWindow::updateCaption (KTextEditor::Document*) {
RK_TRACE (COMMANDEDITOR);
QString name = m_doc->url ().fileName ();
if (name.isEmpty ()) name = m_doc->url ().prettyUrl ();
@@ -235,13 +237,14 @@
RKHelpSearchWindow::mainHelpSearch ()->getContextHelp (line, c.column());
}
-void RKCommandEditorWindow::tryCompletionProxy () {
+void RKCommandEditorWindow::tryCompletionProxy (KTextEditor::Document*) {
completion_timer->start (100, true);
}
void RKCommandEditorWindow::tryCompletion () {
// TODO: merge this with RKConsole::doTabCompletion () somehow
RK_TRACE (COMMANDEDITOR);
+ if (!completion_model) return;
KTextEditor::Cursor c = m_view->cursorPosition();
uint para=c.line(); uint cursor_pos=c.column();
@@ -249,46 +252,25 @@
QString current_line = m_doc->line (para);
if (current_line.findRev ("#", cursor_pos) >= 0) return; // do not hint while in comments
- QString current_symbol = RKCommonFunctions::getCurrentSymbol (current_line, cursor_pos, false);
- if (current_symbol.length () >= 2) {
- RObject::RObjectMap map;
- RObject::RObjectMap::const_iterator it;
- RObjectList::getObjectList ()->findObjectsMatching (current_symbol, &map);
-/* KDE4: TODO rework!
- if (!map.isEmpty ()) {
- Q3ValueList<KTextEditor::CompletionEntry> list;
-
- for (it = map.constBegin (); it != map.constEnd (); ++it) {
- KTextEditor::CompletionEntry entry;
- entry.text = it.key ();
- list.append (entry);
- }
+ int start;
+ int end;
+ RKCommonFunctions::getCurrentSymbolOffset (current_line, cursor_pos, false, &start, &end);
+ if ((end - start) >= 2) {
+ KTextEditor::Range range (para, start, para, end);
- m_view->showCompletionBox (list);
- } */
+ KTextEditor::CodeCompletionInterface *iface = qobject_cast<KTextEditor::CodeCompletionInterface*> (m_view);
+ if (!iface) {
+ RK_ASSERT (false);
+ return;
+ }
+ if (iface->isCompletionActive ()) {
+ completion_model->completionInvoked (m_view, range, KTextEditor::CodeCompletionModel::ManualInvocation);
+ } else {
+ iface->startCompletion (range, completion_model);
+ }
}
}
-/* KDE4 TODO: maybe this is not needed anymore?
-void RKCommandEditorWindow::fixCompletion (KTextEditor::CompletionEntry *entry, QString *string) {
- RK_TRACE (COMMANDEDITOR);
- RK_ASSERT (entry);
- RK_ASSERT (string);
-
- *string = entry->text; // why, oh, why, isn't this always the case?
-
- uint current_line_num=0; uint cursor_pos=0;
- m_view->cursorPosition (¤t_line_num, &cursor_pos);
- QString current_line = m_view->currentTextLine ();
-
- int word_start;
- int word_end;
- RKCommonFunctions::getCurrentSymbolOffset (current_line, cursor_pos, false, &word_start, &word_end);
-
- // remove the start of the word, as the whole string will be inserted by katepart
- m_doc->removeText (current_line_num, word_start, current_line_num, word_end);
-} */
-
bool RKCommandEditorWindow::provideContext (unsigned int line_rev, QString *context, int *cursor_position) {
RK_TRACE (COMMANDEDITOR);
@@ -489,4 +471,74 @@
return false;
}
+//////////////////////// RKCodeCompletionModel ////////////////////
+
+RKCodeCompletionModel::RKCodeCompletionModel (KTextEditor::View *parent) : KTextEditor::CodeCompletionModel (parent) {
+ RK_TRACE (COMMANDEDITOR);
+
+ setRowCount (0);
+
+ KTextEditor::CodeCompletionInterface *iface = qobject_cast<KTextEditor::CodeCompletionInterface*> (parent);
+ if (!iface) {
+ RK_ASSERT (false);
+ return;
+ }
+ iface->registerCompletionModel (this);
+ iface->setAutomaticInvocationEnabled (false);
+}
+
+RKCodeCompletionModel::~RKCodeCompletionModel () {
+ RK_TRACE (COMMANDEDITOR);
+}
+
+void RKCodeCompletionModel::completionInvoked (KTextEditor::View *view, const KTextEditor::Range &range, InvocationType) {
+ RK_TRACE (COMMANDEDITOR);
+qDebug ("invoked");
+
+ QString current_symbol = view->document ()->text (range);
+ RObject::RObjectMap map;
+ RObjectList::getObjectList ()->findObjectsMatching (current_symbol, &map);
+
+ list.clear ();
+ // this is silly, but we need an int indexable storage, so we copy the map to a list
+ for (RObject::RObjectMap::const_iterator it = map.constBegin (); it != map.constEnd (); ++it) {
+ list.append (it.data ());
+ }
+qDebug ("%d", list.count ());
+ setRowCount (list.count ());
+
+ reset ();
+}
+
+void RKCodeCompletionModel::executeCompletionItem (KTextEditor::Document *document, const KTextEditor::Range &word, int row) const {
+ RK_TRACE (COMMANDEDITOR);
+
+ RK_ASSERT (list.size () > row);
+ RObject *object = list[row];
+ RK_ASSERT (object);
+
+ document->replaceText (word, object->getFullName ());
+}
+
+QVariant RKCodeCompletionModel::data (const QModelIndex& index, int role) const {
+
+ int col = index.column ();
+ int row = index.row ();
+
+ if (index.parent ().isValid ()) return QVariant ();
+ if (row > list.count ()) return QVariant ();
+// if (column > 1) return QVariant;
+
+ RObject* object = list[row];
+ RK_ASSERT (object);
+
+ if ((role == Qt::DisplayRole) || (role==KTextEditor::CodeCompletionModel::CompletionRole)) {
+ if (col == KTextEditor::CodeCompletionModel::Name) {
+ return (object->getBaseName ());
+ }
+ }
+
+ return QVariant ();
+}
+
#include "rkcommandeditorwindow.moc"
Modified: branches/KDE4_port/rkward/windows/rkcommandeditorwindow.h
===================================================================
--- branches/KDE4_port/rkward/windows/rkcommandeditorwindow.h 2007-10-22 20:58:59 UTC (rev 2108)
+++ branches/KDE4_port/rkward/windows/rkcommandeditorwindow.h 2007-10-22 23:03:27 UTC (rev 2109)
@@ -19,17 +19,16 @@
#include <QWidget>
#include <qstring.h>
-//Added by qt3to4:
-#include <QEvent>
-#include <QLabel>
-#include <QCloseEvent>
#include <ktexteditor/view.h>
#include <ktexteditor/document.h>
+#include <ktexteditor/codecompletionmodel.h>
#include <kurl.h>
#include "../windows/rkmdiwindow.h"
+class QEvent;
+class QCloseEvent;
class QFrame;
class QLabel;
@@ -46,6 +45,7 @@
virtual bool provideContext (unsigned int line_rev, QString *context, int *cursor_position) = 0;
};
+class RObject;
/** function argument hinting for RKCommandEditorWindow and RKConsole */
class RKFunctionArgHinter : public QObject {
Q_OBJECT
@@ -71,6 +71,19 @@
QLabel *arghints_popup_text;
};
+/** code completion model for RKCommandEditorWindow */
+class RKCodeCompletionModel : public KTextEditor::CodeCompletionModel {
+public:
+ RKCodeCompletionModel (KTextEditor::View* parent);
+ ~RKCodeCompletionModel ();
+ void completionInvoked (KTextEditor::View *view, const KTextEditor::Range &range, InvocationType);
+ void executeCompletionItem (KTextEditor::Document *document, const KTextEditor::Range &word, int row) const;
+// int columnCount (const QModelIndex &parent=QModelIndex()) const;
+ QVariant data (const QModelIndex& index, int role=Qt::DisplayRole) const;
+private:
+ QList<RObject*> list;
+};
+
class QTimer;
/**
@@ -115,14 +128,11 @@
bool provideContext (unsigned int line_rev, QString *context, int *cursor_position);
public slots:
/** update Tab caption according to the current url. Display the filename-component of the URL, or - if not available - a more elaborate description of the url. Also appends a "[modified]" if appropriate */
- void updateCaption ();
+ void updateCaption (KTextEditor::Document* = 0);
/** called whenever it might be appropriate to show a code completion box. The box is not shown immediately, but only after a timeout (if at all) */
- void tryCompletionProxy ();
+ void tryCompletionProxy (KTextEditor::Document*);
/** show a code completion box if appropriate. Use tryCompletionProxy () instead, which will call this function after a timeout */
void tryCompletion ();
-/** called by the Kate part, if an entry was selected from the code completion box. Will remove the current symbol, as the kate part is about to re-add it (in completed form) */
-// KDE4 TODO: still needed?
-// void fixCompletion (KTextEditor::CompletionEntry *, QString *);
//KDE4 void setPopupMenu (KTextEditor::View *);
//KDE4 void setPopupMenu ();
/** Show help about the current word. */
@@ -140,6 +150,7 @@
KTextEditor::Document *m_doc;
KTextEditor::View *m_view;
RKFunctionArgHinter *hinter;
+ RKCodeCompletionModel *completion_model;
QTimer *completion_timer;
/** set syntax highlighting-mode to R syntax */
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
More information about the rkward-tracker
mailing list