diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2014-11-01 21:24:58 -0400 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2014-11-01 21:24:58 -0400 |
commit | 48847fbafc41d93bbc42d7bf5711a47c4982e08c (patch) | |
tree | 4f6c374b801ac8744b2a8c31451df4f88079c1e3 | |
parent | 92173edbc0d35420056034d5139a71575e370538 (diff) | |
parent | d6da42475888bae48104d1de98fcc9640b619de3 (diff) | |
download | rneovim-48847fbafc41d93bbc42d7bf5711a47c4982e08c.tar.gz rneovim-48847fbafc41d93bbc42d7bf5711a47c4982e08c.tar.bz2 rneovim-48847fbafc41d93bbc42d7bf5711a47c4982e08c.zip |
Merge pull request #951 from bfredl/curfix
api: window_set_cursor doesn't always update position.
-rw-r--r-- | src/nvim/api/window.c | 5 | ||||
-rw-r--r-- | src/nvim/move.c | 12 | ||||
-rw-r--r-- | test/functional/api/window_spec.lua | 69 |
3 files changed, 84 insertions, 2 deletions
diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index fde1ebfa4c..597c857ad5 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -9,6 +9,7 @@ #include "nvim/cursor.h" #include "nvim/window.h" #include "nvim/screen.h" +#include "nvim/move.h" #include "nvim/misc2.h" @@ -86,6 +87,10 @@ void window_set_cursor(Window window, ArrayOf(Integer, 2) pos, Error *err) win->w_cursor.coladd = 0; // When column is out of range silently correct it. check_cursor_col_win(win); + + // make sure cursor is in visible range even if win != curwin + update_topline_win(win); + update_screen(VALID); } diff --git a/src/nvim/move.c b/src/nvim/move.c index 2118f58f7e..c43fa1d09d 100644 --- a/src/nvim/move.c +++ b/src/nvim/move.c @@ -34,6 +34,7 @@ #include "nvim/popupmnu.h" #include "nvim/screen.h" #include "nvim/strings.h" +#include "nvim/window.h" typedef struct { linenr_T lnum; /* line number */ @@ -315,6 +316,17 @@ void update_topline(void) } /* + * Update win->w_topline to move the cursor onto the screen. + */ +void update_topline_win(win_T* win) +{ + win_T *save_curwin; + switch_win(&save_curwin, NULL, win, NULL, true); + update_topline(); + restore_win(save_curwin, NULL, true); +} + +/* * Return the scrolljump value to use for the current window. * When 'scrolljump' is positive use it as-is. * When 'scrolljump' is negative use it as a percentage of the window height. diff --git a/test/functional/api/window_spec.lua b/test/functional/api/window_spec.lua index a3814cce0f..4c867d2f5d 100644 --- a/test/functional/api/window_spec.lua +++ b/test/functional/api/window_spec.lua @@ -1,9 +1,28 @@ -- Sanity checks for window_* API calls via msgpack-rpc local helpers = require('test.functional.helpers') local clear, nvim, buffer, curbuf, curbuf_contents, window, curwin, eq, neq, - ok = helpers.clear, helpers.nvim, helpers.buffer, helpers.curbuf, + ok, feed, rawfeed, insert, eval = helpers.clear, helpers.nvim, helpers.buffer, helpers.curbuf, helpers.curbuf_contents, helpers.window, helpers.curwin, helpers.eq, - helpers.neq, helpers.ok + helpers.neq, helpers.ok, helpers.feed, helpers.rawfeed, helpers.insert, helpers.eval + +-- check if str is visible at the beginning of some line +local function is_visible(str) + local slen = string.len(str) + local nlines = eval("&lines") + for i = 1,nlines do + local iseq = true + for j = 1,slen do + if string.byte(str,j) ~= eval("screenchar("..i..","..j..")") then + iseq = false + break + end + end + if iseq then + return true + end + end + return false +end describe('window_* functions', function() before_each(clear) @@ -29,6 +48,52 @@ describe('window_* functions', function() nvim('command', 'normal i dumb') eq('typing\n some dumb text', curbuf_contents()) end) + + it('updates the screen, and also when the window is unfocused', function() + insert("prologue") + feed('100o<esc>') + insert("epilogue") + win = curwin() + feed('gg') + + -- cursor position is at beginning + eq({1, 0}, window('get_cursor', win)) + eq(true, is_visible("prologue")) + eq(false, is_visible("epilogue")) + + -- move cursor to end + window('set_cursor', win, {101, 0}) + eq(false, is_visible("prologue")) + eq(true, is_visible("epilogue")) + + -- move cursor to the beginning again + window('set_cursor', win, {1, 0}) + eq(true, is_visible("prologue")) + eq(false, is_visible("epilogue")) + + -- move focus to new window + nvim('command',"new") + neq(win, curwin()) + + -- sanity check, cursor position is kept + eq({1, 0}, window('get_cursor', win)) + eq(true, is_visible("prologue")) + eq(false, is_visible("epilogue")) + + -- move cursor to end + window('set_cursor', win, {101, 0}) + eq(false, is_visible("prologue")) + eq(true, is_visible("epilogue")) + + -- move cursor to the beginning again + window('set_cursor', win, {1, 0}) + eq(true, is_visible("prologue")) + eq(false, is_visible("epilogue")) + + -- curwin didn't change back + neq(win, curwin()) + end) + end) describe('{get,set}_height', function() |