AutoComplete.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "AutoComplete.h"
  27. #include <AK/HashTable.h>
  28. #include <LibCpp/Lexer.h>
  29. // #define DEBUG_AUTOCOMPLETE
  30. namespace LanguageServers::Cpp {
  31. Vector<AutoCompleteResponse> AutoComplete::get_suggestions(const String& code, GUI::TextPosition autocomplete_position)
  32. {
  33. auto lines = code.split('\n', true);
  34. 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. #ifdef DEBUG_AUTOCOMPLETE
  41. for (auto& suggestion : suggestions) {
  42. dbgln("suggestion: {}", suggestion.completion);
  43. }
  44. #endif
  45. return suggestions;
  46. }
  47. String AutoComplete::text_of_token(const Vector<String> lines, const Cpp::Token& token)
  48. {
  49. return lines[token.m_start.line].substring(token.m_start.column, token.m_end.column - token.m_start.column + 1);
  50. }
  51. Optional<size_t> AutoComplete::token_in_position(const Vector<Cpp::Token>& tokens, GUI::TextPosition position)
  52. {
  53. for (size_t token_index = 0; token_index < tokens.size(); ++token_index) {
  54. auto& token = tokens[token_index];
  55. if (token.m_start.line != position.line())
  56. continue;
  57. if (token.m_start.column > position.column() || token.m_end.column < position.column())
  58. continue;
  59. return token_index;
  60. }
  61. return {};
  62. }
  63. Vector<AutoCompleteResponse> AutoComplete::identifier_prefixes(const Vector<String> lines, const Vector<Cpp::Token>& tokens, size_t target_token_index)
  64. {
  65. auto partial_input = text_of_token(lines, tokens[target_token_index]);
  66. Vector<AutoCompleteResponse> suggestions;
  67. HashTable<String> suggestions_lookup; // To avoid duplicate results
  68. for (size_t i = 0; i < target_token_index; ++i) {
  69. auto& token = tokens[i];
  70. if (token.m_type != Cpp::Token::Type::Identifier)
  71. continue;
  72. auto text = text_of_token(lines, token);
  73. if (text.starts_with(partial_input) && !suggestions_lookup.contains(text)) {
  74. suggestions_lookup.set(text);
  75. suggestions.append({ text, partial_input.length(), HackStudio::CompletionKind::Identifier });
  76. }
  77. }
  78. return suggestions;
  79. }
  80. }