Skip to content

Commit 82702ce

Browse files
LSP: Fix file URI handling + warn about workspace project mismatch
1 parent ef1153b commit 82702ce

File tree

3 files changed

+95
-6
lines changed

3 files changed

+95
-6
lines changed

modules/gdscript/language_server/gdscript_language_protocol.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,35 @@ void GDScriptLanguageProtocol::_bind_methods() {
173173
Dictionary GDScriptLanguageProtocol::initialize(const Dictionary &p_params) {
174174
lsp::InitializeResult ret;
175175

176+
{
177+
// Warn if the workspace root does not match with the project that is currently open in Godot,
178+
// since it might lead to unexpected behaviour, like wrong warnings about duplicate class names.
179+
180+
String root = "";
181+
Variant root_uri_var = p_params["rootUri"];
182+
Variant root_var = p_params["rootUri"];
183+
if (root_uri_var.is_string()) {
184+
root = get_workspace()->get_file_path(root_uri_var);
185+
} else if (root_var.is_string()) {
186+
root = root_var;
187+
}
188+
189+
if (ProjectSettings::get_singleton()->localize_path(root) != "res://") {
190+
lsp::ShowMessageParams params{
191+
lsp::MessageType::Warning,
192+
"The GDScript Language Server might not work correctly with other projects than the one opened in Godot."
193+
};
194+
Variant res = make_notification("window/showMessage", params.to_json());
195+
196+
Ref<LSPeer> peer = clients.get(latest_client_id);
197+
if (peer.is_valid()) {
198+
String msg = res.to_json_string();
199+
msg = format_output(msg);
200+
(*peer)->res_queue.push_back(msg.utf8());
201+
}
202+
}
203+
}
204+
176205
String root_uri = p_params["rootUri"];
177206
String root = p_params["rootPath"];
178207
bool is_same_workspace;

modules/gdscript/language_server/gdscript_workspace.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -559,16 +559,38 @@ Error GDScriptWorkspace::parse_local_script(const String &p_path) {
559559
}
560560

561561
String GDScriptWorkspace::get_file_path(const String &p_uri) const {
562-
String path = p_uri.uri_decode();
563-
String base_uri = root_uri.uri_decode();
564-
path = path.replacen(base_uri + "/", "res://");
565-
return path;
562+
int port;
563+
String scheme, host, path, fragment;
564+
p_uri.parse_url(scheme, host, port, path, fragment);
565+
566+
// TODO: Make the parsing RFC-3986 compliant.
567+
if (scheme != "file" && scheme != "file:" && scheme != "file://") {
568+
// The language server does only support the file protocol.
569+
return "";
570+
}
571+
572+
// Treat host like authority for now and ignore the port. It's an edge case for invalid file URI's anyway.
573+
if (host != "" && host != "localhost") {
574+
// The language server does not support nonlocal files.
575+
return "";
576+
}
577+
578+
// If query or fragment are present, the URI is not a valid file URI as per RFC-8089.
579+
// We currently don't handle the query and it will be part of the path. However,
580+
// this should not be a problem for a correct file URI.
581+
if (fragment != "") {
582+
return "";
583+
}
584+
585+
String local_path = ProjectSettings::get_singleton()->localize_path(path.uri_decode());
586+
587+
return local_path;
566588
}
567589

568590
String GDScriptWorkspace::get_file_uri(const String &p_path) const {
569591
String uri = p_path;
570-
uri = uri.replace("res://", root_uri + "/");
571-
return uri;
592+
593+
return "file://" + ProjectSettings::get_singleton()->globalize_path(p_path).uri_encode();
572594
}
573595

574596
void GDScriptWorkspace::publish_diagnostics(const String &p_path) {

modules/gdscript/language_server/godot_lsp.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,25 @@ struct ReferenceContext {
238238
bool includeDeclaration = false;
239239
};
240240

241+
struct ShowMessageParams {
242+
/**
243+
* The message type. See {@link MessageType}.
244+
*/
245+
int type;
246+
247+
/**
248+
* The actual message.
249+
*/
250+
String message;
251+
252+
_FORCE_INLINE_ Dictionary to_json() const {
253+
Dictionary dict;
254+
dict["type"] = type;
255+
dict["message"] = message;
256+
return dict;
257+
}
258+
};
259+
241260
struct ReferenceParams : TextDocumentPositionParams {
242261
ReferenceContext context;
243262
};
@@ -405,6 +424,25 @@ static const int Full = 1;
405424
static const int Incremental = 2;
406425
}; // namespace TextDocumentSyncKind
407426

427+
namespace MessageType {
428+
/**
429+
* An error message.
430+
*/
431+
static const int Error = 1;
432+
/**
433+
* A warning message.
434+
*/
435+
static const int Warning = 2;
436+
/**
437+
* An information message.
438+
*/
439+
static const int Info = 3;
440+
/**
441+
* A log message.
442+
*/
443+
static const int Log = 4;
444+
}; // namespace MessageType
445+
408446
/**
409447
* Completion options.
410448
*/

0 commit comments

Comments
 (0)