Skip to content

Commit

Permalink
added unified_diffs class and F7, F8 key to iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
antonvw committed Nov 13, 2024
1 parent 13f18b2 commit b865418
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 45 deletions.
4 changes: 3 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-2022 Anton van Wezenbeek
// Copyright: (c) 2020-2024 Anton van Wezenbeek
////////////////////////////////////////////////////////////////////////////////

#pragma once
Expand All @@ -14,6 +14,8 @@ namespace wex::id
enum stc
{
beautify = wxID_HIGHEST + 1,
diff_next,
diff_previous,
edge_clear,
edge_set,
eol_dos,
Expand Down
20 changes: 6 additions & 14 deletions include/wex/stc/stc.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#include <wex/syntax/marker.h>
#include <wex/syntax/stc.h>
#include <wex/ui/item.h>
#include <wex/vcs/unified-diffs.h>
#include <wex/vi/vi.h>
#include <wx/prntbase.h>

#include <set>
#include <vector>

namespace wex
Expand Down Expand Up @@ -105,17 +105,11 @@ class stc : public syntax::stc
/// Returns associated data.
const auto& data() const { return m_data; }

/// Adds a diff line.
void diff_add(int line) { m_lines_diff.insert(line); };
/// Returns diffs.
const unified_diffs& diffs() const { return m_diffs; };

/// Goto first diff line.
bool diff_first();

/// Goto next diff line.
bool diff_next();

/// Goto previous diff line.
bool diff_previous();
/// Returns writable diffs.
unified_diffs& diffs() { return m_diffs; };

/// Shows a menu with current line type checked,
/// and allows you to change it.
Expand Down Expand Up @@ -313,12 +307,10 @@ class stc : public syntax::stc
function_repeat m_function_repeat;
data::stc m_data;
stc_file m_file;
unified_diffs m_diffs;

int m_selection_mode_copy{wxSTC_SEL_STREAM};

std::set<int> m_lines_diff;
std::set<int>::iterator m_lines_diff_it;

// The ex or vi component.
vi* m_vi{nullptr};

Expand Down
5 changes: 5 additions & 0 deletions include/wex/vcs/unified-diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class unified_diff
/// Provide frame, that will receive the unified diff callbacks.
factory::frame* f);

/// Returns true if this is the first diff.
bool is_first() const { return m_is_first; };

/// Parses the input.
/// This routine might invoke callback methods on wex::frame.
/// Returns number of differences present.
Expand Down Expand Up @@ -84,6 +87,8 @@ class unified_diff

path m_path_vcs;

bool m_is_first{true};

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

Expand Down
52 changes: 52 additions & 0 deletions include/wex/vcs/unified-diffs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
////////////////////////////////////////////////////////////////////////////////
// Name: unified-diffs.h
// Purpose: Declaration of class unified_diffs
// Author: Anton van Wezenbeek
// Copyright: (c) 2024 Anton van Wezenbeek
////////////////////////////////////////////////////////////////////////////////

#pragma once

#include <set>

namespace wex
{
class stc;
class unified_diff;

/// Offers a class that collects unified diff invocations to be able
/// to iterate through the differences and show them on stc component.
class unified_diffs
{
public:
/// Constructor, provide stc.
unified_diffs(stc* s);

/// Clears all differences, reset iterator.
void clear();

/// Returns distance of the iterator to begin of collection.
int distance() const;

/// Goto first diff line on stc.
bool first();

/// Inserts a unified diff.
void insert(const unified_diff* diff);

/// Goto next diff line on stc. If at end, goes to next stc.
bool next();

/// Goto previous diff line on stc. If at begin, goes to previous stc.
bool prev();

/// Returns number of differences present.
int size() const { return m_lines.size(); }

private:
stc* m_stc{nullptr};

std::set<int> m_lines;
std::set<int>::iterator m_lines_it;
};
}; // namespace wex
10 changes: 6 additions & 4 deletions src/del/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,8 +940,6 @@ bool wex::del::frame::vcs_unified_diff(
diff->range_from_start() - 1,
get_some_text(diff->text_removed()));
}

stc->diff_add(diff->range_from_start() - 1);
}

if (diff->range_to_count() > 0)
Expand All @@ -955,11 +953,15 @@ bool wex::del::frame::vcs_unified_diff(
log("vcs_unified_diff") << diff->path_vcs().string();
return false;
}
}

stc->diff_add(diff->range_to_start() - 1);
if (diff->is_first())
{
stc->diffs().clear();
}

stc->diff_first();
stc->diffs().insert(diff);
stc->diffs().first();
}

return true;
Expand Down
18 changes: 16 additions & 2 deletions src/stc/bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ void wex::stc::bind_all()
{wxACCEL_CTRL, WXK_INSERT, wxID_COPY},
{wxACCEL_NORMAL, WXK_F3, ID_EDIT_FIND_NEXT},
{wxACCEL_NORMAL, WXK_F4, ID_EDIT_FIND_PREVIOUS},
{wxACCEL_NORMAL, WXK_F7, wxID_SORT_ASCENDING},
{wxACCEL_NORMAL, WXK_F8, wxID_SORT_DESCENDING},
{wxACCEL_NORMAL, WXK_F7, id::stc::diff_next},
{wxACCEL_NORMAL, WXK_F8, id::stc::diff_previous},
{wxACCEL_NORMAL, WXK_F9, id::stc::fold_all},
{wxACCEL_NORMAL, WXK_F10, id::stc::unfold_all},
{wxACCEL_NORMAL, WXK_F11, id::stc::uppercase},
Expand Down Expand Up @@ -359,6 +359,20 @@ void wex::stc::bind_all()
},
ID_EDIT_FIND_PREVIOUS},

