Try to simplify argument selector, abandon pair matching checks
Recognize both , and ; as argument separator
This commit is contained in:
parent
2660793a31
commit
12dd7b04ac
|
@ -431,97 +431,62 @@ Selection select_indent(const Buffer& buffer, const Selection& selection, Object
|
||||||
Selection select_argument(const Buffer& buffer, const Selection& selection,
|
Selection select_argument(const Buffer& buffer, const Selection& selection,
|
||||||
int level, ObjectFlags flags)
|
int level, ObjectFlags flags)
|
||||||
{
|
{
|
||||||
auto get_kind = [](Codepoint c) -> char {
|
enum Class { None, Opening, Closing, Delimiter };
|
||||||
switch (c)
|
|
||||||
{
|
|
||||||
case '(': case ')': return 1;
|
|
||||||
case '[': case ']': return 2;
|
|
||||||
case '{': case '}': return 3;
|
|
||||||
case '<': case '>': return 4;
|
|
||||||
default: kak_assert(false); return 0;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
enum Class { None, Opening, Closing };
|
|
||||||
auto classify = [](Codepoint c) {
|
auto classify = [](Codepoint c) {
|
||||||
switch (c)
|
switch (c)
|
||||||
{
|
{
|
||||||
case '(': case '[': case '{': case '<': return Opening;
|
case '(': case '[': case '{': case '<': return Opening;
|
||||||
case ')': case ']': case '}': case '>': return Closing;
|
case ')': case ']': case '}': case '>': return Closing;
|
||||||
|
case ',': case ';': return Delimiter;
|
||||||
default: return None;
|
default: return None;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Vector<char> pairs;
|
|
||||||
|
|
||||||
BufferIterator pos = buffer.iterator_at(selection.cursor());
|
BufferIterator pos = buffer.iterator_at(selection.cursor());
|
||||||
if (classify(*pos) == Closing)
|
switch (classify(*pos))
|
||||||
++pos;
|
{
|
||||||
|
//case Closing: if (pos+1 != buffer.end()) ++pos; break;
|
||||||
|
case Opening:
|
||||||
|
case Delimiter: if (pos != buffer.begin()) --pos; break;
|
||||||
|
default: break;
|
||||||
|
};
|
||||||
|
|
||||||
bool first_arg = false;
|
bool first_arg = false;
|
||||||
BufferIterator begin = pos;
|
BufferIterator begin = pos;
|
||||||
for (int lev = level; begin != buffer.begin(); --begin)
|
for (int lev = level; begin != buffer.begin(); --begin)
|
||||||
{
|
{
|
||||||
Codepoint cur = *begin;
|
Class c = classify(*begin);
|
||||||
Class c = classify(cur);
|
|
||||||
if (c == Closing)
|
if (c == Closing)
|
||||||
{
|
|
||||||
pairs.push_back(get_kind(cur));
|
|
||||||
++lev;
|
++lev;
|
||||||
}
|
else if (c == Opening and (lev-- == 0))
|
||||||
else if (c == Opening)
|
|
||||||
{
|
{
|
||||||
if (not pairs.empty())
|
first_arg = true;
|
||||||
{
|
++begin;
|
||||||
if (pairs.back() != get_kind(cur))
|
break;
|
||||||
return selection;
|
|
||||||
|
|
||||||
pairs.pop_back();
|
|
||||||
}
|
|
||||||
if (lev-- == 0)
|
|
||||||
{
|
|
||||||
first_arg = true;
|
|
||||||
++begin;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (cur == ',' and lev == 0)
|
else if (c == Delimiter and lev == 0)
|
||||||
{
|
{
|
||||||
++begin;
|
++begin;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
kak_assert(pairs.empty());
|
|
||||||
|
|
||||||
bool last_arg = false;
|
bool last_arg = false;
|
||||||
BufferIterator end = pos;
|
BufferIterator end = pos;
|
||||||
for (int lev = level; end != buffer.end(); ++end)
|
for (int lev = level; end != buffer.end(); ++end)
|
||||||
{
|
{
|
||||||
Codepoint cur = *end;
|
Class c = classify(*end);
|
||||||
Class c = classify(cur);
|
|
||||||
if (c == Opening)
|
if (c == Opening)
|
||||||
{
|
|
||||||
pairs.push_back(get_kind(cur));
|
|
||||||
++lev;
|
++lev;
|
||||||
}
|
else if (end != pos and c == Closing and (lev-- == 0))
|
||||||
else if (c == Closing)
|
|
||||||
{
|
{
|
||||||
if (not pairs.empty())
|
last_arg = true;
|
||||||
{
|
--end;
|
||||||
if (pairs.back() != get_kind(cur))
|
break;
|
||||||
return selection;
|
|
||||||
|
|
||||||
pairs.pop_back();
|
|
||||||
}
|
|
||||||
if (lev-- == 0)
|
|
||||||
{
|
|
||||||
last_arg = true;
|
|
||||||
--end;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (cur == ',' and lev == 0)
|
else if (c == Delimiter and lev == 0)
|
||||||
{
|
{
|
||||||
// include whitespaces *after* the comma only for first argument
|
// include whitespaces *after* the delimiter only for first argument
|
||||||
if (first_arg and not (flags & ObjectFlags::Inner))
|
if (first_arg and not (flags & ObjectFlags::Inner))
|
||||||
{
|
{
|
||||||
while (end + 1 != buffer.end() and is_blank(*(end+1)))
|
while (end + 1 != buffer.end() and is_blank(*(end+1)))
|
||||||
|
@ -538,7 +503,7 @@ Selection select_argument(const Buffer& buffer, const Selection& selection,
|
||||||
skip_while(begin, end, is_blank);
|
skip_while(begin, end, is_blank);
|
||||||
skip_while_reverse(end, begin, is_blank);
|
skip_while_reverse(end, begin, is_blank);
|
||||||
}
|
}
|
||||||
// get starting comma for non inner last arg
|
// get starting delimiter for non inner last arg
|
||||||
else if (not first_arg and last_arg)
|
else if (not first_arg and last_arg)
|
||||||
--begin;
|
--begin;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user