gdscript_extend_parser.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #ifndef GDSCRIPT_EXTEND_PARSER_H
  31. #define GDSCRIPT_EXTEND_PARSER_H
  32. #include "../gdscript_parser.h"
  33. #include "godot_lsp.h"
  34. #include "core/variant/variant.h"
  35. #ifndef LINE_NUMBER_TO_INDEX
  36. #define LINE_NUMBER_TO_INDEX(p_line) ((p_line)-1)
  37. #endif
  38. #ifndef COLUMN_NUMBER_TO_INDEX
  39. #define COLUMN_NUMBER_TO_INDEX(p_column) ((p_column)-1)
  40. #endif
  41. #ifndef SYMBOL_SEPERATOR
  42. #define SYMBOL_SEPERATOR "::"
  43. #endif
  44. #ifndef JOIN_SYMBOLS
  45. #define JOIN_SYMBOLS(p_path, name) ((p_path) + SYMBOL_SEPERATOR + (name))
  46. #endif
  47. typedef HashMap<String, const lsp::DocumentSymbol *> ClassMembers;
  48. /**
  49. * Represents a Position as used by GDScript Parser. Used for conversion to and from `lsp::Position`.
  50. *
  51. * Difference to `lsp::Position`:
  52. * * Line & Char/column: 1-based
  53. * * LSP: both 0-based
  54. * * Tabs are expanded to columns using tab size (`text_editor/behavior/indent/size`).
  55. * * LSP: tab is single char
  56. *
  57. * Example:
  58. * ```gdscript
  59. * →→var my_value = 42
  60. * ```
  61. * `_` is at:
  62. * * Godot: `column=12`
  63. * * using `indent/size=4`
  64. * * Note: counting starts at `1`
  65. * * LSP: `character=8`
  66. * * Note: counting starts at `0`
  67. */
  68. struct GodotPosition {
  69. int line;
  70. int column;
  71. GodotPosition(int p_line, int p_column) :
  72. line(p_line), column(p_column) {}
  73. lsp::Position to_lsp(const Vector<String> &p_lines) const;
  74. static GodotPosition from_lsp(const lsp::Position p_pos, const Vector<String> &p_lines);
  75. bool operator==(const GodotPosition &p_other) const {
  76. return line == p_other.line && column == p_other.column;
  77. }
  78. String to_string() const {
  79. return vformat("(%d,%d)", line, column);
  80. }
  81. };
  82. struct GodotRange {
  83. GodotPosition start;
  84. GodotPosition end;
  85. GodotRange(GodotPosition p_start, GodotPosition p_end) :
  86. start(p_start), end(p_end) {}
  87. lsp::Range to_lsp(const Vector<String> &p_lines) const;
  88. static GodotRange from_lsp(const lsp::Range &p_range, const Vector<String> &p_lines);
  89. bool operator==(const GodotRange &p_other) const {
  90. return start == p_other.start && end == p_other.end;
  91. }
  92. String to_string() const {
  93. return vformat("[%s:%s]", start.to_string(), end.to_string());
  94. }
  95. };
  96. class ExtendGDScriptParser : public GDScriptParser {
  97. String path;
  98. Vector<String> lines;
  99. lsp::DocumentSymbol class_symbol;
  100. Vector<lsp::Diagnostic> diagnostics;
  101. List<lsp::DocumentLink> document_links;
  102. ClassMembers members;
  103. HashMap<String, ClassMembers> inner_classes;
  104. lsp::Range range_of_node(const GDScriptParser::Node *p_node) const;
  105. void update_diagnostics();
  106. void update_symbols();
  107. void update_document_links(const String &p_code);
  108. void parse_class_symbol(const GDScriptParser::ClassNode *p_class, lsp::DocumentSymbol &r_symbol);
  109. void parse_function_symbol(const GDScriptParser::FunctionNode *p_func, lsp::DocumentSymbol &r_symbol);
  110. Dictionary dump_function_api(const GDScriptParser::FunctionNode *p_func) const;
  111. Dictionary dump_class_api(const GDScriptParser::ClassNode *p_class) const;
  112. const lsp::DocumentSymbol *search_symbol_defined_at_line(int p_line, const lsp::DocumentSymbol &p_parent, const String &p_symbol_name = "") const;
  113. Array member_completions;
  114. public:
  115. _FORCE_INLINE_ const String &get_path() const { return path; }
  116. _FORCE_INLINE_ const Vector<String> &get_lines() const { return lines; }
  117. _FORCE_INLINE_ const lsp::DocumentSymbol &get_symbols() const { return class_symbol; }
  118. _FORCE_INLINE_ const Vector<lsp::Diagnostic> &get_diagnostics() const { return diagnostics; }
  119. _FORCE_INLINE_ const ClassMembers &get_members() const { return members; }
  120. _FORCE_INLINE_ const HashMap<String, ClassMembers> &get_inner_classes() const { return inner_classes; }
  121. Error get_left_function_call(const lsp::Position &p_position, lsp::Position &r_func_pos, int &r_arg_index) const;
  122. String get_text_for_completion(const lsp::Position &p_cursor) const;
  123. String get_text_for_lookup_symbol(const lsp::Position &p_cursor, const String &p_symbol = "", bool p_func_required = false) const;
  124. String get_identifier_under_position(const lsp::Position &p_position, lsp::Range &r_range) const;
  125. String get_uri() const;
  126. /**
  127. * `p_symbol_name` gets ignored if empty. Otherwise symbol must match passed in named.
  128. *
  129. * Necessary when multiple symbols at same line for example with `func`:
  130. * `func handle_arg(arg: int):`
  131. * -> Without `p_symbol_name`: returns `handle_arg`. Even if parameter (`arg`) is wanted.
  132. * With `p_symbol_name`: symbol name MUST match `p_symbol_name`: returns `arg`.
  133. */
  134. const lsp::DocumentSymbol *get_symbol_defined_at_line(int p_line, const String &p_symbol_name = "") const;
  135. const lsp::DocumentSymbol *get_member_symbol(const String &p_name, const String &p_subclass = "") const;
  136. const List<lsp::DocumentLink> &get_document_links() const;
  137. const Array &get_member_completions();
  138. Dictionary generate_api() const;
  139. Error parse(const String &p_code, const String &p_path);
  140. };
  141. #endif // GDSCRIPT_EXTEND_PARSER_H