Skip to content

Commit

Permalink
added git show hash (#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonvw authored Feb 14, 2025
1 parent 2e00b55 commit e8c691c
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
- added set field_separator to listview
- added revisions_dialog to git show
- added git grep when text is selected to context menu
- added git show to log file

### Changed

Expand Down
5 changes: 5 additions & 0 deletions include/wex/del/frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ class frame : public wex::frame
int event_id,
const std::vector<wex::path>& paths,
const data::window& arg = data::window()) override;
bool vcs_execute(
const std::string& command,
const std::vector<wex::path>& paths,
const data::window& arg = data::window()) override;

bool vcs_unified_diff(const vcs_entry* e, const unified_diff* uni) override;

bool vi_is_address(syntax::stc* stc, const std::string& text) const override;
Expand Down
3 changes: 2 additions & 1 deletion include/wex/stc/bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Name: bind.h
// Purpose: Declaration of bind id's
// Author: Anton van Wezenbeek
// Copyright: (c) 2020-2024 Anton van Wezenbeek
// Copyright: (c) 2020-2025 Anton van Wezenbeek
////////////////////////////////////////////////////////////////////////////////

#pragma once
Expand Down Expand Up @@ -37,6 +37,7 @@ enum stc
open_link,
open_mime,
open_www,
show_hash,
show_properties,
toggle_fold,
unfold_all,
Expand Down
12 changes: 12 additions & 0 deletions include/wex/ui/frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ class frame : public factory::frame
return false;
}

/// Executes vcs.
virtual bool vcs_execute(
/// the vcs command
const std::string& command,
/// the paths
const std::vector<wex::path>& paths,
/// window data
const data::window& arg = data::window())
{
return false;
}

/// Executes a ex command. Returns true if
/// this command is handled. This method is invoked
/// at the beginning of the ex command handling,
Expand Down
14 changes: 14 additions & 0 deletions src/del/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,20 @@ bool wex::del::frame::vcs_execute(
return wex::vcs_execute(this, event_id, paths, data);
}

bool wex::del::frame::vcs_execute(
const std::string& command,
const std::vector<wex::path>& paths,
const data::window& data)
{
if (wex::vcs vcs(paths); vcs.execute(command))
{
open_file_vcs(path(command), vcs.entry(), data);
return true;
}

return false;
}

bool wex::del::frame::vcs_unified_diff(
const vcs_entry* entry,
const unified_diff* diff)
Expand Down
47 changes: 43 additions & 4 deletions src/stc/bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@

namespace wex
{
bool do_show_hash(stc* stc, const std::function<void(const std::string&)>& f)
{
const auto line(stc->GetLineText(stc->GetCurrentLine()));

if (regex r("commit ([a-z[0-9]+$)"); r.match(line) > 0)
{
if (f)
{
f(r[0]);
}

return true;
}

return false;
}

void edit_control_char(stc* stc)
{
if (stc->GetSelectedText().length() > 2)
Expand Down Expand Up @@ -424,6 +441,19 @@ void wex::stc::bind_all()
},
id::stc::open_mime},

{[=, this](const wxCommandEvent& event)
{
do_show_hash(
this,
[&, this](const std::string& hash)
{
m_frame->vcs_execute(
"show " + hash,
std::vector<wex::path>{m_data.head_path()});
});
},
id::stc::show_hash},

{[=, this](const wxCommandEvent& event)
{
const auto level = GetFoldLevel(get_current_line());
Expand Down Expand Up @@ -500,11 +530,20 @@ void wex::stc::build_popup_menu(menu& menu)
m_frame->debug_add_menu(menu, true);
}

if (
m_data.menu().test(data::stc::MENU_VCS) && path().file_exists() &&
m_frame->vcs_dir_exists(path()))
if (m_data.menu().test(data::stc::MENU_VCS))
{
menu.append({{}, {path(), m_frame}});
if (get_lexer().scintilla_lexer() == "yaml")
{
if (do_show_hash(this, nullptr))
{
menu.append({{}, {id::stc::show_hash, _("show hash")}});
}
}

if (path().file_exists() && m_frame->vcs_dir_exists(path()))
{
menu.append({{}, {path(), m_frame}});
}
}

if (!get_vi().is_active() && GetTextLength() > 0)
Expand Down
11 changes: 11 additions & 0 deletions test/del/test-frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,23 @@ TEST_CASE("wex::del::frame")

SUBCASE("vcs_execute")
{
REQUIRE(!del_frame()->vcs_execute(55, std::vector<wex::path>()));

wex::data::window data;
data.button(wxOK | wxCANCEL | wxAPPLY);
const int ID_VCS_LOG = 11; // in wex-menus.xml
REQUIRE(del_frame()
->vcs_execute(ID_VCS_LOG, {wex::test::get_path("test.h")}, data));
del_frame()->vcs_destroy_dialog();

{
wex::log_none off;
REQUIRE(!del_frame()->vcs_execute("shows", std::vector<wex::path>()));
}

REQUIRE(del_frame()->vcs_execute(
"show",
std::vector<wex::path>{wex::test::get_path()}));
}

SUBCASE("virtual")
Expand Down
9 changes: 7 additions & 2 deletions test/ui/test-frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Name: test-frame.cpp
// Purpose: Implementation for wex unit testing
// Author: Anton van Wezenbeek
// Copyright: (c) 2015-2024 Anton van Wezenbeek
// Copyright: (c) 2015-2025 Anton van Wezenbeek
////////////////////////////////////////////////////////////////////////////////

#include <wx/listctrl.h>
Expand Down Expand Up @@ -162,7 +162,12 @@ TEST_CASE("wex::frame")

REQUIRE(!frame()->vcs_dir_exists(wex::test::get_path()));

frame()->vcs_execute(55, std::vector<wex::path>{wex::test::get_path()});
REQUIRE(
!frame()->vcs_execute(55, std::vector<wex::path>{wex::test::get_path()}));

REQUIRE(!frame()->vcs_execute(
"show",
std::vector<wex::path>{wex::test::get_path()}));

auto* stc = new wex::test::ui_stc();
REQUIRE(!frame()->pane_add(stc).empty());
Expand Down

0 comments on commit e8c691c

Please sign in to comment.