Skip to content

Commit

Permalink
Add command_prelude module (#12291)
Browse files Browse the repository at this point in the history
# Description
When implementing a `Command`, one must also import all the types
present in the function signatures for `Command`. This makes it so that
we often import the same set of types in each command implementation
file. E.g., something like this:
```rust
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
    record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
    ShellError, Signature, Span, Type, Value,
};
```

This PR adds the `nu_engine::command_prelude` module which contains the
necessary and commonly used types to implement a `Command`:
```rust
// command_prelude.rs
pub use crate::CallExt;
pub use nu_protocol::{
    ast::{Call, CellPath},
    engine::{Command, EngineState, Stack},
    record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, IntoSpanned,
    PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
```

This should reduce the boilerplate needed to implement a command and
also gives us a place to track the breadth of the `Command` API. I tried
to be conservative with what went into the prelude modules, since it
might be hard/annoying to remove items from the prelude in the future.
Let me know if something should be included or excluded.
  • Loading branch information
IanManske committed Mar 26, 2024
1 parent f8c1e3a commit c747ec7
Show file tree
Hide file tree
Showing 660 changed files with 1,635 additions and 4,333 deletions.
7 changes: 1 addition & 6 deletions crates/nu-cli/src/commands/commandline/commandline_.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
use unicode_segmentation::UnicodeSegmentation;

#[derive(Clone)]
Expand Down
7 changes: 1 addition & 6 deletions crates/nu-cli/src/commands/commandline/edit.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;

#[derive(Clone)]
pub struct SubCommand;
Expand Down
6 changes: 1 addition & 5 deletions crates/nu-cli/src/commands/commandline/get_cursor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use nu_engine::command_prelude::*;
use unicode_segmentation::UnicodeSegmentation;

#[derive(Clone)]
Expand Down
8 changes: 2 additions & 6 deletions crates/nu-cli/src/commands/commandline/set_cursor.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;

use unicode_segmentation::UnicodeSegmentation;

#[derive(Clone)]
Expand Down
3 changes: 1 addition & 2 deletions crates/nu-cli/src/commands/default_context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use nu_protocol::engine::{EngineState, StateWorkingSet};

use crate::commands::*;
use nu_protocol::engine::{EngineState, StateWorkingSet};

pub fn add_cli_context(mut engine_state: EngineState) -> EngineState {
let delta = {
Expand Down
9 changes: 2 additions & 7 deletions crates/nu-cli/src/commands/history/history_.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, HistoryFileFormat, IntoInterruptiblePipelineData, PipelineData,
ShellError, Signature, Span, Type, Value,
};
use nu_engine::command_prelude::*;
use nu_protocol::HistoryFileFormat;
use reedline::{
FileBackedHistory, History as ReedlineHistory, HistoryItem, SearchDirection, SearchQuery,
SqliteBackedHistory,
Expand Down
6 changes: 1 addition & 5 deletions crates/nu-cli/src/commands/history/history_session.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use nu_engine::command_prelude::*;

#[derive(Clone)]
pub struct HistorySession;
Expand Down
7 changes: 1 addition & 6 deletions crates/nu-cli/src/commands/keybindings.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use nu_engine::get_full_help;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use nu_engine::{command_prelude::*, get_full_help};

#[derive(Clone)]
pub struct Keybindings;
Expand Down
6 changes: 1 addition & 5 deletions crates/nu-cli/src/commands/keybindings_default.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use nu_engine::command_prelude::*;
use reedline::get_reedline_default_keybindings;

#[derive(Clone)]
Expand Down
7 changes: 1 addition & 6 deletions crates/nu-cli/src/commands/keybindings_list.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type,
Value,
};
use nu_engine::command_prelude::*;
use reedline::{
get_reedline_edit_commands, get_reedline_keybinding_modifiers, get_reedline_keycodes,
get_reedline_prompt_edit_modes, get_reedline_reedline_events,
Expand Down
11 changes: 3 additions & 8 deletions crates/nu-cli/src/commands/keybindings_listen.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use crossterm::execute;
use crossterm::QueueableCommand;
use crossterm::{event::Event, event::KeyCode, event::KeyEvent, terminal};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type,
Value,
use crossterm::{
event::Event, event::KeyCode, event::KeyEvent, execute, terminal, QueueableCommand,
};
use nu_engine::command_prelude::*;
use std::io::{stdout, Write};

#[derive(Clone)]
Expand Down
5 changes: 2 additions & 3 deletions crates/nu-cli/src/completions/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ use crate::completions::{
use nu_color_config::{color_record_to_nustyle, lookup_ansi_color_style};
use nu_engine::eval_block;
use nu_parser::{flatten_pipeline_element, parse, FlatShape};
use nu_protocol::debugger::WithoutDebug;
use nu_protocol::{
debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet},
BlockId, PipelineData, Span, Value,
};
use reedline::{Completer as ReedlineCompleter, Suggestion};
use std::str;
use std::sync::Arc;
use std::{str, sync::Arc};

use super::base::{SemanticSuggestion, SuggestionKind};

Expand Down
12 changes: 8 additions & 4 deletions crates/nu-cli/src/completions/completion_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ use crate::completions::{matches, CompletionOptions};
use nu_ansi_term::Style;
use nu_engine::env_to_string;
use nu_path::home_dir;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{engine::StateWorkingSet, Span};
use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet},
Span,
};
use nu_utils::get_ls_colors;
use std::ffi::OsStr;
use std::path::{is_separator, Component, Path, PathBuf, MAIN_SEPARATOR as SEP};
use std::{
ffi::OsStr,
path::{is_separator, Component, Path, PathBuf, MAIN_SEPARATOR as SEP},
};

fn complete_rec(
partial: &[String],
Expand Down
3 changes: 1 addition & 2 deletions crates/nu-cli/src/completions/completion_options.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::fmt::Display;

use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
use nu_parser::trim_quotes_str;
use nu_protocol::CompletionAlgorithm;
use std::fmt::Display;

#[derive(Copy, Clone)]
pub enum SortBy {
Expand Down
13 changes: 6 additions & 7 deletions crates/nu-cli/src/completions/custom_completions.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use crate::completions::{Completer, CompletionOptions, MatchAlgorithm, SortBy};
use crate::completions::{
completer::map_value_completions, Completer, CompletionOptions, MatchAlgorithm,
SemanticSuggestion, SortBy,
};
use nu_engine::eval_call;
use nu_protocol::debugger::WithoutDebug;
use nu_protocol::{
ast::{Argument, Call, Expr, Expression},
debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet},
PipelineData, Span, Type, Value,
};
use nu_utils::IgnoreCaseExt;
use std::collections::HashMap;
use std::sync::Arc;

use super::base::SemanticSuggestion;
use super::completer::map_value_completions;
use std::{collections::HashMap, sync::Arc};

pub struct CustomCompletion {
engine_state: Arc<EngineState>,
Expand Down
6 changes: 4 additions & 2 deletions crates/nu-cli/src/completions/directory_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use nu_protocol::{
levenshtein_distance, Span,
};
use reedline::Suggestion;
use std::path::{Path, MAIN_SEPARATOR as SEP};
use std::sync::Arc;
use std::{
path::{Path, MAIN_SEPARATOR as SEP},
sync::Arc,
};

use super::SemanticSuggestion;

Expand Down
6 changes: 4 additions & 2 deletions crates/nu-cli/src/completions/file_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ use nu_protocol::{
};
use nu_utils::IgnoreCaseExt;
use reedline::Suggestion;
use std::path::{Path, MAIN_SEPARATOR as SEP};
use std::sync::Arc;
use std::{
path::{Path, MAIN_SEPARATOR as SEP},
sync::Arc,
};

use super::SemanticSuggestion;

Expand Down
1 change: 0 additions & 1 deletion crates/nu-cli/src/completions/flag_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use nu_protocol::{
engine::StateWorkingSet,
Span,
};

use reedline::Suggestion;

use super::SemanticSuggestion;
Expand Down
10 changes: 4 additions & 6 deletions crates/nu-cli/src/completions/variable_completions.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use crate::completions::{Completer, CompletionOptions};
use crate::completions::{
Completer, CompletionOptions, MatchAlgorithm, SemanticSuggestion, SuggestionKind,
};
use nu_engine::{column::get_columns, eval_variable};
use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet},
Span, Value,
};

use reedline::Suggestion;
use std::str;
use std::sync::Arc;

use super::{MatchAlgorithm, SemanticSuggestion, SuggestionKind};
use std::{str, sync::Arc};

#[derive(Clone)]
pub struct VariableCompletion {
Expand Down
7 changes: 4 additions & 3 deletions crates/nu-cli/src/config_files.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::util::eval_source;
#[cfg(feature = "plugin")]
use nu_path::canonicalize_with;
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::report_error;
use nu_protocol::{HistoryFileFormat, PipelineData};
use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet},
report_error, HistoryFileFormat, PipelineData,
};
#[cfg(feature = "plugin")]
use nu_protocol::{ParseError, Spanned};
#[cfg(feature = "plugin")]
Expand Down
8 changes: 3 additions & 5 deletions crates/nu-cli/src/eval_cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ use log::info;
use miette::Result;
use nu_engine::{convert_env_values, eval_block};
use nu_parser::parse;
use nu_protocol::debugger::WithoutDebug;
use nu_protocol::engine::Stack;
use nu_protocol::report_error;
use nu_protocol::{
engine::{EngineState, StateWorkingSet},
PipelineData, Spanned, Value,
debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet},
report_error, PipelineData, Spanned, Value,
};

