sort enums add missing items to parse_shape_name (#7450)

# Description

This PR adds missing items in `parse_shape_name`, sorts the
`SyntaxShape` enum and the `Type` enum. It's a pain to hunt around for
particular items in an enum when they're unsorted.

# User-Facing Changes


# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
Darren Schroeder 2022-12-13 10:46:22 -06:00 committed by GitHub
parent 0c4d4632ef
commit e529746294
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 118 additions and 106 deletions

View File

@ -2728,28 +2728,36 @@ pub fn parse_shape_name(
b"any" => SyntaxShape::Any, b"any" => SyntaxShape::Any,
b"binary" => SyntaxShape::Binary, b"binary" => SyntaxShape::Binary,
b"block" => SyntaxShape::Block, //FIXME: Blocks should have known output types b"block" => SyntaxShape::Block, //FIXME: Blocks should have known output types
b"closure" => SyntaxShape::Closure(None), //FIXME: Blocks should have known output types b"bool" => SyntaxShape::Boolean,
b"cell-path" => SyntaxShape::CellPath, b"cell-path" => SyntaxShape::CellPath,
b"duration" => SyntaxShape::Duration, b"closure" => SyntaxShape::Closure(None), //FIXME: Blocks should have known output types
b"path" => SyntaxShape::Filepath, b"cond" => SyntaxShape::RowCondition,
// b"custom" => SyntaxShape::Custom(Box::new(SyntaxShape::Any), SyntaxShape::Int),
b"datetime" => SyntaxShape::DateTime,
b"directory" => SyntaxShape::Directory, b"directory" => SyntaxShape::Directory,
b"duration" => SyntaxShape::Duration,
b"error" => SyntaxShape::Error,
b"expr" => SyntaxShape::Expression, b"expr" => SyntaxShape::Expression,
b"filesize" => SyntaxShape::Filesize, b"filesize" => SyntaxShape::Filesize,
b"full-cell-path" => SyntaxShape::FullCellPath,
b"glob" => SyntaxShape::GlobPattern, b"glob" => SyntaxShape::GlobPattern,
b"int" => SyntaxShape::Int, b"int" => SyntaxShape::Int,
b"import-pattern" => SyntaxShape::ImportPattern,
b"keyword" => SyntaxShape::Keyword(vec![], Box::new(SyntaxShape::Any)),
b"list" => SyntaxShape::List(Box::new(SyntaxShape::Any)),
b"math" => SyntaxShape::MathExpression, b"math" => SyntaxShape::MathExpression,
b"nothing" => SyntaxShape::Nothing,
b"number" => SyntaxShape::Number, b"number" => SyntaxShape::Number,
b"one-of" => SyntaxShape::OneOf(vec![]),
b"operator" => SyntaxShape::Operator, b"operator" => SyntaxShape::Operator,
b"path" => SyntaxShape::Filepath,
b"range" => SyntaxShape::Range, b"range" => SyntaxShape::Range,
b"cond" => SyntaxShape::RowCondition, b"record" => SyntaxShape::Record,
b"bool" => SyntaxShape::Boolean,
b"signature" => SyntaxShape::Signature, b"signature" => SyntaxShape::Signature,
b"string" => SyntaxShape::String, b"string" => SyntaxShape::String,
b"variable" => SyntaxShape::Variable,
b"record" => SyntaxShape::Record,
b"list" => SyntaxShape::List(Box::new(SyntaxShape::Any)),
b"table" => SyntaxShape::Table, b"table" => SyntaxShape::Table,
b"error" => SyntaxShape::Error, b"variable" => SyntaxShape::Variable,
b"var-with-opt-type" => SyntaxShape::VarWithOptType,
_ => { _ => {
if bytes.contains(&b'@') { if bytes.contains(&b'@') {
let str = String::from_utf8_lossy(bytes); let str = String::from_utf8_lossy(bytes);
@ -2785,19 +2793,23 @@ pub fn parse_shape_name(
pub fn parse_type(_working_set: &StateWorkingSet, bytes: &[u8]) -> Type { pub fn parse_type(_working_set: &StateWorkingSet, bytes: &[u8]) -> Type {
match bytes { match bytes {
b"int" => Type::Int,
b"float" => Type::Float,
b"range" => Type::Range,
b"bool" => Type::Bool,
b"string" => Type::String,
b"block" => Type::Block,
b"duration" => Type::Duration,
b"date" => Type::Date,
b"filesize" => Type::Filesize,
b"number" => Type::Number,
b"table" => Type::Table(vec![]), //FIXME
b"error" => Type::Error,
b"binary" => Type::Binary, b"binary" => Type::Binary,
b"block" => Type::Block,
b"bool" => Type::Bool,
b"cellpath" => Type::CellPath,
b"closure" => Type::Closure,
b"date" => Type::Date,
b"duration" => Type::Duration,
b"error" => Type::Error,
b"filesize" => Type::Filesize,
b"float" => Type::Float,
b"int" => Type::Int,
b"list" => Type::List(Box::new(Type::Any)),
b"number" => Type::Number,
b"range" => Type::Range,
b"record" => Type::Record(vec![]),
b"string" => Type::String,
b"table" => Type::Table(vec![]), //FIXME
_ => Type::Any, _ => Type::Any,
} }

View File

@ -7,105 +7,105 @@ use crate::{DeclId, Type};
/// The syntactic shapes that values must match to be passed into a command. You can think of this as the type-checking that occurs when you call a function. /// The syntactic shapes that values must match to be passed into a command. You can think of this as the type-checking that occurs when you call a function.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SyntaxShape { pub enum SyntaxShape {
/// A specific match to a word or symbol
Keyword(Vec<u8>, Box<SyntaxShape>),
/// Any syntactic form is allowed /// Any syntactic form is allowed
Any, Any,
/// Strings and string-like bare words are allowed
String,
/// A dotted path to navigate the table
CellPath,
/// A dotted path to navigate the table (including variable)
FullCellPath,
/// Only a numeric (integer or decimal) value is allowed
Number,
/// A range is allowed (eg, `1..3`)
Range,
/// Only an integer value is allowed
Int,
/// A filepath is allowed
Filepath,
/// A directory is allowed
Directory,
/// A glob pattern is allowed, eg `foo*`
GlobPattern,
/// A module path pattern used for imports
ImportPattern,
/// A binary literal /// A binary literal
Binary, Binary,
/// A closure is allowed, eg `{|| start this thing}`
Closure(Option<Vec<SyntaxShape>>),
/// A block is allowed, eg `{start this thing}` /// A block is allowed, eg `{start this thing}`
Block, Block,
/// A table is allowed, eg `[[first, second]; [1, 2]]` /// A boolean value, eg `true` or `false`
Table, Boolean,
/// A list is allowed, eg `[first second]` /// A dotted path to navigate the table
List(Box<SyntaxShape>), CellPath,
/// A filesize value is allowed, eg `10kb` /// A closure is allowed, eg `{|| start this thing}`
Filesize, Closure(Option<Vec<SyntaxShape>>),
/// A duration value is allowed, eg `19day` /// A custom shape with custom completion logic
Duration, Custom(Box<SyntaxShape>, DeclId),
/// A datetime value, eg `2022-02-02` or `2019-10-12T07:20:50.52+00:00` /// A datetime value, eg `2022-02-02` or `2019-10-12T07:20:50.52+00:00`
DateTime, DateTime,
/// A directory is allowed
Directory,
/// A duration value is allowed, eg `19day`
Duration,
/// An error value
Error,
/// A general expression, eg `1 + 2` or `foo --bar`
Expression,
/// A filepath is allowed
Filepath,
/// A filesize value is allowed, eg `10kb`
Filesize,
/// A dotted path to navigate the table (including variable)
FullCellPath,
/// A glob pattern is allowed, eg `foo*`
GlobPattern,
/// Only an integer value is allowed
Int,
/// A module path pattern used for imports
ImportPattern,
/// A specific match to a word or symbol
Keyword(Vec<u8>, Box<SyntaxShape>),
/// A list is allowed, eg `[first second]`
List(Box<SyntaxShape>),
/// A general math expression, eg `1 + 2`
MathExpression,
/// Nothing
Nothing,
/// Only a numeric (integer or decimal) value is allowed
Number,
/// One of a list of possible items, checked in order
OneOf(Vec<SyntaxShape>),
/// An operator, eg `+` /// An operator, eg `+`
Operator, Operator,
/// A range is allowed (eg, `1..3`)
Range,
/// A record value, eg `{x: 1, y: 2}`
Record,
/// A math expression which expands shorthand forms on the lefthand side, eg `foo > 1` /// A math expression which expands shorthand forms on the lefthand side, eg `foo > 1`
/// The shorthand allows us to more easily reach columns inside of the row being passed in /// The shorthand allows us to more easily reach columns inside of the row being passed in
RowCondition, RowCondition,
/// A general math expression, eg `1 + 2` /// A signature for a definition, `[x:int, --foo]`
MathExpression, Signature,
/// Strings and string-like bare words are allowed
String,
/// A table is allowed, eg `[[first, second]; [1, 2]]`
Table,
/// A variable name, eg `$foo` /// A variable name, eg `$foo`
Variable, Variable,
/// A variable with optional type, `x` or `x: int` /// A variable with optional type, `x` or `x: int`
VarWithOptType, VarWithOptType,
/// A signature for a definition, `[x:int, --foo]`
Signature,
/// A general expression, eg `1 + 2` or `foo --bar`
Expression,
/// A boolean value, eg `true` or `false`
Boolean,
/// A record value, eg `{x: 1, y: 2}`
Record,
/// An error value
Error,
/// A custom shape with custom completion logic
Custom(Box<SyntaxShape>, DeclId),
/// One of a list of possible items, checked in order
OneOf(Vec<SyntaxShape>),
/// Nothing
Nothing,
} }
impl SyntaxShape { impl SyntaxShape {
@ -134,6 +134,7 @@ impl SyntaxShape {
} }
SyntaxShape::Keyword(_, expr) => expr.to_type(), SyntaxShape::Keyword(_, expr) => expr.to_type(),
SyntaxShape::MathExpression => Type::Any, SyntaxShape::MathExpression => Type::Any,
SyntaxShape::Nothing => Type::Any,
SyntaxShape::Number => Type::Number, SyntaxShape::Number => Type::Number,
SyntaxShape::OneOf(_) => Type::Any, SyntaxShape::OneOf(_) => Type::Any,
SyntaxShape::Operator => Type::Any, SyntaxShape::Operator => Type::Any,
@ -146,7 +147,6 @@ impl SyntaxShape {
SyntaxShape::Table => Type::List(Box::new(Type::Any)), // FIXME: What role should columns play in the Table type? SyntaxShape::Table => Type::List(Box::new(Type::Any)), // FIXME: What role should columns play in the Table type?
SyntaxShape::VarWithOptType => Type::Any, SyntaxShape::VarWithOptType => Type::Any,
SyntaxShape::Variable => Type::Any, SyntaxShape::Variable => Type::Any,
SyntaxShape::Nothing => Type::Any,
} }
} }
} }

View File

@ -7,29 +7,29 @@ use crate::SyntaxShape;
#[derive(Clone, Debug, Default, EnumIter, PartialEq, Eq, Serialize, Deserialize, Hash)] #[derive(Clone, Debug, Default, EnumIter, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub enum Type { pub enum Type {
Int, Any,
Float, Binary,
Range,
Bool,
String,
Block, Block,
Closure, Bool,
CellPath, CellPath,
Duration, Closure,
Custom(String),
Date, Date,
Duration,
Error,
Filesize, Filesize,
Float,
Int,
List(Box<Type>), List(Box<Type>),
Number, ListStream,
#[default] #[default]
Nothing, Nothing,
Number,
Range,
Record(Vec<(String, Type)>), Record(Vec<(String, Type)>),
Table(Vec<(String, Type)>),
ListStream,
Any,
Error,
Binary,
Custom(String),
Signature, Signature,
String,
Table(Vec<(String, Type)>),
} }
impl Type { impl Type {