Merge pull request #269 from jonathandturner/enter_fixes

Improve enter and fix bugs
This commit is contained in:
Jonathan Turner 2019-08-11 08:54:08 +12:00 committed by GitHub
commit 8f26fcf653
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 88 additions and 44 deletions

View File

@ -51,7 +51,10 @@ pub fn autoview(
let result = table.run(raw.with_input(input), &context.commands).await.unwrap(); let result = table.run(raw.with_input(input), &context.commands).await.unwrap();
result.collect::<Vec<_>>().await; result.collect::<Vec<_>>().await;
} else { } else {
println!("TODO!") let table = context.expect_command("table");
let result = table.run(raw.with_input(input), &context.commands).await.unwrap();
result.collect::<Vec<_>>().await;
//println!("TODO!")
// TODO // TODO
// let mut host = context.host.lock().unwrap(); // let mut host = context.host.lock().unwrap();
// for i in input.iter() { // for i in input.iter() {

View File

@ -158,13 +158,18 @@ impl InternalCommand {
let full_path = std::path::PathBuf::from(cwd); let full_path = std::path::PathBuf::from(cwd);
let (file_extension, contents, contents_tag, _) = let (file_extension, contents, contents_tag, span_source) =
crate::commands::open::fetch( crate::commands::open::fetch(
&full_path, &full_path,
&location, &location,
Span::unknown(), Span::unknown(),
)?; )?;
if let Some(uuid) = contents_tag.origin {
// If we have loaded something, track its source
context.add_span_source(uuid, span_source);
}
match contents { match contents {
Value::Primitive(Primitive::String(string)) => { Value::Primitive(Primitive::String(string)) => {
let value = crate::commands::open::parse_as_value( let value = crate::commands::open::parse_as_value(
@ -178,7 +183,7 @@ impl InternalCommand {
} }
value => context value => context
.shell_manager .shell_manager
.push(Box::new(ValueShell::new(value.tagged(Tag::unknown())))), .push(Box::new(ValueShell::new(value.tagged(contents_tag)))),
} }
} }
} }

View File

@ -226,35 +226,43 @@ pub fn fetch(
} }
} else { } else {
cwd.push(Path::new(location)); cwd.push(Path::new(location));
match std::fs::read(&cwd) { if let Ok(cwd) = dunce::canonicalize(cwd) {
Ok(bytes) => match std::str::from_utf8(&bytes) { match std::fs::read(&cwd) {
Ok(s) => Ok(( Ok(bytes) => match std::str::from_utf8(&bytes) {
cwd.extension() Ok(s) => Ok((
.map(|name| name.to_string_lossy().to_string()), cwd.extension()
Value::string(s), .map(|name| name.to_string_lossy().to_string()),
Tag { Value::string(s),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
)),
Err(_) => Ok((
None,
Value::Binary(bytes),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
)),
},
Err(_) => {
return Err(ShellError::labeled_error(
"File could not be opened",
"file not found",
span, span,
origin: Some(Uuid::new_v4()), ));
}, }
SpanSource::File(cwd.to_string_lossy().to_string()),
)),
Err(_) => Ok((
None,
Value::Binary(bytes),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::File(cwd.to_string_lossy().to_string()),
)),
},
Err(_) => {
return Err(ShellError::labeled_error(
"File could not be opened",
"file not found",
span,
));
} }
} else {
return Err(ShellError::labeled_error(
"File could not be opened",
"file not found",
span,
));
} }
} }
} }

View File

@ -6,9 +6,17 @@ pub fn shells(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputSt
let mut shells_out = VecDeque::new(); let mut shells_out = VecDeque::new();
let span = args.call_info.name_span; let span = args.call_info.name_span;
for shell in args.shell_manager.shells.lock().unwrap().iter() { let shells_len = args.shell_manager.shells.lock().unwrap().len();
for (index, shell) in args.shell_manager.shells.lock().unwrap().iter().enumerate() {
let mut dict = TaggedDictBuilder::new(Tag::unknown_origin(span)); let mut dict = TaggedDictBuilder::new(Tag::unknown_origin(span));
dict.insert("name", shell.name());
if index == (shells_len - 1) {
dict.insert(" ", "X".to_string());
} else {
dict.insert(" ", " ".to_string());
}
dict.insert("name", shell.name(&args.call_info.source_map));
dict.insert("path", shell.path()); dict.insert("path", shell.path());
shells_out.push_back(dict.into_tagged_value()); shells_out.push_back(dict.into_tagged_value());

View File

@ -15,16 +15,17 @@ pub fn tags(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStre
let mut dict = TaggedDictBuilder::new(v.tag()); let mut dict = TaggedDictBuilder::new(v.tag());
dict.insert("start", Value::int(span.start as i64)); dict.insert("start", Value::int(span.start as i64));
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());
match origin.map(|x| source_map.get(&x)).flatten() { match origin.map(|x| source_map.get(&x)).flatten() {
Some(SpanSource::File(source)) => { Some(SpanSource::File(source)) => {
dict.insert("origin", Value::string(source)); tags.insert("origin", Value::string(source));
} }
Some(SpanSource::Url(source)) => { Some(SpanSource::Url(source)) => {
dict.insert("origin", Value::string(source)); tags.insert("origin", Value::string(source));
} }
_ => {} _ => {}
} }
tags.insert_tagged("span", dict.into_tagged_value());
} }
tags.into_tagged_value() tags.into_tagged_value()

View File

@ -204,7 +204,7 @@ impl fmt::Debug for ValueDebug<'a> {
impl Tagged<Value> { impl Tagged<Value> {
crate fn tagged_type_name(&self) -> Tagged<String> { crate fn tagged_type_name(&self) -> Tagged<String> {
let name = self.type_name(); let name = self.type_name();
Tagged::from_simple_spanned_item(name, self.span()) Tagged::from_item(name, self.tag())
} }
} }

View File

@ -1,3 +1,4 @@
use crate::context::{SourceMap, SpanSource};
use crate::prelude::*; use crate::prelude::*;
use crate::Text; use crate::Text;
use derive_new::new; use derive_new::new;
@ -108,6 +109,14 @@ impl<T> Tagged<T> {
self.tag.origin self.tag.origin
} }
pub fn origin_name(&self, source_map: &SourceMap) -> Option<String> {
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()),
_ => None,
}
}
pub fn item(&self) -> &T { pub fn item(&self) -> &T {
&self.item &self.item
} }

