Browse Source

Merge pull request #98846 from ivanskorikov/autoset-exec-flags

Automatically set `text_editor/external/exec_flags` based on the editor specified in `exec_path` text box
Thaddeus Crews 5 months ago
parent
commit
b73227d2c1
2 changed files with 43 additions and 0 deletions
  1. 42 0
      editor/editor_settings.cpp
  2. 1 0
      editor/editor_settings.h

+ 42 - 0
editor/editor_settings.cpp

@@ -50,6 +50,7 @@
 #include "editor/editor_property_name_processor.h"
 #include "editor/editor_property_name_processor.h"
 #include "editor/editor_translation.h"
 #include "editor/editor_translation.h"
 #include "editor/engine_update_label.h"
 #include "editor/engine_update_label.h"
+#include "modules/regex/regex.h"
 #include "scene/gui/color_picker.h"
 #include "scene/gui/color_picker.h"
 #include "scene/main/node.h"
 #include "scene/main/node.h"
 #include "scene/main/scene_tree.h"
 #include "scene/main/scene_tree.h"
@@ -67,6 +68,13 @@ bool EditorSettings::_set(const StringName &p_name, const Variant &p_value) {
 	bool changed = _set_only(p_name, p_value);
 	bool changed = _set_only(p_name, p_value);
 	if (changed && initialized) {
 	if (changed && initialized) {
 		changed_settings.insert(p_name);
 		changed_settings.insert(p_name);
+		if (p_name == SNAME("text_editor/external/exec_path")) {
+			const StringName exec_args_name = "text_editor/external/exec_flags";
+			const String exec_args_value = _guess_exec_args_for_extenal_editor(p_value);
+			if (!exec_args_value.is_empty() && _set_only(exec_args_name, exec_args_value)) {
+				changed_settings.insert(exec_args_name);
+			}
+		}
 		emit_signal(SNAME("settings_changed"));
 		emit_signal(SNAME("settings_changed"));
 	}
 	}
 	return true;
 	return true;
@@ -1108,6 +1116,40 @@ void EditorSettings::_load_default_visual_shader_editor_theme() {
 	_initial_set("editors/visual_editors/category_colors/particle_color", Color(0.12, 0.358, 0.8));
 	_initial_set("editors/visual_editors/category_colors/particle_color", Color(0.12, 0.358, 0.8));
 }
 }
 
 
+String EditorSettings::_guess_exec_args_for_extenal_editor(const String &p_path) {
+	Ref<RegEx> regex;
+	regex.instantiate();
+
+	const String editor_pattern = R"([\\/]((?:jetbrains\s*)?rider(?:\s*(eap|\d{4}\.\d+|\d{4}\.\d+\s*dev)?)?|visual\s*studio\s*code|subl(ime\s*text)?|sublime_text|(g)?vim|emacs|atom|geany|kate|code|(vs)?codium)(?:\.app|\.exe|\.bat|\.sh)?)";
+	regex->compile(editor_pattern);
+	Ref<RegExMatch> editor_match = regex->search(p_path.to_lower());
+
+	if (editor_match.is_null()) {
+		return String();
+	}
+
+	const String editor = editor_match->get_string(1).to_lower();
+	String new_exec_flags = "{file}";
+
+	if (editor.begins_with("rider")) {
+		new_exec_flags = "{project} --line {line} {file}";
+	} else if (editor == "subl" || editor == "sublime text" || editor == "sublime_text") {
+		new_exec_flags = "{project} {file}:{line}:{column}";
+	} else if (editor == "vim" || editor == "gvim") {
+		new_exec_flags = "\"+call cursor({line}, {col})\" {file}";
+	} else if (editor == "emacs") {
+		new_exec_flags = "emacs +{line}:{col} {file}";
+	} else if (editor == "atom") {
+		new_exec_flags = "{file}:{line}";
+	} else if (editor == "geany" || editor == "kate") {
+		new_exec_flags = "{file} --line {line} --column {col}";
+	} else if (editor == "code" || editor == "visual studio code" || editor == "codium" || editor == "vscodium") {
+		new_exec_flags = "{project} --goto {file}:{line}:{col}";
+	}
+
+	return new_exec_flags;
+}
+
 bool EditorSettings::_save_text_editor_theme(const String &p_file) {
 bool EditorSettings::_save_text_editor_theme(const String &p_file) {
 	String theme_section = "color_theme";
 	String theme_section = "color_theme";
 	Ref<ConfigFile> cf = memnew(ConfigFile); // hex is better?
 	Ref<ConfigFile> cf = memnew(ConfigFile); // hex is better?

+ 1 - 0
editor/editor_settings.h

@@ -122,6 +122,7 @@ private:
 	void _load_default_visual_shader_editor_theme();
 	void _load_default_visual_shader_editor_theme();
 	bool _save_text_editor_theme(const String &p_file);
 	bool _save_text_editor_theme(const String &p_file);
 	bool _is_default_text_editor_theme(const String &p_theme_name);
 	bool _is_default_text_editor_theme(const String &p_theme_name);
+	static String _guess_exec_args_for_extenal_editor(const String &p_value);
 	const String _get_project_metadata_path() const;
 	const String _get_project_metadata_path() const;
 #ifndef DISABLE_DEPRECATED
 #ifndef DISABLE_DEPRECATED
 	void _remove_deprecated_settings();
 	void _remove_deprecated_settings();