Path: blob/master/modules/gdscript/language_server/gdscript_extend_parser.h
10278 views
/**************************************************************************/1/* gdscript_extend_parser.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "../gdscript_parser.h"33#include "godot_lsp.h"3435#include "core/variant/variant.h"3637#ifndef LINE_NUMBER_TO_INDEX38#define LINE_NUMBER_TO_INDEX(p_line) ((p_line) - 1)39#endif40#ifndef COLUMN_NUMBER_TO_INDEX41#define COLUMN_NUMBER_TO_INDEX(p_column) ((p_column) - 1)42#endif4344#ifndef SYMBOL_SEPARATOR45#define SYMBOL_SEPARATOR "::"46#endif4748#ifndef JOIN_SYMBOLS49#define JOIN_SYMBOLS(p_path, name) ((p_path) + SYMBOL_SEPARATOR + (name))50#endif5152typedef HashMap<String, const LSP::DocumentSymbol *> ClassMembers;5354/**55* Represents a Position as used by GDScript Parser. Used for conversion to and from `LSP::Position`.56*57* Difference to `LSP::Position`:58* * Line & Char/column: 1-based59* * LSP: both 0-based60* * Tabs are expanded to columns using tab size (`text_editor/behavior/indent/size`).61* * LSP: tab is single char62*63* Example:64* ```gdscript65* →→var my_value = 4266* ```67* `_` is at:68* * Godot: `column=12`69* * using `indent/size=4`70* * Note: counting starts at `1`71* * LSP: `character=8`72* * Note: counting starts at `0`73*/74struct GodotPosition {75int line;76int column;7778GodotPosition(int p_line, int p_column) :79line(p_line), column(p_column) {}8081LSP::Position to_lsp(const Vector<String> &p_lines) const;82static GodotPosition from_lsp(const LSP::Position p_pos, const Vector<String> &p_lines);8384bool operator==(const GodotPosition &p_other) const {85return line == p_other.line && column == p_other.column;86}8788String to_string() const {89return vformat("(%d,%d)", line, column);90}91};9293struct GodotRange {94GodotPosition start;95GodotPosition end;9697GodotRange(GodotPosition p_start, GodotPosition p_end) :98start(p_start), end(p_end) {}99100LSP::Range to_lsp(const Vector<String> &p_lines) const;101static GodotRange from_lsp(const LSP::Range &p_range, const Vector<String> &p_lines);102103bool operator==(const GodotRange &p_other) const {104return start == p_other.start && end == p_other.end;105}106107String to_string() const {108return vformat("[%s:%s]", start.to_string(), end.to_string());109}110};111112class ExtendGDScriptParser : public GDScriptParser {113String path;114Vector<String> lines;115116LSP::DocumentSymbol class_symbol;117Vector<LSP::Diagnostic> diagnostics;118List<LSP::DocumentLink> document_links;119ClassMembers members;120HashMap<String, ClassMembers> inner_classes;121122LSP::Range range_of_node(const GDScriptParser::Node *p_node) const;123124void update_diagnostics();125126void update_symbols();127void update_document_links(const String &p_code);128void parse_class_symbol(const GDScriptParser::ClassNode *p_class, LSP::DocumentSymbol &r_symbol);129void parse_function_symbol(const GDScriptParser::FunctionNode *p_func, LSP::DocumentSymbol &r_symbol);130131Dictionary dump_function_api(const GDScriptParser::FunctionNode *p_func) const;132Dictionary dump_class_api(const GDScriptParser::ClassNode *p_class) const;133134const LSP::DocumentSymbol *search_symbol_defined_at_line(int p_line, const LSP::DocumentSymbol &p_parent, const String &p_symbol_name = "") const;135136Array member_completions;137138public:139_FORCE_INLINE_ const String &get_path() const { return path; }140_FORCE_INLINE_ const Vector<String> &get_lines() const { return lines; }141_FORCE_INLINE_ const LSP::DocumentSymbol &get_symbols() const { return class_symbol; }142_FORCE_INLINE_ const Vector<LSP::Diagnostic> &get_diagnostics() const { return diagnostics; }143_FORCE_INLINE_ const ClassMembers &get_members() const { return members; }144_FORCE_INLINE_ const HashMap<String, ClassMembers> &get_inner_classes() const { return inner_classes; }145146Error get_left_function_call(const LSP::Position &p_position, LSP::Position &r_func_pos, int &r_arg_index) const;147148String get_text_for_completion(const LSP::Position &p_cursor) const;149String get_text_for_lookup_symbol(const LSP::Position &p_cursor, const String &p_symbol = "", bool p_func_required = false) const;150String get_identifier_under_position(const LSP::Position &p_position, LSP::Range &r_range) const;151String get_uri() const;152153/**154* `p_symbol_name` gets ignored if empty. Otherwise symbol must match passed in named.155*156* Necessary when multiple symbols at same line for example with `func`:157* `func handle_arg(arg: int):`158* -> Without `p_symbol_name`: returns `handle_arg`. Even if parameter (`arg`) is wanted.159* With `p_symbol_name`: symbol name MUST match `p_symbol_name`: returns `arg`.160*/161const LSP::DocumentSymbol *get_symbol_defined_at_line(int p_line, const String &p_symbol_name = "") const;162const LSP::DocumentSymbol *get_member_symbol(const String &p_name, const String &p_subclass = "") const;163const List<LSP::DocumentLink> &get_document_links() const;164165const Array &get_member_completions();166Dictionary generate_api() const;167168Error parse(const String &p_code, const String &p_path);169};170171172