1/**************************************************************************/
2/* gdscript_language_server.cpp */
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#include "gdscript_language_server.h"
32
33#include "core/io/file_access.h"
34#include "core/os/os.h"
35#include "editor/editor_log.h"
36#include "editor/editor_node.h"
37#include "editor/editor_settings.h"
38
39GDScriptLanguageServer::GDScriptLanguageServer() {
40 _EDITOR_DEF("network/language_server/remote_host", host);
41 _EDITOR_DEF("network/language_server/remote_port", port);
42 _EDITOR_DEF("network/language_server/enable_smart_resolve", true);
43 _EDITOR_DEF("network/language_server/show_native_symbols_in_editor", false);
44 _EDITOR_DEF("network/language_server/use_thread", use_thread);
45}
46
47void GDScriptLanguageServer::_notification(int p_what) {
48 switch (p_what) {
49 case NOTIFICATION_ENTER_TREE: {
50 start();
51 } break;
52
53 case NOTIFICATION_EXIT_TREE: {
54 stop();
55 } break;
56
57 case NOTIFICATION_INTERNAL_PROCESS: {
58 if (started && !use_thread) {
59 protocol.poll();
60 }
61 } break;
62
63 case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
64 String remote_host = String(_EDITOR_GET("network/language_server/remote_host"));
65 int remote_port = (int)_EDITOR_GET("network/language_server/remote_port");
66 bool remote_use_thread = (bool)_EDITOR_GET("network/language_server/use_thread");
67 if (remote_host != host || remote_port != port || remote_use_thread != use_thread) {
68 stop();
69 start();
70 }
71 } break;
72 }
73}
74
75void GDScriptLanguageServer::thread_main(void *p_userdata) {
76 set_current_thread_safe_for_nodes(true);
77 GDScriptLanguageServer *self = static_cast<GDScriptLanguageServer *>(p_userdata);
78 while (self->thread_running) {
79 // Poll 20 times per second
80 self->protocol.poll();
81 OS::get_singleton()->delay_usec(50000);
82 }
83}
84
85void GDScriptLanguageServer::start() {
86 host = String(_EDITOR_GET("network/language_server/remote_host"));
87 port = (int)_EDITOR_GET("network/language_server/remote_port");
88 use_thread = (bool)_EDITOR_GET("network/language_server/use_thread");
89 if (protocol.start(port, IPAddress(host)) == OK) {
90 EditorNode::get_log()->add_message("--- GDScript language server started ---", EditorLog::MSG_TYPE_EDITOR);
91 if (use_thread) {
92 thread_running = true;
93 thread.start(GDScriptLanguageServer::thread_main, this);
94 }
95 set_process_internal(!use_thread);
96 started = true;
97 }
98}
99
100void GDScriptLanguageServer::stop() {
101 if (use_thread) {
102 ERR_FAIL_COND(!thread.is_started());
103 thread_running = false;
104 thread.wait_to_finish();
105 }
106 protocol.stop();
107 started = false;
108 EditorNode::get_log()->add_message("--- GDScript language server stopped ---", EditorLog::MSG_TYPE_EDITOR);
109}
110
111void register_lsp_types() {
112 GDREGISTER_CLASS(GDScriptLanguageProtocol);
113 GDREGISTER_CLASS(GDScriptTextDocument);
114 GDREGISTER_CLASS(GDScriptWorkspace);
115}
116