Skip to content

Commit

Permalink
allow git show on stc for statusbar git as well
Browse files Browse the repository at this point in the history
  • Loading branch information
antonvw committed Nov 10, 2024
1 parent d439bc1 commit 326539d
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 40 deletions.
10 changes: 6 additions & 4 deletions include/wex/vcs/unified-diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class unified_diff
/// The path (under vcs).
const path& p,
/// Provide vcs entry to use the process std out as input.
vcs_entry* entry,
const vcs_entry* entry,
/// Provide frame, that will receive the unified diff callbacks.
factory::frame* f);

Expand Down Expand Up @@ -82,10 +82,12 @@ class unified_diff
std::array<path, 2> m_path;
std::array<std::vector<std::string>, 2> m_text;

vcs_entry* m_vcs_entry{nullptr};
factory::frame* m_frame{nullptr};
path m_path_vcs;

const vcs_entry* m_vcs_entry{nullptr};
factory::frame* m_frame{nullptr};

const path m_path_toplevel;
const std::string m_input;
const path m_path_vcs;
};
}; // namespace wex
48 changes: 30 additions & 18 deletions src/del/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ const std::string find_replace_string(bool replace)
const std::string get_some_text(const std::vector<std::string>& text)
{
std::stringstream ss;
ss << "deleted " << text.size() << " lines";
const std::string info(!text.front().empty() ? text.front() : std::string());
ss << "deleted " << text.size() << " lines" << info;
return ss.str();
}

Expand Down Expand Up @@ -920,28 +921,39 @@ bool wex::del::frame::vcs_unified_diff(
const vcs_entry* entry,
const unified_diff* diff)
{
if (auto* sf = dynamic_cast<syntax::stc*>(open_file(diff->path_vcs()));
sf != nullptr && diff->range_from_count() > 0)
if (!diff->path_vcs().file_exists())
{
// deleted text, just a marker, and annotation with text
sf->MarkerAdd(diff->range_from_start() - 1, m_marker_del.number());
return false;
}

if (!diff->text_removed().empty())
if (auto* stc = dynamic_cast<syntax::stc*>(open_file(diff->path_vcs()));
stc != nullptr)
{
if (diff->range_from_count() > 0)
{
sf->AnnotationSetText(
diff->range_from_start() - 1,
get_some_text(diff->text_removed()));
// deleted text, just a marker, and annotation with text
stc->MarkerAdd(diff->range_from_start() - 1, m_marker_del.number());

if (!diff->text_removed().empty())
{
stc->AnnotationSetText(
diff->range_from_start() - 1,
get_some_text(diff->text_removed()));
}
}
}

if (auto* st = dynamic_cast<syntax::stc*>(open_file(diff->path_vcs()));
st != nullptr)
{
st->set_indicator(
m_indicator_add,
st->PositionFromLine(diff->range_to_start() - 1),
st->GetLineEndPosition(
diff->range_to_start() - 2 + diff->range_to_count()));
if (diff->range_to_count() > 0)
{
if (!stc->set_indicator(
m_indicator_add,
stc->PositionFromLine(diff->range_to_start() - 1),
stc->GetLineEndPosition(
diff->range_to_start() - 2 + diff->range_to_count())))
{
log("vcs_unified_diff") << diff->path_vcs().string();
return false;
}
}
}

return true;
Expand Down
52 changes: 36 additions & 16 deletions src/vcs/unified-diff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <wex/factory/frame.h>
#include <wex/vcs/unified-diff.h>
#include <wex/vcs/vcs-entry.h>
#include <wex/vcs/vcs.h>

#include <iostream>

Expand All @@ -36,12 +37,12 @@
NEXT_TOKEN

#define SKIP_LINES \
for (int i = 0; i < 2; i++) \
while (tok_iter != tokens.end()) \
{ \
if (++tok_iter == tokens.end()) \
NEXT_TOKEN \
if (!tok_iter->starts_with("diff ") && !tok_iter->starts_with("index ")) \
{ \
log("unified_diff") << "missing lines"; \
return std::nullopt; \
break; \
} \
}

Expand All @@ -59,10 +60,14 @@ wex::unified_diff::unified_diff(const std::string& input)
m_range.fill({0});
}

