Skip to content

Commit b865418

Browse files
committed
added unified_diffs class and F7, F8 key to iterate
1 parent 13f18b2 commit b865418

File tree

11 files changed

+246
-45
lines changed

11 files changed

+246
-45
lines changed

include/wex/stc/bind.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Name: bind.h
33
// Purpose: Declaration of bind id's
44
// Author: Anton van Wezenbeek
5-
// Copyright: (c) 2020-2022 Anton van Wezenbeek
5+
// Copyright: (c) 2020-2024 Anton van Wezenbeek
66
////////////////////////////////////////////////////////////////////////////////
77

88
#pragma once
@@ -14,6 +14,8 @@ namespace wex::id
1414
enum stc
1515
{
1616
beautify = wxID_HIGHEST + 1,
17+
diff_next,
18+
diff_previous,
1719
edge_clear,
1820
edge_set,
1921
eol_dos,

include/wex/stc/stc.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
#include <wex/syntax/marker.h>
1515
#include <wex/syntax/stc.h>
1616
#include <wex/ui/item.h>
17+
#include <wex/vcs/unified-diffs.h>
1718
#include <wex/vi/vi.h>
1819
#include <wx/prntbase.h>
1920

20-
#include <set>
2121
#include <vector>
2222

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

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

111-
/// Goto first diff line.
112-
bool diff_first();
113-
114-
/// Goto next diff line.
115-
bool diff_next();
116-
117-
/// Goto previous diff line.
118-
bool diff_previous();
111+
/// Returns writable diffs.
112+
unified_diffs& diffs() { return m_diffs; };
119113

120114
/// Shows a menu with current line type checked,
121115
/// and allows you to change it.
@@ -313,12 +307,10 @@ class stc : public syntax::stc
313307
function_repeat m_function_repeat;
314308
data::stc m_data;
315309
stc_file m_file;
310+
unified_diffs m_diffs;
316311

317312
int m_selection_mode_copy{wxSTC_SEL_STREAM};
318313

319-
std::set<int> m_lines_diff;
320-
std::set<int>::iterator m_lines_diff_it;
321-
322314
// The ex or vi component.
323315
vi* m_vi{nullptr};
324316

include/wex/vcs/unified-diff.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class unified_diff
4343
/// Provide frame, that will receive the unified diff callbacks.
4444
factory::frame* f);
4545

46+
/// Returns true if this is the first diff.
47+
bool is_first() const { return m_is_first; };
48+
4649
/// Parses the input.
4750
/// This routine might invoke callback methods on wex::frame.
4851
/// Returns number of differences present.
@@ -84,6 +87,8 @@ class unified_diff
8487

8588
path m_path_vcs;
8689

90+
bool m_is_first{true};
91+
8792
const vcs_entry* m_vcs_entry{nullptr};
8893
factory::frame* m_frame{nullptr};
8994

include/wex/vcs/unified-diffs.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// Name: unified-diffs.h
3+
// Purpose: Declaration of class unified_diffs
4+
// Author: Anton van Wezenbeek
5+
// Copyright: (c) 2024 Anton van Wezenbeek
6+
////////////////////////////////////////////////////////////////////////////////
7+
8+
#pragma once
9+
10+
#include <set>
11+
12+
namespace wex
13+
{
14+
class stc;
15+
class unified_diff;
16+
17+
/// Offers a class that collects unified diff invocations to be able
18+
/// to iterate through the differences and show them on stc component.
19+
class unified_diffs
20+
{
21+
public:
22+
/// Constructor, provide stc.
23+
unified_diffs(stc* s);
24+
25+
/// Clears all differences, reset iterator.
26+
void clear();
27+
28+
/// Returns distance of the iterator to begin of collection.
29+
int distance() const;
30+
31+
/// Goto first diff line on stc.
32+
bool first();
33+
34+
/// Inserts a unified diff.
35+
void insert(const unified_diff* diff);
36+
37+
/// Goto next diff line on stc. If at end, goes to next stc.
38+
bool next();
39+
40+
/// Goto previous diff line on stc. If at begin, goes to previous stc.
41+
bool prev();
42+
43+
/// Returns number of differences present.
44+
int size() const { return m_lines.size(); }
45+
46+
private:
47+
stc* m_stc{nullptr};
48+
49+
std::set<int> m_lines;
50+
std::set<int>::iterator m_lines_it;
51+
};
52+
}; // namespace wex

