Skip to content

Commit

Permalink
Merge pull request #500 from gauge-sh/fix-windows-lsp
Browse files Browse the repository at this point in the history
Fix LSP diagnostics on Windows
  • Loading branch information
emdoyle authored Dec 23, 2024
2 parents b82ca54 + d335378 commit 620a557
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/lsp/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use lsp_types::notification::Notification;
use lsp_types::request::Request;
use lsp_types::{InitializeParams, Uri};
use std::path::PathBuf;
use std::path::{PathBuf, MAIN_SEPARATOR_STR};
use std::thread::{self, JoinHandle};

use lsp_server::{Connection, Message, Notification as NotificationMessage, RequestId};
Expand Down Expand Up @@ -34,6 +34,23 @@ impl ServerHandle {
}
}

fn uri_to_path(uri: &Uri) -> PathBuf {
// This assumes that the URI has an absolute file path
let segments: Vec<_> = uri
.path()
.segments()
.map(|estr| estr.decode().into_string_lossy().into_owned())
.collect();

if cfg!(windows) {
// On Windows, join segments directly (first segment will be like "c:")
segments.join(MAIN_SEPARATOR_STR).into()
} else {
// On POSIX, ensure path starts with /
format!("/{}", segments.join(MAIN_SEPARATOR_STR)).into()
}
}

impl LSPServer {
pub fn new(project_root: PathBuf, project_config: config::ProjectConfig) -> Self {
Self {
Expand Down Expand Up @@ -134,9 +151,9 @@ impl LSPServer {
&self,
uri: Uri,
) -> Result<lsp_types::PublishDiagnosticsParams, ServerError> {
// TODO: This is probably not a robust translation
let uri_path = uri.as_str().trim_start_matches("file://");
let uri_pathbuf = PathBuf::from(uri_path);
let uri_pathbuf = uri_to_path(&uri);
eprintln!("Linting for diagnostics: {uri_pathbuf:?}");
eprintln!("Project root: {}", self.project_root.display());

let check_result = check(
self.project_root.clone(),
Expand Down

0 comments on commit 620a557

Please sign in to comment.