View File

@ -1,4 +1,5 @@
use crate::commands::command::EvaluatedStaticCommandArgs; use crate::commands::command::EvaluatedStaticCommandArgs;
use crate::context::SourceMap;
use crate::object::dir_entry_dict; use crate::object::dir_entry_dict;
use crate::prelude::*; use crate::prelude::*;
use crate::shell::completer::NuCompleter; use crate::shell::completer::NuCompleter;
@ -55,7 +56,7 @@ impl FilesystemShell {
} }
impl Shell for FilesystemShell { impl Shell for FilesystemShell {
fn name(&self) -> String { fn name(&self, _source_map: &SourceMap) -> String {
"filesystem".to_string() "filesystem".to_string()
} }

View File

@ -1,9 +1,10 @@
use crate::commands::command::EvaluatedStaticCommandArgs; use crate::commands::command::EvaluatedStaticCommandArgs;
use crate::context::SourceMap;
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::stream::OutputStream; use crate::stream::OutputStream;
pub trait Shell { pub trait Shell {
fn name(&self) -> String; fn name(&self, source_map: &SourceMap) -> String;
fn ls(&self, args: EvaluatedStaticCommandArgs) -> Result<OutputStream, ShellError>; fn ls(&self, args: EvaluatedStaticCommandArgs) -> Result<OutputStream, ShellError>;
fn cd(&self, args: EvaluatedStaticCommandArgs) -> Result<OutputStream, ShellError>; fn cd(&self, args: EvaluatedStaticCommandArgs) -> Result<OutputStream, ShellError>;
fn path(&self) -> String; fn path(&self) -> String;

View File

@ -73,8 +73,8 @@ impl ShellManager {
pub fn next(&mut self) { pub fn next(&mut self) {
{ {
let mut x = self.shells.lock().unwrap(); let mut x = self.shells.lock().unwrap();
let shell = x.pop().unwrap(); let shell = x.remove(0);
x.insert(0, shell); x.push(shell);
} }
self.set_path(self.path()); self.set_path(self.path());
} }
@ -82,8 +82,8 @@ impl ShellManager {
pub fn prev(&mut self) { pub fn prev(&mut self) {
{ {
let mut x = self.shells.lock().unwrap(); let mut x = self.shells.lock().unwrap();
let shell = x.remove(0); let shell = x.pop().unwrap();
x.push(shell); x.insert(0, shell);
} }
self.set_path(self.path()); self.set_path(self.path());
} }

View File

@ -1,4 +1,5 @@
use crate::commands::command::EvaluatedStaticCommandArgs; use crate::commands::command::EvaluatedStaticCommandArgs;
use crate::context::SourceMap;
use crate::prelude::*; use crate::prelude::*;
use crate::shell::shell::Shell; use crate::shell::shell::Shell;
use std::ffi::OsStr; use std::ffi::OsStr;
@ -53,8 +54,15 @@ impl ValueShell {
} }
impl Shell for ValueShell { impl Shell for ValueShell {
fn name(&self) -> String { fn name(&self, source_map: &SourceMap) -> String {
"value".to_string() let origin_name = self.value.origin_name(source_map);
format!(
"{}",
match origin_name {
Some(x) => format!("{{{}}}", x),
None => format!("<{}>", self.value.item.type_name(),),
}
)
} }
fn ls(&self, _args: EvaluatedStaticCommandArgs) -> Result<OutputStream, ShellError> { fn ls(&self, _args: EvaluatedStaticCommandArgs) -> Result<OutputStream, ShellError> {