def GetUnsavedFilesVector(self): # 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 # objects outlive our UnsavedFile objects. This is why we need the # contents_holder and filename_holder lists, to make sure the string objects # are still around when we call CandidatesForQueryAndLocationInFile. We do # this to avoid an extra copy of the entire file contents. files = ycm_core.UnsavedFileVec() self.contents_holder = [] self.filename_holder = [] for buffer in vimsupport.GetUnsavedBuffers(): if not ClangAvailableForBuffer(buffer): continue contents = '\n'.join(buffer) name = buffer.name if not contents or not name: continue self.contents_holder.append(contents) self.filename_holder.append(name) unsaved_file = ycm_core.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
def get_unsaved_files_vector(files): files_vector = ycm_core.UnsavedFileVector() for f in files: unsaved_file = ycm_core.UnsavedFile() contents = open(f).read() unsaved_file.contents_ = contents unsaved_file.length_ = len(contents) unsaved_file.filename_ = to_utf8(f) files_vector.append(unsaved_file) return files_vector
def CppBindings_UnsavedFile_test(): unsaved_file = ycm_core.UnsavedFile() filename = ToCppStr( 'foo' ) contents = ToCppStr( 'bar\\n' ) length = len( contents ) unsaved_file.filename_ = filename unsaved_file.contents_ = contents unsaved_file.length_ = length del filename del contents del length assert_that( unsaved_file, has_properties( { 'filename_': 'foo', 'contents_': 'bar\\n', 'length_': len( 'bar\\n' ) } ) )
def GetUnsavedFilesVector(self, request_data): files = ycm_core.UnsavedFileVector() for filename, file_data in request_data['file_data'].iteritems(): if not ClangAvailableForFiletypes(file_data['filetypes']): continue contents = file_data['contents'] if not contents or not filename: continue unsaved_file = ycm_core.UnsavedFile() utf8_contents = ToUtf8IfNeeded(contents) unsaved_file.contents_ = utf8_contents unsaved_file.length_ = len(utf8_contents) unsaved_file.filename_ = ToUtf8IfNeeded(filename) files.append(unsaved_file) return files
def CppBindings_UnsavedFileVector_test(): unsaved_file_vector = ycm_core.UnsavedFileVector() unsaved_file = ycm_core.UnsavedFile() unsaved_file.filename_ = ToCppStr('foo') unsaved_file.contents_ = ToCppStr('bar') unsaved_file.length_ = 3 unsaved_file_vector.append(unsaved_file) EmplaceBack(unsaved_file_vector, unsaved_file) del unsaved_file assert_that( unsaved_file_vector, contains( has_properties({ 'filename_': 'foo', 'contents_': 'bar', 'length_': len('bar') }), has_properties({ 'filename_': 'foo', 'contents_': 'bar', 'length_': len('bar') })))