/// Run a command (or commands) given to us by the user
Expand Down
14 changes: 5 additions & 9 deletions crates/nu-cli/src/eval_file.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
use std::sync::Arc;

use crate::util::eval_source;
use log::info;
use log::trace;
use log::{info, trace};
use miette::{IntoDiagnostic, Result};
use nu_engine::eval_block;
use nu_engine::{convert_env_values, current_dir};
use nu_engine::{convert_env_values, current_dir, eval_block};
use nu_parser::parse;
use nu_path::canonicalize_with;
use nu_protocol::debugger::WithoutDebug;
use nu_protocol::report_error;
use nu_protocol::{
ast::Call,
debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet},
Config, PipelineData, ShellError, Span, Value,
report_error, Config, PipelineData, ShellError, Span, Value,
};
use nu_utils::stdout_write_all_and_flush;
use std::sync::Arc;

/// Main function used when a file path is found as argument for nu
pub fn evaluate_file(
Expand Down
3 changes: 1 addition & 2 deletions crates/nu-cli/src/menus/help_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use nu_engine::documentation::get_flags_section;
use nu_protocol::{engine::EngineState, levenshtein_distance};
use nu_utils::IgnoreCaseExt;
use reedline::{Completer, Suggestion};
use std::fmt::Write;
use std::sync::Arc;
use std::{fmt::Write, sync::Arc};

pub struct NuHelpCompleter(Arc<EngineState>);

Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cli/src/menus/menu_completions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use nu_engine::eval_block;
use nu_protocol::debugger::WithoutDebug;
use nu_protocol::{
debugger::WithoutDebug,
engine::{EngineState, Stack},
IntoPipelineData, Span, Value,
};
Expand Down
4 changes: 1 addition & 3 deletions crates/nu-cli/src/nu_highlight.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Type, Value};
use nu_engine::command_prelude::*;
use reedline::{Highlighter, StyledText};

