Always select inserted text after piping
Relying on general selection update code is error prone due to diffing. Fixes #2394
This commit is contained in:
parent
c07f052de7
commit
370d10ccc7
|
@ -484,7 +484,7 @@ BufferCoord Buffer::do_insert(BufferCoord pos, StringView content)
|
||||||
: BufferCoord{ last_line, m_lines[last_line].length() - suffix.length() };
|
: BufferCoord{ last_line, m_lines[last_line].length() - suffix.length() };
|
||||||
|
|
||||||
m_changes.push_back({ Change::Insert, pos, end });
|
m_changes.push_back({ Change::Insert, pos, end });
|
||||||
return pos;
|
return end;
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferCoord Buffer::do_erase(BufferCoord begin, BufferCoord end)
|
BufferCoord Buffer::do_erase(BufferCoord begin, BufferCoord end)
|
||||||
|
@ -809,7 +809,8 @@ UnitTest test_buffer{[]()
|
||||||
UnitTest test_undo{[]()
|
UnitTest test_undo{[]()
|
||||||
{
|
{
|
||||||
Buffer buffer("test", Buffer::Flags::None, "allo ?\nmais que fais la police\n hein ?\n youpi\n");
|
Buffer buffer("test", Buffer::Flags::None, "allo ?\nmais que fais la police\n hein ?\n youpi\n");
|
||||||
auto pos = buffer.insert(buffer.end_coord(), "kanaky\n"); // change 1
|
auto pos = buffer.end_coord();
|
||||||
|
buffer.insert(pos, "kanaky\n"); // change 1
|
||||||
buffer.commit_undo_group();
|
buffer.commit_undo_group();
|
||||||
buffer.erase(pos, buffer.end_coord()); // change 2
|
buffer.erase(pos, buffer.end_coord()); // change 2
|
||||||
buffer.commit_undo_group();
|
buffer.commit_undo_group();
|
||||||
|
|
|
@ -489,7 +489,7 @@ void command(Context& context, NormalParams params)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void apply_diff(Buffer& buffer, BufferCoord pos, StringView before, StringView after)
|
BufferCoord apply_diff(Buffer& buffer, BufferCoord pos, StringView before, StringView after)
|
||||||
{
|
{
|
||||||
// The diff algorithm is O(ND) with N the sum of string len, and D the diff count
|
// The diff algorithm is O(ND) with N the sum of string len, and D the diff count
|
||||||
// do not use it if our data is too big
|
// do not use it if our data is too big
|
||||||
|
@ -497,8 +497,7 @@ void apply_diff(Buffer& buffer, BufferCoord pos, StringView before, StringView a
|
||||||
if (before.length() + after.length() > size_limit)
|
if (before.length() + after.length() > size_limit)
|
||||||
{
|
{
|
||||||
buffer.erase(pos, buffer.advance(pos, before.length()));
|
buffer.erase(pos, buffer.advance(pos, before.length()));
|
||||||
buffer.insert(pos, after);
|
return buffer.insert(pos, after);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto diffs = find_diff(before.begin(), (int)before.length(), after.begin(), (int)after.length());
|
auto diffs = find_diff(before.begin(), (int)before.length(), after.begin(), (int)after.length());
|
||||||
|
@ -511,14 +510,14 @@ void apply_diff(Buffer& buffer, BufferCoord pos, StringView before, StringView a
|
||||||
pos = buffer.advance(pos, diff.len);
|
pos = buffer.advance(pos, diff.len);
|
||||||
break;
|
break;
|
||||||
case Diff::Add:
|
case Diff::Add:
|
||||||
buffer.insert(pos, after.substr(ByteCount{diff.posB}, ByteCount{diff.len}));
|
pos = buffer.insert(pos, after.substr(ByteCount{diff.posB}, ByteCount{diff.len}));
|
||||||
pos = buffer.advance(pos, diff.len);
|
|
||||||
break;
|
break;
|
||||||
case Diff::Remove:
|
case Diff::Remove:
|
||||||
buffer.erase(pos, buffer.advance(pos, diff.len));
|
pos = buffer.erase(pos, buffer.advance(pos, diff.len));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<bool replace>
|
template<bool replace>
|
||||||
|
@ -544,19 +543,14 @@ void pipe(Context& context, NormalParams)
|
||||||
|
|
||||||
Buffer& buffer = context.buffer();
|
Buffer& buffer = context.buffer();
|
||||||
SelectionList selections = context.selections();
|
SelectionList selections = context.selections();
|
||||||
auto restore_sels = on_scope_end([&, old_main = selections.main_index()] {
|
|
||||||
selections.set_main_index(old_main);
|
|
||||||
context.selections() = std::move(selections);
|
|
||||||
});
|
|
||||||
if (replace)
|
if (replace)
|
||||||
{
|
{
|
||||||
ScopedEdition edition(context);
|
ScopedEdition edition(context);
|
||||||
ForwardChangesTracker changes_tracker;
|
ForwardChangesTracker changes_tracker;
|
||||||
size_t timestamp = buffer.timestamp();
|
size_t timestamp = buffer.timestamp();
|
||||||
for (int i = 0; i < selections.size(); ++i)
|
Vector<Selection> new_sels;
|
||||||
|
for (auto& sel : selections)
|
||||||
{
|
{
|
||||||
selections.set_main_index(i);
|
|
||||||
auto& sel = selections.main();
|
|
||||||
const auto beg = changes_tracker.get_new_coord_tolerant(sel.min());
|
const auto beg = changes_tracker.get_new_coord_tolerant(sel.min());
|
||||||
const auto end = changes_tracker.get_new_coord_tolerant(sel.max());
|
const auto end = changes_tracker.get_new_coord_tolerant(sel.max());
|
||||||
|
|
||||||
|
@ -566,7 +560,7 @@ void pipe(Context& context, NormalParams)
|
||||||
in += '\n';
|
in += '\n';
|
||||||
|
|
||||||
// Needed in case we read selections inside the cmdline
|
// Needed in case we read selections inside the cmdline
|
||||||
context.selections_write_only() = selections;
|
context.selections_write_only().set({keep_direction(Selection{beg, end}, sel)}, 0);
|
||||||
|
|
||||||
String out = ShellManager::instance().eval(
|
String out = ShellManager::instance().eval(
|
||||||
cmdline, context, in,
|
cmdline, context, in,
|
||||||
|
@ -578,13 +572,23 @@ void pipe(Context& context, NormalParams)
|
||||||
if (not out.empty() and out.back() == '\n')
|
if (not out.empty() and out.back() == '\n')
|
||||||
out.resize(out.length()-1, 0);
|
out.resize(out.length()-1, 0);
|
||||||
}
|
}
|
||||||
apply_diff(buffer, beg, in, out);
|
auto new_end = apply_diff(buffer, beg, in, out);
|
||||||
|
if (new_end != beg)
|
||||||
|
new_sels.push_back(keep_direction({beg, buffer.char_prev(new_end), std::move(sel.captures())}, sel));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (new_end != BufferCoord{})
|
||||||
|
new_end = buffer.char_prev(new_end);
|
||||||
|
new_sels.push_back({new_end, new_end, std::move(sel.captures())});
|
||||||
|
}
|
||||||
|
|
||||||
changes_tracker.update(buffer, timestamp);
|
changes_tracker.update(buffer, timestamp);
|
||||||
}
|
}
|
||||||
|
context.selections_write_only().set(std::move(new_sels), selections.main_index());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
const auto old_main = selections.main_index();
|
||||||
for (int i = 0; i < selections.size(); ++i)
|
for (int i = 0; i < selections.size(); ++i)
|
||||||
{
|
{
|
||||||
selections.set_main_index(i);
|
selections.set_main_index(i);
|
||||||
|
@ -592,6 +596,7 @@ void pipe(Context& context, NormalParams)
|
||||||
content(buffer, selections.main()),
|
content(buffer, selections.main()),
|
||||||
ShellManager::Flags::None);
|
ShellManager::Flags::None);
|
||||||
}
|
}
|
||||||
|
selections.set_main_index(old_main);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -409,8 +409,12 @@ void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
|
||||||
const String& str = strings[std::min(index, strings.size()-1)];
|
const String& str = strings[std::min(index, strings.size()-1)];
|
||||||
|
|
||||||
const auto pos = (mode == InsertMode::Replace) ?
|
const auto pos = (mode == InsertMode::Replace) ?
|
||||||
replace(*m_buffer, sel, str)
|
sel.min() : changes_tracker.get_new_coord(insert_pos[index]);
|
||||||
: m_buffer->insert(changes_tracker.get_new_coord(insert_pos[index]), str);
|
|
||||||
|
if (mode == InsertMode::Replace)
|
||||||
|
replace(*m_buffer, sel, str);
|
||||||
|
else
|
||||||
|
m_buffer->insert(pos, str);
|
||||||
|
|
||||||
size_t old_timestamp = m_timestamp;
|
size_t old_timestamp = m_timestamp;
|
||||||
changes_tracker.update(*m_buffer, m_timestamp);
|
changes_tracker.update(*m_buffer, m_timestamp);
|
||||||
|
|
1
test/compose/complex-pipe/cmd
Normal file
1
test/compose/complex-pipe/cmd
Normal file
|
@ -0,0 +1 @@
|
||||||
|
|sort<ret>
|
8
test/compose/complex-pipe/in
Normal file
8
test/compose/complex-pipe/in
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
foo %(baz
|
||||||
|
bar
|
||||||
|
qux) quux
|
||||||
|
|
||||||
|
foo %(baz
|
||||||
|
bar
|
||||||
|
qux
|
||||||
|
)
|
8
test/compose/complex-pipe/out
Normal file
8
test/compose/complex-pipe/out
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
foo bar
|
||||||
|
baz
|
||||||
|
qux quux
|
||||||
|
|
||||||
|
foo bar
|
||||||
|
baz
|
||||||
|
qux
|
||||||
|
|
6
test/compose/complex-pipe/selections
Normal file
6
test/compose/complex-pipe/selections
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
'bar
|
||||||
|
baz
|
||||||
|
qux' 'bar
|
||||||
|
baz
|
||||||
|
qux
|
||||||
|
'
|
|
@ -0,0 +1,2 @@
|
||||||
|
'yes
|
||||||
|
'
|
|
@ -0,0 +1 @@
|
||||||
|
1.1,1.1
|
Loading…
Reference in New Issue
Block a user