YouCompleteMe/python/ycm.py

216 lines
5.6 KiB
Python
Raw Normal View History

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 re
import vim
import indexer
min_num_chars = int( vim.eval( "g:ycm_min_num_of_chars_for_completion" ) )
2012-05-08 00:23:38 -04:00
2012-04-15 19:57:10 -04:00
class CompletionSystem( object ):
def __init__( self ):
self.completer = indexer.Completer()
self.completer.EnableThreading()
2012-04-15 19:57:10 -04:00
self.pattern = re.compile( r"[_a-zA-Z]\w*" )
self.future = None
2012-04-15 19:57:10 -04:00
2012-05-08 00:23:38 -04:00
def CandidatesForQueryAsync( self, query ):
filetype = vim.eval( "&filetype" )
self.future = self.completer.CandidatesForQueryAndTypeAsync(
SanitizeQuery( query ),
filetype )
2012-05-08 00:23:38 -04:00
def AsyncCandidateRequestReady( self ):
return self.future.ResultsReady()
2012-05-08 00:23:38 -04:00
def CandidatesFromStoredRequest( self ):
if not self.future:
return []
results = []
self.future.GetResults( results )
return results
2012-04-15 19:57:10 -04:00
2012-05-08 00:23:38 -04:00
def AddIdentifier( self, identifier ):
# print identifier
filetype = vim.eval( "&filetype" )
filepath = vim.eval( "expand('%:p')" )
if not filetype or not filepath or not identifier:
return
self.completer.AddCandidatesToDatabase( [ identifier ],
filetype,
filepath,
False )
def AddPreviousIdentifier( self ):
self.AddIdentifier( PreviousIdentifier() )
2012-04-15 19:57:10 -04:00
def AddBufferIdentifiers( self ):
text = "\n".join( vim.current.buffer )
text = RemoveIdentFreeText( text )
2012-04-15 19:57:10 -04:00
idents = re.findall( self.pattern, text )
filetype = vim.eval( "&filetype" )
filepath = vim.eval( "expand('%:p')" )
if not filetype or not filepath:
return
self.completer.AddCandidatesToDatabase( idents,
filetype,
filepath,
True )
2012-04-15 19:57:10 -04:00
2012-05-08 00:23:38 -04:00
2012-04-15 19:57:10 -04:00
def CurrentColumn():
"""Do NOT access the CurrentColumn in vim.current.line. It doesn't exist yet.
Only the chars befor the current column exist in vim.current.line."""
2012-04-15 19:57:10 -04:00
# vim's columns start at 1 while vim.current.line columns start at 0
return int( vim.eval( "col('.')" ) ) - 1
2012-05-08 00:23:38 -04:00
def CurrentLineAndColumn():
result = vim.eval( "getpos('.')")
line_num = int( result[ 1 ] ) - 1
column_num = int( result[ 2 ] ) - 1
return line_num, column_num
def IsIdentifierChar( char ):
return char.isalnum() or char == '_'
2012-04-15 19:57:10 -04:00
def CompletionStartColumn():
line = vim.current.line
current_column = CurrentColumn()
start_column = current_column
while start_column > 0 and IsIdentifierChar( line[ start_column - 1 ] ):
2012-04-15 19:57:10 -04:00
start_column -= 1
if current_column - start_column < min_num_chars:
# for vim, -2 means not found but don't trigger an error message
return -2
2012-04-15 19:57:10 -04:00
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
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
if end_column - start_column < min_num_chars:
return ""
return line[ start_column : end_column ]
2012-04-15 19:57:10 -04:00
def CurrentCursorText():
start_column = CompletionStartColumn()
current_column = CurrentColumn()
if current_column - start_column < min_num_chars:
return ""
return vim.current.line[ start_column : current_column ]
def CurrentCursorTextVim():
return EscapeForVim( CurrentCursorText() )
def ShouldAddIdentifier():
current_column = CurrentColumn()
previous_char_index = current_column - 1
if previous_char_index < 0:
return True
line = vim.current.line
try:
previous_char = line[ previous_char_index ]
except IndexError:
return False
if IsIdentifierChar( previous_char ):
return False
if ( not IsIdentifierChar( previous_char ) and
previous_char_index > 0 and
IsIdentifierChar( line[ previous_char_index - 1 ] ) ):
return True
else:
return line[ : current_column ].isspace()
2012-04-15 19:57:10 -04:00
def SanitizeQuery( query ):
return query.strip()
2012-05-08 00:23:38 -04:00
def RemoveIdentFreeText( text ):
"""Removes commented-out code and code in quotes."""
# TODO: do we still need this sub-func?
2012-04-15 19:57:10 -04:00
def replacer( match ):
s = match.group( 0 )
if s.startswith( '/' ):
return ""
else:
return s
pattern = re.compile(
r'//.*?$|#.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
2012-04-15 19:57:10 -04:00
re.DOTALL | re.MULTILINE )
return re.sub( pattern, replacer, text )