Path: blob/master/modules/gdscript/language_server/gdscript_language_protocol.h
20816 views
/**************************************************************************/1/* gdscript_language_protocol.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_text_document.h"33#include "gdscript_workspace.h"34#include "scene_cache.h"3536#include "core/io/stream_peer_tcp.h"37#include "core/io/tcp_server.h"3839#include "modules/jsonrpc/jsonrpc.h"4041#define LSP_MAX_BUFFER_SIZE 419430442#define LSP_MAX_CLIENTS 84344#define LSP_NO_CLIENT -14546class GDScriptLanguageProtocol : public JSONRPC {47GDCLASS(GDScriptLanguageProtocol, JSONRPC)4849#ifdef TESTS_ENABLED50friend class TestGDScriptLanguageProtocolInitializer;51#endif5253private:54struct LSPeer : RefCounted {55Ref<StreamPeerTCP> connection;5657uint8_t req_buf[LSP_MAX_BUFFER_SIZE];58int req_pos = 0;59bool has_header = false;60bool has_content = false;61int content_length = 0;62Vector<CharString> res_queue;63int res_sent = 0;6465Error handle_data();66Error send_data();6768/**69* Tracks all files that the client claimed, however for files deemed not relevant70* to the server the `text` might not be persisted.71*/72HashMap<String, LSP::TextDocumentItem> managed_files;73HashMap<String, ExtendGDScriptParser *> parse_results;7475void remove_cached_parser(const String &p_path);76ExtendGDScriptParser *parse_script(const String &p_path);7778~LSPeer();7980private:81void clear_stale_parsers();82// Paths of parsers which we can't cache longterm.83// Can be cleared up using `clear_stale_parsers()`.84HashSet<String> stale_parsers;85};8687enum LSPErrorCode {88RequestCancelled = -32800,89ContentModified = -32801,90};9192static GDScriptLanguageProtocol *singleton;9394HashMap<int, Ref<LSPeer>> clients;95SceneCache scene_cache;96Ref<TCPServer> server;97int latest_client_id = LSP_NO_CLIENT;98int next_client_id = 0;99100int next_server_id = 0;101102Ref<GDScriptTextDocument> text_document;103Ref<GDScriptWorkspace> workspace;104105Error on_client_connected();106void on_client_disconnected(const int &p_client_id);107108String process_message(const String &p_text);109String format_output(const String &p_text);110111bool _initialized = false;112113protected:114static void _bind_methods();115116Dictionary initialize(const Dictionary &p_params);117void initialized(const Variant &p_params);118119public:120_FORCE_INLINE_ static GDScriptLanguageProtocol *get_singleton() { return singleton; }121_FORCE_INLINE_ Ref<GDScriptWorkspace> get_workspace() { return workspace; }122_FORCE_INLINE_ Ref<GDScriptTextDocument> get_text_document() { return text_document; }123_FORCE_INLINE_ SceneCache *get_scene_cache() { return &scene_cache; }124125_FORCE_INLINE_ bool is_initialized() const { return _initialized; }126127void poll(int p_limit_usec);128Error start(int p_port, const IPAddress &p_bind_ip);129void stop();130131void notify_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1);132void request_client(const String &p_method, const Variant &p_params = Variant(), int p_client_id = -1);133134bool is_smart_resolve_enabled() const;135bool is_goto_native_symbols_enabled() const;136137// Text Document Synchronization138void lsp_did_open(const Dictionary &p_params);139void lsp_did_change(const Dictionary &p_params);140void lsp_did_close(const Dictionary &p_params);141142/**143* Returns a list of symbols that might be related to the document position.144*145* The result fulfills no semantic guarantees, nor is it guaranteed to be complete.146* Should only be used for "smart resolve".147*/148void resolve_related_symbols(const LSP::TextDocumentPositionParams &p_doc_pos, List<const LSP::DocumentSymbol *> &r_list);149150/**151* Returns parse results for the given path, using the cache if available.152* If no such file exists, or the file is not a GDScript file a `nullptr` is returned.153*/154ExtendGDScriptParser *get_parse_result(const String &p_path);155156GDScriptLanguageProtocol();157~GDScriptLanguageProtocol() {158clients.clear();159}160};161162163