Fix autoview breakage

This commit is contained in:
Jonathan Turner 2019-09-18 18:37:04 +12:00
parent 0beb067211
commit 2cf7249794
5 changed files with 95 additions and 34 deletions

View File

@ -110,6 +110,7 @@ fn is_single_origined_text_value(input: &Vec<Tagged<Value>>) -> bool {
if input.len() != 1 {
return false;
}
if let Tagged {
item: Value::Primitive(Primitive::String(_)),
tag: Tag {

View File

@ -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();

View File

@ -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<String>, 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,
));
}
}

View File

@ -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<String>, 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,
));
}
}

View File

@ -86,6 +86,10 @@ impl<T> Tagged<T> {
self.tag
}
pub fn span(&self) -> Span {
self.tag.span
}
// TODO: This should not be optional
pub fn origin(&self) -> Option<uuid::Uuid> {
self.tag.origin