{[=, this](const wxCommandEvent& event)
{
m_diffs.next();
log::status("diff") << m_diffs.distance() << "from" << m_diffs.size();
},
id::stc::diff_next},

{[=, this](const wxCommandEvent& event)
{
m_diffs.prev();
log::status("diff") << m_diffs.distance() << "from" << m_diffs.size();
},
id::stc::diff_previous},

{[=, this](const wxCommandEvent& event)
{
link_open(link_t().set(LINK_OPEN_MIME));
Expand Down
24 changes: 1 addition & 23 deletions src/stc/stc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ wex::stc::stc(const wex::path& p, const data::stc& data)
, m_file(this, wex::path(data.window().name()))
, m_hexmode(hexmode(this))
, m_frame(dynamic_cast<frame*>(wxTheApp->GetTopWindow()))
, m_diffs(this)
, m_function_repeat(
"stc",
this,
Expand Down Expand Up @@ -246,29 +247,6 @@ void wex::stc::Cut()
}
}

bool wex::stc::diff_first()
{
if (m_lines_diff.empty())
{
return false;
}

m_lines_diff_it = m_lines_diff.begin();
goto_line(*m_lines_diff_it);

return true;
}

bool wex::stc::diff_next()
{
return true;
}

bool wex::stc::diff_previous()
{
return true;
}

bool wex::stc::file_readonly_attribute_changed()
{
SetReadOnly(path().is_readonly()); // does not return anything
Expand Down
2 changes: 2 additions & 0 deletions src/vcs/unified-diff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ std::optional<int> wex::unified_diff::parse()
}
}

m_is_first = false;

if (++tok_iter != tokens.end() && !(*tok_iter).starts_with("@@"))
{
break; // this was last chunk, continue with header lines
Expand Down
85 changes: 85 additions & 0 deletions src/vcs/unified-diffs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
////////////////////////////////////////////////////////////////////////////////
// Name: unified-diffs.cpp
// Purpose: Implementation of class unified_diffs
// Author: Anton van Wezenbeek
// Copyright: (c) 2024 Anton van Wezenbeek
////////////////////////////////////////////////////////////////////////////////

#include <wex/stc/stc.h>
#include <wex/vcs/unified-diff.h>
#include <wex/vcs/unified-diffs.h>

wex::unified_diffs::unified_diffs(stc* s)
: m_stc(s)
, m_lines_it(m_lines.begin())
{
}

void wex::unified_diffs::clear()
{
m_lines.clear();
m_lines_it = m_lines.begin();
}

int wex::unified_diffs::distance() const
{
return std::distance(m_lines.begin(), m_lines_it);
}

bool wex::unified_diffs::first()
{
if (m_lines.empty())
{
return false;
}

m_lines_it = m_lines.begin();

m_stc->goto_line(*m_lines_it);

return true;
}

void wex::unified_diffs::insert(const unified_diff* diff)
{
if (diff->range_from_start() != 0)
{
m_lines.insert(diff->range_from_start() - 1);
}

if (diff->range_to_start() != 0)
{
m_lines.insert(diff->range_to_start() - 1);
}

m_lines_it = m_lines.begin();
}

bool wex::unified_diffs::next()
{
if (m_lines_it == m_lines.end() ||
m_lines_it == std::prev(m_lines.end()))
{
return m_stc->get_vi().command(":n");
}

m_lines_it++;

m_stc->goto_line(*m_lines_it);

return true;
}

bool wex::unified_diffs::prev()
{
if (m_lines_it == m_lines.begin())
{
return m_stc->get_vi().command(":prev");
}

m_lines_it--;

m_stc->goto_line(*m_lines_it);

return true;
}
4 changes: 3 additions & 1 deletion test/vcs/test-unified-diff.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
////////////////////////////////////////////////////////////////////////////////
// Name: test-vcs-entry.cpp
// Name: test-unified-diff.cpp
// Purpose: Implementation for wex unit testing
// Author: Anton van Wezenbeek
// Copyright: (c) 2024 Anton van Wezenbeek
Expand Down Expand Up @@ -61,6 +61,7 @@ TEST_CASE("wex::unified_diff")
"@@ -38,0 +37 @@ The format is based on [Keep a Changelog].\n"
"+- test\n");

REQUIRE(uni.is_first());
const auto res(uni.parse());
REQUIRE(res);
REQUIRE(*res == 4);
Expand All @@ -72,6 +73,7 @@ TEST_CASE("wex::unified_diff")
REQUIRE(uni.range_to_count() == 1);
REQUIRE(uni.text_added().front() == "- test");
REQUIRE(uni.text_removed().empty());
REQUIRE(!uni.is_first());
}

SUBCASE("parse-valid-other")
Expand Down
Loading

0 comments on commit b865418

Please sign in to comment.