Add `command_prelude` module (#12291)
# 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.
2024-03-26 22:17:30 +01:00
|
|
|
use crate::LanguageServer;
|
2023-11-16 00:35:48 +01:00
|
|
|
use lsp_types::{
|
|
|
|
notification::{Notification, PublishDiagnostics},
|
|
|
|
Diagnostic, DiagnosticSeverity, PublishDiagnosticsParams, Url,
|
|
|
|
};
|
|
|
|
use miette::{IntoDiagnostic, Result};
|
|
|
|
use nu_parser::parse;
|
|
|
|
use nu_protocol::{
|
|
|
|
engine::{EngineState, StateWorkingSet},
|
2024-05-10 01:29:27 +02:00
|
|
|
Value,
|
2023-11-16 00:35:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
impl LanguageServer {
|
|
|
|
pub(crate) fn publish_diagnostics_for_file(
|
|
|
|
&self,
|
|
|
|
uri: Url,
|
|
|
|
engine_state: &mut EngineState,
|
|
|
|
) -> Result<()> {
|
|
|
|
let cwd = std::env::current_dir().expect("Could not get current working directory.");
|
|
|
|
engine_state.add_env_var("PWD".into(), Value::test_string(cwd.to_string_lossy()));
|
2024-05-10 01:29:27 +02:00
|
|
|
engine_state.generate_nu_constant();
|
2023-11-16 00:35:48 +01:00
|
|
|
|
|
|
|
let mut working_set = StateWorkingSet::new(engine_state);
|
|
|
|
|
|
|
|
let Some((rope_of_file, file_path)) = self.rope(&uri) else {
|
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
|
|
|
|
let contents = rope_of_file.bytes().collect::<Vec<u8>>();
|
|
|
|
let offset = working_set.next_span_start();
|
|
|
|
parse(
|
|
|
|
&mut working_set,
|
|
|
|
Some(&file_path.to_string_lossy()),
|
|
|
|
&contents,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut diagnostics = PublishDiagnosticsParams {
|
|
|
|
uri,
|
|
|
|
diagnostics: Vec::new(),
|
|
|
|
version: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
for err in working_set.parse_errors.iter() {
|
|
|
|
let message = err.to_string();
|
|
|
|
|
|
|
|
diagnostics.diagnostics.push(Diagnostic {
|
|
|
|
range: Self::span_to_range(&err.span(), rope_of_file, offset),
|
|
|
|
severity: Some(DiagnosticSeverity::ERROR),
|
|
|
|
message,
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
self.connection
|
|
|
|
.sender
|
|
|
|
.send(lsp_server::Message::Notification(
|
|
|
|
lsp_server::Notification::new(PublishDiagnostics::METHOD.to_string(), diagnostics),
|
|
|
|
))
|
|
|
|
.into_diagnostic()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use assert_json_diff::assert_json_eq;
|
|
|
|
use lsp_types::Url;
|
|
|
|
use nu_test_support::fs::fixtures;
|
|
|
|
|
2024-01-11 22:24:49 +01:00
|
|
|
use crate::tests::{initialize_language_server, open_unchecked, update};
|
2023-11-16 00:35:48 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn publish_diagnostics_variable_does_not_exists() {
|
|
|
|
let (client_connection, _recv) = initialize_language_server();
|
|
|
|
|
|
|
|
let mut script = fixtures();
|
|
|
|
script.push("lsp");
|
|
|
|
script.push("diagnostics");
|
|
|
|
script.push("var.nu");
|
|
|
|
let script = Url::from_file_path(script).unwrap();
|
|
|
|
|
2024-01-11 22:24:49 +01:00
|
|
|
let notification = open_unchecked(&client_connection, script.clone());
|
2023-11-16 00:35:48 +01:00
|
|
|
|
|
|
|
assert_json_eq!(
|
|
|
|
notification,
|
|
|
|
serde_json::json!({
|
|
|
|
"method": "textDocument/publishDiagnostics",
|
|
|
|
"params": {
|
|
|
|
"uri": script,
|
|
|
|
"diagnostics": [{
|
|
|
|
"range": {
|
|
|
|
"start": { "line": 0, "character": 6 },
|
|
|
|
"end": { "line": 0, "character": 30 }
|
|
|
|
},
|
|
|
|
"message": "Variable not found.",
|
|
|
|
"severity": 1
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn publish_diagnostics_fixed_unknown_variable() {
|
|
|
|
let (client_connection, _recv) = initialize_language_server();
|
|
|
|
|
|
|
|
let mut script = fixtures();
|
|
|
|
script.push("lsp");
|
|
|
|
script.push("diagnostics");
|
|
|
|
script.push("var.nu");
|
|
|
|
let script = Url::from_file_path(script).unwrap();
|
|
|
|
|
2024-01-11 22:24:49 +01:00
|
|
|
open_unchecked(&client_connection, script.clone());
|
2023-11-16 00:35:48 +01:00
|
|
|
let notification = update(
|
|
|
|
&client_connection,
|
|
|
|
script.clone(),
|
|
|
|
String::from("$env"),
|
|
|
|
Some(lsp_types::Range {
|
|
|
|
start: lsp_types::Position {
|
|
|
|
line: 0,
|
|
|
|
character: 6,
|
|
|
|
},
|
|
|
|
end: lsp_types::Position {
|
|
|
|
line: 0,
|
|
|
|
character: 30,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_json_eq!(
|
|
|
|
notification,
|
|
|
|
serde_json::json!({
|
|
|
|
"method": "textDocument/publishDiagnostics",
|
|
|
|
"params": {
|
|
|
|
"uri": script,
|
|
|
|
"diagnostics": []
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|