[rkward/work/new_completion_to_console] /: Add a new set of helper classes to reduce the amount of boilerplate code involved in adding settings.

Thomas Friedrichsmeier null at kde.org
Sat May 2 09:48:04 BST 2020


Git commit f41cef5ccd1404deafa2591bae89ba2a18ca821b by Thomas Friedrichsmeier.
Committed on 02/05/2020 at 08:47.
Pushed by tfry into branch 'work/new_completion_to_console'.

Add a new set of helper classes to reduce the amount of boilerplate code involved in adding settings.

Also this provides to change the default for a single repeated setting, easily (as for "tab-key invokes completions").

For now use for the code completion settings, only.

M  +2    -3    ChangeLog
M  +64   -0    rkward/settings/rksettingsmodule.h
M  +5    -34   rkward/settings/rksettingsmodulecommandeditor.cpp
M  +16   -11   rkward/settings/rksettingsmodulecommandeditor.h
M  +1    -0    rkward/settings/rksettingsmoduleconsole.cpp

https://commits.kde.org/rkward/f41cef5ccd1404deafa2591bae89ba2a18ca821b

diff --git a/ChangeLog b/ChangeLog
index cff680ca..c307d929 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,9 +5,8 @@
     - Should all KIO-schemes be supported (not too hard to do, help:/ already supported)?
     - Support pdf?
 - <text> elements in plugins may now also contain clickable links, including rkward://-scheme links
-* TODO: Bring new code hinting features to the console window!
-  - Tab key-option should default to on in console, but not script editor
-  - Clean up unused code
+- The new code hinting features from version 0.7.1 are now also available in the console
+  * TODO: Clean up unused code
 - On unix-systems, RKWard can now be run without installation
   - TODO: common.js is not found in plugins!
 - Kate addons are now supported within RKWard. Intially, search-in-files, snippets, and projects are loaded by default
diff --git a/rkward/settings/rksettingsmodule.h b/rkward/settings/rksettingsmodule.h
index 91ed7df5..e4b392b4 100644
--- a/rkward/settings/rksettingsmodule.h
+++ b/rkward/settings/rksettingsmodule.h
@@ -26,6 +26,70 @@ class KConfig;
 class RKSettings;
 class RCommandChain;
 
