gdscript_extend_parser.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**************************************************************************/
  2. /* gdscript_extend_parser.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "../gdscript_parser.h"
  32. #include "godot_lsp.h"
  33. #include "core/variant/variant.h"
  34. #ifndef LINE_NUMBER_TO_INDEX
  35. #define LINE_NUMBER_TO_INDEX(p_line) ((p_line) - 1)
  36. #endif
  37. #ifndef COLUMN_NUMBER_TO_INDEX
  38. #define COLUMN_NUMBER_TO_INDEX(p_column) ((p_column) - 1)
  39. #endif
  40. #ifndef SYMBOL_SEPARATOR
  41. #define SYMBOL_SEPARATOR "::"
  42. #endif
  43. #ifndef JOIN_SYMBOLS
  44. #define JOIN_SYMBOLS(p_path, name) ((p_path) + SYMBOL_SEPARATOR + (name))
  45. #endif
  46. typedef HashMap<String, const LSP::DocumentSymbol *> ClassMembers;
  47. /**
  48. * Represents a Position as used by GDScript Parser. Used for conversion to and from `LSP::Position`.
  49. *
  50. * Difference to `LSP::Position`:
  51. * * Line & Char/column: 1-based
  52. * * LSP: both 0-based
  53. * * Tabs are expanded to columns using tab size (`text_editor/behavior/indent/size`).
  54. * * LSP: tab is single char
  55. *
  56. * Example:
  57. * ```gdscript
  58. * →→var my_value = 42
  59. * ```
  60. * `_` is at:
  61. * * Godot: `column=12`
  62. * * using `indent/size=4`
  63. * * Note: counting starts at `1`
  64. * * LSP: `character=8`
  65. * * Note: counting starts at `0`
  66. */
  67. struct GodotPosition {
  68. int line;
  69. int column;
  70. GodotPosition(int p_line, int p_column) :
  71. line(p_line), column(p_column) {}
  72. LSP::Position to_lsp(const Vector<String> &p_lines) const;
  73. static GodotPosition from_lsp(const LSP::Position p_pos, const Vector<String> &p_lines);
  74. bool operator==(const GodotPosition &p_other) const {
  75. return line == p_other.line && column == p_other.column;
  76. }
  77. String to_string() const {
  78. return vformat("(%d,%d)", line, column);
  79. }
  80. };
  81. struct GodotRange {
  82. GodotPosition start;
  83. GodotPosition end;
  84. GodotRange(GodotPosition p_start, GodotPosition p_end) :
  85. start(p_start), end(p_end) {}
  86. LSP::Range to_lsp(const Vector<String> &p_lines) const;
  87. static GodotRange from_lsp(const LSP::Range &p_range, const Vector<String> &p_lines);
  88. bool operator==(const GodotRange &p_other) const {
  89. return start == p_other.start && end == p_other.end;
  90. }
  91. String to_string() const {
  92. return vformat("[%s:%s]", start.to_string(), end.to_string());
  93. }
  94. };
  95. class ExtendGDScriptParser : public GDScriptParser {
  96. String path;
  97. Vector<String> lines;
  98. LSP::DocumentSymbol class_symbol;
  99. Vector<LSP::Diagnostic> diagnostics;
  100. List<LSP::DocumentLink> document_links;
  101. ClassMembers members;
  102. HashMap<String, ClassMembers> inner_classes;
  103. LSP::Range range_of_node(const GDScriptParser::Node *p_node) const;
  104. void update_diagnostics();
  105. void update_symbols();
  106. void update_document_links(const String &p_code);
  107. void parse_class_symbol(const GDScriptParser::ClassNode *p_class, LSP::DocumentSymbol &r_symbol);
  108. void parse_function_symbol(const GDScriptParser::FunctionNode *p_func, LSP::DocumentSymbol &r_symbol);
  109. Dictionary dump_function_api(const GDScriptParser::FunctionNode *p_func) const;
  110. Dictionary dump_class_api(const GDScriptParser::ClassNode *p_class) const;
  111. const LSP::DocumentSymbol *search_symbol_defined_at_line(int p_line, const LSP::DocumentSymbol &p_parent, const String &p_symbol_name = "") const;
  112. Array member_completions;
  113. public:
  114. _FORCE_INLINE_ const String &get_path() const { return path; }
  115. _FORCE_INLINE_ const Vector<String> &get_lines() const { return lines; }
  116. _FORCE_INLINE_ const LSP::DocumentSymbol &get_symbols() const { return class_symbol; }
  117. _FORCE_INLINE_ const Vector<LSP::Diagnostic> &get_diagnostics() const { return diagnostics; }
  118. _FORCE_INLINE_ const ClassMembers &get_members() const { return members; }
  119. _FORCE_INLINE_ const HashMap<String, ClassMembers> &get_inner_classes() const { return inner_classes; }
  120. Error get_left_function_call(const LSP::Position &p_position, LSP::Position &r_func_pos, int &r_arg_index) const;
  121. String get_text_for_completion(const LSP::Position &p_cursor) const;
  122. String get_text_for_lookup_symbol(const LSP::Position &p_cursor, const String &p_symbol = "", bool p_func_required = false) const;
  123. String get_identifier_under_position(const LSP::Position &p_position, LSP::Range &r_range) const;
  124. String get_uri() const;
  125. /**
  126. * `p_symbol_name` gets ignored if empty. Otherwise symbol must match passed in named.
  127. *
  128. * Necessary when multiple symbols at same line for example with `func`:
  129. * `func handle_arg(arg: int):`
  130. * -> Without `p_symbol_name`: returns `handle_arg`. Even if parameter (`arg`) is wanted.
  131. * With `p_symbol_name`: symbol name MUST match `p_symbol_name`: returns `arg`.
  132. */
  133. const LSP::DocumentSymbol *get_symbol_defined_at_line(int p_line, const String &p_symbol_name = "") const;
  134. const LSP::DocumentSymbol *get_member_symbol(const String &p_name, const String &p_subclass = "") const;
  135. const List<LSP::DocumentLink> &get_document_links() const;
  136. const Array &get_member_completions();
  137. Dictionary generate_api() const;
  138. Error parse(const String &p_code, const String &p_path);
  139. };