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