+/** Base class for RKWard config settings.
+ *
+ *  Meant to reduce the amount of boilerplate code involved in defining, loading, saving config settings (and in the future probably also creating widgets and applying changes). */
+class RKConfigBase {
+public:
+	virtual void loadConfig(KConfigGroup &cg) = 0;
+	virtual void saveConfig(KConfigGroup &cg) const = 0;
+protected:
+	RKConfigBase(const char* name) : name(name) {};
+	virtual ~RKConfigBase() {};
+	const char* name;
+};
+
+/** A single value stored in the RKWard config file.
+ *
+ *  The value set initially (in the constructor or via setDefaultValue() represents the default value. */
+template<typename T> class RKConfigValue : public RKConfigBase {
+public:
+	RKConfigValue(const char* name, const T &default_value) : RKConfigBase(name), value(default_value) {};
+	~RKConfigValue() {};
+
+	void loadConfig(KConfigGroup &cg) override {
+		value = cg.readEntry(name, value);
+	}
+	void saveConfig(KConfigGroup &cg) const override {
+		cg.writeEntry(name, value);
+	}
+	void setDefaultValue(const T& value) { RKConfigValue<T>::value = value; }
+	operator T() const { return(value); }
+	RKConfigValue& operator= (const T v) { value = v; return *this; };
+private:
+	T value;
+};
+
+#include <vector>
+#include <initializer_list>
+/** A group of values stored in the RKWard config file.
+ *
+ *  If a name is given and non-empty, loadConfig()/saveConfig() will load from/save to a sub config-group by that name. */
+class RKConfigGroup : public RKConfigBase {
+public:
+	RKConfigGroup(const char *name, std::initializer_list<RKConfigBase*> values) : RKConfigBase(name),
+		values(values) {};
+	template<typename T> RKConfigGroup(const char *name, size_t count, RKConfigValue<T>* _values) : RKConfigBase(name),
+		values(count) { for (size_t i = 0; i < count; ++i) values[i] = (_values + i); };
+	~RKConfigGroup() {};
+	void loadConfig(KConfigGroup &cg) override {
+		KConfigGroup lcg = cg;
+		if (name && name[0]) {
+			lcg = cg.group(name);
+		}
+		for (auto it = values.begin(); it != values.end(); ++it) (*it)->loadConfig(lcg);
+	}
+	void saveConfig(KConfigGroup &cg) const override {
+		KConfigGroup lcg = cg;
+		if (name && name[0]) {
+			lcg = cg.group(name);
+		}
+		for (auto it = values.begin(); it != values.end(); ++it) (*it)->saveConfig(lcg);
+	}
+private:
+	std::vector<RKConfigBase*> values;
+};
+
 /**
 Base class for settings modules. Provides some pure virtual calls.
 
diff --git a/rkward/settings/rksettingsmodulecommandeditor.cpp b/rkward/settings/rksettingsmodulecommandeditor.cpp
index 11ae8ce9..7f4166c5 100644
--- a/rkward/settings/rksettingsmodulecommandeditor.cpp
+++ b/rkward/settings/rksettingsmodulecommandeditor.cpp
@@ -139,11 +139,11 @@ void RKCodeCompletionSettingsWidget::applyChanges() {
 
 	if (show_common) {
 		settings->completion_options = 0;
-		if (completion_list_member_operator_box->currentIndex () == 0) settings->completion_options += RObject::DollarExpansion;
-		if (completion_slot_operator_box->currentIndex () == 1) settings->completion_options += RObject::ExplicitSlotsExpansion;
-		if (completion_object_qualification_box->currentIndex () == 2) settings->completion_options += RObject::IncludeEnvirForGlobalEnv | RObject::IncludeEnvirIfNotGlobalEnv;
-		else if (completion_object_qualification_box->currentIndex () == 1) settings->completion_options += RObject::IncludeEnvirIfNotGlobalEnv;
-		else settings->completion_options += RObject::IncludeEnvirIfMasked;
+		if (completion_list_member_operator_box->currentIndex () == 0) settings->completion_options = settings->completion_options + RObject::DollarExpansion;
+		if (completion_slot_operator_box->currentIndex () == 1) settings->completion_options = settings->completion_options + RObject::ExplicitSlotsExpansion;
+		if (completion_object_qualification_box->currentIndex () == 2) settings->completion_options = settings->completion_options + (RObject::IncludeEnvirForGlobalEnv | RObject::IncludeEnvirIfNotGlobalEnv);
+		else if (completion_object_qualification_box->currentIndex () == 1) settings->completion_options = settings->completion_options + RObject::IncludeEnvirIfNotGlobalEnv;
+		else settings->completion_options = settings->completion_options + RObject::IncludeEnvirIfMasked;
 	}
 }
 
@@ -250,20 +250,6 @@ QString completionTypeToConfigKey (int cat) {
 	return QString ();
 }
 
-void RKCodeCompletionSettings::saveSettings(KConfigGroup& cg) {
-	RK_TRACE (SETTINGS);
-	cg.writeEntry ("Completion enabled", auto_completion_enabled);
-	cg.writeEntry ("Completion min chars", auto_completion_min_chars);
-	cg.writeEntry ("Completion timeout", auto_completion_timeout);
-	cg.writeEntry ("Auto completion on cursor navigation", auto_completion_cursor_activated);
-	cg.writeEntry ("Completion option flags", completion_options);
-	cg.writeEntry ("Cursor navigate completions", cursor_navigates_completions);
-	cg.writeEntry ("Tabkey invokes completion", tabkey_invokes_completion);
-	for (int i = 0; i < N_COMPLETION_CATEGORIES; ++i) {
-		cg.writeEntry (completionTypeToConfigKey (i), completion_type_enabled[i]);
-	}
-}
-
 void RKSettingsModuleCommandEditor::saveSettings (KConfig *config) {
 	RK_TRACE (SETTINGS);
 
@@ -278,21 +264,6 @@ void RKSettingsModuleCommandEditor::saveSettings (KConfig *config) {
 	cg.writeEntry ("Script file filter", script_file_filter);
 }
 
-void RKCodeCompletionSettings::loadSettings(KConfigGroup& cg) {
-	RK_TRACE (SETTINGS);
-
-	auto_completion_enabled = cg.readEntry ("Completion enabled", true);
-	auto_completion_min_chars = cg.readEntry ("Completion min chars", 2);
-	auto_completion_timeout = cg.readEntry ("Completion timeout", 250);
-	auto_completion_cursor_activated = cg.readEntry ("Auto completion on cursor navigation", false);
-	completion_options = cg.readEntry ("Completion option flags", (int) RObject::IncludeEnvirIfMasked);
-	cursor_navigates_completions = cg.readEntry ("Cursor navigate completions", false);
-	tabkey_invokes_completion = cg.readEntry ("Tabkey invokes completion", false);
-	for (int i = 0; i < N_COMPLETION_CATEGORIES; ++i) {
-		completion_type_enabled[i] = cg.readEntry (completionTypeToConfigKey (i), true);
-	}
-}
-
 void RKSettingsModuleCommandEditor::loadSettings (KConfig *config) {
 	RK_TRACE (SETTINGS);
 
diff --git a/rkward/settings/rksettingsmodulecommandeditor.h b/rkward/settings/rksettingsmodulecommandeditor.h
index 2112d6bc..d7fae4ed 100644
--- a/rkward/settings/rksettingsmodulecommandeditor.h
+++ b/rkward/settings/rksettingsmodulecommandeditor.h
@@ -2,7 +2,7 @@
                           rksettingsmodulecommandeditor  -  description
                              -------------------
     begin                : Tue Oct 23 2007
-    copyright            : (C) 2007-2019 by Thomas Friedrichsmeier
+    copyright            : (C) 2007-2020 by Thomas Friedrichsmeier
     email                : thomas.friedrichsmeier at kdemail.net
  ***************************************************************************/
 
