mirror of
https://github.com/nushell/nushell
synced 2024-12-27 05:23:11 +00:00
Enable syntax/completions for source (#3589)
This commit is contained in:
parent
383e874166
commit
e8dfd4ba39
3 changed files with 43 additions and 33 deletions
|
@ -21,7 +21,7 @@ impl WholeStreamCommand for Source {
|
|||
fn signature(&self) -> Signature {
|
||||
Signature::build("source").required(
|
||||
"filename",
|
||||
SyntaxShape::String,
|
||||
SyntaxShape::FilePath,
|
||||
"the filepath to the script file to source",
|
||||
)
|
||||
}
|
||||
|
|
|
@ -155,6 +155,8 @@ pub enum ArgumentError {
|
|||
/// A sequence of characters was found that was not syntactically valid (but would have
|
||||
/// been valid if the command was an external command)
|
||||
InvalidExternalWord,
|
||||
/// A bad value in this location
|
||||
BadValue(String),
|
||||
}
|
||||
|
||||
impl PrettyDebug for ArgumentError {
|
||||
|
@ -186,6 +188,11 @@ impl PrettyDebug for ArgumentError {
|
|||
+ DbgDocBldr::description("`")
|
||||
}
|
||||
ArgumentError::InvalidExternalWord => DbgDocBldr::description("invalid word"),
|
||||
ArgumentError::BadValue(msg) => {
|
||||
DbgDocBldr::description("bad value `")
|
||||
+ DbgDocBldr::description(msg)
|
||||
+ DbgDocBldr::description("`")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -522,6 +529,7 @@ impl ShellError {
|
|||
),
|
||||
)
|
||||
.with_labels(vec![Label::primary(0, command.span)]),
|
||||
ArgumentError::BadValue(msg) => Diagnostic::error().with_message(msg.clone()).with_labels(vec![Label::primary(0, command.span).with_message(msg)])
|
||||
}),
|
||||
ProximateShellError::TypeError {
|
||||
expected,
|
||||
|
|
|
@ -1836,38 +1836,6 @@ fn parse_call(
|
|||
error,
|
||||
);
|
||||
}
|
||||
} else if lite_cmd.parts[0].item == "source" {
|
||||
if lite_cmd.parts.len() != 2 {
|
||||
return (
|
||||
None,
|
||||
Some(ParseError::argument_error(
|
||||
lite_cmd.parts[0].clone(),
|
||||
ArgumentError::MissingMandatoryPositional("a path for sourcing".into()),
|
||||
)),
|
||||
);
|
||||
}
|
||||
if lite_cmd.parts[1].item.starts_with('$') {
|
||||
return (
|
||||
None,
|
||||
Some(ParseError::mismatch(
|
||||
"a filepath constant",
|
||||
lite_cmd.parts[1].clone(),
|
||||
)),
|
||||
);
|
||||
}
|
||||
if let Ok(contents) =
|
||||
std::fs::read_to_string(expand_path(&lite_cmd.parts[1].item).into_owned())
|
||||
{
|
||||
let _ = parse(&contents, 0, scope);
|
||||
} else {
|
||||
return (
|
||||
None,
|
||||
Some(ParseError::mismatch(
|
||||
"a filepath to a source file",
|
||||
lite_cmd.parts[1].clone(),
|
||||
)),
|
||||
);
|
||||
}
|
||||
} else if lite_cmd.parts.len() > 1 {
|
||||
// Check if it's a sub-command
|
||||
if let Some(signature) = scope.get_signature(&format!(
|
||||
|
@ -1899,6 +1867,40 @@ fn parse_call(
|
|||
}
|
||||
let (mut internal_command, err) = parse_internal_command(&lite_cmd, scope, &signature, 0);
|
||||
|
||||
if internal_command.name == "source" {
|
||||
if lite_cmd.parts.len() != 2 {
|
||||
return (
|
||||
Some(ClassifiedCommand::Internal(internal_command)),
|
||||
Some(ParseError::argument_error(
|
||||
lite_cmd.parts[0].clone(),
|
||||
ArgumentError::MissingMandatoryPositional("a path for sourcing".into()),
|
||||
)),
|
||||
);
|
||||
}
|
||||
if lite_cmd.parts[1].item.starts_with('$') {
|
||||
return (
|
||||
Some(ClassifiedCommand::Internal(internal_command)),
|
||||
Some(ParseError::mismatch(
|
||||
"a filepath constant",
|
||||
lite_cmd.parts[1].clone(),
|
||||
)),
|
||||
);
|
||||
}
|
||||
if let Ok(contents) =
|
||||
std::fs::read_to_string(expand_path(&lite_cmd.parts[1].item).into_owned())
|
||||
{
|
||||
let _ = parse(&contents, 0, scope);
|
||||
} else {
|
||||
return (
|
||||
Some(ClassifiedCommand::Internal(internal_command)),
|
||||
Some(ParseError::argument_error(
|
||||
lite_cmd.parts[1].clone(),
|
||||
ArgumentError::BadValue("can't load source file".into()),
|
||||
)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
error = error.or(err);
|
||||
internal_command.args.external_redirection = if end_of_pipeline {
|
||||
ExternalRedirection::None
|
||||
|
|
Loading…
Reference in a new issue