Explorar o código

Inlay Hints: cache results and reinstate on insert leave

Ben Jackson %!s(int64=2) %!d(string=hai) anos
pai
achega
777f61c816
Modificáronse 3 ficheiros con 27 adicións e 30 borrados
  1. 9 4
      autoload/youcompleteme.vim
  2. 1 17
      python/ycm/buffer.py
  3. 17 9
      python/ycm/inlay_hints.py

+ 9 - 4
autoload/youcompleteme.vim

@@ -797,7 +797,7 @@ function! s:UpdateInlayHints( bufnr )
         \ get( b:, 'ycm_enable_inlay_hints',
         \   get( g:, 'ycm_enable_inlay_hints', 0 ) )
 
-    py3 ycm_state.Buffer( int( vim.eval( 'a:bufnr' ) ) ).SendInlayHintsRequest()
+    py3 ycm_state.Buffer( int( vim.eval( 'a:bufnr' ) ) ).inlay_hints.Request()
     let s:pollers.inlay_hints.id = timer_start(
           \ s:pollers.inlay_hints.wait_milliseconds,
           \ function( 's:PollInlayHints', [ a:bufnr ] ) )
@@ -840,12 +840,14 @@ endfunction
 
 function! s:PollInlayHints( bufnr, ... )
   if !py3eval(
-      \ 'ycm_state.Buffer( int( vim.eval( "a:bufnr" ) ) ).InlayHintsReady()' )
+      \ 'ycm_state.Buffer( int( vim.eval( "a:bufnr" ) ) )'
+      \ . '.inlay_hints.Ready()' )
     let s:pollers.inlay_hints.id = timer_start(
           \ s:pollers.inlay_hints.wait_milliseconds,
           \ function( 's:PollInlayHints', [ a:bufnr ] ) )
   elseif ! py3eval(
-      \ 'ycm_state.Buffer( int( vim.eval( "a:bufnr" ) ) ).UpdateInlayHints()' )
+      \ 'ycm_state.Buffer( int( vim.eval( "a:bufnr" ) ) )'
+      \ . '.inlay_hints.Update()' )
     let s:pollers.inlay_hints.id = timer_start(
           \ s:pollers.inlay_hints.wait_milliseconds,
           \ function( 's:PollInlayHints', [ a:bufnr ] ) )
@@ -982,7 +984,7 @@ endfunction
 
 function! s:OnInsertEnter() abort
   let s:current_cursor_position = getpos( '.' )
-  py3 ycm_state.CurrentBuffer().ClearInlayHints()
+  py3 ycm_state.CurrentBuffer().inlay_hints.Clear()
 endfunction
 
 function! s:OnInsertLeave()
@@ -1004,6 +1006,9 @@ function! s:OnInsertLeave()
   endif
 
   call s:ClearSignatureHelp()
+  " We cleared inlay hints on insert enter
+  " TODO: Probalby should use ModeChange
+  py3 ycm_state.CurrentBuffer().inlay_hints.Refresh()
 endfunction
 
 

+ 1 - 17
python/ycm/buffer.py

@@ -38,7 +38,7 @@ class Buffer:
     self._open_loclist_on_ycm_diags = user_options[
                                         'open_loclist_on_ycm_diags' ]
     self._semantic_highlighting = SemanticHighlighting( bufnr, user_options )
-    self._inlay_hints = InlayHints( bufnr, user_options )
+    self.inlay_hints = InlayHints( bufnr, user_options )
     self.UpdateFromFileTypes( filetypes )
 
 
@@ -149,22 +149,6 @@ class Buffer:
     return self._semantic_highlighting.Update()
 
 
-  def SendInlayHintsRequest( self ):
-    self._inlay_hints.SendRequest()
-
-
-  def InlayHintsReady( self ):
-    return self._inlay_hints.IsResponseReady()
-
-
-  def UpdateInlayHints( self ):
-    return self._inlay_hints.Update()
-
-
-  def ClearInlayHints( self ):
-    return self._inlay_hints.Clear()
-
-
   def _ChangedTick( self ):
     return vimsupport.GetBufferChangedTick( self._number )
 

+ 17 - 9
python/ycm/inlay_hints.py

@@ -58,10 +58,11 @@ class InlayHints:
     self._bufnr = bufnr
     self._prop_ids = set()
     self.tick = -1
+    self._latest_inlay_hints = []
 
 
-  def SendRequest( self ):
-    if self._request and not self.IsResponseReady():
+  def Request( self ):
+    if self._request and not self.Ready():
       return
 
     self.tick = vimsupport.GetBufferChangedTick( self._bufnr )
@@ -79,7 +80,8 @@ class InlayHints:
     self._request = InlayHintsRequest( request_data )
     self._request.Start()
 
-  def IsResponseReady( self ):
+
+  def Ready( self ):
     return self._request is not None and self._request.Done()
 
 
@@ -94,11 +96,12 @@ class InlayHints:
       # Nothing to update
       return True
 
-    assert self.IsResponseReady()
+    assert self.Ready()
 
     # We're ready to use this response. Clear it (to avoid repeatedly
     # re-polling).
-    inlay_hints = self._request.Response()
+    self._latest_inlay_hints = [] ;# in case there was an error in request
+    self._latest_inlay_hints = self._request.Response()
     self._request = None
 
     if self.tick != vimsupport.GetBufferChangedTick( self._bufnr ):
@@ -106,10 +109,17 @@ class InlayHints:
       self.SendRequest()
       return False # poll again
 
+    self.Refresh()
+
+    # No need to re-poll
+    return True
+
+
+  def Refresh( self ):
     self.Clear()
 
-    for inlay_hint in inlay_hints:
-      if 'kind' not in  inlay_hint:
+    for inlay_hint in self._latest_inlay_hints:
+      if 'kind' not in inlay_hint:
         prop_type = 'YCM_INLAY_UNKNOWN'
       elif inlay_hint[ 'kind' ] not in HIGHLIGHT_GROUP:
         prop_type = 'YCM_INLAY_UNKNOWN'
@@ -128,5 +138,3 @@ class InlayHints:
                               'text': inlay_hint[ 'label' ]
                             } ) )
 
-    # No need to re-poll
-    return True