diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2016-04-06 12:03:32 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2016-04-06 13:46:25 +0200 |
| commit | 329f18b8260f5d589b49678f41595fbff5f7d30a (patch) | |
| tree | d20477b0f3fc6d3db9bd0b45bcbb7f1bc4fb1b9a /text-objects.c | |
| parent | 700ec992a2ed6fc16362052fb27933390fe2d7ee (diff) | |
| download | vis-329f18b8260f5d589b49678f41595fbff5f7d30a.tar.gz vis-329f18b8260f5d589b49678f41595fbff5f7d30a.tar.xz | |
vis: make af and if text objects more robust
It still assumes that a function is terminated by the "\n}" seqeuence
and uses the same code as the % motion for matching curly braces.
Hence this won't work for functions containing an unbalanced
number of { or } symbols (e.g. due to comments or strings etc).
Also due to the way text objects are implemented in general the inner
variant will only work properly if the cursor is located somewhere
within the function. Otherwise it will include everything from the
current cursor position up to the end of the function body.
Diffstat (limited to 'text-objects.c')
| -rw-r--r-- | text-objects.c | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/text-objects.c b/text-objects.c index b0351cc..010f1df 100644 --- a/text-objects.c +++ b/text-objects.c @@ -176,23 +176,34 @@ Filerange text_object_paragraph(Text *txt, size_t pos) { return r; } -Filerange text_object_function(Text *txt, size_t pos) { - size_t a = text_function_start_prev(txt, pos); - size_t b = text_function_end_next(txt, pos); - if (text_function_end_next(txt, a) == b) { - Filerange r = text_range_new(a, b+1); - return text_range_linewise(txt, &r); +static Filerange object_function(Text *txt, size_t pos) { + size_t start_prev = text_function_start_prev(txt, pos); + size_t end_next = text_function_end_next(txt, pos); + size_t start = text_function_start_next(txt, start_prev); + size_t end = text_function_end_prev(txt, end_next); + if (start == pos) + start_prev = pos; + if (end == pos) + end_next = pos; + if (text_function_end_next(txt, start_prev) == end_next && + text_function_start_prev(txt, end_next) == start_prev) { + return text_range_new(start_prev, end_next); } return text_range_empty(); } +Filerange text_object_function(Text *txt, size_t pos) { + Filerange r = object_function(txt, pos); + if (!text_range_valid(&r)) + return r; + return text_range_linewise(txt, &r); +} + Filerange text_object_function_inner(Text *txt, size_t pos) { - Filerange r = text_object_function(txt, pos); + Filerange r = object_function(txt, pos); if (!text_range_valid(&r)) return r; - size_t b = text_function_end_next(txt, pos); - size_t a = text_bracket_match(txt, b); - return text_range_new(a+1, b-1); + return text_range_new(text_bracket_match(txt, r.end)+1, r.end); } static Filerange text_object_bracket(Text *txt, size_t pos, char type) { |
