Improve describe to be more accurate (#5116)

This commit is contained in:
JT
2022-04-07 16:34:09 +12:00
committed by GitHub
parent ef1934a7ee
commit 4409185e1b
8 changed files with 168 additions and 148 deletions

View File

@ -18,7 +18,7 @@ impl Expression {
Expression {
expr: Expr::Garbage,
span,
ty: Type::Unknown,
ty: Type::Any,
custom_completion: None,
}
}

View File

@ -192,11 +192,11 @@ impl EngineState {
files: im::vector![],
file_contents: im::vector![],
vars: im::vector![
Variable::new(Span::new(0, 0), Type::Unknown),
Variable::new(Span::new(0, 0), Type::Unknown),
Variable::new(Span::new(0, 0), Type::Unknown),
Variable::new(Span::new(0, 0), Type::Unknown),
Variable::new(Span::new(0, 0), Type::Unknown)
Variable::new(Span::new(0, 0), Type::Any),
Variable::new(Span::new(0, 0), Type::Any),
Variable::new(Span::new(0, 0), Type::Any),
Variable::new(Span::new(0, 0), Type::Any),
Variable::new(Span::new(0, 0), Type::Any)
],
decls: im::vector![],
aliases: im::vector![],

View File

@ -96,37 +96,37 @@ pub enum SyntaxShape {
impl SyntaxShape {
pub fn to_type(&self) -> Type {
match self {
SyntaxShape::Any => Type::Unknown,
SyntaxShape::Any => Type::Any,
SyntaxShape::Block(_) => Type::Block,
SyntaxShape::Binary => Type::Binary,
SyntaxShape::CellPath => Type::Unknown,
SyntaxShape::CellPath => Type::Any,
SyntaxShape::Custom(custom, _) => custom.to_type(),
SyntaxShape::DateTime => Type::Date,
SyntaxShape::Duration => Type::Duration,
SyntaxShape::Expression => Type::Unknown,
SyntaxShape::Expression => Type::Any,
SyntaxShape::Filepath => Type::String,
SyntaxShape::Filesize => Type::Filesize,
SyntaxShape::FullCellPath => Type::Unknown,
SyntaxShape::FullCellPath => Type::Any,
SyntaxShape::GlobPattern => Type::String,
SyntaxShape::ImportPattern => Type::Unknown,
SyntaxShape::ImportPattern => Type::Any,
SyntaxShape::Int => Type::Int,
SyntaxShape::List(x) => {
let contents = x.to_type();
Type::List(Box::new(contents))
}
SyntaxShape::Keyword(_, expr) => expr.to_type(),
SyntaxShape::MathExpression => Type::Unknown,
SyntaxShape::MathExpression => Type::Any,
SyntaxShape::Number => Type::Number,
SyntaxShape::Operator => Type::Unknown,
SyntaxShape::Range => Type::Unknown,
SyntaxShape::Operator => Type::Any,
SyntaxShape::Range => Type::Any,
SyntaxShape::Record => Type::Record(vec![]), // FIXME: Add actual record type
SyntaxShape::RowCondition => Type::Bool,
SyntaxShape::Boolean => Type::Bool,
SyntaxShape::Signature => Type::Signature,
SyntaxShape::String => Type::String,
SyntaxShape::Table => Type::List(Box::new(Type::Unknown)), // FIXME: Tables should have better types
SyntaxShape::VarWithOptType => Type::Unknown,
SyntaxShape::Variable => Type::Unknown,
SyntaxShape::Table => Type::List(Box::new(Type::Any)), // FIXME: Tables should have better types
SyntaxShape::VarWithOptType => Type::Any,
SyntaxShape::Variable => Type::Any,
}
}
}

View File

@ -20,9 +20,9 @@ pub enum Type {
Number,
Nothing,
Record(Vec<(String, Type)>),
Table,
Table(Vec<(String, Type)>),
ListStream,
Unknown,
Any,
Error,
Binary,
Custom,
@ -46,9 +46,9 @@ impl Type {
Type::Number => SyntaxShape::Number,
Type::Nothing => SyntaxShape::Any,
Type::Record(_) => SyntaxShape::Record,
Type::Table => SyntaxShape::Table,
Type::Table(_) => SyntaxShape::Table,
Type::ListStream => SyntaxShape::List(Box::new(SyntaxShape::Any)),
Type::Unknown => SyntaxShape::Any,
Type::Any => SyntaxShape::Any,
Type::Error => SyntaxShape::Any,
Type::Binary => SyntaxShape::Binary,
Type::Custom => SyntaxShape::Any,
@ -78,13 +78,21 @@ impl Display for Type {
.collect::<Vec<String>>()
.join(", "),
),
Type::Table => write!(f, "table"),
Type::Table(columns) => write!(
f,
"table<{}>",
columns
.iter()
.map(|(x, y)| format!("{}: {}", x, y))
.collect::<Vec<String>>()
.join(", ")
),
Type::List(l) => write!(f, "list<{}>", l),
Type::Nothing => write!(f, "nothing"),
Type::Number => write!(f, "number"),
Type::String => write!(f, "string"),
Type::ListStream => write!(f, "list stream"),
Type::Unknown => write!(f, "unknown"),
Type::Any => write!(f, "any"),
Type::Error => write!(f, "error"),
Type::Binary => write!(f, "binary"),
Type::Custom => write!(f, "custom"),

View File

@ -378,7 +378,26 @@ impl Value {
.map(|(x, y)| (x.clone(), y.get_type()))
.collect(),
),
Value::List { .. } => Type::List(Box::new(Type::Unknown)), // FIXME
Value::List { vals, .. } => {
let mut ty = None;
for val in vals {
let val_ty = val.get_type();
match &ty {
Some(x) => {
if &val_ty != x {
ty = Some(Type::Any)
}
}
None => ty = Some(val_ty),
}
}
match ty {
Some(Type::Record(columns)) => Type::Table(columns),
Some(ty) => Type::List(Box::new(ty)),
None => Type::List(Box::new(ty.unwrap_or(Type::Any))),
}
}
Value::Nothing { .. } => Type::Nothing,
Value::Block { .. } => Type::Block,
Value::Error { .. } => Type::Error,
@ -1735,8 +1754,8 @@ impl Value {
}
if !type_compatible(self.get_type(), rhs.get_type())
&& (self.get_type() != Type::Unknown)
&& (rhs.get_type() != Type::Unknown)
&& (self.get_type() != Type::Any)
&& (rhs.get_type() != Type::Any)
{
return Err(ShellError::TypeMismatch("compatible type".to_string(), op));
}
@ -1763,8 +1782,8 @@ impl Value {
}
if !type_compatible(self.get_type(), rhs.get_type())
&& (self.get_type() != Type::Unknown)
&& (rhs.get_type() != Type::Unknown)
&& (self.get_type() != Type::Any)
&& (rhs.get_type() != Type::Any)
{
return Err(ShellError::TypeMismatch("compatible type".to_string(), op));
}
@ -1791,8 +1810,8 @@ impl Value {
}
if !type_compatible(self.get_type(), rhs.get_type())
&& (self.get_type() != Type::Unknown)
&& (rhs.get_type() != Type::Unknown)
&& (self.get_type() != Type::Any)
&& (rhs.get_type() != Type::Any)
{
return Err(ShellError::TypeMismatch("compatible type".to_string(), op));
}
@ -1819,8 +1838,8 @@ impl Value {
}
if !type_compatible(self.get_type(), rhs.get_type())
&& (self.get_type() != Type::Unknown)
&& (rhs.get_type() != Type::Unknown)
&& (self.get_type() != Type::Any)
&& (rhs.get_type() != Type::Any)
{
return Err(ShellError::TypeMismatch("compatible type".to_string(), op));
}