src/del/frame.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -940,8 +940,6 @@ bool wex::del::frame::vcs_unified_diff(
940940
diff->range_from_start() - 1,
941941
get_some_text(diff->text_removed()));
942942
}
943-
944-
stc->diff_add(diff->range_from_start() - 1);
945943
}
946944

947945
if (diff->range_to_count() > 0)
@@ -955,11 +953,15 @@ bool wex::del::frame::vcs_unified_diff(
955953
log("vcs_unified_diff") << diff->path_vcs().string();
956954
return false;
957955
}
956+
}
958957

959-
stc->diff_add(diff->range_to_start() - 1);
958+
if (diff->is_first())
959+
{
960+
stc->diffs().clear();
960961
}
961962

962-
stc->diff_first();
963+
stc->diffs().insert(diff);
964+
stc->diffs().first();
963965
}
964966

965967
return true;

src/stc/bind.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ void wex::stc::bind_all()
139139
{wxACCEL_CTRL, WXK_INSERT, wxID_COPY},
140140
{wxACCEL_NORMAL, WXK_F3, ID_EDIT_FIND_NEXT},
141141
{wxACCEL_NORMAL, WXK_F4, ID_EDIT_FIND_PREVIOUS},
142-
{wxACCEL_NORMAL, WXK_F7, wxID_SORT_ASCENDING},
143-
{wxACCEL_NORMAL, WXK_F8, wxID_SORT_DESCENDING},
142+
{wxACCEL_NORMAL, WXK_F7, id::stc::diff_next},
143+
{wxACCEL_NORMAL, WXK_F8, id::stc::diff_previous},
144144
{wxACCEL_NORMAL, WXK_F9, id::stc::fold_all},
145145
{wxACCEL_NORMAL, WXK_F10, id::stc::unfold_all},
146146
{wxACCEL_NORMAL, WXK_F11, id::stc::uppercase},
@@ -359,6 +359,20 @@ void wex::stc::bind_all()
359359
},
360360
ID_EDIT_FIND_PREVIOUS},
361361

362+
{[=, this](const wxCommandEvent& event)
363+
{
364+
m_diffs.next();
365+
log::status("diff") << m_diffs.distance() << "from" << m_diffs.size();
366+
},
367+
id::stc::diff_next},
368+
369+
{[=, this](const wxCommandEvent& event)
370+
{
371+
m_diffs.prev();
372+
log::status("diff") << m_diffs.distance() << "from" << m_diffs.size();
373+
},
374+
id::stc::diff_previous},
375+
362376
{[=, this](const wxCommandEvent& event)
363377
{
364378
link_open(link_t().set(LINK_OPEN_MIME));

src/stc/stc.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ wex::stc::stc(const wex::path& p, const data::stc& data)
3636
, m_file(this, wex::path(data.window().name()))
3737
, m_hexmode(hexmode(this))
3838
, m_frame(dynamic_cast<frame*>(wxTheApp->GetTopWindow()))
39+
, m_diffs(this)
3940
, m_function_repeat(
4041
"stc",
4142
this,
@@ -246,29 +247,6 @@ void wex::stc::Cut()
246247
}
247248
}
248249

249-
bool wex::stc::diff_first()
250-
{
251-
if (m_lines_diff.empty())
252-
{
253-
return false;
254-
}
255-
256-
m_lines_diff_it = m_lines_diff.begin();
257-
goto_line(*m_lines_diff_it);
258-
259-
return true;
260-
}
261-
262-
bool wex::stc::diff_next()
263-
{
264-
return true;
265-
}
266-
267-
bool wex::stc::diff_previous()
268-
{
269-
return true;
270-
}
271-
272250
bool wex::stc::file_readonly_attribute_changed()
273251
{
274252
SetReadOnly(path().is_readonly()); // does not return anything

src/vcs/unified-diff.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ std::optional<int> wex::unified_diff::parse()
137137
}
138138
}
139139

