Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: let filenames in errors be relative to the current dir if possible #5642

Merged
merged 2 commits into from
Jul 30, 2024
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 @@
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 All @@ -340,7 +346,7 @@
location: noirc_errors::Location,
files: &'a FileMap,
interner: &'a NodeInterner,
interners: &'a HashMap<String, NodeInterner>,

Check warning on line 349 in tooling/lsp/src/requests/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (interners)
root_crate_name: String,
root_crate_dependencies: &'a Vec<Dependency>,
}
Expand Down Expand Up @@ -398,7 +404,7 @@
location,
files,
interner,
interners: &state.cached_definitions,

Check warning on line 407 in tooling/lsp/src/requests/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (interners)
root_crate_name: package.name.to_string(),
root_crate_dependencies: &context.crate_graph[context.root_crate_id()].dependencies,
}))
Expand All @@ -406,7 +412,7 @@
pub(crate) fn find_all_references_in_workspace(
location: noirc_errors::Location,
interner: &NodeInterner,
cached_interners: &HashMap<String, NodeInterner>,

Check warning on line 415 in tooling/lsp/src/requests/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (interners)
files: &FileMap,
include_declaration: bool,
include_self_type_name: bool,
Expand All @@ -418,8 +424,8 @@
// If we found the referenced node, find its location
let referenced_location = interner.reference_location(referenced);

// Now we find all references that point to this location, in all interners

Check warning on line 427 in tooling/lsp/src/requests/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (interners)
// (there's one interner per package, and all interners in a workspace rely on the

Check warning on line 428 in tooling/lsp/src/requests/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (interners)
// same FileManager so a Location/FileId in one package is the same as in another package)
let mut locations = find_all_references(
referenced_location,
Expand All @@ -438,7 +444,7 @@
));
}

// The LSP client usually removes duplicate loctions, but we do it here just in case they don't

Check warning on line 447 in tooling/lsp/src/requests/mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (loctions)
locations.sort_by_key(|location| {
(
location.uri.to_string(),
Expand Down
Loading