Skip to content

Commit

Permalink
Refactor large completion item methods
Browse files Browse the repository at this point in the history
  • Loading branch information
puremourning committed Oct 22, 2017
1 parent 5168175 commit f5f2863
Showing 1 changed file with 43 additions and 31 deletions.
74 changes: 43 additions & 31 deletions ycmd/completers/language_server/language_server_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,15 +754,9 @@ def ResolveCompletionItems( self, items, request_data ):

# Build a ycmd-compatible completion for the text as we received it. Later
# we might mofify insertion_text should we see a lower start codepoint.
completions.append( responses.BuildCompletionData(
insertion_text,
extra_menu_info = item.get( 'detail', None ),
detailed_info = ( item[ 'label' ] +
'\n\n' +
item.get( 'documentation', '' ) ),
menu_text = item[ 'label' ],
kind = lsapi.ITEM_KIND[ item.get( 'kind', 0 ) ],
extra_data = fixits ) )
completions.append( CompletionItemToCompletionData( insertion_text,
item,
fixits ) )
start_codepoints.append( start_codepoint )

if ( len( completions ) > 1 and
Expand Down Expand Up @@ -1201,6 +1195,18 @@ def RefactorRename( self, request_data, args ):
[ WorkspaceEditToFixIt( request_data, response[ 'result' ] ) ] )


def CompletionItemToCompletionData( insertion_text, item, fixits ):
return responses.BuildCompletionData(
insertion_text,
extra_menu_info = item.get( 'detail', None ),
detailed_info = ( item[ 'label' ] +
'\n\n' +
item.get( 'documentation', '' ) ),
menu_text = item[ 'label' ],
kind = lsp.ITEM_KIND[ item.get( 'kind', 0 ) ],
extra_data = fixits )


def FixUpCompletionPrefixes( completions,
start_codepoints,
request_data,
Expand Down Expand Up @@ -1263,30 +1269,12 @@ def InsertionTextForItem( request_data, item ):
# opposed to 'content assist').
if 'textEdit' in item and item[ 'textEdit' ]:
textEdit = item[ 'textEdit' ]
edit_range = textEdit[ 'range' ]
start_codepoint = edit_range[ 'start' ][ 'character' ] + 1
end_codepoint = edit_range[ 'end' ][ 'character' ] + 1

# Conservatively rejecting candidates that breach the protocol
if edit_range[ 'start' ][ 'line' ] != edit_range[ 'end' ][ 'line' ]:
raise IncompatibleCompletionException(
"The TextEdit '{0}' spans multiple lines".format(
textEdit[ 'newText' ] ) )

if start_codepoint > request_data[ 'start_codepoint' ]:
raise IncompatibleCompletionException(
"The TextEdit '{0}' starts after the start position".format(
textEdit[ 'newText' ] ) )

if end_codepoint < request_data[ 'start_codepoint' ]:
raise IncompatibleCompletionException(
"The TextEdit '{0}' ends before the start position".format(
textEdit[ 'newText' ] ) )

start_codepoint = _GetCompletionItemStartCodepointOrReject( textEdit,
request_data )

insertion_text = textEdit[ 'newText' ]

if '\n' in textEdit[ 'newText' ]:
if '\n' in insertion_text:
# jdt.ls can return completions which generate code, such as
# getters/setters and entire anonymous classes.
#
Expand All @@ -1305,7 +1293,7 @@ def InsertionTextForItem( request_data, item ):
#
# These sorts of completions aren't really in the spirit of ycmd at the
# moment anyway. So for now, we just ignore this candidate.
raise IncompatibleCompletionException( textEdit[ 'newText' ] )
raise IncompatibleCompletionException( insertion_text )

additional_text_edits.extend( item.get( 'additionalTextEdits', [] ) )

Expand All @@ -1322,6 +1310,30 @@ def InsertionTextForItem( request_data, item ):
return ( insertion_text, fixits, start_codepoint )


def _GetCompletionItemStartCodepointOrReject( textEdit, request_data ):
edit_range = textEdit[ 'range' ]
start_codepoint = edit_range[ 'start' ][ 'character' ] + 1
end_codepoint = edit_range[ 'end' ][ 'character' ] + 1

# Conservatively rejecting candidates that breach the protocol
if edit_range[ 'start' ][ 'line' ] != edit_range[ 'end' ][ 'line' ]:
raise IncompatibleCompletionException(
"The TextEdit '{0}' spans multiple lines".format(
textEdit[ 'newText' ] ) )

if start_codepoint > request_data[ 'start_codepoint' ]:
raise IncompatibleCompletionException(
"The TextEdit '{0}' starts after the start position".format(
textEdit[ 'newText' ] ) )

if end_codepoint < request_data[ 'start_codepoint' ]:
raise IncompatibleCompletionException(
"The TextEdit '{0}' ends before the start position".format(
textEdit[ 'newText' ] ) )

return start_codepoint


def LocationListToGoTo( request_data, response ):
"""Convert a LSP list of locations to a ycmd GoTo response."""
if not response:
Expand Down

0 comments on commit f5f2863

Please sign in to comment.