Merge pull request #680 from jonathandturner/fix_673

Add origins back to open
This commit is contained in:
Jonathan Turner 2019-09-18 19:43:56 +12:00 committed by GitHub
commit 4017f67855
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 133 additions and 66 deletions

View file

@ -110,12 +110,10 @@ fn is_single_origined_text_value(input: &Vec<Tagged<Value>>) -> bool {
if input.len() != 1 { if input.len() != 1 {
return false; return false;
} }
if let Tagged { if let Tagged {
item: Value::Primitive(Primitive::String(_)), item: Value::Primitive(Primitive::String(_)),
tag: Tag { tag: Tag { origin, .. },
origin: Some(origin),
..
},
} = input[0] } = input[0]
{ {
origin != uuid::Uuid::nil() origin != uuid::Uuid::nil()

View file

@ -1,6 +1,7 @@
use crate::commands::command::CommandAction; use crate::commands::command::CommandAction;
use crate::commands::PerItemCommand; use crate::commands::PerItemCommand;
use crate::commands::UnevaluatedCallInfo; use crate::commands::UnevaluatedCallInfo;
use crate::data::meta::Span;
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::parser::registry; use crate::parser::registry;
use crate::prelude::*; use crate::prelude::*;
@ -70,14 +71,14 @@ impl PerItemCommand for Enter {
crate::commands::open::fetch( crate::commands::open::fetch(
&full_path, &full_path,
&location_clone, &location_clone,
Tag::unknown(), Span::unknown(),
) )
.await.unwrap(); .await.unwrap();
if let Some(uuid) = contents_tag.origin { if contents_tag.origin != uuid::Uuid::nil() {
// If we have loaded something, track its source // If we have loaded something, track its source
yield ReturnSuccess::action(CommandAction::AddSpanSource( yield ReturnSuccess::action(CommandAction::AddSpanSource(
uuid, contents_tag.origin,
span_source, span_source,
)); ));
} }

View file

@ -1,5 +1,6 @@
use crate::commands::UnevaluatedCallInfo; use crate::commands::UnevaluatedCallInfo;
use crate::context::SpanSource; use crate::context::SpanSource;
use crate::data::meta::Span;
use crate::data::Value; use crate::data::Value;
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::parser::hir::SyntaxShape; use crate::parser::hir::SyntaxShape;
@ -9,6 +10,7 @@ use mime::Mime;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use surf::mime; use surf::mime;
use uuid::Uuid;
pub struct Fetch; pub struct Fetch;
impl PerItemCommand for Fetch { impl PerItemCommand for Fetch {
@ -51,14 +53,14 @@ fn run(
}; };
let path_buf = path.as_path()?; let path_buf = path.as_path()?;
let path_str = path_buf.display().to_string(); 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 has_raw = call_info.args.has("raw");
let registry = registry.clone(); let registry = registry.clone();
let raw_args = raw_args.clone(); let raw_args = raw_args.clone();
let stream = async_stream_block! { 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 { if let Err(e) = result {
yield Err(e); yield Err(e);
@ -74,10 +76,10 @@ fn run(
file_extension.or(path_str.split('.').last().map(String::from)) 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 // If we have loaded something, track its source
yield ReturnSuccess::action(CommandAction::AddSpanSource( yield ReturnSuccess::action(CommandAction::AddSpanSource(
uuid, contents_tag.origin,
span_source, span_source,
)); ));
} }
@ -129,13 +131,13 @@ fn run(
pub async fn fetch( pub async fn fetch(
location: &str, location: &str,
tag: Tag, span: Span,
) -> Result<(Option<String>, Value, Tag, SpanSource), ShellError> { ) -> Result<(Option<String>, Value, Tag, SpanSource), ShellError> {
if let Err(_) = url::Url::parse(location) { if let Err(_) = url::Url::parse(location) {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Incomplete or incorrect url", "Incomplete or incorrect url",
"expected a full url", "expected a full url",
tag, span,
)); ));
} }
@ -151,10 +153,13 @@ pub async fn fetch(
ShellError::labeled_error( ShellError::labeled_error(
"Could not load text from remote url", "Could not load text from remote url",
"could not load", "could not load",
tag, span,
) )
})?), })?),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)), )),
(mime::APPLICATION, mime::JSON) => Ok(( (mime::APPLICATION, mime::JSON) => Ok((
@ -163,10 +168,13 @@ pub async fn fetch(
ShellError::labeled_error( ShellError::labeled_error(
"Could not load text from remote url", "Could not load text from remote url",
"could not load", "could not load",
tag, span,
) )
})?), })?),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)), )),
(mime::APPLICATION, mime::OCTET_STREAM) => { (mime::APPLICATION, mime::OCTET_STREAM) => {
@ -174,13 +182,16 @@ pub async fn fetch(
ShellError::labeled_error( ShellError::labeled_error(
"Could not load binary file", "Could not load binary file",
"could not load", "could not load",
tag, span,
) )
})?; })?;
Ok(( Ok((
None, None,
Value::binary(buf), Value::binary(buf),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)) ))
} }
@ -190,10 +201,13 @@ pub async fn fetch(
ShellError::labeled_error( ShellError::labeled_error(
"Could not load svg from remote url", "Could not load svg from remote url",
"could not load", "could not load",
tag, span,
) )
})?), })?),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)), )),
(mime::IMAGE, image_ty) => { (mime::IMAGE, image_ty) => {
@ -201,13 +215,16 @@ pub async fn fetch(
ShellError::labeled_error( ShellError::labeled_error(
"Could not load image file", "Could not load image file",
"could not load", "could not load",
tag, span,
) )
})?; })?;
Ok(( Ok((
Some(image_ty.to_string()), Some(image_ty.to_string()),
Value::binary(buf), Value::binary(buf),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)) ))
} }
@ -217,10 +234,13 @@ pub async fn fetch(
ShellError::labeled_error( ShellError::labeled_error(
"Could not load text from remote url", "Could not load text from remote url",
"could not load", "could not load",
tag, span,
) )
})?), })?),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)), )),
(mime::TEXT, mime::PLAIN) => { (mime::TEXT, mime::PLAIN) => {
@ -241,17 +261,23 @@ pub async fn fetch(
ShellError::labeled_error( ShellError::labeled_error(
"Could not load text from remote url", "Could not load text from remote url",
"could not load", "could not load",
tag, span,
) )
})?), })?),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)) ))
} }
(ty, sub_ty) => Ok(( (ty, sub_ty) => Ok((
None, None,
Value::string(format!("Not yet supported MIME type: {} {}", ty, sub_ty)), Value::string(format!("Not yet supported MIME type: {} {}", ty, sub_ty)),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)), )),
} }
@ -259,7 +285,10 @@ pub async fn fetch(
None => Ok(( None => Ok((
None, None,
Value::string(format!("No content type found")), Value::string(format!("No content type found")),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::Url(location.to_string()), SpanSource::Url(location.to_string()),
)), )),
}, },
@ -267,7 +296,7 @@ pub async fn fetch(
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"URL could not be opened", "URL could not be opened",
"url not found", "url not found",
tag, span,
)); ));
} }
} }

