From 2cf7249794d8ff8350a34f7a496ae86e43e664da Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Wed, 18 Sep 2019 18:37:04 +1200 Subject: [PATCH 1/3] Fix autoview breakage --- src/commands/autoview.rs | 1 + src/commands/enter.rs | 3 +- src/commands/fetch.rs | 71 ++++++++++++++++++++++++++++------------ src/commands/open.rs | 50 +++++++++++++++++++++------- src/data/meta.rs | 4 +++ 5 files changed, 95 insertions(+), 34 deletions(-) diff --git a/src/commands/autoview.rs b/src/commands/autoview.rs index c135fecd67..7bf9b9e310 100644 --- a/src/commands/autoview.rs +++ b/src/commands/autoview.rs @@ -110,6 +110,7 @@ fn is_single_origined_text_value(input: &Vec>) -> bool { if input.len() != 1 { return false; } + if let Tagged { item: Value::Primitive(Primitive::String(_)), tag: Tag { diff --git a/src/commands/enter.rs b/src/commands/enter.rs index ee19b096ed..cca73ca0ff 100644 --- a/src/commands/enter.rs +++ b/src/commands/enter.rs @@ -1,6 +1,7 @@ use crate::commands::command::CommandAction; use crate::commands::PerItemCommand; use crate::commands::UnevaluatedCallInfo; +use crate::data::meta::Span; use crate::errors::ShellError; use crate::parser::registry; use crate::prelude::*; @@ -70,7 +71,7 @@ impl PerItemCommand for Enter { crate::commands::open::fetch( &full_path, &location_clone, - Tag::unknown(), + Span::unknown(), ) .await.unwrap(); diff --git a/src/commands/fetch.rs b/src/commands/fetch.rs index c9e16cb45a..e8520f3eca 100644 --- a/src/commands/fetch.rs +++ b/src/commands/fetch.rs @@ -1,5 +1,6 @@ use crate::commands::UnevaluatedCallInfo; use crate::context::SpanSource; +use crate::data::meta::Span; use crate::data::Value; use crate::errors::ShellError; use crate::parser::hir::SyntaxShape; @@ -9,6 +10,7 @@ use mime::Mime; use std::path::PathBuf; use std::str::FromStr; use surf::mime; +use uuid::Uuid; pub struct Fetch; impl PerItemCommand for Fetch { @@ -51,14 +53,14 @@ fn run( }; let path_buf = path.as_path()?; let path_str = path_buf.display().to_string(); - let path_tag = path.tag(); + let path_span = path.span(); let has_raw = call_info.args.has("raw"); let registry = registry.clone(); let raw_args = raw_args.clone(); let stream = async_stream_block! { - let result = fetch(&path_str, path_tag).await; + let result = fetch(&path_str, path_span).await; if let Err(e) = result { yield Err(e); @@ -129,13 +131,13 @@ fn run( pub async fn fetch( location: &str, - tag: Tag, + span: Span, ) -> Result<(Option, Value, Tag, SpanSource), ShellError> { if let Err(_) = url::Url::parse(location) { return Err(ShellError::labeled_error( "Incomplete or incorrect url", "expected a full url", - tag, + span, )); } @@ -151,10 +153,13 @@ pub async fn fetch( ShellError::labeled_error( "Could not load text from remote url", "could not load", - tag, + span, ) })?), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::Url(location.to_string()), )), (mime::APPLICATION, mime::JSON) => Ok(( @@ -163,10 +168,13 @@ pub async fn fetch( ShellError::labeled_error( "Could not load text from remote url", "could not load", - tag, + span, ) })?), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::Url(location.to_string()), )), (mime::APPLICATION, mime::OCTET_STREAM) => { @@ -174,13 +182,16 @@ pub async fn fetch( ShellError::labeled_error( "Could not load binary file", "could not load", - tag, + span, ) })?; Ok(( None, Value::binary(buf), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::Url(location.to_string()), )) } @@ -190,10 +201,13 @@ pub async fn fetch( ShellError::labeled_error( "Could not load svg from remote url", "could not load", - tag, + span, ) })?), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::Url(location.to_string()), )), (mime::IMAGE, image_ty) => { @@ -201,13 +215,16 @@ pub async fn fetch( ShellError::labeled_error( "Could not load image file", "could not load", - tag, + span, ) })?; Ok(( Some(image_ty.to_string()), Value::binary(buf), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::Url(location.to_string()), )) } @@ -217,10 +234,13 @@ pub async fn fetch( ShellError::labeled_error( "Could not load text from remote url", "could not load", - tag, + span, ) })?), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::Url(location.to_string()), )), (mime::TEXT, mime::PLAIN) => { @@ -241,17 +261,23 @@ pub async fn fetch( ShellError::labeled_error( "Could not load text from remote url", "could not load", - tag, + span, ) })?), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::Url(location.to_string()), )) } (ty, sub_ty) => Ok(( None, Value::string(format!("Not yet supported MIME type: {} {}", ty, sub_ty)), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::Url(location.to_string()), )), } @@ -259,7 +285,10 @@ pub async fn fetch( None => Ok(( None, Value::string(format!("No content type found")), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::Url(location.to_string()), )), }, @@ -267,7 +296,7 @@ pub async fn fetch( return Err(ShellError::labeled_error( "URL could not be opened", "url not found", - tag, + span, )); } } diff --git a/src/commands/open.rs b/src/commands/open.rs index fa068d63e6..6f33412d56 100644 --- a/src/commands/open.rs +++ b/src/commands/open.rs @@ -1,11 +1,13 @@ use crate::commands::UnevaluatedCallInfo; use crate::context::SpanSource; +use crate::data::meta::Span; use crate::data::Value; use crate::errors::ShellError; use crate::parser::hir::SyntaxShape; use crate::parser::registry::Signature; use crate::prelude::*; use std::path::{Path, PathBuf}; +use uuid::Uuid; pub struct Open; impl PerItemCommand for Open { @@ -52,7 +54,7 @@ fn run( }; let path_buf = path.as_path()?; let path_str = path_buf.display().to_string(); - let path_span = path.tag(); + let path_span = path.span(); let has_raw = call_info.args.has("raw"); let registry = registry.clone(); let raw_args = raw_args.clone(); @@ -131,7 +133,7 @@ fn run( pub async fn fetch( cwd: &PathBuf, location: &str, - tag: Tag, + span: Span, ) -> Result<(Option, Value, Tag, SpanSource), ShellError> { let mut cwd = cwd.clone(); @@ -143,7 +145,10 @@ pub async fn fetch( cwd.extension() .map(|name| name.to_string_lossy().to_string()), Value::string(s), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::File(cwd.to_string_lossy().to_string()), )), Err(_) => { @@ -159,13 +164,19 @@ pub async fn fetch( cwd.extension() .map(|name| name.to_string_lossy().to_string()), Value::string(s), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::File(cwd.to_string_lossy().to_string()), )), Err(_) => Ok(( None, Value::binary(bytes), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::File(cwd.to_string_lossy().to_string()), )), } @@ -173,7 +184,10 @@ pub async fn fetch( Ok(( None, Value::binary(bytes), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::File(cwd.to_string_lossy().to_string()), )) } @@ -188,13 +202,19 @@ pub async fn fetch( cwd.extension() .map(|name| name.to_string_lossy().to_string()), Value::string(s), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::File(cwd.to_string_lossy().to_string()), )), Err(_) => Ok(( None, Value::binary(bytes), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::File(cwd.to_string_lossy().to_string()), )), } @@ -202,7 +222,10 @@ pub async fn fetch( Ok(( None, Value::binary(bytes), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::File(cwd.to_string_lossy().to_string()), )) } @@ -210,7 +233,10 @@ pub async fn fetch( _ => Ok(( None, Value::binary(bytes), - tag, + Tag { + span, + origin: Some(Uuid::new_v4()), + }, SpanSource::File(cwd.to_string_lossy().to_string()), )), } @@ -220,7 +246,7 @@ pub async fn fetch( return Err(ShellError::labeled_error( "File could not be opened", "file not found", - tag, + span, )); } } @@ -228,7 +254,7 @@ pub async fn fetch( return Err(ShellError::labeled_error( "File could not be opened", "file not found", - tag, + span, )); } } diff --git a/src/data/meta.rs b/src/data/meta.rs index fd1c597c10..78711cc882 100644 --- a/src/data/meta.rs +++ b/src/data/meta.rs @@ -86,6 +86,10 @@ impl Tagged { self.tag } + pub fn span(&self) -> Span { + self.tag.span + } + // TODO: This should not be optional pub fn origin(&self) -> Option { self.tag.origin From 72e6222992bc1a5dea2c236f8b7499c6bf784e03 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Wed, 18 Sep 2019 19:05:33 +1200 Subject: [PATCH 2/3] Switch to using Uuid::nil() and fix test --- src/commands/autoview.rs | 5 +---- src/commands/enter.rs | 4 ++-- src/commands/fetch.rs | 22 +++++++++++----------- src/commands/open.rs | 20 ++++++++++---------- src/commands/post.rs | 4 ++-- src/commands/save.rs | 2 +- src/commands/tags.rs | 2 +- src/data/meta.rs | 39 ++++++++++++++++++++++++--------------- src/parser/parse/files.rs | 2 +- src/plugins/textview.rs | 2 +- 10 files changed, 54 insertions(+), 48 deletions(-) diff --git a/src/commands/autoview.rs b/src/commands/autoview.rs index 7bf9b9e310..b9b9d8941c 100644 --- a/src/commands/autoview.rs +++ b/src/commands/autoview.rs @@ -113,10 +113,7 @@ fn is_single_origined_text_value(input: &Vec>) -> bool { if let Tagged { item: Value::Primitive(Primitive::String(_)), - tag: Tag { - origin: Some(origin), - .. - }, + tag: Tag { origin, .. }, } = input[0] { origin != uuid::Uuid::nil() diff --git a/src/commands/enter.rs b/src/commands/enter.rs index cca73ca0ff..9388abb941 100644 --- a/src/commands/enter.rs +++ b/src/commands/enter.rs @@ -75,10 +75,10 @@ impl PerItemCommand for Enter { ) .await.unwrap(); - if let Some(uuid) = contents_tag.origin { + if contents_tag.origin != uuid::Uuid::nil() { // If we have loaded something, track its source yield ReturnSuccess::action(CommandAction::AddSpanSource( - uuid, + contents_tag.origin, span_source, )); } diff --git a/src/commands/fetch.rs b/src/commands/fetch.rs index e8520f3eca..79806e76b2 100644 --- a/src/commands/fetch.rs +++ b/src/commands/fetch.rs @@ -76,10 +76,10 @@ fn run( file_extension.or(path_str.split('.').last().map(String::from)) }; - if let Some(uuid) = contents_tag.origin { + if contents_tag.origin != uuid::Uuid::nil() { // If we have loaded something, track its source yield ReturnSuccess::action(CommandAction::AddSpanSource( - uuid, + contents_tag.origin, span_source, )); } @@ -158,7 +158,7 @@ pub async fn fetch( })?), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::Url(location.to_string()), )), @@ -173,7 +173,7 @@ pub async fn fetch( })?), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::Url(location.to_string()), )), @@ -190,7 +190,7 @@ pub async fn fetch( Value::binary(buf), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::Url(location.to_string()), )) @@ -206,7 +206,7 @@ pub async fn fetch( })?), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::Url(location.to_string()), )), @@ -223,7 +223,7 @@ pub async fn fetch( Value::binary(buf), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::Url(location.to_string()), )) @@ -239,7 +239,7 @@ pub async fn fetch( })?), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::Url(location.to_string()), )), @@ -266,7 +266,7 @@ pub async fn fetch( })?), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::Url(location.to_string()), )) @@ -276,7 +276,7 @@ pub async fn fetch( Value::string(format!("Not yet supported MIME type: {} {}", ty, sub_ty)), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::Url(location.to_string()), )), @@ -287,7 +287,7 @@ pub async fn fetch( Value::string(format!("No content type found")), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::Url(location.to_string()), )), diff --git a/src/commands/open.rs b/src/commands/open.rs index 6f33412d56..603bb4da0b 100644 --- a/src/commands/open.rs +++ b/src/commands/open.rs @@ -77,10 +77,10 @@ fn run( file_extension.or(path_str.split('.').last().map(String::from)) }; - if let Some(uuid) = contents_tag.origin { + if contents_tag.origin != uuid::Uuid::nil() { // If we have loaded something, track its source yield ReturnSuccess::action(CommandAction::AddSpanSource( - uuid, + contents_tag.origin, span_source, )); } @@ -147,7 +147,7 @@ pub async fn fetch( Value::string(s), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::File(cwd.to_string_lossy().to_string()), )), @@ -166,7 +166,7 @@ pub async fn fetch( Value::string(s), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::File(cwd.to_string_lossy().to_string()), )), @@ -175,7 +175,7 @@ pub async fn fetch( Value::binary(bytes), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::File(cwd.to_string_lossy().to_string()), )), @@ -186,7 +186,7 @@ pub async fn fetch( Value::binary(bytes), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::File(cwd.to_string_lossy().to_string()), )) @@ -204,7 +204,7 @@ pub async fn fetch( Value::string(s), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::File(cwd.to_string_lossy().to_string()), )), @@ -213,7 +213,7 @@ pub async fn fetch( Value::binary(bytes), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::File(cwd.to_string_lossy().to_string()), )), @@ -224,7 +224,7 @@ pub async fn fetch( Value::binary(bytes), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::File(cwd.to_string_lossy().to_string()), )) @@ -235,7 +235,7 @@ pub async fn fetch( Value::binary(bytes), Tag { span, - origin: Some(Uuid::new_v4()), + origin: Uuid::new_v4(), }, SpanSource::File(cwd.to_string_lossy().to_string()), )), diff --git a/src/commands/post.rs b/src/commands/post.rs index f653e6492a..6d5627a65f 100644 --- a/src/commands/post.rs +++ b/src/commands/post.rs @@ -85,10 +85,10 @@ fn run( file_extension.or(path_str.split('.').last().map(String::from)) }; - if let Some(uuid) = contents_tag.origin { + if contents_tag.origin != uuid::Uuid::nil() { // If we have loaded something, track its source yield ReturnSuccess::action(CommandAction::AddSpanSource( - uuid, + contents_tag.origin, span_source, )); } diff --git a/src/commands/save.rs b/src/commands/save.rs index 253045b3f9..9c12fd2414 100644 --- a/src/commands/save.rs +++ b/src/commands/save.rs @@ -136,7 +136,7 @@ fn save( // If there is no filename, check the metadata for the origin filename if input.len() > 0 { let origin = input[0].origin(); - match origin.and_then(|x| source_map.get(&x)) { + match source_map.get(&origin) { Some(path) => match path { SpanSource::File(file) => { full_path.push(Path::new(file)); diff --git a/src/commands/tags.rs b/src/commands/tags.rs index 2b45105c2d..9180ba2c61 100644 --- a/src/commands/tags.rs +++ b/src/commands/tags.rs @@ -42,7 +42,7 @@ fn tags(args: CommandArgs, _registry: &CommandRegistry) -> Result { tags.insert("origin", Value::string(source)); } diff --git a/src/data/meta.rs b/src/data/meta.rs index 78711cc882..010c98037f 100644 --- a/src/data/meta.rs +++ b/src/data/meta.rs @@ -39,7 +39,7 @@ pub trait TaggedItem: Sized { self, Tag { span: Span::unknown(), - origin: None, + origin: uuid::Uuid::nil(), }, ) } @@ -90,15 +90,14 @@ impl Tagged { self.tag.span } - // TODO: This should not be optional - pub fn origin(&self) -> Option { + pub fn origin(&self) -> uuid::Uuid { self.tag.origin } pub fn origin_name(&self, source_map: &SourceMap) -> Option { - match self.tag.origin.map(|x| source_map.get(&x)) { - Some(Some(SpanSource::File(file))) => Some(file.clone()), - Some(Some(SpanSource::Url(url))) => Some(url.clone()), + match source_map.get(&self.tag.origin) { + Some(SpanSource::File(file)) => Some(file.clone()), + Some(SpanSource::Url(url)) => Some(url.clone()), _ => None, } } @@ -168,20 +167,23 @@ impl From<&std::ops::Range> for Span { Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize, Hash, Getters, )] pub struct Tag { - pub origin: Option, + pub origin: Uuid, pub span: Span, } impl From for Tag { fn from(span: Span) -> Self { - Tag { origin: None, span } + Tag { + origin: uuid::Uuid::nil(), + span, + } } } impl From<&Span> for Tag { fn from(span: &Span) -> Self { Tag { - origin: None, + origin: uuid::Uuid::nil(), span: *span, } } @@ -190,7 +192,7 @@ impl From<&Span> for Tag { impl From<(usize, usize, Uuid)> for Tag { fn from((start, end, origin): (usize, usize, Uuid)) -> Self { Tag { - origin: Some(origin), + origin, span: Span { start, end }, } } @@ -199,7 +201,11 @@ impl From<(usize, usize, Uuid)> for Tag { impl From<(usize, usize, Option)> for Tag { fn from((start, end, origin): (usize, usize, Option)) -> Self { Tag { - origin, + origin: if let Some(uuid) = origin { + uuid + } else { + uuid::Uuid::nil() + }, span: Span { start, end }, } } @@ -208,7 +214,7 @@ impl From<(usize, usize, Option)> for Tag { impl From> for Tag { fn from(input: nom_locate::LocatedSpanEx<&str, Uuid>) -> Tag { Tag { - origin: Some(input.extra), + origin: input.extra, span: Span { start: input.offset, end: input.offset + input.fragment.len(), @@ -231,19 +237,22 @@ impl From<&Tag> for Span { impl Tag { pub fn unknown_origin(span: Span) -> Tag { - Tag { origin: None, span } + Tag { + origin: uuid::Uuid::nil(), + span, + } } pub fn unknown_span(origin: Uuid) -> Tag { Tag { - origin: Some(origin), + origin, span: Span::unknown(), } } pub fn unknown() -> Tag { Tag { - origin: None, + origin: uuid::Uuid::nil(), span: Span::unknown(), } } diff --git a/src/parser/parse/files.rs b/src/parser/parse/files.rs index 6cedb1e99c..65a2620936 100644 --- a/src/parser/parse/files.rs +++ b/src/parser/parse/files.rs @@ -22,7 +22,7 @@ impl language_reporting::ReportingFiles for Files { } fn file_id(&self, tag: Self::Span) -> Self::FileId { - tag.origin.unwrap() + tag.origin } fn file_name(&self, _file: Self::FileId) -> FileName { diff --git a/src/plugins/textview.rs b/src/plugins/textview.rs index 423cae8765..cad2e16e62 100644 --- a/src/plugins/textview.rs +++ b/src/plugins/textview.rs @@ -219,7 +219,7 @@ fn view_text_value(value: &Tagged, source_map: &SourceMap) { let value_origin = value.origin(); match value.item { Value::Primitive(Primitive::String(ref s)) => { - let source = value_origin.and_then(|x| source_map.get(&x)); + let source = source_map.get(&value_origin); if let Some(source) = source { let extension: Option = match source { From 3659e511633993ff0807ad34dde5057d9009c912 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Wed, 18 Sep 2019 19:18:58 +1200 Subject: [PATCH 3/3] Fix origin in binaryview --- src/plugins/binaryview.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/binaryview.rs b/src/plugins/binaryview.rs index 3b92177374..895ec97fe2 100644 --- a/src/plugins/binaryview.rs +++ b/src/plugins/binaryview.rs @@ -24,7 +24,7 @@ impl Plugin for BinaryView { let value_origin = v.origin(); match v.item { Value::Primitive(Primitive::Binary(b)) => { - let source = value_origin.and_then(|x| call_info.source_map.get(&x)); + let source = call_info.source_map.get(&value_origin); let _ = view_binary(&b, source, call_info.args.has("lores")); } _ => {}