Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions src/cli/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ enum FormattingError {
FileIsIgnored,
}

fn uri_to_pathbuf(uri: &Uri) -> PathBuf {
let raw = uri.path().as_str();

if raw.len() >= 4 {
let bytes = raw.as_bytes();
if bytes[0] == b'/' && bytes[2] == b':' && bytes[1].is_ascii_alphabetic() {
return raw[1..].into();
}
}

raw.into()
}

impl LanguageServer<'_> {
fn new<'a>(
workspace_folders: Vec<WorkspaceFolder>,
Expand All @@ -119,7 +132,7 @@ impl LanguageServer<'_> {
let check_str = uri.as_str();
for workspace in &self.workspace_folders {
if *uri == workspace.uri {
return workspace.uri.path().as_str().into();
return uri_to_pathbuf(&workspace.uri);
}

let prefix_str = workspace.uri.as_str();
Expand All @@ -134,9 +147,9 @@ impl LanguageServer<'_> {
}

match best_workspace {
Some(workspace) => workspace.path().as_str().into(),
Some(workspace) => uri_to_pathbuf(workspace),
None => match &self.root_uri {
Some(root_uri) => root_uri.path().as_str().into(),
Some(root_uri) => uri_to_pathbuf(root_uri),
None => std::env::current_dir().expect("Could not find current directory"),
},
}
Expand Down Expand Up @@ -164,10 +177,10 @@ impl LanguageServer<'_> {
}

let search_root = Some(self.find_config_root(uri));
let path = uri.path().as_str().as_ref();
let path = uri_to_pathbuf(uri);

if stylua_ignore::path_is_stylua_ignored(
path,
&path,
self.search_parent_directories,
search_root.clone(),
)
Expand All @@ -180,7 +193,7 @@ impl LanguageServer<'_> {

let mut config = self
.config_resolver
.load_configuration_with_search_root(path, search_root)
.load_configuration_with_search_root(&path, search_root)
.unwrap_or_default();

if let Some(formatting_options) = formatting_options {
Expand Down Expand Up @@ -403,6 +416,7 @@ mod tests {
lsp::{main_loop, InitializationOptions},
opt::Opt,
};
use super::uri_to_pathbuf;

use assert_fs::prelude::*;

Expand Down Expand Up @@ -570,6 +584,18 @@ mod tests {
}
}

#[test]
fn test_uri_to_pathbuf_unix_path() {
let uri = Uri::from_str("file:///home/user/test.lua").unwrap();
assert_eq!(uri_to_pathbuf(&uri), Path::new("/home/user/test.lua"));
}

#[test]
fn test_uri_to_pathbuf_windows_drive_path() {
let uri = Uri::from_str("file:///C:/Users/test/init.lua").unwrap();
assert_eq!(uri_to_pathbuf(&uri), Path::new("C:/Users/test/init.lua"));
}

#[test]
fn test_lsp_initialize() {
let opt = Opt::parse_from(vec!["BINARY_NAME"]);
Expand Down