#[derive(Clone)]
Expand Down
8 changes: 1 addition & 7 deletions crates/nu-cli/src/print.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type,
Value,
};
use nu_engine::command_prelude::*;

#[derive(Clone)]
pub struct Print;
Expand Down
10 changes: 4 additions & 6 deletions crates/nu-cli/src/prompt.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::prompt_update::{POST_PROMPT_MARKER, PRE_PROMPT_MARKER};
#[cfg(windows)]
use nu_utils::enable_vt_processing;
use reedline::DefaultPrompt;
use {
reedline::{
Prompt, PromptEditMode, PromptHistorySearch, PromptHistorySearchStatus, PromptViMode,
},
std::borrow::Cow,
use reedline::{
DefaultPrompt, Prompt, PromptEditMode, PromptHistorySearch, PromptHistorySearchStatus,
PromptViMode,
};
use std::borrow::Cow;

/// Nushell prompt definition
#[derive(Clone)]
Expand Down
4 changes: 1 addition & 3 deletions crates/nu-cli/src/prompt_update.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::NushellPrompt;
use log::trace;
use nu_engine::get_eval_subexpression;

use nu_protocol::report_error;
use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet},
Config, PipelineData, Value,
report_error, Config, PipelineData, Value,
};
use reedline::Prompt;

Expand Down
5 changes: 2 additions & 3 deletions crates/nu-cli/src/reedline_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crossterm::event::{KeyCode, KeyModifiers};
use nu_color_config::{color_record_to_nustyle, lookup_ansi_color_style};
use nu_engine::eval_block;
use nu_parser::parse;
use nu_protocol::debugger::WithoutDebug;
use nu_protocol::{
create_menus,
debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet},
extract_value, Config, EditBindings, ParsedKeybinding, ParsedMenu, PipelineData, Record,
ShellError, Span, Value,
Expand Down Expand Up @@ -1311,9 +1311,8 @@ fn extract_char(value: &Value, config: &Config) -> Result<char, ShellError> {

#[cfg(test)]
mod test {
use nu_protocol::record;

use super::*;
use nu_protocol::record;

#[test]
fn test_send_event() {
Expand Down

0 comments on commit c747ec7

Please sign in to comment.