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.
This commit is contained in:
Campbell Barton
2023-06-07 15:13:32 +10:00
parent 0a891184fa
commit 1251b7c400
5 changed files with 92 additions and 45 deletions

View File

@@ -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'},

View File

@@ -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)

View File

@@ -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);

View File

@@ -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);

View File

@@ -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
* \{ */