IdentifierCompleter.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (C) 2011, 2012 Google Inc.
  2. //
  3. // This file is part of YouCompleteMe.
  4. //
  5. // YouCompleteMe is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // YouCompleteMe is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. #include "IdentifierCompleter.h"
  18. #include "standard.h"
  19. #include "Candidate.h"
  20. #include "IdentifierUtils.h"
  21. #include "Result.h"
  22. #include "Utils.h"
  23. #include "ReleaseGil.h"
  24. #include <algorithm>
  25. namespace YouCompleteMe {
  26. IdentifierCompleter::IdentifierCompleter() {}
  27. IdentifierCompleter::IdentifierCompleter(
  28. const std::vector< std::string > &candidates ) {
  29. identifier_database_.AddIdentifiers( candidates, "", "" );
  30. }
  31. IdentifierCompleter::IdentifierCompleter(
  32. const std::vector< std::string > &candidates,
  33. const std::string &filetype,
  34. const std::string &filepath ) {
  35. identifier_database_.AddIdentifiers( candidates, filetype, filepath );
  36. }
  37. void IdentifierCompleter::AddIdentifiersToDatabase(
  38. const std::vector< std::string > &new_candidates,
  39. const std::string &filetype,
  40. const std::string &filepath ) {
  41. ReleaseGil unlock;
  42. identifier_database_.AddIdentifiers( new_candidates,
  43. filetype,
  44. filepath );
  45. }
  46. void IdentifierCompleter::AddIdentifiersToDatabaseFromTagFiles(
  47. const std::vector< std::string > &absolute_paths_to_tag_files ) {
  48. ReleaseGil unlock;
  49. foreach( const std::string & path, absolute_paths_to_tag_files ) {
  50. identifier_database_.AddIdentifiers(
  51. ExtractIdentifiersFromTagsFile( path ) );
  52. }
  53. }
  54. void IdentifierCompleter::AddIdentifiersToDatabaseFromBuffer(
  55. const std::string &buffer_contents,
  56. const std::string &filetype,
  57. const std::string &filepath,
  58. bool collect_from_comments_and_strings ) {
  59. ReleaseGil unlock;
  60. identifier_database_.ClearCandidatesStoredForFile( filetype, filepath );
  61. std::string new_contents =
  62. collect_from_comments_and_strings ?
  63. buffer_contents :
  64. RemoveIdentifierFreeText( buffer_contents );
  65. identifier_database_.AddIdentifiers(
  66. ExtractIdentifiersFromText( new_contents ),
  67. filetype,
  68. filepath );
  69. }
  70. std::vector< std::string > IdentifierCompleter::CandidatesForQuery(
  71. const std::string &query ) const {
  72. return CandidatesForQueryAndType( query, "" );
  73. }
  74. std::vector< std::string > IdentifierCompleter::CandidatesForQueryAndType(
  75. const std::string &query,
  76. const std::string &filetype ) const {
  77. ReleaseGil unlock;
  78. std::vector< Result > results;
  79. identifier_database_.ResultsForQueryAndType( query, filetype, results );
  80. std::vector< std::string > candidates;
  81. candidates.reserve( results.size() );
  82. foreach ( const Result & result, results ) {
  83. candidates.push_back( *result.Text() );
  84. }
  85. return candidates;
  86. }
  87. } // namespace YouCompleteMe