nu_command/debug/
view_source.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use nu_engine::command_prelude::*;
use nu_protocol::{Config, DataSource, PipelineMetadata};

use std::fmt::Write;

#[derive(Clone)]
pub struct ViewSource;

impl Command for ViewSource {
    fn name(&self) -> &str {
        "view source"
    }

    fn description(&self) -> &str {
        "View a block, module, or a definition."
    }

    fn signature(&self) -> nu_protocol::Signature {
        Signature::build("view source")
            .input_output_types(vec![(Type::Nothing, Type::String)])
            .required("item", SyntaxShape::Any, "Name or block to view.")
            .category(Category::Debug)
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let arg: Value = call.req(engine_state, stack, 0)?;
        let arg_span = arg.span();

        let source = match arg {
            Value::Int { val, .. } => {
                if let Some(block) =
                    engine_state.try_get_block(nu_protocol::BlockId::new(val as usize))
                {
                    if let Some(span) = block.span {
                        let contents = engine_state.get_span_contents(span);
                        Ok(Value::string(String::from_utf8_lossy(contents), call.head)
                            .into_pipeline_data())
                    } else {
                        Err(ShellError::GenericError {
                            error: "Cannot view int value".to_string(),
                            msg: "the block does not have a viewable span".to_string(),
                            span: Some(arg_span),
                            help: None,
                            inner: vec![],
                        })
                    }
                } else {
                    Err(ShellError::GenericError {
                        error: format!("Block Id {} does not exist", arg.coerce_into_string()?),
                        msg: "this number does not correspond to a block".to_string(),
                        span: Some(arg_span),
                        help: None,
                        inner: vec![],
                    })
                }
            }

            Value::String { val, .. } => {
                if let Some(decl_id) = engine_state.find_decl(val.as_bytes(), &[]) {
                    // arg is a command
                    let decl = engine_state.get_decl(decl_id);
                    let sig = decl.signature();
                    let vec_of_required = &sig.required_positional;
                    let vec_of_optional = &sig.optional_positional;
                    let rest = &sig.rest_positional;
                    let vec_of_flags = &sig.named;
                    let type_signatures = &sig.input_output_types;

                    if decl.is_alias() {
                        if let Some(alias) = &decl.as_alias() {
                            let contents = String::from_utf8_lossy(
                                engine_state.get_span_contents(alias.wrapped_call.span),
                            );
                            Ok(Value::string(contents, call.head).into_pipeline_data())
                        } else {
                            Ok(Value::string("no alias found", call.head).into_pipeline_data())
                        }
                    }
                    // gets vector of positionals.
                    else if let Some(block_id) = decl.block_id() {
                        let block = engine_state.get_block(block_id);
                        if let Some(block_span) = block.span {
                            let contents = engine_state.get_span_contents(block_span);
                            // name of function
                            let mut final_contents = String::new();
                            if val.contains(' ') {
                                let _ = write!(&mut final_contents, "def \"{val}\" [");
                            } else {
                                let _ = write!(&mut final_contents, "def {val} [");
                            };
                            if !vec_of_required.is_empty()
                                || !vec_of_optional.is_empty()
                                || vec_of_flags.len() != 1
                                || rest.is_some()
                            {
                                final_contents.push(' ');
                            }
                            for n in vec_of_required {
                                let _ = write!(&mut final_contents, "{}: {} ", n.name, n.shape);
                                // positional arguments
                            }
                            for n in vec_of_optional {
                                if let Some(s) = n.default_value.clone() {
                                    let _ = write!(
                                        &mut final_contents,
                                        "{}: {} = {} ",
                                        n.name,
                                        n.shape,
                                        s.to_expanded_string(" ", &Config::default())
                                    );
                                } else {
                                    let _ =
                                        write!(&mut final_contents, "{}?: {} ", n.name, n.shape);
                                }
                            }
                            for n in vec_of_flags {
                                // skip adding the help flag
                                if n.long == "help" {
                                    continue;
                                }
                                let _ = write!(&mut final_contents, "--{}", n.long);
                                if let Some(short) = n.short {
                                    let _ = write!(&mut final_contents, "(-{})", short);
                                }
                                if let Some(arg) = &n.arg {
                                    let _ = write!(&mut final_contents, ": {}", arg);
                                }
                                final_contents.push(' ');
                            }
                            if let Some(rest_arg) = rest {
                                let _ = write!(
                                    &mut final_contents,
                                    "...{}:{}",
                                    rest_arg.name, rest_arg.shape
                                );
                            }
                            let len = type_signatures.len();
                            if len != 0 {
                                final_contents.push_str("]: [");
                                let mut c = 0;
                                for (insig, outsig) in type_signatures {
                                    c += 1;
                                    let s = format!("{} -> {}", insig, outsig);
                                    final_contents.push_str(&s);
                                    if c != len {
                                        final_contents.push_str(", ")
                                    }
                                }
                            }
                            final_contents.push_str("] ");
                            final_contents.push_str(&String::from_utf8_lossy(contents));
                            Ok(Value::string(final_contents, call.head).into_pipeline_data())
                        } else {
                            Err(ShellError::GenericError {
                                error: "Cannot view string value".to_string(),
                                msg: "the command does not have a viewable block span".to_string(),
                                span: Some(arg_span),
                                help: None,
                                inner: vec![],
                            })
                        }
                    } else {
                        Err(ShellError::GenericError {
                            error: "Cannot view string decl value".to_string(),
                            msg: "the command does not have a viewable block".to_string(),
                            span: Some(arg_span),
                            help: None,
                            inner: vec![],
                        })
                    }
                } else if let Some(module_id) = engine_state.find_module(val.as_bytes(), &[]) {
                    // arg is a module
                    let module = engine_state.get_module(module_id);
                    if let Some(module_span) = module.span {
                        let contents = engine_state.get_span_contents(module_span);
                        Ok(Value::string(String::from_utf8_lossy(contents), call.head)
                            .into_pipeline_data())
                    } else {
                        Err(ShellError::GenericError {
                            error: "Cannot view string module value".to_string(),
                            msg: "the module does not have a viewable block".to_string(),
                            span: Some(arg_span),
                            help: None,
                            inner: vec![],
                        })
                    }
                } else {
                    Err(ShellError::GenericError {
                        error: "Cannot view string value".to_string(),
                        msg: "this name does not correspond to a viewable value".to_string(),
                        span: Some(arg_span),
                        help: None,
                        inner: vec![],
                    })
                }
            }
            value => {
                if let Ok(closure) = value.as_closure() {
                    let block = engine_state.get_block(closure.block_id);

                    if let Some(span) = block.span {
                        let contents = engine_state.get_span_contents(span);
                        Ok(Value::string(String::from_utf8_lossy(contents), call.head)
                            .into_pipeline_data())
                    } else {
                        Ok(Value::string("<internal command>", call.head).into_pipeline_data())
                    }
                } else {
                    Err(ShellError::GenericError {
                        error: "Cannot view value".to_string(),
                        msg: "this value cannot be viewed".to_string(),
                        span: Some(arg_span),
                        help: None,
                        inner: vec![],
                    })
                }
            }
        };
        source.map(|x| {
            x.set_metadata(Some(PipelineMetadata {
                data_source: DataSource::None,
                content_type: Some("application/x-nuscript".into()),
            }))
        })
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "View the source of a code block",
                example: r#"let abc = {|| echo 'hi' }; view source $abc"#,
                result: Some(Value::test_string("{|| echo 'hi' }")),
            },
            Example {
                description: "View the source of a custom command",
                example: r#"def hi [] { echo 'Hi!' }; view source hi"#,
                result: Some(Value::test_string("def hi [] { echo 'Hi!' }")),
            },
            Example {
                description: "View the source of a custom command, which participates in the caller environment",
                example: r#"def --env foo [] { $env.BAR = 'BAZ' }; view source foo"#,
                result: Some(Value::test_string("def foo [] { $env.BAR = 'BAZ' }")),
            },
            Example {
                description: "View the source of a custom command with flags and arguments",
                example: r#"def test [a?:any --b:int ...rest:string] { echo 'test' }; view source test"#,
                result: Some(Value::test_string("def test [ a?: any --b: int ...rest: string] { echo 'test' }")),
            },
            Example {
                description: "View the source of a module",
                example: r#"module mod-foo { export-env { $env.FOO_ENV = 'BAZ' } }; view source mod-foo"#,
                result: Some(Value::test_string(" export-env { $env.FOO_ENV = 'BAZ' }")),
            },
            Example {
                description: "View the source of an alias",
                example: r#"alias hello = echo hi; view source hello"#,
                result: Some(Value::test_string("echo hi")),
            },
        ]
    }
}