mirror of
https://github.com/nushell/nushell.git
synced 2025-01-03 21:09:52 +01:00
Eliminate repetitive code and fix Unix failure
This commit is contained in:
parent
5fbea31d15
commit
3008434c0f
@ -0,0 +1,3 @@
|
|||||||
|
[build]
|
||||||
|
|
||||||
|
#rustflags = ["--cfg", "coloring_in_tokens"]
|
2
TODO.md
2
TODO.md
@ -33,8 +33,6 @@ This pattern is extremely repetitive and can be abstracted:
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Do we need Display impls?
|
|
||||||
|
|
||||||
Mandatory and Optional in parse_command
|
Mandatory and Optional in parse_command
|
||||||
|
|
||||||
trace_remaining?
|
trace_remaining?
|
||||||
|
@ -38,7 +38,7 @@ pub(crate) fn dir_entry_dict(
|
|||||||
{
|
{
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
let mode = metadata.permissions().mode();
|
let mode = metadata.permissions().mode();
|
||||||
dict.insert(
|
dict.insert_untagged(
|
||||||
"mode",
|
"mode",
|
||||||
UntaggedValue::string(umask::Mode::from(mode).to_string()),
|
UntaggedValue::string(umask::Mode::from(mode).to_string()),
|
||||||
);
|
);
|
||||||
|
@ -26,7 +26,9 @@ impl ExpandExpression for NumberShape {
|
|||||||
) -> Result<hir::Expression, ParseError> {
|
) -> Result<hir::Expression, ParseError> {
|
||||||
parse_single_node(token_nodes, "Number", |token, token_span, err| {
|
parse_single_node(token_nodes, "Number", |token, token_span, err| {
|
||||||
Ok(match token {
|
Ok(match token {
|
||||||
UnspannedToken::GlobPattern | UnspannedToken::Operator(..) => return Err(err.error()),
|
UnspannedToken::GlobPattern | UnspannedToken::Operator(..) => {
|
||||||
|
return Err(err.error())
|
||||||
|
}
|
||||||
UnspannedToken::Variable(tag) if tag.slice(context.source) == "it" => {
|
UnspannedToken::Variable(tag) if tag.slice(context.source) == "it" => {
|
||||||
hir::Expression::it_variable(tag, token_span)
|
hir::Expression::it_variable(tag, token_span)
|
||||||
}
|
}
|
||||||
|
@ -389,9 +389,47 @@ impl ColorSyntax for CommandTailShape {
|
|||||||
token_nodes: &'b mut TokensIterator<'a>,
|
token_nodes: &'b mut TokensIterator<'a>,
|
||||||
context: &ExpandContext,
|
context: &ExpandContext,
|
||||||
) -> Self::Info {
|
) -> Self::Info {
|
||||||
|
use crate::parser::hir::syntax_shape::SyntaxShape;
|
||||||
|
|
||||||
let mut args = ColoringArgs::new(token_nodes.len());
|
let mut args = ColoringArgs::new(token_nodes.len());
|
||||||
trace_remaining("nodes", &token_nodes, context.source());
|
trace_remaining("nodes", &token_nodes, context.source());
|
||||||
|
|
||||||
|
fn insert_flag(
|
||||||
|
token_nodes: &mut TokensIterator,
|
||||||
|
syntax_type: &SyntaxShape,
|
||||||
|
args: &mut ColoringArgs,
|
||||||
|
flag: Flag,
|
||||||
|
pos: usize,
|
||||||
|
context: &ExpandContext,
|
||||||
|
) {
|
||||||
|
let (_, shapes) = token_nodes.atomic_returning_shapes(|token_nodes| {
|
||||||
|
token_nodes.color_shape(flag.color());
|
||||||
|
token_nodes.move_to(pos);
|
||||||
|
|
||||||
|
if token_nodes.at_end() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// We still want to color the flag even if the following tokens don't match, so don't
|
||||||
|
// propagate the error to the parent atomic block if it fails
|
||||||
|
let _ = token_nodes.atomic(|token_nodes| {
|
||||||
|
// We can live with unmatched syntax after a mandatory flag
|
||||||
|
color_syntax(&MaybeSpaceShape, token_nodes, context);
|
||||||
|
|
||||||
|
// If the part after a mandatory flag isn't present, that's ok, but we
|
||||||
|
// should roll back any whitespace we chomped
|
||||||
|
color_fallible_syntax(syntax_type, token_nodes, context)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
|
args.insert(pos, shapes);
|
||||||
|
token_nodes.restart();
|
||||||
|
}
|
||||||
|
|
||||||
for (name, kind) in &signature.named {
|
for (name, kind) in &signature.named {
|
||||||
trace!(target: "nu::color_syntax", "looking for {} : {:?}", name, kind);
|
trace!(target: "nu::color_syntax", "looking for {} : {:?}", name, kind);
|
||||||
|
|
||||||
@ -414,32 +452,7 @@ impl ColorSyntax for CommandTailShape {
|
|||||||
// The mandatory flag didn't exist at all, so there's nothing to color
|
// The mandatory flag didn't exist at all, so there's nothing to color
|
||||||
}
|
}
|
||||||
Ok((pos, flag)) => {
|
Ok((pos, flag)) => {
|
||||||
let (_, shapes) = token_nodes.atomic_returning_shapes(|token_nodes| {
|
insert_flag(token_nodes, syntax_type, &mut args, flag, pos, context)
|
||||||
token_nodes.color_shape(flag.color());
|
|
||||||
token_nodes.move_to(pos);
|
|
||||||
|
|
||||||
if token_nodes.at_end() {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
// We still want to color the flag even if the following tokens don't match, so don't
|
|
||||||
// propagate the error to the parent atomic block if it fails
|
|
||||||
let _ = token_nodes.atomic(|token_nodes| {
|
|
||||||
// We can live with unmatched syntax after a mandatory flag
|
|
||||||
color_syntax(&MaybeSpaceShape, token_nodes, context);
|
|
||||||
|
|
||||||
// If the part after a mandatory flag isn't present, that's ok, but we
|
|
||||||
// should roll back any whitespace we chomped
|
|
||||||
color_fallible_syntax(syntax_type, token_nodes, context)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
|
|
||||||
args.insert(pos, shapes);
|
|
||||||
token_nodes.restart();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -449,32 +462,7 @@ impl ColorSyntax for CommandTailShape {
|
|||||||
// The optional flag didn't exist at all, so there's nothing to color
|
// The optional flag didn't exist at all, so there's nothing to color
|
||||||
}
|
}
|
||||||
Ok(Some((pos, flag))) => {
|
Ok(Some((pos, flag))) => {
|
||||||
let (_, shapes) = token_nodes.atomic_returning_shapes(|token_nodes| {
|
insert_flag(token_nodes, syntax_type, &mut args, flag, pos, context)
|
||||||
token_nodes.color_shape(flag.color());
|
|
||||||
token_nodes.move_to(pos);
|
|
||||||
|
|
||||||
if token_nodes.at_end() {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
// We still want to color the flag even if the following tokens don't match, so don't
|
|
||||||
// propagate the error to the parent atomic block if it fails
|
|
||||||
let _ = token_nodes.atomic(|token_nodes| {
|
|
||||||
// We can live with unmatched syntax after a mandatory flag
|
|
||||||
color_syntax(&MaybeSpaceShape, token_nodes, context);
|
|
||||||
|
|
||||||
// If the part after a mandatory flag isn't present, that's ok, but we
|
|
||||||
// should roll back any whitespace we chomped
|
|
||||||
color_fallible_syntax(syntax_type, token_nodes, context)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
|
|
||||||
args.insert(pos, shapes);
|
|
||||||
token_nodes.restart();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user