View file

@ -1,11 +1,13 @@
use crate::commands::UnevaluatedCallInfo; use crate::commands::UnevaluatedCallInfo;
use crate::context::SpanSource; use crate::context::SpanSource;
use crate::data::meta::Span;
use crate::data::Value; use crate::data::Value;
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::parser::hir::SyntaxShape; use crate::parser::hir::SyntaxShape;
use crate::parser::registry::Signature; use crate::parser::registry::Signature;
use crate::prelude::*; use crate::prelude::*;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use uuid::Uuid;
pub struct Open; pub struct Open;
impl PerItemCommand for Open { impl PerItemCommand for Open {
@ -52,7 +54,7 @@ fn run(
}; };
let path_buf = path.as_path()?; let path_buf = path.as_path()?;
let path_str = path_buf.display().to_string(); 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 has_raw = call_info.args.has("raw");
let registry = registry.clone(); let registry = registry.clone();
let raw_args = raw_args.clone(); let raw_args = raw_args.clone();
@ -75,10 +77,10 @@ fn run(
file_extension.or(path_str.split('.').last().map(String::from)) 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 // If we have loaded something, track its source
yield ReturnSuccess::action(CommandAction::AddSpanSource( yield ReturnSuccess::action(CommandAction::AddSpanSource(
uuid, contents_tag.origin,
span_source, span_source,
)); ));
} }
@ -131,7 +133,7 @@ fn run(
pub async fn fetch( pub async fn fetch(
cwd: &PathBuf, cwd: &PathBuf,
location: &str, location: &str,
tag: Tag, span: Span,
) -> Result<(Option<String>, Value, Tag, SpanSource), ShellError> { ) -> Result<(Option<String>, Value, Tag, SpanSource), ShellError> {
let mut cwd = cwd.clone(); let mut cwd = cwd.clone();
@ -143,7 +145,10 @@ pub async fn fetch(
cwd.extension() cwd.extension()
.map(|name| name.to_string_lossy().to_string()), .map(|name| name.to_string_lossy().to_string()),
Value::string(s), Value::string(s),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)), )),
Err(_) => { Err(_) => {
@ -159,13 +164,19 @@ pub async fn fetch(
cwd.extension() cwd.extension()
.map(|name| name.to_string_lossy().to_string()), .map(|name| name.to_string_lossy().to_string()),
Value::string(s), Value::string(s),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)), )),
Err(_) => Ok(( Err(_) => Ok((
None, None,
Value::binary(bytes), Value::binary(bytes),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)), )),
} }
@ -173,7 +184,10 @@ pub async fn fetch(
Ok(( Ok((
None, None,
Value::binary(bytes), Value::binary(bytes),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)) ))
} }
@ -188,13 +202,19 @@ pub async fn fetch(
cwd.extension() cwd.extension()
.map(|name| name.to_string_lossy().to_string()), .map(|name| name.to_string_lossy().to_string()),
Value::string(s), Value::string(s),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)), )),
Err(_) => Ok(( Err(_) => Ok((
None, None,
Value::binary(bytes), Value::binary(bytes),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)), )),
} }
@ -202,7 +222,10 @@ pub async fn fetch(
Ok(( Ok((
None, None,
Value::binary(bytes), Value::binary(bytes),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)) ))
} }
@ -210,7 +233,10 @@ pub async fn fetch(
_ => Ok(( _ => Ok((
None, None,
Value::binary(bytes), Value::binary(bytes),
tag, Tag {
span,
origin: Uuid::new_v4(),
},
SpanSource::File(cwd.to_string_lossy().to_string()), SpanSource::File(cwd.to_string_lossy().to_string()),
)), )),
} }
@ -220,7 +246,7 @@ pub async fn fetch(
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"File could not be opened", "File could not be opened",
"file not found", "file not found",
tag, span,
)); ));
} }
} }
@ -228,7 +254,7 @@ pub async fn fetch(
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"File could not be opened", "File could not be opened",
"file not found", "file not found",
tag, span,
)); ));
} }
} }

