Fix tabs-to-spaces an spaces-to-tabs with the selection refactor
This commit is contained in:
parent
3791e74743
commit
e6a9780490
|
@ -1105,23 +1105,24 @@ void tabs_to_spaces(Context& context, int ts)
|
||||||
auto& buffer = context.buffer();
|
auto& buffer = context.buffer();
|
||||||
const CharCount opt_tabstop = context.options()["tabstop"].get<int>();
|
const CharCount opt_tabstop = context.options()["tabstop"].get<int>();
|
||||||
const CharCount tabstop = ts == 0 ? opt_tabstop : ts;
|
const CharCount tabstop = ts == 0 ? opt_tabstop : ts;
|
||||||
|
std::vector<Selection> tabs;
|
||||||
|
std::vector<String> spaces;
|
||||||
for (auto& sel : context.selections())
|
for (auto& sel : context.selections())
|
||||||
{
|
{
|
||||||
for (auto it = buffer.iterator_at(sel.min()),
|
for (auto it = buffer.iterator_at(sel.min()),
|
||||||
end = buffer.iterator_at(sel.max())+1; it != end;)
|
end = buffer.iterator_at(sel.max())+1; it != end; ++it)
|
||||||
{
|
{
|
||||||
if (*it == '\t')
|
if (*it == '\t')
|
||||||
{
|
{
|
||||||
CharCount col = get_column(buffer, opt_tabstop, it.coord());
|
CharCount col = get_column(buffer, opt_tabstop, it.coord());
|
||||||
CharCount end_col = (col / tabstop + 1) * tabstop;
|
CharCount end_col = (col / tabstop + 1) * tabstop;
|
||||||
it = buffer.erase(it, it+1);
|
tabs.push_back({ it.coord() });
|
||||||
it = buffer.insert(it, String{ ' ', end_col - col }) + (int)(end_col - col);
|
spaces.push_back(String{ ' ', end_col - col });
|
||||||
end = buffer.iterator_at(sel.max())+1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
++it;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (not tabs.empty())
|
||||||
|
SelectionList{ buffer, std::move(tabs) }.insert(spaces, InsertMode::Replace);
|
||||||
}
|
}
|
||||||
|
|
||||||
void spaces_to_tabs(Context& context, int ts)
|
void spaces_to_tabs(Context& context, int ts)
|
||||||
|
@ -1129,6 +1130,7 @@ void spaces_to_tabs(Context& context, int ts)
|
||||||
auto& buffer = context.buffer();
|
auto& buffer = context.buffer();
|
||||||
const CharCount opt_tabstop = context.options()["tabstop"].get<int>();
|
const CharCount opt_tabstop = context.options()["tabstop"].get<int>();
|
||||||
const CharCount tabstop = ts == 0 ? opt_tabstop : ts;
|
const CharCount tabstop = ts == 0 ? opt_tabstop : ts;
|
||||||
|
std::vector<Selection> spaces;
|
||||||
for (auto& sel : context.selections())
|
for (auto& sel : context.selections())
|
||||||
{
|
{
|
||||||
for (auto it = buffer.iterator_at(sel.min()),
|
for (auto it = buffer.iterator_at(sel.min()),
|
||||||
|
@ -1144,20 +1146,16 @@ void spaces_to_tabs(Context& context, int ts)
|
||||||
++spaces_end;
|
++spaces_end;
|
||||||
++col;
|
++col;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((col % tabstop) == 0)
|
if ((col % tabstop) == 0)
|
||||||
{
|
spaces.push_back({spaces_beg.coord(), (spaces_end-1).coord()});
|
||||||
it = buffer.erase(spaces_beg, spaces_end);
|
|
||||||
it = buffer.insert(it, "\t") + 1;
|
|
||||||
end = buffer.iterator_at(sel.max())+1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
it = spaces_end;
|
it = spaces_end;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (not spaces.empty())
|
||||||
|
SelectionList{ buffer, std::move(spaces) }.insert("\t"_str, InsertMode::Replace);
|
||||||
}
|
}
|
||||||
|
|
||||||
void undo(Context& context, int)
|
void undo(Context& context, int)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user