[education/rkward] /: Add R dynamic completions for "::", "?", "$" and "@" operators

Thomas Friedrichsmeier null at kde.org
Wed Oct 5 16:33:03 BST 2022


Git commit adf0cb79f872f9c3e3d76b234eff195f511ebc2c by Thomas Friedrichsmeier.
Committed on 05/10/2022 at 15:18.
Pushed by tfry into branch 'master'.

Add R dynamic completions for "::", "?", "$" and "@" operators

M  +1    -0    ChangeLog
M  +28   -19   rkward/windows/rkcodecompletion.cpp
M  +1    -0    rkward/windows/rkcodecompletion.h

https://invent.kde.org/education/rkward/commit/adf0cb79f872f9c3e3d76b234eff195f511ebc2c

diff --git a/ChangeLog b/ChangeLog
index 0073b4be..568637a5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -15,6 +15,7 @@
 - Fixed: Fix zooming help/output pages with Ctrl+scroll wheel, when compiled with QWebEngine
 - Fixed: Fix problem handling rkward:// links from dialogs on some sytems
 - Fixed: Fix object name completion for (irregular) names starting with numbers or underscores
+- Added: R's dynamic completions (importantly for ":::", "?", and "@") are merged into the already provided completions
 - Added: Provide tooltips on symbols in scripts and R console
 - Added: Many new basic and advanced R, R Markdown and LaTeX snippets
 			- Complex R Markdown template part of the snippets
diff --git a/rkward/windows/rkcodecompletion.cpp b/rkward/windows/rkcodecompletion.cpp
index 6234744b..cf57be07 100644
--- a/rkward/windows/rkcodecompletion.cpp
+++ b/rkward/windows/rkcodecompletion.cpp
@@ -565,20 +565,27 @@ void RKCodeCompletionModel::updateCompletionList(const QString& symbol, bool is_
 
 	// copy the map to two lists. For one thing, we need an int indexable storage, for another, caching this information is safer
 	// in case objects are removed while the completion mode is active.
-	n_completions = matches.size ();
-	icons.clear ();
-	icons.reserve (n_completions);
+	n_completions = matches.size();
+	icons.clear();
+	icons.reserve(n_completions);
+	shortnames.clear();
+	shortnames.reserve(n_completions);
 	names = RObject::getFullNames (matches, RKSettingsModuleCommandEditor::completionSettings()->options());  // NOTE: Intentionally using the script editor completion settings object, here. the completion options are shared with the console!
 	for (int i = 0; i < n_completions; ++i) {
 		icons.append (RKStandardIcons::iconForObject (matches[i]));
+		shortnames.append(matches[i]->getShortName());
 	}
 
-	if ((objectpath.size() == 2 || objectpath.size() == 3) && objectpath.at(1) == ":::") {
-		QStringList shortnames;
-		for (int i = 0; i < matches.count(); ++i) {
-			shortnames.append(matches[i]->getShortName());
+	if ((objectpath.size() == 2 || objectpath.size() == 3) && objectpath.at(1).startsWith("::")) {
+		rcompletions->update(objectpath.at(1), objectpath.at(0), objectpath.value(2), shortnames);
+	} else if (is_help) {
+		rcompletions->update("?", symbol, symbol, shortnames);
+	} else if (objectpath.size() > 1) {
+		QString op = objectpath.at(objectpath.size() - 1 - objectpath.size() % 2);
+		QString start = objectpath.at(objectpath.size() - 2 - objectpath.size() % 2);
+		if (op == "$" || op == "@") {
+			rcompletions->update(op, start, objectpath.size() % 2 ? objectpath.last() : QString(), shortnames);
 		}
-		rcompletions->update(":::", objectpath.at(0), objectpath.value(2), shortnames);
 	}
 
 	current_symbol = symbol;
@@ -591,11 +598,19 @@ void RKCodeCompletionModel::addRCompletions() {
 
 	QStringList addlist = rcompletions->results();
 	if (addlist.isEmpty()) return;
+	bool help_mode = (rcompletions->mode() == "?");
 	beginInsertRows(index(0, 0), n_completions, n_completions + addlist.size());
 	n_completions += addlist.size();
 	for (int i = 0; i < addlist.size(); ++i) {
-		names.append(rcompletions->fragment() + rcompletions->mode() + addlist.at(i));
-		icons.append(RKStandardIcons::getIcon(RKStandardIcons::WindowConsole));
+		if (help_mode) {
+			names.append(addlist.at(i));
+			shortnames.append(addlist.at(i));
+			icons.append(RKStandardIcons::getIcon(RKStandardIcons::WindowHelp));
+		} else {
+			names.append(rcompletions->fragment() + rcompletions->mode() + addlist.at(i));
+			shortnames.append(addlist.at(i));
+			icons.append(RKStandardIcons::getIcon(RKStandardIcons::WindowConsole));
+		}
 	}
 	endInsertRows();
 	manager->modelGainedLateData(this);
@@ -677,15 +692,9 @@ bool RKCodeCompletionModel::partialCompletion(QString *comp, bool* exact_match)
 	// both packageA::object and packageB::object.
 	// Thus as a first step, we look up the short names. We do this, lazily, as this function is only called on demand.
 	QStringList objectpath = RObject::parseObjectPath (current_symbol);
-	if (objectpath.isEmpty () || objectpath[0].isEmpty()) return (false);
-	RObject::ObjectList matches = RObjectList::getObjectList ()->findObjectsMatching (current_symbol);
-
-	QStringList shortnames;
-	for (int i = 0; i < matches.count (); ++i) {
-		shortnames.append (matches[i]->getShortName ());
-	}
-	QString lead = objectpath.last ();
-	if (!shortnames.value (0).startsWith (lead)) lead.clear ();  // This could happen if the current path ends with '$', for instance
+	if (objectpath.isEmpty() || objectpath[0].isEmpty()) return (false);
+	QString lead = objectpath.last();
+	if (!shortnames.value(0).startsWith(lead)) lead.clear ();  // This could happen if the current path ends with '$', for instance
 
 	return findCommonCompletion(comp, shortnames, lead, exact_match);
 }
diff --git a/rkward/windows/rkcodecompletion.h b/rkward/windows/rkcodecompletion.h
index bf0a172c..a7e4c932 100644
--- a/rkward/windows/rkcodecompletion.h
+++ b/rkward/windows/rkcodecompletion.h
@@ -124,6 +124,7 @@ public:
 private:
 	QList<QIcon> icons;
 	QStringList names;
+	QStringList shortnames;
 	QString current_symbol;
 	void fetchRCompletions();
 	QString r_base_symbol;



More information about the rkward-tracker mailing list