From 1251b7c4001b3f7de158c089256b04c887d6c5bb Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 7 Jun 2023 15:13:32 +1000 Subject: [PATCH] Text: always use TEXT_OT_jump_to_file_at_point for jumping to source Now the operator is used for both the internal & external editor, so there is no need for the caller to call this operator only when the preferences are set. --- scripts/startup/bl_operators/text.py | 9 +++ .../editors/interface/interface_ops.cc | 55 +++----------- .../blender/editors/space_text/space_text.c | 1 + .../blender/editors/space_text/text_intern.h | 1 + source/blender/editors/space_text/text_ops.c | 71 +++++++++++++++++++ 5 files changed, 92 insertions(+), 45 deletions(-) diff --git a/scripts/startup/bl_operators/text.py b/scripts/startup/bl_operators/text.py index 03f537ffea0..b6f8d6136a6 100644 --- a/scripts/startup/bl_operators/text.py +++ b/scripts/startup/bl_operators/text.py @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-or-later +import bpy from bpy.types import Operator from bpy.props import ( IntProperty, @@ -32,6 +33,14 @@ class TEXT_OT_jump_to_file_at_point(Operator): text_editor = context.preferences.filepaths.text_editor text_editor_args = context.preferences.filepaths.text_editor_args + # Use the internal text editor. + if not text_editor: + return bpy.ops.text.jump_to_file_at_point_internal( + filepath=self.filepath, + line=self.line, + column=self.column, + ) + if not text_editor_args: self.report( {'ERROR_INVALID_INPUT'}, diff --git a/source/blender/editors/interface/interface_ops.cc b/source/blender/editors/interface/interface_ops.cc index 4f268c5284f..7215d13aa5d 100644 --- a/source/blender/editors/interface/interface_ops.cc +++ b/source/blender/editors/interface/interface_ops.cc @@ -1739,56 +1739,21 @@ void UI_editsource_but_replace(const uiBut *old_but, uiBut *new_but) } static int editsource_text_edit(bContext *C, - wmOperator *op, + wmOperator * /*op*/, const char filepath[FILE_MAX], const int line) { - Main *bmain = CTX_data_main(C); - Text *text = nullptr; + wmOperatorType *ot = WM_operatortype_find("TEXT_OT_jump_to_file_at_point", true); + PointerRNA op_props; - if (U.text_editor[0] != '\0') { - wmOperatorType *ot = WM_operatortype_find("TEXT_OT_jump_to_file_at_point", true); - PointerRNA op_props; + WM_operator_properties_create_ptr(&op_props, ot); + RNA_string_set(&op_props, "filepath", filepath); + RNA_int_set(&op_props, "line", line - 1); + RNA_int_set(&op_props, "column", 0); - WM_operator_properties_create_ptr(&op_props, ot); - RNA_string_set(&op_props, "filepath", filepath); - RNA_int_set(&op_props, "line", line - 1); - RNA_int_set(&op_props, "column", 0); - - int result = WM_operator_name_call_ptr(C, ot, WM_OP_EXEC_DEFAULT, &op_props, NULL); - WM_operator_properties_free(&op_props); - - if (result & OPERATOR_FINISHED) { - return OPERATOR_FINISHED; - } - } - LISTBASE_FOREACH (Text *, text_iter, &bmain->texts) { - if (text_iter->filepath && BLI_path_cmp(text_iter->filepath, filepath) == 0) { - text = text_iter; - break; - } - } - - if (text == nullptr) { - text = BKE_text_load(bmain, filepath, BKE_main_blendfile_path(bmain)); - } - - if (text == nullptr) { - BKE_reportf(op->reports, RPT_WARNING, "File '%s' cannot be opened", filepath); - return OPERATOR_CANCELLED; - } - - txt_move_toline(text, line - 1, false); - - /* naughty!, find text area to set, not good behavior - * but since this is a developer tool lets allow it - campbell */ - if (!ED_text_activate_in_screen(C, text)) { - BKE_reportf(op->reports, RPT_INFO, "See '%s' in the text editor", text->id.name + 2); - } - - WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text); - - return OPERATOR_FINISHED; + int result = WM_operator_name_call_ptr(C, ot, WM_OP_EXEC_DEFAULT, &op_props, NULL); + WM_operator_properties_free(&op_props); + return result; } static int editsource_exec(bContext *C, wmOperator *op) diff --git a/source/blender/editors/space_text/space_text.c b/source/blender/editors/space_text/space_text.c index 037e366ff59..799b043038b 100644 --- a/source/blender/editors/space_text/space_text.c +++ b/source/blender/editors/space_text/space_text.c @@ -205,6 +205,7 @@ static void text_operatortypes(void) WM_operatortype_append(TEXT_OT_replace_set_selected); WM_operatortype_append(TEXT_OT_start_find); + WM_operatortype_append(TEXT_OT_jump_to_file_at_point_internal); WM_operatortype_append(TEXT_OT_to_3d_object); diff --git a/source/blender/editors/space_text/text_intern.h b/source/blender/editors/space_text/text_intern.h index 22c0a123389..8848790e37d 100644 --- a/source/blender/editors/space_text/text_intern.h +++ b/source/blender/editors/space_text/text_intern.h @@ -164,6 +164,7 @@ void TEXT_OT_find(struct wmOperatorType *ot); void TEXT_OT_find_set_selected(struct wmOperatorType *ot); void TEXT_OT_replace(struct wmOperatorType *ot); void TEXT_OT_replace_set_selected(struct wmOperatorType *ot); +void TEXT_OT_jump_to_file_at_point_internal(struct wmOperatorType *ot); /* text_find = open properties, activate search button */ void TEXT_OT_start_find(struct wmOperatorType *ot); diff --git a/source/blender/editors/space_text/text_ops.c b/source/blender/editors/space_text/text_ops.c index 5fa51ec4ab3..d0f622f7ed1 100644 --- a/source/blender/editors/space_text/text_ops.c +++ b/source/blender/editors/space_text/text_ops.c @@ -3836,6 +3836,77 @@ void TEXT_OT_replace_set_selected(wmOperatorType *ot) /** \} */ +/* -------------------------------------------------------------------- */ +/** \name Jump to File at Point (Internal) + * + * \note This is the internal implementation, typically `TEXT_OT_jump_to_file_at_point` + * should be used because it respects the "External Editor" preference. + * \{ */ + +static int text_jump_to_file_at_point_internal_exec(bContext *C, wmOperator *op) +{ + char filepath[FILE_MAX]; + RNA_string_get(op->ptr, "filepath", filepath); + const int line = RNA_int_get(op->ptr, "line"); + const int column = RNA_int_get(op->ptr, "column"); + + Main *bmain = CTX_data_main(C); + Text *text = NULL; + + LISTBASE_FOREACH (Text *, text_iter, &bmain->texts) { + if (text_iter->filepath && BLI_path_cmp(text_iter->filepath, filepath) == 0) { + text = text_iter; + break; + } + } + + if (text == NULL) { + text = BKE_text_load(bmain, filepath, BKE_main_blendfile_path(bmain)); + } + + if (text == NULL) { + BKE_reportf(op->reports, RPT_WARNING, "File '%s' cannot be opened", filepath); + return OPERATOR_CANCELLED; + } + + txt_move_to(text, line, column, false); + + /* naughty!, find text area to set, not good behavior + * but since this is a developer tool lets allow it - campbell */ + if (!ED_text_activate_in_screen(C, text)) { + BKE_reportf(op->reports, RPT_INFO, "See '%s' in the text editor", text->id.name + 2); + } + + WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text); + + return OPERATOR_FINISHED; +} + +void TEXT_OT_jump_to_file_at_point_internal(wmOperatorType *ot) +{ + PropertyRNA *prop; + + /* identifiers */ + ot->name = "Jump to File at Point (Internal)"; + ot->idname = "TEXT_OT_jump_to_file_at_point_internal"; + ot->description = "Jump to a file for the internal text editor"; + + /* api callbacks */ + ot->exec = text_jump_to_file_at_point_internal_exec; + + /* flags */ + ot->flag = 0; + + prop = RNA_def_string(ot->srna, "filepath", NULL, FILE_MAX, "Filepath", ""); + RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE); + prop = RNA_def_int(ot->srna, "line", 0, 0, INT_MAX, "Line", "Line to jump to", 1, 10000); + RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE); + prop = RNA_def_int(ot->srna, "column", 0, 0, INT_MAX, "Column", "Column to jump to", 1, 10000); + RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE); +} + +/** \} */ + /* -------------------------------------------------------------------- */ /** \name Resolve Conflict Operator * \{ */