浏览代码

Handle possible errors from RangeVisibleInBuffer()

First and more common error is that by the time we execute

    buffer = vim.buffers[ bufnr ]

the buffer might not be there any more. This is because
`RangeVisibleInBuffer()` is called asynchronously and the user may bwipeout
a buffer in between polls.
This regularly happens in our vim tests. In such a case, we get a nasty
traceback from `vimsupport` module.
The solution is to catch the KeyError and return None.

However, `ScrollingBufferRange()` also was not ready to handle None values
from `RangeVisibleInBuffer()`, even though `RangeVisibleInBuffer()` could return
None even before, if a visible window for `bufnr` can not be found.
Boris Staletic 1 年之前
父节点
当前提交
4b3df69efa
共有 2 个文件被更改,包括 11 次插入1 次删除
  1. 7 0
      python/ycm/scrolling_range.py
  2. 4 1
      python/ycm/vimsupport.py

+ 7 - 0
python/ycm/scrolling_range.py

@@ -55,6 +55,13 @@ class ScrollingBufferRange( object ):
     #  - look up the actual visible range, then call this function
     #  - if not overlapping, do the factor expansion and request
     self._last_requested_range = vimsupport.RangeVisibleInBuffer( self._bufnr )
+    # If this is false, either the self._bufnr is not a valid buffer number or
+    # the buffer is not visible in any window.
+    # Since this is called asynchronously, a user may bwipeout a buffer with
+    # self._bufnr number between polls.
+    if self._last_requested_range is None:
+      return False
+
     self._tick = vimsupport.GetBufferChangedTick( self._bufnr )
 
     # We'll never use the last response again, so clear it

+ 4 - 1
python/ycm/vimsupport.py

@@ -206,7 +206,10 @@ def RangeVisibleInBuffer( bufnr, grow_factor=0.5 ):
     start: Location = Location()
     end: Location = Location()
 
-  buffer = vim.buffers[ bufnr ]
+  try:
+    buffer = vim.buffers[ bufnr ]
+  except KeyError:
+    return None
 
   if not windows:
     return None