1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

refactor(lsp): fewer clones (#17551)

This commit is contained in:
Geert-Jan Zwiers 2023-01-26 23:24:03 +01:00 committed by GitHub
parent 21065797f6
commit 7f38f30a5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 22 additions and 31 deletions

View file

@ -154,7 +154,7 @@ fn get_validated_scheme(
/// the value of a content type header. /// the value of a content type header.
pub fn map_content_type( pub fn map_content_type(
specifier: &ModuleSpecifier, specifier: &ModuleSpecifier,
maybe_content_type: Option<String>, maybe_content_type: Option<&String>,
) -> (MediaType, Option<String>) { ) -> (MediaType, Option<String>) {
if let Some(content_type) = maybe_content_type { if let Some(content_type) = maybe_content_type {
let mut content_types = content_type.split(';'); let mut content_types = content_type.split(';');
@ -226,7 +226,7 @@ impl FileFetcher {
.ok_or_else(|| { .ok_or_else(|| {
generic_error("Cannot convert specifier to cached filename.") generic_error("Cannot convert specifier to cached filename.")
})?; })?;
let maybe_content_type = headers.get("content-type").cloned(); let maybe_content_type = headers.get("content-type");
let (media_type, maybe_charset) = let (media_type, maybe_charset) =
map_content_type(specifier, maybe_content_type); map_content_type(specifier, maybe_content_type);
let source = get_source_from_bytes(bytes, maybe_charset)?; let source = get_source_from_bytes(bytes, maybe_charset)?;
@ -308,8 +308,7 @@ impl FileFetcher {
} }
let (source, content_type) = get_source_from_data_url(specifier)?; let (source, content_type) = get_source_from_data_url(specifier)?;
let (media_type, _) = let (media_type, _) = map_content_type(specifier, Some(&content_type));
map_content_type(specifier, Some(content_type.clone()));
let local = let local =
self self
@ -372,7 +371,7 @@ impl FileFetcher {
let bytes = blob.read_all().await?; let bytes = blob.read_all().await?;
let (media_type, maybe_charset) = let (media_type, maybe_charset) =
map_content_type(specifier, Some(content_type.clone())); map_content_type(specifier, Some(&content_type));
let source = get_source_from_bytes(bytes, maybe_charset)?; let source = get_source_from_bytes(bytes, maybe_charset)?;
let local = let local =
@ -1028,7 +1027,7 @@ mod tests {
for (specifier, maybe_content_type, media_type, maybe_charset) in fixtures { for (specifier, maybe_content_type, media_type, maybe_charset) in fixtures {
let specifier = resolve_url_or_path(specifier).unwrap(); let specifier = resolve_url_or_path(specifier).unwrap();
assert_eq!( assert_eq!(
map_content_type(&specifier, maybe_content_type), map_content_type(&specifier, maybe_content_type.as_ref()),
(media_type, maybe_charset) (media_type, maybe_charset)
); );
} }

View file

@ -324,7 +324,7 @@ fn get_import_map_completions(
new_text: label.clone(), new_text: label.clone(),
})); }));
items.push(lsp::CompletionItem { items.push(lsp::CompletionItem {
label: label.clone(), label,
kind, kind,
detail: Some("(import map)".to_string()), detail: Some("(import map)".to_string()),
sort_text: Some("1".to_string()), sort_text: Some("1".to_string()),
@ -523,12 +523,7 @@ mod tests {
for (specifier, source, version, language_id) in fixtures { for (specifier, source, version, language_id) in fixtures {
let specifier = let specifier =
resolve_url(specifier).expect("failed to create specifier"); resolve_url(specifier).expect("failed to create specifier");
documents.open( documents.open(specifier, *version, *language_id, (*source).into());
specifier.clone(),
*version,
*language_id,
(*source).into(),
);
} }
let http_cache = HttpCache::new(location); let http_cache = HttpCache::new(location);
for (specifier, source) in source_fixtures { for (specifier, source) in source_fixtures {

View file

@ -560,8 +560,7 @@ impl Config {
for (workspace, folder) in workspace_folders { for (workspace, folder) in workspace_folders {
if let Ok(settings) = client.specifier_configuration(&folder.uri).await if let Ok(settings) = client.specifier_configuration(&folder.uri).await
{ {
if self.update_enabled_paths_entry(&workspace, settings.enable_paths) if self.update_enabled_paths_entry(workspace, settings.enable_paths) {
{
touched = true; touched = true;
} }
} }
@ -569,7 +568,7 @@ impl Config {
touched touched
} else if let Some(root_uri) = self.root_uri.clone() { } else if let Some(root_uri) = self.root_uri.clone() {
self.update_enabled_paths_entry( self.update_enabled_paths_entry(
&root_uri, root_uri,
self.settings.workspace.enable_paths.clone(), self.settings.workspace.enable_paths.clone(),
) )
} else { } else {
@ -580,10 +579,10 @@ impl Config {
/// Update a specific entry in the enabled paths for a given workspace. /// Update a specific entry in the enabled paths for a given workspace.
fn update_enabled_paths_entry( fn update_enabled_paths_entry(
&mut self, &mut self,
workspace: &ModuleSpecifier, workspace: ModuleSpecifier,
enabled_paths: Vec<String>, enabled_paths: Vec<String>,
) -> bool { ) -> bool {
let workspace = ensure_directory_specifier(workspace.clone()); let workspace = ensure_directory_specifier(workspace);
let key = workspace.to_string(); let key = workspace.to_string();
let mut touched = false; let mut touched = false;
if !enabled_paths.is_empty() { if !enabled_paths.is_empty() {

View file

@ -718,8 +718,7 @@ impl FileSystemDocuments {
} else { } else {
let cache_filename = cache.get_cache_filename(specifier)?; let cache_filename = cache.get_cache_filename(specifier)?;
let specifier_metadata = CachedUrlMetadata::read(&cache_filename).ok()?; let specifier_metadata = CachedUrlMetadata::read(&cache_filename).ok()?;
let maybe_content_type = let maybe_content_type = specifier_metadata.headers.get("content-type");
specifier_metadata.headers.get("content-type").cloned();
let maybe_headers = Some(&specifier_metadata.headers); let maybe_headers = Some(&specifier_metadata.headers);
let (_, maybe_charset) = map_content_type(specifier, maybe_content_type); let (_, maybe_charset) = map_content_type(specifier, maybe_content_type);
let content = get_source_from_bytes(bytes, maybe_charset).ok()?; let content = get_source_from_bytes(bytes, maybe_charset).ok()?;
@ -1055,9 +1054,7 @@ impl Documents {
} else if let Some(Resolved::Ok { specifier, .. }) = } else if let Some(Resolved::Ok { specifier, .. }) =
self.resolve_imports_dependency(&specifier) self.resolve_imports_dependency(&specifier)
{ {
// clone here to avoid double borrow of self results.push(self.resolve_dependency(specifier, maybe_npm_resolver));
let specifier = specifier.clone();
results.push(self.resolve_dependency(&specifier, maybe_npm_resolver));
} else if let Ok(npm_ref) = NpmPackageReference::from_str(&specifier) { } else if let Ok(npm_ref) = NpmPackageReference::from_str(&specifier) {
results.push(maybe_npm_resolver.map(|npm_resolver| { results.push(maybe_npm_resolver.map(|npm_resolver| {
NodeResolution::into_specifier_and_media_type( NodeResolution::into_specifier_and_media_type(

View file

@ -1528,7 +1528,7 @@ impl Inner {
.extend(refactor_info.to_code_actions(&specifier, &params.range)); .extend(refactor_info.to_code_actions(&specifier, &params.range));
} }
all_actions.extend( all_actions.extend(
refactor::prune_invalid_actions(&refactor_actions, 5) refactor::prune_invalid_actions(refactor_actions, 5)
.into_iter() .into_iter()
.map(CodeActionOrCommand::CodeAction), .map(CodeActionOrCommand::CodeAction),
); );

View file

@ -121,10 +121,11 @@ impl Performance {
averages averages
.into_iter() .into_iter()
.map(|(k, d)| { .map(|(k, d)| {
let a = d.clone().into_iter().sum::<Duration>() / d.len() as u32; let count = d.len() as u32;
let a = d.into_iter().sum::<Duration>() / count;
PerformanceAverage { PerformanceAverage {
name: k, name: k,
count: d.len() as u32, count,
average_duration: a.as_millis() as u32, average_duration: a.as_millis() as u32,
} }
}) })

View file

@ -157,7 +157,7 @@ pub struct RefactorCodeActionData {
} }
pub fn prune_invalid_actions( pub fn prune_invalid_actions(
actions: &[lsp::CodeAction], actions: Vec<lsp::CodeAction>,
number_of_invalid: usize, number_of_invalid: usize,
) -> Vec<lsp::CodeAction> { ) -> Vec<lsp::CodeAction> {
let mut available_actions = Vec::<lsp::CodeAction>::new(); let mut available_actions = Vec::<lsp::CodeAction>::new();
@ -165,7 +165,7 @@ pub fn prune_invalid_actions(
let mut invalid_uncommon_actions = Vec::<lsp::CodeAction>::new(); let mut invalid_uncommon_actions = Vec::<lsp::CodeAction>::new();
for action in actions { for action in actions {
if action.disabled.is_none() { if action.disabled.is_none() {
available_actions.push(action.clone()); available_actions.push(action);
continue; continue;
} }
@ -175,12 +175,12 @@ pub fn prune_invalid_actions(
if action_kind.starts_with(EXTRACT_CONSTANT.kind.as_str()) if action_kind.starts_with(EXTRACT_CONSTANT.kind.as_str())
|| action_kind.starts_with(EXTRACT_FUNCTION.kind.as_str()) || action_kind.starts_with(EXTRACT_FUNCTION.kind.as_str())
{ {
invalid_common_actions.push(action.clone()); invalid_common_actions.push(action);
continue; continue;
} }
// These are the remaining refactors that we can show if we haven't reached the max limit with just common refactors. // These are the remaining refactors that we can show if we haven't reached the max limit with just common refactors.
invalid_uncommon_actions.push(action.clone()); invalid_uncommon_actions.push(action);
} }
let mut prioritized_actions = Vec::<lsp::CodeAction>::new(); let mut prioritized_actions = Vec::<lsp::CodeAction>::new();

View file

@ -107,7 +107,7 @@ impl LspUrlMap {
.map_err(|e| uri_error(format!("{:?}", e)))?; .map_err(|e| uri_error(format!("{:?}", e)))?;
let mime = data_url.mime_type(); let mime = data_url.mime_type();
let (media_type, _) = let (media_type, _) =
map_content_type(specifier, Some(format!("{}", mime))); map_content_type(specifier, Some(&format!("{}", mime)));
let extension = if media_type == MediaType::Unknown { let extension = if media_type == MediaType::Unknown {
"" ""
} else { } else {