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

CodeMap::remove_file #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 31 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,25 @@ impl CodeMap {
file
}

/// Remove the file previously added to this `CodeMap`.
///
/// This operation is no-op if file is already removed
/// or if file is added to a different `CodeMap`.
pub fn remove_file(&mut self, file: &Arc<File>) {
// TODO: make faster than linear
self.files.retain(|f| !Arc::ptr_eq(f, file));
}

fn end_pos(&self) -> Pos {
self.files.last().map(|x| x.span.high).unwrap_or(Pos(0))
}

/// Looks up the `File` that contains the specified position.
pub fn find_file(&self, pos: Pos) -> &Arc<File> {
self.find_file_opt(pos).expect("Mapping unknown source location")
}

fn find_file_opt(&self, pos: Pos) -> Option<&Arc<File>> {
self.files.binary_search_by(|file| {
if file.span.high < pos {
Ordering::Less
Expand All @@ -163,7 +176,7 @@ impl CodeMap {
} else {
Ordering::Equal
}
}).ok().map(|i| &self.files[i]).expect("Mapping unknown source location")
}).ok().map(|i| &self.files[i])
}

/// Gets the file, line, and column represented by a `Pos`.
Expand Down Expand Up @@ -372,6 +385,23 @@ fn test_codemap() {
assert_eq!(codemap.find_file(x.high()).name(), "test2.rs");
}

#[test]
fn test_remove_file() {
let mut codemap = CodeMap::new();
let f1 = codemap.add_file("test1.py".to_owned(), "abc".to_owned());
let f2 = codemap.add_file("test1.py".to_owned(), "def".to_owned());

let f2l = f2.span.low;

codemap.remove_file(&f1);

assert!(Arc::ptr_eq(&f2, codemap.find_file(f2l)));

codemap.remove_file(&f2);

assert!(codemap.find_file_opt(f2l).is_none());
}

#[test]
fn test_issue2() {
let mut codemap = CodeMap::new();
Expand Down