2012-04-15 19:57:10 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2012-04-15 23:28:46 -04:00
|
|
|
# Copyright (C) 2011, 2012 Strahinja Val Markovic <val@markovic.io>
|
2012-04-15 19:57:10 -04:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
import vim
|
|
|
|
import indexer
|
|
|
|
|
2012-07-21 15:06:18 -04:00
|
|
|
MIN_NUM_CHARS = int( vim.eval( "g:ycm_min_num_of_chars_for_completion" ) )
|
2012-07-23 14:15:25 -04:00
|
|
|
CLANG_COMPLETION_ENABLED = int( vim.eval( "g:ycm_clang_completion_enabled" ) )
|
2012-07-21 15:06:18 -04:00
|
|
|
CLANG_FILETYPES = set( [ 'c', 'cpp', 'objc', 'objcpp' ] )
|
2012-07-21 15:17:29 -04:00
|
|
|
MAX_IDENTIFIER_COMPLETIONS_RETURNED = 10
|
2012-07-21 15:06:18 -04:00
|
|
|
|
2012-05-08 00:23:38 -04:00
|
|
|
|
2012-07-15 21:11:26 -04:00
|
|
|
class Completer( object ):
|
|
|
|
def __init__( self ):
|
|
|
|
self.future = None
|
|
|
|
|
2012-07-21 18:33:59 -04:00
|
|
|
|
2012-07-15 21:11:26 -04:00
|
|
|
def AsyncCandidateRequestReady( self ):
|
2012-07-26 23:50:56 -04:00
|
|
|
if not self.future:
|
|
|
|
# We return True so that the caller can extract the default value from the
|
|
|
|
# future
|
|
|
|
return True
|
2012-07-15 21:11:26 -04:00
|
|
|
return self.future.ResultsReady()
|
|
|
|
|
|
|
|
|
|
|
|
def CandidatesFromStoredRequest( self ):
|
|
|
|
if not self.future:
|
|
|
|
return []
|
|
|
|
return self.future.GetResults()
|
|
|
|
|
2012-07-11 02:13:12 -04:00
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
def OnFileReadyToParse( self ):
|
2012-07-21 18:33:59 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2012-07-15 21:11:26 -04:00
|
|
|
class IdentifierCompleter( Completer ):
|
2012-04-15 19:57:10 -04:00
|
|
|
def __init__( self ):
|
2012-07-11 02:13:12 -04:00
|
|
|
self.completer = indexer.IdentifierCompleter()
|
2012-05-06 02:48:22 -04:00
|
|
|
self.completer.EnableThreading()
|
2012-04-15 19:57:10 -04:00
|
|
|
|
2012-05-08 00:23:38 -04:00
|
|
|
|
2012-05-06 02:48:22 -04:00
|
|
|
def CandidatesForQueryAsync( self, query ):
|
2012-04-29 22:51:20 -04:00
|
|
|
filetype = vim.eval( "&filetype" )
|
2012-05-06 02:48:22 -04:00
|
|
|
self.future = self.completer.CandidatesForQueryAndTypeAsync(
|
|
|
|
SanitizeQuery( query ),
|
|
|
|
filetype )
|
|
|
|
|
2012-05-08 00:23:38 -04:00
|
|
|
|
2012-05-12 18:20:03 -04:00
|
|
|
def AddIdentifier( self, identifier ):
|
|
|
|
filetype = vim.eval( "&filetype" )
|
|
|
|
filepath = vim.eval( "expand('%:p')" )
|
|
|
|
|
|
|
|
if not filetype or not filepath or not identifier:
|
|
|
|
return
|
|
|
|
|
2012-07-08 18:34:44 -04:00
|
|
|
vector = indexer.StringVec()
|
|
|
|
vector.append( identifier )
|
|
|
|
self.completer.AddCandidatesToDatabase( vector,
|
2012-05-12 18:20:03 -04:00
|
|
|
filetype,
|
2012-07-23 23:17:59 -04:00
|
|
|
filepath )
|
2012-05-12 18:20:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
def AddPreviousIdentifier( self ):
|
|
|
|
self.AddIdentifier( PreviousIdentifier() )
|
|
|
|
|
|
|
|
|
2012-04-15 19:57:10 -04:00
|
|
|
def AddBufferIdentifiers( self ):
|
2012-04-29 22:51:20 -04:00
|
|
|
filetype = vim.eval( "&filetype" )
|
2012-04-29 19:36:31 -04:00
|
|
|
filepath = vim.eval( "expand('%:p')" )
|
2012-05-05 21:12:15 -04:00
|
|
|
|
|
|
|
if not filetype or not filepath:
|
|
|
|
return
|
|
|
|
|
2012-07-23 23:17:59 -04:00
|
|
|
text = "\n".join( vim.current.buffer )
|
2012-07-24 23:09:09 -04:00
|
|
|
self.completer.AddCandidatesToDatabaseFromBufferAsync( text,
|
|
|
|
filetype,
|
|
|
|
filepath )
|
2012-04-15 19:57:10 -04:00
|
|
|
|
2012-07-21 18:33:59 -04:00
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
def OnFileReadyToParse( self ):
|
2012-07-21 18:33:59 -04:00
|
|
|
self.AddBufferIdentifiers()
|
|
|
|
|
|
|
|
|
2012-07-21 15:06:18 -04:00
|
|
|
def CandidatesFromStoredRequest( self ):
|
|
|
|
if not self.future:
|
|
|
|
return []
|
2012-07-21 15:17:29 -04:00
|
|
|
completions = self.future.GetResults()[
|
|
|
|
: MAX_IDENTIFIER_COMPLETIONS_RETURNED ]
|
|
|
|
|
|
|
|
# We will never have duplicates in completions so with 'dup':1 we tell Vim
|
|
|
|
# to add this candidate even if it's a duplicate of an existing one (which
|
|
|
|
# will never happen). This saves us some expensive string matching
|
|
|
|
# operations in Vim.
|
|
|
|
return [ { 'word': x, 'dup': 1 } for x in completions ]
|
2012-07-21 15:06:18 -04:00
|
|
|
|
2012-07-15 23:49:56 -04:00
|
|
|
|
2012-07-15 21:11:26 -04:00
|
|
|
class ClangCompleter( Completer ):
|
2012-07-10 18:26:07 -04:00
|
|
|
def __init__( self ):
|
2012-07-11 02:28:58 -04:00
|
|
|
self.completer = indexer.ClangCompleter()
|
2012-07-15 23:49:56 -04:00
|
|
|
self.completer.EnableThreading()
|
|
|
|
self.contents_holder = []
|
|
|
|
self.filename_holder = []
|
2012-07-10 18:26:07 -04:00
|
|
|
|
2012-07-22 18:19:28 -04:00
|
|
|
|
2012-07-23 21:35:48 -04:00
|
|
|
def GetUnsavedFilesVector( self ):
|
|
|
|
files = indexer.UnsavedFileVec()
|
|
|
|
self.contents_holder = []
|
|
|
|
self.filename_holder = []
|
|
|
|
for buffer in GetUnsavedBuffers():
|
|
|
|
self.contents_holder.append( '\n'.join( buffer ) )
|
|
|
|
self.filename_holder.append( buffer.name )
|
|
|
|
|
|
|
|
unsaved_file = indexer.UnsavedFile()
|
|
|
|
unsaved_file.contents_ = self.contents_holder[ -1 ]
|
|
|
|
unsaved_file.length_ = len( self.contents_holder[ -1 ] )
|
|
|
|
unsaved_file.filename_ = self.filename_holder[ -1 ]
|
|
|
|
|
|
|
|
files.append( unsaved_file )
|
|
|
|
|
|
|
|
return files
|
|
|
|
|
|
|
|
|
2012-07-15 23:49:56 -04:00
|
|
|
def CandidatesForQueryAsync( self, query ):
|
2012-07-26 23:50:56 -04:00
|
|
|
if self.completer.UpdatingTranslationUnit():
|
|
|
|
PostVimMessage( 'Still parsing file, no completions yet.' )
|
|
|
|
self.future = None
|
|
|
|
return
|
|
|
|
|
2012-07-15 22:40:24 -04:00
|
|
|
# TODO: sanitize query
|
2012-07-10 18:26:07 -04:00
|
|
|
|
|
|
|
# CAREFUL HERE! For UnsavedFile filename and contents we are referring
|
|
|
|
# directly to Python-allocated and -managed memory since we are accepting
|
|
|
|
# pointers to data members of python objects. We need to ensure that those
|
2012-07-10 23:27:46 -04:00
|
|
|
# objects outlive our UnsavedFile objects. This is why we need the
|
|
|
|
# contents_holder and filename_holder lists, to make sure the string objects
|
2012-07-15 23:49:56 -04:00
|
|
|
# are still around when we call CandidatesForQueryAndLocationInFile. We do
|
|
|
|
# this to avoid an extra copy of the entire file contents.
|
2012-07-10 18:26:07 -04:00
|
|
|
|
2012-07-23 21:35:48 -04:00
|
|
|
files = indexer.UnsavedFileVec()
|
2012-07-15 23:49:56 -04:00
|
|
|
if not query:
|
2012-07-23 21:35:48 -04:00
|
|
|
files = self.GetUnsavedFilesVector()
|
2012-07-10 18:26:07 -04:00
|
|
|
|
2012-07-10 23:50:03 -04:00
|
|
|
line, _ = vim.current.window.cursor
|
2012-07-10 18:39:59 -04:00
|
|
|
column = int( vim.eval( "s:completion_start_column" ) ) + 1
|
2012-07-10 23:27:46 -04:00
|
|
|
current_buffer = vim.current.buffer
|
2012-07-15 23:49:56 -04:00
|
|
|
self.future = self.completer.CandidatesForQueryAndLocationInFileAsync(
|
|
|
|
query,
|
|
|
|
current_buffer.name,
|
|
|
|
line,
|
|
|
|
column,
|
|
|
|
files )
|
2012-05-08 00:23:38 -04:00
|
|
|
|
2012-07-10 23:27:46 -04:00
|
|
|
|
2012-07-21 13:10:19 -04:00
|
|
|
def CandidatesFromStoredRequest( self ):
|
|
|
|
if not self.future:
|
|
|
|
return []
|
2012-07-28 15:06:59 -04:00
|
|
|
results = [ CompletionDataToDict( x ) for x in self.future.GetResults() ]
|
|
|
|
if not results:
|
|
|
|
PostVimMessage( 'No completions found; errors in the file?' )
|
|
|
|
return results
|
2012-07-21 13:10:19 -04:00
|
|
|
|
|
|
|
|
2012-07-28 15:06:38 -04:00
|
|
|
def OnFileReadyToParse( self ):
|
2012-07-26 23:50:56 -04:00
|
|
|
self.future = self.completer.UpdateTranslationUnitAsync(
|
2012-07-23 21:35:48 -04:00
|
|
|
vim.current.buffer.name,
|
|
|
|
self.GetUnsavedFilesVector() )
|
2012-07-22 18:19:28 -04:00
|
|
|
|
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
def PostVimMessage( message ):
|
|
|
|
# TODO: escape the message string before formating it
|
|
|
|
vim.command( 'echohl WarningMsg | echomsg "{0}" | echohl None'
|
|
|
|
.format( message ) )
|
|
|
|
|
2012-07-22 18:19:28 -04:00
|
|
|
|
2012-07-10 23:27:46 -04:00
|
|
|
def GetUnsavedBuffers():
|
|
|
|
def BufferModified( buffer_number ):
|
|
|
|
to_eval = 'getbufvar({0}, "&mod")'.format( buffer_number )
|
|
|
|
return bool( int( vim.eval( to_eval ) ) )
|
|
|
|
|
|
|
|
return ( x for x in vim.buffers if BufferModified( x.number ) )
|
|
|
|
|
|
|
|
|
2012-07-20 00:17:39 -04:00
|
|
|
def CompletionDataToDict( completion_data ):
|
|
|
|
# see :h complete-items for a description of the dictionary fields
|
|
|
|
return {
|
|
|
|
'word' : completion_data.TextToInsertInBuffer(),
|
|
|
|
'abbr' : completion_data.original_string_,
|
|
|
|
'menu' : completion_data.extra_menu_info_,
|
|
|
|
'kind' : completion_data.kind_,
|
2012-07-21 15:17:29 -04:00
|
|
|
'dup' : 1,
|
2012-07-20 00:17:39 -04:00
|
|
|
# TODO: add detailed_info_ as 'info'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-15 19:57:10 -04:00
|
|
|
def CurrentColumn():
|
2012-05-12 18:20:03 -04:00
|
|
|
"""Do NOT access the CurrentColumn in vim.current.line. It doesn't exist yet.
|
2012-07-10 23:50:03 -04:00
|
|
|
Only the chars before the current column exist in vim.current.line."""
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-07-10 23:50:03 -04:00
|
|
|
# vim's columns are 1-based while vim.current.line columns are 0-based
|
|
|
|
# ... but vim.current.window.cursor (which returns a (line, column) tuple)
|
|
|
|
# columns are 0-based, while the line from that same tuple is 1-based.
|
2012-07-15 21:11:26 -04:00
|
|
|
# vim.buffers buffer objects OTOH have 0-based lines and columns.
|
2012-07-10 23:50:03 -04:00
|
|
|
# Pigs have wings and I'm a loopy purple duck. Everything makes sense now.
|
|
|
|
return vim.current.window.cursor[ 1 ]
|
2012-04-15 19:57:10 -04:00
|
|
|
|
2012-05-08 00:23:38 -04:00
|
|
|
|
2012-05-12 18:20:03 -04:00
|
|
|
def CurrentLineAndColumn():
|
2012-07-10 23:50:03 -04:00
|
|
|
# See the comment in CurrentColumn about the calculation for the line and
|
|
|
|
# column number
|
|
|
|
line, column = vim.current.window.cursor
|
|
|
|
line -= 1
|
|
|
|
return line, column
|
2012-05-12 18:20:03 -04:00
|
|
|
|
|
|
|
|
2012-07-23 21:35:48 -04:00
|
|
|
def ClangAvailableForFile():
|
2012-07-10 18:26:07 -04:00
|
|
|
filetype = vim.eval( "&filetype" )
|
2012-07-23 21:35:48 -04:00
|
|
|
return filetype in CLANG_FILETYPES
|
|
|
|
|
|
|
|
|
|
|
|
def ShouldUseClang( start_column ):
|
|
|
|
if not CLANG_COMPLETION_ENABLED or not ClangAvailableForFile():
|
2012-07-10 18:26:07 -04:00
|
|
|
return False
|
|
|
|
|
|
|
|
line = vim.current.line
|
|
|
|
previous_char_index = start_column - 1
|
|
|
|
if ( not len( line ) or
|
|
|
|
previous_char_index < 0 or
|
|
|
|
previous_char_index >= len( line ) ):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if line[ previous_char_index ] == '.':
|
|
|
|
return True
|
|
|
|
|
|
|
|
if previous_char_index - 1 < 0:
|
|
|
|
return False
|
|
|
|
|
|
|
|
two_previous_chars = line[ previous_char_index - 1 : start_column ]
|
|
|
|
if ( two_previous_chars == '->' or two_previous_chars == '::' ):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2012-05-12 18:20:03 -04:00
|
|
|
def IsIdentifierChar( char ):
|
|
|
|
return char.isalnum() or char == '_'
|
|
|
|
|
|
|
|
|
2012-04-15 19:57:10 -04:00
|
|
|
def CompletionStartColumn():
|
2012-07-10 18:26:07 -04:00
|
|
|
"""Returns the 0-based index where the completion string should start. So if
|
|
|
|
the user enters:
|
|
|
|
foo.bar^
|
|
|
|
with the cursor being at the location of the caret, then the starting column
|
|
|
|
would be the index of the letter 'b'.
|
|
|
|
"""
|
|
|
|
|
2012-04-15 19:57:10 -04:00
|
|
|
line = vim.current.line
|
2012-07-10 18:26:07 -04:00
|
|
|
start_column = CurrentColumn()
|
2012-04-15 19:57:10 -04:00
|
|
|
|
2012-05-12 18:20:03 -04:00
|
|
|
while start_column > 0 and IsIdentifierChar( line[ start_column - 1 ] ):
|
2012-04-15 19:57:10 -04:00
|
|
|
start_column -= 1
|
|
|
|
return start_column
|
|
|
|
|
2012-05-08 00:23:38 -04:00
|
|
|
|
2012-04-15 19:57:10 -04:00
|
|
|
def EscapeForVim( text ):
|
|
|
|
return text.replace( "'", "''" )
|
|
|
|
|
2012-05-08 00:23:38 -04:00
|
|
|
|
2012-05-12 18:20:03 -04:00
|
|
|
def PreviousIdentifier():
|
|
|
|
line_num, column_num = CurrentLineAndColumn()
|
|
|
|
buffer = vim.current.buffer
|
|
|
|
line = buffer[ line_num ]
|
|
|
|
|
|
|
|
end_column = column_num
|
|
|
|
|
|
|
|
while end_column > 0 and not IsIdentifierChar( line[ end_column - 1 ] ):
|
|
|
|
end_column -= 1
|
|
|
|
|
|
|
|
# Look at the previous line if we reached the end of the current one
|
|
|
|
if end_column == 0:
|
|
|
|
try:
|
|
|
|
line = buffer[ line_num - 1]
|
|
|
|
except:
|
|
|
|
return ""
|
|
|
|
end_column = len( line )
|
|
|
|
while end_column > 0 and not IsIdentifierChar( line[ end_column - 1 ] ):
|
|
|
|
end_column -= 1
|
|
|
|
print end_column, line
|
|
|
|
|
|
|
|
start_column = end_column
|
|
|
|
while start_column > 0 and IsIdentifierChar( line[ start_column - 1 ] ):
|
|
|
|
start_column -= 1
|
|
|
|
|
2012-07-21 15:06:18 -04:00
|
|
|
if end_column - start_column < MIN_NUM_CHARS:
|
2012-05-12 18:20:03 -04:00
|
|
|
return ""
|
|
|
|
|
|
|
|
return line[ start_column : end_column ]
|
|
|
|
|
|
|
|
|
2012-07-28 01:16:23 -04:00
|
|
|
def CurrentIdentifierFinished():
|
2012-05-12 18:20:03 -04:00
|
|
|
current_column = CurrentColumn()
|
|
|
|
previous_char_index = current_column - 1
|
|
|
|
if previous_char_index < 0:
|
2012-05-12 23:42:45 -04:00
|
|
|
return True
|
2012-05-12 18:20:03 -04:00
|
|
|
line = vim.current.line
|
|
|
|
try:
|
|
|
|
previous_char = line[ previous_char_index ]
|
|
|
|
except IndexError:
|
2012-05-12 23:42:45 -04:00
|
|
|
return False
|
2012-05-12 18:20:03 -04:00
|
|
|
|
|
|
|
if IsIdentifierChar( previous_char ):
|
2012-05-12 23:42:45 -04:00
|
|
|
return False
|
2012-05-12 18:20:03 -04:00
|
|
|
|
|
|
|
if ( not IsIdentifierChar( previous_char ) and
|
|
|
|
previous_char_index > 0 and
|
|
|
|
IsIdentifierChar( line[ previous_char_index - 1 ] ) ):
|
2012-05-12 23:42:45 -04:00
|
|
|
return True
|
2012-05-12 18:20:03 -04:00
|
|
|
else:
|
2012-05-12 23:42:45 -04:00
|
|
|
return line[ : current_column ].isspace()
|
2012-04-15 19:57:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
def SanitizeQuery( query ):
|
|
|
|
return query.strip()
|