[rkward/work/new_completion_to_console] rkward/windows: Remove now obsolete RKFunctionArgHinter
Thomas Friedrichsmeier
null at kde.org
Sat May 2 11:10:46 BST 2020
Git commit c8f8e8e3751a9e14ec8ba19da942230af2bc45ad by Thomas Friedrichsmeier.
Committed on 02/05/2020 at 10:09.
Pushed by tfry into branch 'work/new_completion_to_console'.
Remove now obsolete RKFunctionArgHinter
M +0 -162 rkward/windows/rkcommandeditorwindow.cpp
M +1 -32 rkward/windows/rkcommandeditorwindow.h
https://commits.kde.org/rkward/c8f8e8e3751a9e14ec8ba19da942230af2bc45ad
diff --git a/rkward/windows/rkcommandeditorwindow.cpp b/rkward/windows/rkcommandeditorwindow.cpp
index 123d98fd..73337bfd 100644
--- a/rkward/windows/rkcommandeditorwindow.cpp
+++ b/rkward/windows/rkcommandeditorwindow.cpp
@@ -242,7 +242,6 @@ RKCommandEditorWindow::RKCommandEditorWindow (QWidget *parent, const QUrl _url,
// somehow the katepart loses the context menu each time it loses focus
connect (m_view, &KTextEditor::View::focusIn, this, &RKCommandEditorWindow::focusIn);
- hinter = 0;
if (use_r_highlighting) {
RKCommandHighlighter::setHighlighting (m_doc, RKCommandHighlighter::RScript);
if (flags & RKCommandEditorFlags::UseCodeHinting) {
@@ -275,7 +274,6 @@ RKCommandEditorWindow::~RKCommandEditorWindow () {
m_view->writeSessionConfig (viewconf);
}
- delete hinter;
discardPreview ();
delete m_view;
QList<KTextEditor::View*> views = m_doc->views ();
@@ -1038,166 +1036,6 @@ void RKCommandEditorWindow::selectionChanged (KTextEditor::View* view) {
}
}
-//////////////////////// RKFunctionArgHinter //////////////////////////////
-
-#include <QToolTip>
-#include <QStyle>
-
-#include "../core/rfunctionobject.h"
-
-RKFunctionArgHinter::RKFunctionArgHinter (RKScriptContextProvider *provider, KTextEditor::View* view) {
- RK_TRACE (COMMANDEDITOR);
-
- RKFunctionArgHinter::provider = provider;
- RKFunctionArgHinter::view = view;
-
- const QObjectList children = view->children ();
- for (QObjectList::const_iterator it = children.constBegin(); it != children.constEnd (); ++it) {
- QObject *obj = *it;
- obj->installEventFilter (this);
- }
-
- arghints_popup = new QLabel (0, Qt::ToolTip);
- arghints_popup->setMargin (2);
- // HACK trying hard to trick the style into using the correct color
- // ... and sometimes we still get white on yellow in some styles. Sigh...
- // A simple heuristic tries to detect the worst cases of unreasonably low contrast, and forces black on light blue, then.
- QPalette p = QToolTip::palette ();
- QColor b = p.color (QPalette::Inactive, QPalette::ToolTipBase);
- QColor f = p.color (QPalette::Inactive, QPalette::ToolTipText);
- if ((qAbs (f.greenF () - b.greenF ()) + qAbs (f.redF () - b.redF ()) + qAbs (f.yellowF () - b.yellowF ())) < .6) {
- f = Qt::black;
- b = QColor (192, 219, 255);
- }
- p.setColor (QPalette::Inactive, QPalette::WindowText, f);
- p.setColor (QPalette::Inactive, QPalette::Window, b);
- p.setColor (QPalette::Inactive, QPalette::ToolTipText, f);
- p.setColor (QPalette::Inactive, QPalette::ToolTipBase, b);
- arghints_popup->setForegroundRole (QPalette::ToolTipText);
- arghints_popup->setBackgroundRole (QPalette::ToolTipBase);
- arghints_popup->setPalette (p);
- arghints_popup->setFrameStyle (QFrame::Box);
- arghints_popup->setLineWidth (1);
- arghints_popup->setWordWrap (true);
- arghints_popup->setWindowOpacity (arghints_popup->style ()->styleHint (QStyle::SH_ToolTipLabel_Opacity, 0, arghints_popup) / 255.0);
- arghints_popup->hide ();
- active = false;
-
- connect (&updater, &QTimer::timeout, this, &RKFunctionArgHinter::updateArgHintWindow);
-}
-
-RKFunctionArgHinter::~RKFunctionArgHinter () {
- RK_TRACE (COMMANDEDITOR);
- delete arghints_popup;
-}
-
-void RKFunctionArgHinter::tryArgHint () {
- RK_TRACE (COMMANDEDITOR);
-
- if (!RKSettingsModuleCommandEditor::completionSettings()->argHintingEnabled ()) return;
-
- // do this in the next event cycle to make sure any inserted characters have truly been inserted
- QTimer::singleShot (0, this, SLOT (tryArgHintNow()));
-}
-
-void RKFunctionArgHinter::tryArgHintNow () {
- RK_TRACE (COMMANDEDITOR);
-
- // find the active opening brace
- int line_rev = -1;
- QList<int> unclosed_braces;
- QString full_context;
- while (unclosed_braces.isEmpty ()) {
- QString context_line = provider->provideContext (++line_rev);
- if (context_line.isNull ()) break;
- full_context.prepend (context_line);
- for (int i = 0; i < context_line.length (); ++i) {
- QChar c = context_line.at (i);
- if (c == '"' || c == '\'' || c == '`') { // NOTE: this algo does not produce good results on string constants spanning newlines.
- i = RKCommonFunctions::quoteEndPosition (c, context_line, i + 1);
- if (i < 0) break;
- continue;
- } else if (c == '\\') {
- ++i;
- continue;
- } else if (c == '(') {
- unclosed_braces.append (i);
- } else if (c == ')') {
- if (!unclosed_braces.isEmpty()) unclosed_braces.pop_back ();
- }
- }
- }
-
- int potential_symbol_end = unclosed_braces.isEmpty () ? -1 : unclosed_braces.last () - 1;
-
- // now find out where the symbol to the left of the opening brace ends
- // there cannot be a line-break between the opening brace, and the symbol name (or can there?), so no need to fetch further context
- while ((potential_symbol_end >= 0) && full_context.at (potential_symbol_end).isSpace ()) {
- --potential_symbol_end;
- }
- if (potential_symbol_end <= 0) {
- hideArgHint ();
- return;
- }
-
- // now identify the symbol and object (if any)
- QString effective_symbol = RKCommonFunctions::getCurrentSymbol (full_context, potential_symbol_end);
- if (effective_symbol.isEmpty ()) {
- hideArgHint ();
- return;
- }
-
- RObject *object = RObjectList::getObjectList ()->findObject (effective_symbol);
- if ((!object) || (!object->isType (RObject::Function))) {
- hideArgHint ();
- return;
- }
-
- // initialize and show popup
- arghints_popup->setText (effective_symbol + " (" + static_cast<RFunctionObject*> (object)->printArgs () + ')');
- arghints_popup->resize (arghints_popup->sizeHint () + QSize (2, 2));
- active = true;
- updater.start (50);
- updateArgHintWindow ();
-}
-
-void RKFunctionArgHinter::updateArgHintWindow () {
- RK_TRACE (COMMANDEDITOR);
-
- if (!active) return;
-
- arghints_popup->move (view->mapToGlobal (view->cursorPositionCoordinates () + QPoint (0, arghints_popup->fontMetrics ().lineSpacing () + arghints_popup->margin ()*2)));
- if (view->hasFocus ()) arghints_popup->show ();
- else arghints_popup->hide ();
-}
-
-void RKFunctionArgHinter::hideArgHint () {
- RK_TRACE (COMMANDEDITOR);
- arghints_popup->hide ();
- active = false;
- updater.stop ();
-}
-
-bool RKFunctionArgHinter::eventFilter (QObject *, QEvent *e) {
- if (e->type () == QEvent::KeyPress || e->type () == QEvent::ShortcutOverride) {
- RK_TRACE (COMMANDEDITOR); // avoid loads of empty traces, putting this here
- QKeyEvent *k = static_cast<QKeyEvent *> (e);
-
- if (k->key() == Qt::Key_Enter || k->key() == Qt::Key_Return || k->key () == Qt::Key_Up || k->key () == Qt::Key_Down || k->key () == Qt::Key_Left || k->key () == Qt::Key_Right || k->key () == Qt::Key_Home || k->key () == Qt::Key_Tab) {
- hideArgHint ();
- } else if (k->key () == Qt::Key_Backspace || k->key () == Qt::Key_Delete){
- tryArgHint ();
- } else {
- QString text = k->text ();
- if ((text == "(") || (text == ")") || (text == ",")) {
- tryArgHint ();
- }
- }
- }
-
- return false;
-}
-
// static
KTextEditor::Document* RKCommandHighlighter::_doc = 0;
KTextEditor::View* RKCommandHighlighter::_view = 0;
diff --git a/rkward/windows/rkcommandeditorwindow.h b/rkward/windows/rkcommandeditorwindow.h
index d0885348..86ea47f5 100644
--- a/rkward/windows/rkcommandeditorwindow.h
+++ b/rkward/windows/rkcommandeditorwindow.h
@@ -55,7 +55,7 @@ friend class RKCommandEditorWindow;
~RKCommandEditorWindowPart ();
};
-/** classes wishing to use RKFunctionArgHinter should derive from this, and implement provideContext () */
+/** classes wishing to use context help should derive from this, and implement provideContext () */
class RKScriptContextProvider {
public:
RKScriptContextProvider () {};
@@ -72,36 +72,6 @@ public:
virtual void currentHelpContext (QString *symbol, QString *package) = 0;
};
-class RObject;
-/** function argument hinting for RKCommandEditorWindow and RKConsole */
-class RKFunctionArgHinter : public QObject {
- Q_OBJECT
-public:
- RKFunctionArgHinter (RKScriptContextProvider *provider, KTextEditor::View* view);
- ~RKFunctionArgHinter ();
-
- /** Try to show an arg hint now */
- void tryArgHint ();
- /** Hide the arg hint (if shown) */
- void hideArgHint ();
-public slots:
- /** Internal worker function for tryArgHint () */
- void tryArgHintNow ();
-
- void updateArgHintWindow ();
-protected:
- /** The (keypress) events of the view are filtered to determine, when to show / hide an argument hint */
- bool eventFilter (QObject *, QEvent *e) override;
-private:
- RKScriptContextProvider *provider;
- KTextEditor::View *view;
-
- /** A timer to refresh the hint window periodically. This is a bit sorry, but it's really hard to find out, when the view has been moved, or gains/loses focus. While possible, this approach uses much less code. */
- QTimer updater;
- bool active;
- QLabel *arghints_popup;
-};
-
class RKJobSequence;
class RKXMLGUIPreviewArea;
class RKPreviewManager;
@@ -206,7 +176,6 @@ private:
KTextEditor::Document *m_doc;
KTextEditor::View *m_view;
KTextEditor::MovingInterface *smart_iface;
- RKFunctionArgHinter *hinter;
void initializeActions (KActionCollection* ac);
More information about the rkward-tracker
mailing list