140+
m_is_first = false;
141+
140142
if (++tok_iter != tokens.end() && !(*tok_iter).starts_with("@@"))
141143
{
142144
break; // this was last chunk, continue with header lines

src/vcs/unified-diffs.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// Name: unified-diffs.cpp
3+
// Purpose: Implementation of class unified_diffs
4+
// Author: Anton van Wezenbeek
5+
// Copyright: (c) 2024 Anton van Wezenbeek
6+
////////////////////////////////////////////////////////////////////////////////
7+
8+
#include <wex/stc/stc.h>
9+
#include <wex/vcs/unified-diff.h>
10+
#include <wex/vcs/unified-diffs.h>
11+
12+
wex::unified_diffs::unified_diffs(stc* s)
13+
: m_stc(s)
14+
, m_lines_it(m_lines.begin())
15+
{
16+
}
17+
18+
void wex::unified_diffs::clear()
19+
{
20+
m_lines.clear();
21+
m_lines_it = m_lines.begin();
22+
}
23+
24+
int wex::unified_diffs::distance() const
25+
{
26+
return std::distance(m_lines.begin(), m_lines_it);
27+
}
28+
29+
bool wex::unified_diffs::first()
30+
{
31+
if (m_lines.empty())
32+
{
33+
return false;
34+
}
35+
36+
m_lines_it = m_lines.begin();
37+
38+
m_stc->goto_line(*m_lines_it);
39+
40+
return true;
41+
}
42+
43+
void wex::unified_diffs::insert(const unified_diff* diff)
44+
{
45+
if (diff->range_from_start() != 0)
46+
{
47+
m_lines.insert(diff->range_from_start() - 1);
48+
}
49+
50+
if (diff->range_to_start() != 0)
51+
{
52+
m_lines.insert(diff->range_to_start() - 1);
53+
}
54+
55+
m_lines_it = m_lines.begin();
56+
}
57+
58+
bool wex::unified_diffs::next()
59+
{
60+
if (m_lines_it == m_lines.end() ||
61+
m_lines_it == std::prev(m_lines.end()))
62+
{
63+
return m_stc->get_vi().command(":n");
64+
}
65+
66+
m_lines_it++;
67+
68+
m_stc->goto_line(*m_lines_it);
69+
70+
return true;
71+
}
72+
73+
bool wex::unified_diffs::prev()
74+
{
75+
if (m_lines_it == m_lines.begin())
76+
{
77+
return m_stc->get_vi().command(":prev");
78+
}
79+
80+
m_lines_it--;
81+
82+
m_stc->goto_line(*m_lines_it);
83+
84+
return true;
85+
}

test/vcs/test-unified-diff.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
////////////////////////////////////////////////////////////////////////////////
2-
// Name: test-vcs-entry.cpp
2+
// Name: test-unified-diff.cpp
33
// Purpose: Implementation for wex unit testing
44
// Author: Anton van Wezenbeek
55
// Copyright: (c) 2024 Anton van Wezenbeek
@@ -61,6 +61,7 @@ TEST_CASE("wex::unified_diff")
6161
"@@ -38,0 +37 @@ The format is based on [Keep a Changelog].\n"
6262
"+- test\n");
6363

64+
REQUIRE(uni.is_first());
6465
const auto res(uni.parse());
6566
REQUIRE(res);
6667
REQUIRE(*res == 4);
@@ -72,6 +73,7 @@ TEST_CASE("wex::unified_diff")
7273
REQUIRE(uni.range_to_count() == 1);
7374
REQUIRE(uni.text_added().front() == "- test");
7475
REQUIRE(uni.text_removed().empty());
76+
REQUIRE(!uni.is_first());
7577
}
7678

7779
SUBCASE("parse-valid-other")

0 commit comments

Comments
 (0)