View file

@ -85,10 +85,10 @@ fn run(
file_extension.or(path_str.split('.').last().map(String::from)) 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 // If we have loaded something, track its source
yield ReturnSuccess::action(CommandAction::AddSpanSource( yield ReturnSuccess::action(CommandAction::AddSpanSource(
uuid, contents_tag.origin,
span_source, span_source,
)); ));
} }

View file

@ -136,7 +136,7 @@ fn save(
// If there is no filename, check the metadata for the origin filename // If there is no filename, check the metadata for the origin filename
if input.len() > 0 { if input.len() > 0 {
let origin = input[0].origin(); let origin = input[0].origin();
match origin.and_then(|x| source_map.get(&x)) { match source_map.get(&origin) {
Some(path) => match path { Some(path) => match path {
SpanSource::File(file) => { SpanSource::File(file) => {
full_path.push(Path::new(file)); full_path.push(Path::new(file));

View file

@ -42,7 +42,7 @@ fn tags(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream,
dict.insert("end", Value::int(span.end as i64)); dict.insert("end", Value::int(span.end as i64));
tags.insert_tagged("span", dict.into_tagged_value()); tags.insert_tagged("span", dict.into_tagged_value());
match origin.and_then(|x| source_map.get(&x)) { match source_map.get(&origin) {
Some(SpanSource::File(source)) => { Some(SpanSource::File(source)) => {
tags.insert("origin", Value::string(source)); tags.insert("origin", Value::string(source));
} }

View file

@ -39,7 +39,7 @@ pub trait TaggedItem: Sized {
self, self,
Tag { Tag {
span: Span::unknown(), span: Span::unknown(),
origin: None, origin: uuid::Uuid::nil(),
}, },
) )
} }
@ -86,15 +86,18 @@ impl<T> Tagged<T> {
self.tag self.tag
} }
// TODO: This should not be optional pub fn span(&self) -> Span {
pub fn origin(&self) -> Option<uuid::Uuid> { self.tag.span
}
pub fn origin(&self) -> uuid::Uuid {
self.tag.origin self.tag.origin
} }
pub fn origin_name(&self, source_map: &SourceMap) -> Option<String> { pub fn origin_name(&self, source_map: &SourceMap) -> Option<String> {
match self.tag.origin.map(|x| source_map.get(&x)) { match source_map.get(&self.tag.origin) {
Some(Some(SpanSource::File(file))) => Some(file.clone()), Some(SpanSource::File(file)) => Some(file.clone()),
Some(Some(SpanSource::Url(url))) => Some(url.clone()), Some(SpanSource::Url(url)) => Some(url.clone()),
_ => None, _ => None,
} }
} }
@ -164,20 +167,23 @@ impl From<&std::ops::Range<usize>> for Span {
Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize, Hash, Getters, Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize, Hash, Getters,
)] )]
pub struct Tag { pub struct Tag {
pub origin: Option<Uuid>, pub origin: Uuid,
pub span: Span, pub span: Span,
} }
impl From<Span> for Tag { impl From<Span> for Tag {
fn from(span: Span) -> Self { fn from(span: Span) -> Self {
Tag { origin: None, span } Tag {
origin: uuid::Uuid::nil(),
span,
}
} }
} }
impl From<&Span> for Tag { impl From<&Span> for Tag {
fn from(span: &Span) -> Self { fn from(span: &Span) -> Self {
Tag { Tag {
origin: None, origin: uuid::Uuid::nil(),
span: *span, span: *span,
} }
} }
@ -186,7 +192,7 @@ impl From<&Span> for Tag {
impl From<(usize, usize, Uuid)> for Tag { impl From<(usize, usize, Uuid)> for Tag {
fn from((start, end, origin): (usize, usize, Uuid)) -> Self { fn from((start, end, origin): (usize, usize, Uuid)) -> Self {
Tag { Tag {
origin: Some(origin), origin,
span: Span { start, end }, span: Span { start, end },
} }
} }
@ -195,7 +201,11 @@ impl From<(usize, usize, Uuid)> for Tag {
impl From<(usize, usize, Option<Uuid>)> for Tag { impl From<(usize, usize, Option<Uuid>)> for Tag {
fn from((start, end, origin): (usize, usize, Option<Uuid>)) -> Self { fn from((start, end, origin): (usize, usize, Option<Uuid>)) -> Self {
Tag { Tag {
origin, origin: if let Some(uuid) = origin {
uuid
} else {
uuid::Uuid::nil()
},
span: Span { start, end }, span: Span { start, end },
} }
} }
@ -204,7 +214,7 @@ impl From<(usize, usize, Option<Uuid>)> for Tag {
impl From<nom_locate::LocatedSpanEx<&str, Uuid>> for Tag { impl From<nom_locate::LocatedSpanEx<&str, Uuid>> for Tag {
fn from(input: nom_locate::LocatedSpanEx<&str, Uuid>) -> Tag { fn from(input: nom_locate::LocatedSpanEx<&str, Uuid>) -> Tag {
Tag { Tag {
origin: Some(input.extra), origin: input.extra,
span: Span { span: Span {
start: input.offset, start: input.offset,
end: input.offset + input.fragment.len(), end: input.offset + input.fragment.len(),
@ -227,19 +237,22 @@ impl From<&Tag> for Span {
impl Tag { impl Tag {
pub fn unknown_origin(span: Span) -> 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 { pub fn unknown_span(origin: Uuid) -> Tag {
Tag { Tag {
origin: Some(origin), origin,
span: Span::unknown(), span: Span::unknown(),
} }
} }
pub fn unknown() -> Tag { pub fn unknown() -> Tag {
Tag { Tag {
origin: None, origin: uuid::Uuid::nil(),
span: Span::unknown(), span: Span::unknown(),
} }
} }

View file

@ -22,7 +22,7 @@ impl language_reporting::ReportingFiles for Files {
} }
fn file_id(&self, tag: Self::Span) -> Self::FileId { fn file_id(&self, tag: Self::Span) -> Self::FileId {
tag.origin.unwrap() tag.origin
} }
fn file_name(&self, _file: Self::FileId) -> FileName { fn file_name(&self, _file: Self::FileId) -> FileName {

View file

@ -24,7 +24,7 @@ impl Plugin for BinaryView {
let value_origin = v.origin(); let value_origin = v.origin();
match v.item { match v.item {
Value::Primitive(Primitive::Binary(b)) => { 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")); let _ = view_binary(&b, source, call_info.args.has("lores"));
} }
_ => {} _ => {}

View file

@ -219,7 +219,7 @@ fn view_text_value(value: &Tagged<Value>, source_map: &SourceMap) {
let value_origin = value.origin(); let value_origin = value.origin();
match value.item { match value.item {
Value::Primitive(Primitive::String(ref s)) => { 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 { if let Some(source) = source {
let extension: Option<String> = match source { let extension: Option<String> = match source {