refactor: less clone

This commit is contained in:
blindfs 2025-04-09 13:58:44 +08:00
parent 8b6d6708e0
commit 3f9e6ab985
8 changed files with 27 additions and 30 deletions

View File

@ -355,7 +355,7 @@ fn try_find_id_in_overlay(
None
}
/// For variable references `$foo`
/// Trim leading `$` sign For variable references `$foo`
fn strip_dollar_sign(span: Span, working_set: &StateWorkingSet<'_>) -> (Box<[u8]>, Span) {
let content = working_set.get_span_contents(span);
if content.starts_with(b"$") {
@ -379,7 +379,6 @@ fn find_id_in_expr(
}
let span = expr.span;
match &expr.expr {
// trim leading `$` sign
Expr::VarDecl(var_id) | Expr::Var(var_id) => {
let (name, clean_span) = strip_dollar_sign(span, working_set);
FindMapResult::Found((Id::Variable(*var_id, name), clean_span))

View File

@ -588,7 +588,7 @@ mod tests {
])
);
let resp = send_complete_request(&client_connection, script.clone(), 7, 15);
let resp = send_complete_request(&client_connection, script, 7, 15);
assert_json_include!(
actual: result_from_message(resp),
expected: serde_json::json!([

View File

@ -213,7 +213,7 @@ mod tests {
let script = path_to_uri(&script);
open_unchecked(&client_connection, script.clone());
let resp = send_inlay_hint_request(&client_connection, script.clone());
let resp = send_inlay_hint_request(&client_connection, script);
assert_json_eq!(
result_from_message(resp),
@ -240,7 +240,7 @@ mod tests {
let script = path_to_uri(&script);
open_unchecked(&client_connection, script.clone());
let resp = send_inlay_hint_request(&client_connection, script.clone());
let resp = send_inlay_hint_request(&client_connection, script);
assert_json_eq!(
result_from_message(resp),
@ -268,7 +268,7 @@ mod tests {
let script = path_to_uri(&script);
open_unchecked(&client_connection, script.clone());
let resp = send_inlay_hint_request(&client_connection, script.clone());
let resp = send_inlay_hint_request(&client_connection, script);
assert_json_eq!(
result_from_message(resp),
@ -333,7 +333,7 @@ mod tests {
let (client_connection, _recv) = initialize_language_server(Some(&config), None);
open_unchecked(&client_connection, script.clone());
let resp = send_inlay_hint_request(&client_connection, script.clone());
let resp = send_inlay_hint_request(&client_connection, script);
assert_json_eq!(
result_from_message(resp),

View File

@ -352,7 +352,7 @@ impl LanguageServer {
if self.need_parse {
// TODO: incremental parsing
// add block to working_set for later references
working_set.add_block(block.clone());
working_set.add_block(block);
self.cached_state_delta = Some(working_set.delta.clone());
self.need_parse = false;
}
@ -615,7 +615,7 @@ mod tests {
method: DidChangeTextDocument::METHOD.to_string(),
params: serde_json::to_value(DidChangeTextDocumentParams {
text_document: lsp_types::VersionedTextDocumentIdentifier {
uri: uri.clone(),
uri,
version: 2,
},
content_changes: vec![TextDocumentContentChangeEvent {

View File

@ -19,20 +19,19 @@ impl LanguageServer {
docs.listen(notification.method.as_str(), &notification.params);
match notification.method.as_str() {
DidOpenTextDocument::METHOD => {
let params: DidOpenTextDocumentParams =
serde_json::from_value(notification.params.clone())
let params: DidOpenTextDocumentParams = serde_json::from_value(notification.params)
.expect("Expect receive DidOpenTextDocumentParams");
Some(params.text_document.uri)
}
DidChangeTextDocument::METHOD => {
let params: DidChangeTextDocumentParams =
serde_json::from_value(notification.params.clone())
serde_json::from_value(notification.params)
.expect("Expect receive DidChangeTextDocumentParams");
Some(params.text_document.uri)
}
DidCloseTextDocument::METHOD => {
let params: DidCloseTextDocumentParams =
serde_json::from_value(notification.params.clone())
serde_json::from_value(notification.params)
.expect("Expect receive DidCloseTextDocumentParams");
let uri = params.text_document.uri;
self.symbol_cache.drop(&uri);
@ -41,7 +40,7 @@ impl LanguageServer {
}
DidChangeWorkspaceFolders::METHOD => {
let params: DidChangeWorkspaceFoldersParams =
serde_json::from_value(notification.params.clone())
serde_json::from_value(notification.params)
.expect("Expect receive DidChangeWorkspaceFoldersParams");
for added in params.event.added {
self.workspace_folders.insert(added.name.clone(), added);
@ -155,7 +154,7 @@ mod tests {
let script = path_to_uri(&script);
open_unchecked(&client_connection, script.clone());
let resp = send_hover_request(&client_connection, script.clone(), 0, 0);
let resp = send_hover_request(&client_connection, script, 0, 0);
assert_json_eq!(
result_from_message(resp),
@ -190,7 +189,7 @@ hello"#,
),
None,
);
let resp = send_hover_request(&client_connection, script.clone(), 3, 0);
let resp = send_hover_request(&client_connection, script, 3, 0);
assert_json_eq!(
result_from_message(resp),
@ -229,7 +228,7 @@ hello"#,
},
}),
);
let resp = send_hover_request(&client_connection, script.clone(), 3, 0);
let resp = send_hover_request(&client_connection, script, 3, 0);
assert_json_eq!(
result_from_message(resp),

View File

@ -144,7 +144,7 @@ mod tests {
let script = path_to_uri(&script);
open_unchecked(&client_connection, script.clone());
let resp = send_semantic_token_request(&client_connection, script.clone());
let resp = send_semantic_token_request(&client_connection, script);
assert_json_eq!(
result_from_message(resp),

View File

@ -236,7 +236,7 @@ impl SymbolCache {
self.cache
.get(uri)?
.iter()
.map(|s| s.clone().to_symbol_information(uri))
.map(|s| s.to_symbol_information(uri))
.collect(),
)
}
@ -250,7 +250,7 @@ impl SymbolCache {
);
self.cache
.iter()
.flat_map(|(uri, symbols)| symbols.iter().map(|s| s.clone().to_symbol_information(uri)))
.flat_map(|(uri, symbols)| symbols.iter().map(|s| s.to_symbol_information(uri)))
.filter_map(|s| {
let mut buf = Vec::new();
let name = Utf32Str::new(&s.name, &mut buf);
@ -470,7 +470,7 @@ mod tests {
script.push("bar.nu");
let script_bar = path_to_uri(&script);
open_unchecked(&client_connection, script_foo.clone());
open_unchecked(&client_connection, script_foo);
open_unchecked(&client_connection, script_bar.clone());
let resp = send_workspace_symbol_request(&client_connection, "br".to_string());
@ -531,7 +531,7 @@ mod tests {
let script_bar = path_to_uri(&script);
open_unchecked(&client_connection, script_foo.clone());
open_unchecked(&client_connection, script_bar.clone());
open_unchecked(&client_connection, script_bar);
let resp = send_workspace_symbol_request(&client_connection, "foo".to_string());
assert_json_eq!(

View File

@ -138,7 +138,6 @@ impl LanguageServer {
/// The rename request only happens after the client received a `PrepareRenameResponse`,
/// and a new name typed in, could happen before ranges ready for all files in the workspace folder
pub(crate) fn rename(&mut self, params: &RenameParams) -> Option<WorkspaceEdit> {
let new_name = params.new_name.to_owned();
// changes in WorkspaceEdit have mutable key
#[allow(clippy::mutable_key_type)]
let changes: HashMap<Uri, Vec<TextEdit>> = self
@ -151,7 +150,7 @@ impl LanguageServer {
.iter()
.map(|range| TextEdit {
range: *range,
new_text: new_name.clone(),
new_text: params.new_name.to_owned(),
})
.collect(),
)
@ -462,7 +461,7 @@ impl LanguageServer {
});
}
data_sender
.send(InternalMessage::Finished(token.clone()))
.send(InternalMessage::Finished(token))
.into_diagnostic()?;
Ok(())
});
@ -555,7 +554,7 @@ mod tests {
// use a hover request to interrupt
if immediate_cancellation {
send_hover_request(client_connection, uri.clone(), line, character);
send_hover_request(client_connection, uri, line, character);
}
(0..num)
@ -684,7 +683,7 @@ mod tests {
));
assert!(array.contains(&serde_json::json!(
{
"uri": script.to_string(),
"uri": script,
"range": { "start": { "line": 0, "character": 11 }, "end": { "line": 0, "character": 16 } }
}
)
@ -745,7 +744,7 @@ mod tests {
));
assert!(array.contains(&serde_json::json!(
{
"uri": script.to_string().replace("bar", "foo"),
"uri": script,
"range": { "start": { "line": 6, "character": 13 }, "end": { "line": 6, "character": 20 } }
}
)
@ -799,7 +798,7 @@ mod tests {
));
assert!(array.contains(&serde_json::json!(
{
"uri": script.to_string(),
"uri": script,
"range": { "start": { "line": 6, "character": 4 }, "end": { "line": 6, "character": 12 } }
}
)