Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 16 additions & 2 deletions compiler/fm/src/file_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl From<&PathBuf> for PathString {
pub struct FileMap {
files: SimpleFiles<PathString, String>,
name_to_id: HashMap<PathString, FileId>,
current_dir: Option<PathBuf>,
}

// XXX: Note that we derive Default here due to ModuleOrigin requiring us to set a FileId
Expand Down Expand Up @@ -82,7 +83,11 @@ impl FileMap {
}
impl Default for FileMap {
fn default() -> Self {
FileMap { files: SimpleFiles::new(), name_to_id: HashMap::new() }
FileMap {
files: SimpleFiles::new(),
name_to_id: HashMap::new(),
current_dir: std::env::current_dir().ok(),
}
}
}

Expand All @@ -92,7 +97,16 @@ impl<'a> Files<'a> for FileMap {
type Source = &'a str;

fn name(&self, file_id: Self::FileId) -> Result<Self::Name, Error> {
Ok(self.files.get(file_id.as_usize())?.name().clone())
let name = self.files.get(file_id.as_usize())?.name().clone();

// See if we can make the file name a bit shorter/easier to read if it starts with the current directory
if let Some(current_dir) = &self.current_dir {
if let Ok(name_without_prefix) = name.0.strip_prefix(current_dir) {
return Ok(PathString::from_path(name_without_prefix.to_path_buf()));
}
}

Ok(name)
}

fn source(&'a self, file_id: Self::FileId) -> Result<Self::Source, Error> {
Expand Down
8 changes: 7 additions & 1 deletion tooling/lsp/src/requests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::path::PathBuf;
use std::{collections::HashMap, future::Future};

use crate::insert_all_files_for_workspace_into_file_manager;
Expand Down Expand Up @@ -324,7 +325,12 @@ where
let file_name = files.name(file_id).ok()?;

let path = file_name.to_string();
let uri = Url::from_file_path(path).ok()?;

// `path` might be a relative path so we canonicalize it to get an absolute path
let path_buf = PathBuf::from(path);
let path_buf = path_buf.canonicalize().unwrap_or(path_buf);

let uri = Url::from_file_path(path_buf.to_str()?).ok()?;

Some(Location { uri, range })
}
Expand Down