@@ -18,6 +18,7 @@
 #define RKSETTINGSMODULECOMMANDEDITOR_H
 
 #include "rksettingsmodule.h"
+#include "../core/robject.h"
 
 class RKSpinBox;
 class QCheckBox;
@@ -32,9 +33,10 @@ public:
 	RKCodeCompletionSettings() {};
 	~RKCodeCompletionSettings() {};
 
-	void loadSettings(KConfigGroup &config);
-	void saveSettings(KConfigGroup &config);
+	void loadSettings(KConfigGroup &config) { group.loadConfig(config); };
+	void saveSettings(KConfigGroup &config) { group.saveConfig(config); };
 
+	// NOTE: Don't insert values inbetween existing values, without also adjusting the sloppy config load/save/apply code
 	enum CompletionCategories {
 		Calltip = 0,
 		Arghint,
@@ -56,14 +58,17 @@ public:
 	bool tabKeyInvokesCompletion() const { return tabkey_invokes_completion; };
 private:
 friend class RKCodeCompletionSettingsWidget;
-	int auto_completion_min_chars;
-	int auto_completion_timeout;
-	bool auto_completion_enabled;
-	bool auto_completion_cursor_activated;
-	bool tabkey_invokes_completion;
-	bool completion_type_enabled[N_COMPLETION_CATEGORIES];
-	bool cursor_navigates_completions;
-	int completion_options;
+friend class RKSettingsModuleConsole;
+	RKConfigValue<bool> auto_completion_enabled {"Completion enabled", true};
+	RKConfigValue<int> auto_completion_min_chars {"Completion min chars", 2};
+	RKConfigValue<int> auto_completion_timeout {"Completion timeout", 250};
+	RKConfigValue<bool> auto_completion_cursor_activated {"Auto completion on cursor navigation", false};
+	RKConfigValue<bool> tabkey_invokes_completion {"Tabkey invokes completion", false};
+	RKConfigValue<bool> completion_type_enabled[N_COMPLETION_CATEGORIES] {{"Calltips", true}, {"Argument completion", true}, {"Object completion", true}, {"Filename completion", true}, {"Auto word completion", true}};
+	RKConfigValue<bool> cursor_navigates_completions {"Cursor navigate completions", false};
+	RKConfigValue<int> completion_options {"Completion option flags", (int) RObject::IncludeEnvirIfMasked};
+	RKConfigGroup dummyoptions = RKConfigGroup(0, N_COMPLETION_CATEGORIES, completion_type_enabled);
+	RKConfigGroup group {"Completion", { &dummyoptions, &auto_completion_enabled, &auto_completion_min_chars, &auto_completion_timeout, &auto_completion_cursor_activated, &tabkey_invokes_completion, &cursor_navigates_completions, &completion_options }};
 };
 
 class RKCodeCompletionSettingsWidget : public RKSettingsModuleWidget {
diff --git a/rkward/settings/rksettingsmoduleconsole.cpp b/rkward/settings/rksettingsmoduleconsole.cpp
index db6774d9..816a382a 100644
--- a/rkward/settings/rksettingsmoduleconsole.cpp
+++ b/rkward/settings/rksettingsmoduleconsole.cpp
@@ -145,6 +145,7 @@ void RKSettingsModuleConsole::loadSettings (KConfig *config) {
 	pipe_user_commands_through_console = cg.readEntry ("pipe user commands through console", true);
 	add_piped_commands_to_history = (PipedCommandsHistoryMode) cg.readEntry ("add piped commands to history", (int) AddSingleLine);
 	context_sensitive_history_by_default = cg.readEntry ("command history defaults to context sensitive", false);
+	completion_settings.tabkey_invokes_completion = true;
 	completion_settings.loadSettings(cg);
 }
 



More information about the rkward-tracker mailing list