LexerAutoComplete.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "LexerAutoComplete.h"
  27. #include <AK/Debug.h>
  28. #include <AK/HashTable.h>
  29. #include <LibCpp/Lexer.h>
  30. namespace LanguageServers::Cpp {
  31. Vector<GUI::AutocompleteProvider::Entry> LexerAutoComplete::get_suggestions(const String& code, const GUI::TextPosition& autocomplete_position)
  32. {
  33. auto lines = code.split('\n', true);
  34. Cpp::Lexer lexer(code);
  35. auto tokens = lexer.lex();
  36. auto index_of_target_token = token_in_position(tokens, autocomplete_position);
  37. if (!index_of_target_token.has_value())
  38. return {};
  39. auto suggestions = identifier_prefixes(lines, tokens, index_of_target_token.value());
  40. #if AUTOCOMPLETE_DEBUG
  41. for (auto& suggestion : suggestions) {
  42. dbgln("suggestion: {}", suggestion.completion);
  43. }
  44. #endif
  45. return suggestions;
  46. }
  47. StringView LexerAutoComplete::text_of_token(const Vector<String>& lines, const Cpp::Token& token)
  48. {
  49. ASSERT(token.m_start.line == token.m_end.line);
  50. ASSERT(token.m_start.column <= token.m_end.column);
  51. return lines[token.m_start.line].substring_view(token.m_start.column, token.m_end.column - token.m_start.column + 1);
  52. }
  53. Optional<size_t> LexerAutoComplete::token_in_position(const Vector<Cpp::Token>& tokens, const GUI::TextPosition& position)
  54. {
  55. for (size_t token_index = 0; token_index < tokens.size(); ++token_index) {
  56. auto& token = tokens[token_index];
  57. if (token.m_start.line != token.m_end.line)
  58. continue;
  59. if (token.m_start.line != position.line())
  60. continue;
  61. if (token.m_start.column + 1 > position.column() || token.m_end.column + 1 < position.column())
  62. continue;
  63. return token_index;
  64. }
  65. return {};
  66. }
  67. Vector<GUI::AutocompleteProvider::Entry> LexerAutoComplete::identifier_prefixes(const Vector<String>& lines, const Vector<Cpp::Token>& tokens, size_t target_token_index)
  68. {
  69. auto partial_input = text_of_token(lines, tokens[target_token_index]);
  70. Vector<GUI::AutocompleteProvider::Entry> suggestions;
  71. HashTable<String> suggestions_lookup; // To avoid duplicate results
  72. for (size_t i = 0; i < target_token_index; ++i) {
  73. auto& token = tokens[i];
  74. if (token.m_type != Cpp::Token::Type::Identifier)
  75. continue;
  76. auto text = text_of_token(lines, token);
  77. if (text.starts_with(partial_input) && suggestions_lookup.set(text) == AK::HashSetResult::InsertedNewEntry) {
  78. suggestions.append({ text, partial_input.length(), GUI::AutocompleteProvider::CompletionKind::Identifier });
  79. }
  80. }
  81. return suggestions;
  82. }
  83. }