diff options
Diffstat (limited to 'src/nvim/api/buffer.c')
| -rw-r--r-- | src/nvim/api/buffer.c | 56 | 
1 files changed, 52 insertions, 4 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 81b3851c53..41d7d8ba6b 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -12,8 +12,10 @@  #include "nvim/api/buffer.h"  #include "nvim/api/private/helpers.h"  #include "nvim/api/private/defs.h" +#include "nvim/lua/executor.h"  #include "nvim/vim.h"  #include "nvim/buffer.h" +#include "nvim/change.h"  #include "nvim/charset.h"  #include "nvim/cursor.h"  #include "nvim/getchar.h" @@ -108,9 +110,11 @@ String buffer_get_line(Buffer buffer, Integer index, Error *err)  ///        `nvim_buf_lines_event`. Otherwise, the first notification will be  ///        a `nvim_buf_changedtick_event`. Not used for lua callbacks.  /// @param  opts  Optional parameters. -///               `on_lines`: lua callback received on change. +///               `on_lines`:       lua callback received on change.  ///               `on_changedtick`: lua callback received on changedtick  ///                                 increment without text change. +///               `utf_sizes`:      include UTF-32 and UTF-16 size of +///                                 the replaced region.  ///               See |api-buffer-updates-lua| for more information  /// @param[out] err Error details, if any  /// @return False when updates couldn't be enabled because the buffer isn't @@ -137,24 +141,44 @@ Boolean nvim_buf_attach(uint64_t channel_id,      if (is_lua && strequal("on_lines", k.data)) {        if (v->type != kObjectTypeLuaRef) {          api_set_error(err, kErrorTypeValidation, "callback is not a function"); -        return false; +        goto error;        }        cb.on_lines = v->data.luaref;        v->data.integer = LUA_NOREF;      } else if (is_lua && strequal("on_changedtick", k.data)) {        if (v->type != kObjectTypeLuaRef) {          api_set_error(err, kErrorTypeValidation, "callback is not a function"); -        return false; +        goto error;        }        cb.on_changedtick = v->data.luaref;        v->data.integer = LUA_NOREF; +    } else if (is_lua && strequal("on_detach", k.data)) { +      if (v->type != kObjectTypeLuaRef) { +        api_set_error(err, kErrorTypeValidation, "callback is not a function"); +        goto error; +      } +      cb.on_detach = v->data.luaref; +      v->data.integer = LUA_NOREF; +    } else if (is_lua && strequal("utf_sizes", k.data)) { +      if (v->type != kObjectTypeBoolean) { +        api_set_error(err, kErrorTypeValidation, "utf_sizes must be boolean"); +        goto error; +      } +      cb.utf_sizes = v->data.boolean;      } else {        api_set_error(err, kErrorTypeValidation, "unexpected key: %s", k.data); -      return false; +      goto error;      }    }    return buf_updates_register(buf, channel_id, cb, send_buffer); + +error: +  // TODO(bfredl): ASAN build should check that the ref table is empty? +  executor_free_luaref(cb.on_lines); +  executor_free_luaref(cb.on_changedtick); +  executor_free_luaref(cb.on_detach); +  return false;  }  /// Deactivates buffer-update events on the channel. @@ -1161,6 +1185,30 @@ free_exit:    return 0;  } +Dictionary nvim__buf_stats(Buffer buffer, Error *err) +{ +  Dictionary rv = ARRAY_DICT_INIT; + +  buf_T *buf = find_buffer_by_handle(buffer, err); +  if (!buf) { +    return rv; +  } + +  // Number of times the cached line was flushed. +  // This should generally not increase while editing the same +  // line in the same mode. +  PUT(rv, "flush_count", INTEGER_OBJ(buf->flush_count)); +  // lnum of current line +  PUT(rv, "current_lnum", INTEGER_OBJ(buf->b_ml.ml_line_lnum)); +  // whether the line has unflushed changes. +  PUT(rv, "line_dirty", BOOLEAN_OBJ(buf->b_ml.ml_flags & ML_LINE_DIRTY)); +  // NB: this should be zero at any time API functions are called, +  // this exists to debug issues +  PUT(rv, "dirty_bytes", INTEGER_OBJ((Integer)buf->deleted_bytes)); + +  return rv; +} +  // Check if deleting lines made the cursor position invalid.  // Changed lines from `lo` to `hi`; added `extra` lines (negative if deleted).  static void fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra)  | 