wex::unified_diff::unified_diff(const path& p, vcs_entry* e, factory::frame* f)
wex::unified_diff::unified_diff(
const path& p,
const vcs_entry* e,
factory::frame* f)
: m_path_vcs(p)
, m_input(e->std_out())
, m_frame(f)
, m_path_toplevel(vcs().toplevel())
, m_vcs_entry(e)
{
m_range.fill({0});
Expand All @@ -87,21 +92,21 @@ std::optional<int> wex::unified_diff::parse()
HEADER_LINES("--- a/(.*)", m_path[0]);
HEADER_LINES("\\+\\+\\+ b/(.*)", m_path[1]);

// Next come one or more hunks of differences
// Next come one or more chunks of differences
while (tok_iter != tokens.end())
{
if (regex r_hunk("@@ -([0-9]+),?([0-9]*) \\+([0-9]+),?([0-9]*) @@.*");
r_hunk.match(*tok_iter) != 4)
if (regex r_chunk("@@ -([0-9]+),?([0-9]*) \\+([0-9]+),?([0-9]*) @@.*");
r_chunk.match(*tok_iter) != 4)
{
log("unified_diff") << *tok_iter << r_hunk.size();
log("unified_diff") << *tok_iter << r_chunk.size();
return std::nullopt;
}
else
{
m_range[0] = wex::stoi(r_hunk[0]);
m_range[1] = wex::stoi(r_hunk[1]);
m_range[2] = wex::stoi(r_hunk[2]);
m_range[3] = wex::stoi(r_hunk[3]);
m_range[0] = wex::stoi(r_chunk[0]);
m_range[1] = wex::stoi(r_chunk[1]);
m_range[2] = wex::stoi(r_chunk[2]);
m_range[3] = wex::stoi(r_chunk[3]);
}

diffs++;
Expand All @@ -112,14 +117,29 @@ std::optional<int> wex::unified_diff::parse()
CHANGES_LINES(1, 0);
CHANGES_LINES(3, 1);

if (m_frame != nullptr && !m_frame->vcs_unified_diff(m_vcs_entry, this))
if (m_vcs_entry != nullptr)
{
return std::nullopt;
m_path_vcs = path(m_path_toplevel).append(m_path[0]);

if (!m_path_vcs.dir_exists())
{
if (!m_path_vcs.file_exists())
{
log("unified_diff") << m_path_vcs.string() << "does not exist";
return std::nullopt;
}

if (
m_frame != nullptr && !m_frame->vcs_unified_diff(m_vcs_entry, this))
{
return std::nullopt;
}
}
}

if (++tok_iter != tokens.end() && !(*tok_iter).starts_with("@@"))
{
break; // this was last hunk, continue with header lines
break; // this was last chunk, continue with header lines
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/vcs/vcs-entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <wex/syntax/path-lexer.h>
#include <wex/ui/frame.h>
#include <wex/ui/menus.h>
#include <wex/vcs/unified-diff.h>
#include <wex/vcs/vcs-entry.h>
#include <wx/app.h>

Expand Down Expand Up @@ -240,6 +241,13 @@ void wex::vcs_entry::show_output(const std::string& caption) const
{
vcs_command_stc(get_command(), m_lexer, get_shell());
}

if (get_command().get_command() == "diff")
{
auto* frame = dynamic_cast<wex::frame*>(wxTheApp->GetTopWindow());
unified_diff diff(path(), this, frame);
diff.parse();
}
}

if (get_shell() != nullptr)
Expand Down
3 changes: 1 addition & 2 deletions src/vcs/vcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,7 @@ bool wex::vcs_execute(
{
if (vcs.entry().get_command().get_command() == "diff")
{
unified_diff diff(it, &vcs.entry(), frame);
diff.parse();
unified_diff(it, &vcs.entry(), frame).parse();
}
else
{
Expand Down
40 changes: 40 additions & 0 deletions test/vcs/test-unified-diff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,44 @@ TEST_CASE("wex::unified_diff")
REQUIRE(uni.text_added().front() == "- test");
REQUIRE(uni.text_removed().empty());
}

SUBCASE("parse-valid-other")
{
wex::unified_diff uni(
"diff --git a/external/pugixml b/external/pugixml\n"
"--- a/external/pugixml\n"
"+++ b/external/pugixml\n"
"@@ -1 +1 @@\n"
"-Subproject commit 6909df2478f7eb092e8e5b5cda097616b2595cc6\n"
"+Subproject commit 6909df2478f7eb092e8e5b5cda097616b2595cc6-dirty\n"
"diff --git a/external/wxWidgets b/external/wxWidgets\n"
"--- a/external/wxWidgets\n"
"+++ b/external/wxWidgets\n"
"@@ -1 +1 @@\n"
"-Subproject commit 12b09a5e5ea76a1a0c27b769e821b37d803a4cb7\n"
"+Subproject commit 12b09a5e5ea76a1a0c27b769e821b37d803a4cb7-dirty\n"
"diff --git a/include/wex/vcs/unified-diff.h "
"b/include/wex/vcs/unified-diff.h\n"
"index 396ed3f8b..3d274e39e 100644\n"
"--- a/include/wex/vcs/unified-diff.h\n"
"+++ b/include/wex/vcs/unified-diff.h\n"
"@@ -42 +42 @@ public:\n"
"- vcs_entry* entry,\n"
"+ const vcs_entry* entry,\n"
"@@ -85,2 +85,2 @@ private:\n"
"- vcs_entry* m_vcs_entry{nullptr};\n"
"- factory::frame* m_frame{nullptr};\n"
"+ const vcs_entry* m_vcs_entry{nullptr};\n"
"+ factory::frame* m_frame{nullptr};\n");

const auto res(uni.parse());
REQUIRE(res);
REQUIRE(*res == 4);
REQUIRE(uni.path_from().string() == "include/wex/vcs/unified-diff.h");
REQUIRE(uni.path_to().string() == "include/wex/vcs/unified-diff.h");
REQUIRE(uni.range_from_start() == 85);
REQUIRE(uni.range_from_count() == 2);
REQUIRE(uni.range_to_start() == 85);
REQUIRE(uni.range_to_count() == 2);
}
}

0 comments on commit 326539d

Please sign in to comment.