diff --git a/lldb/include/lldb/Core/Debugger.h b/lldb/include/lldb/Core/Debugger.h --- a/lldb/include/lldb/Core/Debugger.h +++ b/lldb/include/lldb/Core/Debugger.h @@ -293,9 +293,12 @@ void SetPrompt(const char *) = delete; bool GetUseExternalEditor() const; - bool SetUseExternalEditor(bool use_external_editor_p); + llvm::StringRef GetExternalEditor() const; + + bool SetExternalEditor(llvm::StringRef editor); + bool GetUseColor() const; bool SetUseColor(bool use_color); diff --git a/lldb/include/lldb/Host/Host.h b/lldb/include/lldb/Host/Host.h --- a/lldb/include/lldb/Host/Host.h +++ b/lldb/include/lldb/Host/Host.h @@ -236,7 +236,8 @@ bool run_in_shell = true, bool hide_stderr = false); - static llvm::Error OpenFileInExternalEditor(const FileSpec &file_spec, + static llvm::Error OpenFileInExternalEditor(llvm::StringRef editor, + const FileSpec &file_spec, uint32_t line_no); /// Check if we're running in an interactive graphical session. diff --git a/lldb/source/Core/CoreProperties.td b/lldb/source/Core/CoreProperties.td --- a/lldb/source/Core/CoreProperties.td +++ b/lldb/source/Core/CoreProperties.td @@ -135,6 +135,10 @@ Global, DefaultFalse, Desc<"Whether to use an external editor or not.">; + def ExternalEditor: Property<"external-editor", "String">, + Global, + DefaultStringValue<"">, + Desc<"External editor to use when use-external-editor is enabled.">; def UseColor: Property<"use-color", "Boolean">, Global, DefaultTrue, diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp --- a/lldb/source/Core/Debugger.cpp +++ b/lldb/source/Core/Debugger.cpp @@ -372,6 +372,16 @@ return m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b); } +llvm::StringRef Debugger::GetExternalEditor() const { + const uint32_t idx = ePropertyExternalEditor; + return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, ""); +} + +bool Debugger::SetExternalEditor(llvm::StringRef editor) { + const uint32_t idx = ePropertyExternalEditor; + return m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, editor); +} + bool Debugger::GetUseColor() const { const uint32_t idx = ePropertyUseColor; return m_collection_sp->GetPropertyAtIndexAsBoolean( diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp --- a/lldb/source/Host/common/Host.cpp +++ b/lldb/source/Host/common/Host.cpp @@ -546,7 +546,8 @@ #endif #if !defined(__APPLE__) -llvm::Error Host::OpenFileInExternalEditor(const FileSpec &file_spec, +llvm::Error Host::OpenFileInExternalEditor(llvm::StringRef editor, + const FileSpec &file_spec, uint32_t line_no) { return llvm::errorCodeToError( std::error_code(ENOTSUP, std::system_category())); diff --git a/lldb/source/Host/macosx/objcxx/Host.mm b/lldb/source/Host/macosx/objcxx/Host.mm --- a/lldb/source/Host/macosx/objcxx/Host.mm +++ b/lldb/source/Host/macosx/objcxx/Host.mm @@ -325,7 +325,8 @@ #endif // TARGET_OS_OSX -llvm::Error Host::OpenFileInExternalEditor(const FileSpec &file_spec, +llvm::Error Host::OpenFileInExternalEditor(llvm::StringRef editor, + const FileSpec &file_spec, uint32_t line_no) { #if !TARGET_OS_OSX return llvm::errorCodeToError( @@ -391,41 +392,36 @@ auto on_exit = llvm::make_scope_exit( [&]() { AEDisposeDesc(&(file_and_line_desc.descContent)); }); - static std::optional g_app_fsref; - static std::string g_app_error; - static std::once_flag g_once_flag; - std::call_once(g_once_flag, [&]() { - if (const char *external_editor = ::getenv("LLDB_EXTERNAL_EDITOR")) { - LLDB_LOG(log, "Looking for external editor: {0}", external_editor); - - FSRef app_fsref; - CFCString editor_name(external_editor, kCFStringEncodingUTF8); - long app_error = ::LSFindApplicationForInfo( - /*inCreator=*/kLSUnknownCreator, /*inBundleID=*/NULL, - /*inName=*/editor_name.get(), /*outAppRef=*/&app_fsref, - /*outAppURL=*/NULL); - if (app_error == noErr) { - g_app_fsref = app_fsref; - } else { - g_app_error = - llvm::formatv("could not find external editor \"{0}\": " - "LSFindApplicationForInfo returned error {1}", - external_editor, app_error) - .str(); - } - } - }); + if (editor.empty()) { + if (const char *lldb_external_editor = ::getenv("LLDB_EXTERNAL_EDITOR")) + editor = lldb_external_editor; + } - if (!g_app_error.empty()) - return llvm::createStringError(llvm::inconvertibleErrorCode(), g_app_error); + std::optional app_fsref; + if (!editor.empty()) { + LLDB_LOG(log, "Looking for external editor: {0}", editor); + + app_fsref.emplace(); + CFCString editor_name(editor.data(), kCFStringEncodingUTF8); + long app_error = ::LSFindApplicationForInfo( + /*inCreator=*/kLSUnknownCreator, /*inBundleID=*/NULL, + /*inName=*/editor_name.get(), /*outAppRef=*/&(*app_fsref), + /*outAppURL=*/NULL); + if (app_error != noErr) + return llvm::createStringError( + llvm::inconvertibleErrorCode(), + llvm::formatv("could not find external editor \"{0}\": " + "LSFindApplicationForInfo returned error {1}", + editor, app_error)); + } // Build app launch parameters. LSApplicationParameters app_params; ::memset(&app_params, 0, sizeof(app_params)); app_params.flags = kLSLaunchDefaults | kLSLaunchDontAddToRecents | kLSLaunchDontSwitch; - if (g_app_fsref) - app_params.application = &(g_app_fsref.value()); + if (app_fsref) + app_params.application = &(*app_fsref); ProcessSerialNumber psn; std::array file_array = {file_URL.get()}; diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp --- a/lldb/source/Interpreter/CommandInterpreter.cpp +++ b/lldb/source/Interpreter/CommandInterpreter.cpp @@ -3272,7 +3272,8 @@ const FileSpec file_spec; error = file->GetFileSpec(const_cast(file_spec)); if (error.Success()) { - if (llvm::Error e = Host::OpenFileInExternalEditor(file_spec, 1)) + if (llvm::Error e = Host::OpenFileInExternalEditor( + m_debugger.GetExternalEditor(), file_spec, 1)) result.AppendError(llvm::toString(std::move(e))); } } diff --git a/lldb/source/Target/Thread.cpp b/lldb/source/Target/Thread.cpp --- a/lldb/source/Target/Thread.cpp +++ b/lldb/source/Target/Thread.cpp @@ -303,10 +303,12 @@ bool already_shown = false; SymbolContext frame_sc( frame_sp->GetSymbolContext(eSymbolContextLineEntry)); - if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && - frame_sc.line_entry.file && frame_sc.line_entry.line != 0) { + const Debugger &debugger = GetProcess()->GetTarget().GetDebugger(); + if (debugger.GetUseExternalEditor() && frame_sc.line_entry.file && + frame_sc.line_entry.line != 0) { if (llvm::Error e = Host::OpenFileInExternalEditor( - frame_sc.line_entry.file, frame_sc.line_entry.line)) { + debugger.GetExternalEditor(), frame_sc.line_entry.file, + frame_sc.line_entry.line)) { LLDB_LOG_ERROR(GetLog(LLDBLog::Host), std::move(e), "OpenFileInExternalEditor failed: {0}"); } else { @@ -1731,6 +1733,7 @@ frame_sp->GetSymbolContext(eSymbolContextLineEntry)); if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file) { if (llvm::Error e = Host::OpenFileInExternalEditor( + target->GetDebugger().GetExternalEditor(), frame_sc.line_entry.file, frame_sc.line_entry.line)) { LLDB_LOG_ERROR(GetLog(LLDBLog::Host), std::move(e), "OpenFileInExternalEditor failed: {0}"); diff --git a/lldb/test/Shell/Settings/TestExternalEditor.test b/lldb/test/Shell/Settings/TestExternalEditor.test new file mode 100644 --- /dev/null +++ b/lldb/test/Shell/Settings/TestExternalEditor.test @@ -0,0 +1,4 @@ +REQUIRES: system-darwin +RUN: %lldb -o 'settings set use-external-editor true' -o 'setting set external-editor foo' -o 'session save' -b 2>&1 | FileCheck %s +RUN: LLDB_EXTERNAL_EDITOR="foo" %lldb -o 'settings set use-external-editor true' -o 'session save' -b 2>&1 | FileCheck %s +CHECK: error: could not find external editor "foo": LSFindApplicationForInfo returned error