File: resolve_completion_request.py

package info (click to toggle)
vim-youcompleteme 0%2B20200825%2Bgit2afee9d%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,016 kB
  • sloc: python: 9,384; sh: 167; cpp: 51; makefile: 25; ansic: 4; xml: 1
file content (92 lines) | stat: -rw-r--r-- 3,282 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Copyright (C) 2020 YouCompleteMe contributors
#
# This file is part of YouCompleteMe.
#
# YouCompleteMe is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# YouCompleteMe is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with YouCompleteMe.  If not, see <http://www.gnu.org/licenses/>.

from ycm.client.base_request import ( BaseRequest,
                                      DisplayServerException,
                                      MakeServerException )
from ycm.client.completion_request import ConvertCompletionDataToVimData

import logging
import json
_logger = logging.getLogger( __name__ )


class ResolveCompletionRequest( BaseRequest ):
  def __init__( self, request_data ):
    super().__init__()
    self.request_data = request_data

  def Start( self ):
    self._response_future = self.PostDataToHandlerAsync( self.request_data,
                                                         'resolve_completion' )

  def Done( self ):
    return bool( self._response_future ) and self._response_future.done()


  def OnCompleteDone( self ):
    # This is required to be compatible with the "CompletionRequest" API. We're
    # not really a CompletionRequest, but we are mutually exclusive with
    # completion requests, so we impleent this API.
    pass


  def Response( self ):
    response = self.HandleFuture( self._response_future,
                                  truncate_message = True,
                                  display_message = True )

    if not response or not response[ 'completion' ]:
      return { 'completion': [] }

    # Vim may not be able to convert the 'errors' entry to its internal format
    # so we remove it from the response.
    errors = response.pop( 'errors', [] )
    for e in errors:
      exception = MakeServerException( e )
      _logger.error( exception )
      DisplayServerException( exception, truncate_message = True )

    response[ 'completion' ] = ConvertCompletionDataToVimData(
        response[ 'completion' ] )
    return response


def ResolveCompletionItem( completion_request, item ):
  if not completion_request.Done():
    return None
  try:
    completion_extra_data = json.loads( item[ 'user_data' ] )
  except KeyError:
    return None
  except ( TypeError, json.JSONDecodeError ):
    # Can happen with the omni completer
    return None

  request_data = completion_request.request_data
  try:
    # Note: We mutate the request_data inside the original completion request
    # and pass it into the new request object. this is just a big efficiency
    # saving. The request_data for a Done() request is almost certainly no
    # longer needed.
    request_data[ 'resolve' ] = completion_extra_data[ 'resolve' ]
  except KeyError:
    return None

  resolve_request = ResolveCompletionRequest( request_data )
  resolve_request.Start()
  return resolve_request