|
| 1 | +use tower_lsp::jsonrpc::Result as LspResult; |
| 2 | +use url::Url; |
| 3 | + |
| 4 | +use crate::core::types::{CodeownersCache, OwnerType}; |
| 5 | + |
| 6 | +use super::server::LspServer; |
| 7 | +use super::types::*; |
| 8 | + |
| 9 | +impl LspServer { |
| 10 | + pub async fn list_files(&self, workspace_uri: Option<Url>) -> LspResult<ListFilesResponse> { |
| 11 | + let workspaces = self.workspaces.read().await; |
| 12 | + |
| 13 | + let files = if let Some(uri) = workspace_uri { |
| 14 | + if let Some(state) = workspaces.get(&uri) { |
| 15 | + Self::collect_files_from_cache(&state.cache) |
| 16 | + } else { |
| 17 | + Vec::new() |
| 18 | + } |
| 19 | + } else { |
| 20 | + let mut all_files = Vec::new(); |
| 21 | + for state in workspaces.values() { |
| 22 | + all_files.extend(Self::collect_files_from_cache(&state.cache)); |
| 23 | + } |
| 24 | + all_files |
| 25 | + }; |
| 26 | + |
| 27 | + Ok(ListFilesResponse { files }) |
| 28 | + } |
| 29 | + |
| 30 | + pub async fn list_owners(&self, workspace_uri: Option<Url>) -> LspResult<ListOwnersResponse> { |
| 31 | + let workspaces = self.workspaces.read().await; |
| 32 | + |
| 33 | + let owners = if let Some(uri) = workspace_uri { |
| 34 | + if let Some(state) = workspaces.get(&uri) { |
| 35 | + Self::collect_owners_from_cache(&state.cache) |
| 36 | + } else { |
| 37 | + Vec::new() |
| 38 | + } |
| 39 | + } else { |
| 40 | + let mut all_owners = Vec::new(); |
| 41 | + for state in workspaces.values() { |
| 42 | + all_owners.extend(Self::collect_owners_from_cache(&state.cache)); |
| 43 | + } |
| 44 | + all_owners |
| 45 | + }; |
| 46 | + |
| 47 | + Ok(ListOwnersResponse { owners }) |
| 48 | + } |
| 49 | + |
| 50 | + pub async fn list_tags(&self, workspace_uri: Option<Url>) -> LspResult<ListTagsResponse> { |
| 51 | + let workspaces = self.workspaces.read().await; |
| 52 | + |
| 53 | + let tags = if let Some(uri) = workspace_uri { |
| 54 | + if let Some(state) = workspaces.get(&uri) { |
| 55 | + Self::collect_tags_from_cache(&state.cache) |
| 56 | + } else { |
| 57 | + Vec::new() |
| 58 | + } |
| 59 | + } else { |
| 60 | + let mut all_tags = Vec::new(); |
| 61 | + for state in workspaces.values() { |
| 62 | + all_tags.extend(Self::collect_tags_from_cache(&state.cache)); |
| 63 | + } |
| 64 | + all_tags |
| 65 | + }; |
| 66 | + |
| 67 | + Ok(ListTagsResponse { tags }) |
| 68 | + } |
| 69 | + |
| 70 | + fn collect_files_from_cache(cache: &CodeownersCache) -> Vec<FileOwnershipInfo> { |
| 71 | + cache |
| 72 | + .files |
| 73 | + .iter() |
| 74 | + .map(|entry| FileOwnershipInfo { |
| 75 | + path: entry.path.clone(), |
| 76 | + owners: entry.owners.clone(), |
| 77 | + tags: entry.tags.clone(), |
| 78 | + is_unowned: entry.owners.is_empty() |
| 79 | + || entry |
| 80 | + .owners |
| 81 | + .iter() |
| 82 | + .any(|o| matches!(o.owner_type, OwnerType::Unowned)), |
| 83 | + }) |
| 84 | + .collect() |
| 85 | + } |
| 86 | + |
| 87 | + fn collect_owners_from_cache(cache: &CodeownersCache) -> Vec<OwnerInfo> { |
| 88 | + cache |
| 89 | + .owners_map |
| 90 | + .iter() |
| 91 | + .map(|(owner, files)| OwnerInfo { |
| 92 | + owner: owner.clone(), |
| 93 | + files: files.clone(), |
| 94 | + }) |
| 95 | + .collect() |
| 96 | + } |
| 97 | + |
| 98 | + fn collect_tags_from_cache(cache: &CodeownersCache) -> Vec<TagInfo> { |
| 99 | + cache |
| 100 | + .tags_map |
| 101 | + .iter() |
| 102 | + .map(|(tag, files)| TagInfo { |
| 103 | + tag: tag.clone(), |
| 104 | + files: files.clone(), |
| 105 | + }) |
| 106 | + .collect() |
| 107 | + } |
| 108 | +} |
| 109 | + |
0 commit comments