codeinput 0.1.0

A powerful library for parsing, analyzing, and managing CODEOWNERS files. Provides advanced querying capabilities, ownership analysis, and tag-based file organization.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use serde::Serialize;
use serde_json::Value;
use tower_lsp::LanguageServer;
use tower_lsp::jsonrpc::Result as LspResult;
use tower_lsp::lsp_types::*;
use url::Url;

use super::server::{LspServer, is_codeowners_file, uri_to_path};

#[tower_lsp::async_trait]
impl LanguageServer for LspServer {
    async fn initialize(&self, params: InitializeParams) -> LspResult<InitializeResult> {
        // Initialize workspaces from workspace folders
        let workspace_folders = params.workspace_folders.unwrap_or_default();

        for folder in workspace_folders {
            if let Err(e) = self.initialize_workspace(folder.uri, None).await {
                self.client
                    .log_message(
                        MessageType::WARNING,
                        format!("Failed to initialize workspace: {}", e),
                    )
                    .await;
            }
        }

        // If no workspace folders, try to use root_uri
        if let Some(root_uri) = params.root_uri {
            if let Err(e) = self.initialize_workspace(root_uri, None).await {
                self.client
                    .log_message(
                        MessageType::WARNING,
                        format!("Failed to initialize root workspace: {}", e),
                    )
                    .await;
            }
        }

        Ok(InitializeResult {
            capabilities: ServerCapabilities {
                text_document_sync: Some(TextDocumentSyncCapability::Kind(
                    TextDocumentSyncKind::FULL,
                )),
                hover_provider: Some(HoverProviderCapability::Simple(true)),
                code_lens_provider: Some(CodeLensOptions {
                    resolve_provider: Some(false),
                }),
                inlay_hint_provider: Some(OneOf::Left(true)),
                execute_command_provider: Some(ExecuteCommandOptions {
                    commands: vec![
                        "codeinput.listFiles".to_string(),
                        "codeinput.listOwners".to_string(),
                        "codeinput.listTags".to_string(),
                        "codeinput.getFileOwnership".to_string(),
                    ],
                    work_done_progress_options: WorkDoneProgressOptions {
                        work_done_progress: None,
                    },
                }),
                workspace: Some(WorkspaceServerCapabilities {
                    workspace_folders: Some(WorkspaceFoldersServerCapabilities {
                        supported: Some(true),
                        change_notifications: Some(OneOf::Left(true)),
                    }),
                    file_operations: None,
                }),
                ..ServerCapabilities::default()
            },
            server_info: Some(ServerInfo {
                name: "codeinput-lsp".to_string(),
                version: Some(env!("CARGO_PKG_VERSION").to_string()),
            }),
        })
    }

    async fn initialized(&self, _: InitializedParams) {
        self.client
            .log_message(MessageType::INFO, "codeinput LSP server initialized")
            .await;

        // Publish initial diagnostics
        self.publish_unowned_diagnostics().await;
    }

    async fn shutdown(&self) -> LspResult<()> {
        Ok(())
    }

    async fn did_open(&self, params: DidOpenTextDocumentParams) {
        let file_uri = params.text_document.uri;

        // Check if this is a CODEOWNERS file and refresh cache if so
        if is_codeowners_file(&file_uri) {
            // Find which workspace this file belongs to
            let matching_root = {
                let workspaces = self.workspaces.read().await;
                workspaces
                    .keys()
                    .find(|root_uri| file_uri.as_str().starts_with(root_uri.as_str()))
                    .cloned()
            };

            if let Some(root_uri) = matching_root {
                if let Err(e) = self.refresh_workspace_cache(&root_uri).await {
                    self.client
                        .log_message(
                            MessageType::WARNING,
                            format!("Failed to refresh cache: {}", e),
                        )
                        .await;
                }
                // Re-publish diagnostics after cache refresh
                self.publish_unowned_diagnostics().await;
            }
        }
    }

    async fn did_change(&self, _params: DidChangeTextDocumentParams) {
        // We use full sync, so we don't need to handle incremental changes
    }

    async fn did_save(&self, params: DidSaveTextDocumentParams) {
        let file_uri = params.text_document.uri;

        // If CODEOWNERS file was saved, refresh the cache
        if is_codeowners_file(&file_uri) {
            let matching_root = {
                let workspaces = self.workspaces.read().await;
                workspaces
                    .keys()
                    .find(|root_uri| file_uri.as_str().starts_with(root_uri.as_str()))
                    .cloned()
            };

            if let Some(root_uri) = matching_root {
                if let Err(e) = self.refresh_workspace_cache(&root_uri).await {
                    self.client
                        .log_message(
                            MessageType::WARNING,
                            format!("Failed to refresh cache: {}", e),
                        )
                        .await;
                }
                self.publish_unowned_diagnostics().await;
            }
        }
    }

    async fn hover(&self, params: HoverParams) -> LspResult<Option<Hover>> {
        let file_uri = params.text_document_position_params.text_document.uri;

        // Get ownership info for this file
        if let Some(info) = self.get_file_ownership(&file_uri).await {
            let mut contents = vec![];

            // Add owners section
            if info.owners.is_empty() {
                contents.push(MarkedString::String("**Owners:** (none)".to_string()));
            } else {
                let owners_str = info
                    .owners
                    .iter()
                    .map(|o| format!("`{}`", o.identifier))
                    .collect::<Vec<_>>()
                    .join(", ");
                contents.push(MarkedString::String(format!("**Owners:** {}", owners_str)));
            }

            // Add tags section
            if !info.tags.is_empty() {
                let tags_str = info
                    .tags
                    .iter()
                    .map(|t| format!("`#{}`", t.0))
                    .collect::<Vec<_>>()
                    .join(", ");
                contents.push(MarkedString::String(format!("**Tags:** {}", tags_str)));
            }

            // Add warning if unowned
            if info.is_unowned {
                contents.push(MarkedString::String(
                    "⚠️ **Warning:** This file has no CODEOWNERS assignment".to_string(),
                ));
            }

            return Ok(Some(Hover {
                contents: HoverContents::Array(contents),
                range: None,
            }));
        }

        Ok(None)
    }

    async fn code_lens(&self, params: CodeLensParams) -> LspResult<Option<Vec<CodeLens>>> {
        let file_uri = params.text_document.uri;

        // Get ownership info for this file
        if let Some(info) = self.get_file_ownership(&file_uri).await {
            let mut lenses = vec![];

            // Create a CodeLens showing ownership at the top of the file
            if !info.owners.is_empty() {
                let owners_str = info
                    .owners
                    .iter()
                    .map(|o| o.identifier.clone())
                    .collect::<Vec<_>>()
                    .join(", ");

                // Safely serialize arguments
                let args = (
                    serde_json::to_value(file_uri.to_string()).ok(),
                    serde_json::to_value(&info.owners).ok(),
                );
                if let (Some(uri_val), Some(owners_val)) = args {
                    lenses.push(CodeLens {
                        range: Range {
                            start: Position::new(0, 0),
                            end: Position::new(0, 0),
                        },
                        command: Some(Command {
                            title: format!("$(organization)  {}", owners_str),
                            command: "codeinput.showOwners".to_string(),
                            arguments: Some(vec![uri_val, owners_val]),
                        }),
                        data: None,
                    });
                }
            }

            // Add tags CodeLens if any
            if !info.tags.is_empty() {
                let tags_str = info
                    .tags
                    .iter()
                    .map(|t| format!("#{}", t.0))
                    .collect::<Vec<_>>()
                    .join(", ");

                // Safely serialize arguments
                let args = (
                    serde_json::to_value(file_uri.to_string()).ok(),
                    serde_json::to_value(&info.tags).ok(),
                );
                if let (Some(uri_val), Some(tags_val)) = args {
                    lenses.push(CodeLens {
                        range: Range {
                            start: Position::new(0, 0),
                            end: Position::new(0, 0),
                        },
                        command: Some(Command {
                            title: format!("$(tag)  {}", tags_str),
                            command: "codeinput.showTags".to_string(),
                            arguments: Some(vec![uri_val, tags_val]),
                        }),
                        data: None,
                    });
                }
            }

            return Ok(Some(lenses));
        }

        Ok(None)
    }

    async fn inlay_hint(&self, params: InlayHintParams) -> LspResult<Option<Vec<InlayHint>>> {
        let file_uri = params.text_document.uri;

        let path = uri_to_path(&file_uri);
        let file_path = match path {
            Ok(p) => p,
            Err(_) => return Ok(None),
        };

        if file_path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or_default()
            .to_lowercase()
            != "codeowners"
        {
            return Ok(None);
        }

        let workspaces = self.workspaces.read().await;

        for (_root_uri, state) in workspaces.iter() {
            let file_entries: Vec<_> = state
                .cache
                .entries
                .iter()
                .filter(|e| e.source_file == file_path)
                .collect();

            if file_entries.is_empty() {
                continue;
            }

            // Read the CODEOWNERS file content asynchronously to get line lengths
            let content = match tokio::fs::read_to_string(&file_path).await {
                Ok(c) => c,
                Err(_) => continue,
            };

            let lines: Vec<&str> = content.lines().collect();
            let mut hints = vec![];

            for entry in file_entries {
                let line_idx = if entry.line_number > 0 {
                    entry.line_number - 1
                } else {
                    0
                };
                let line_length = lines.get(line_idx).map(|l| l.len() as u32).unwrap_or(0);

                let matcher = crate::core::types::codeowners_entry_to_matcher(entry);

                let mut match_count = 0;
                for file_entry in &state.cache.files {
                    if matcher
                        .override_matcher
                        .matched(&file_entry.path, false)
                        .is_whitelist()
                    {
                        match_count += 1;
                    }
                }

                hints.push(InlayHint {
                    position: Position::new(line_idx as u32, line_length),
                    label: InlayHintLabel::String(format!(
                        "  {} file{}",
                        match_count,
                        if match_count == 1 { "" } else { "s" }
                    )),
                    kind: Some(InlayHintKind::TYPE),
                    text_edits: None,
                    tooltip: Some(InlayHintTooltip::String(format!(
                        "This pattern matches {} file(s) in the repository",
                        match_count
                    ))),
                    padding_left: Some(true),
                    padding_right: Some(false),
                    data: None,
                });
            }

            if !hints.is_empty() {
                return Ok(Some(hints));
            }
        }

        Ok(None)
    }

    async fn did_change_workspace_folders(&self, params: DidChangeWorkspaceFoldersParams) {
        // Handle removed workspaces - collect URIs first, then remove them
        let removed_uris: Vec<Url> = params.event.removed.iter().map(|f| f.uri.clone()).collect();
        {
            let mut workspaces = self.workspaces.write().await;
            for uri in removed_uris {
                workspaces.remove(&uri);
            }
        }

        // Handle added workspaces
        for folder in params.event.added {
            if let Err(e) = self.initialize_workspace(folder.uri, None).await {
                self.client
                    .log_message(
                        MessageType::WARNING,
                        format!("Failed to initialize workspace: {}", e),
                    )
                    .await;
            }
        }
    }

    async fn execute_command(&self, params: ExecuteCommandParams) -> LspResult<Option<Value>> {
        fn to_value<T: Serialize>(v: T) -> LspResult<Value> {
            serde_json::to_value(v)
                .map_err(|e| tower_lsp::jsonrpc::Error::invalid_params(e.to_string()))
        }

        match params.command.as_str() {
            "codeinput.getFileOwnership" => {
                let uri_val = params
                    .arguments
                    .first()
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| {
                        tower_lsp::jsonrpc::Error::invalid_params(
                            "Expected a single URI string argument",
                        )
                    })?;
                let result = self.get_file_ownership_command(uri_val.to_string()).await?;
                to_value(result).map(Some)
            }
            "codeinput.listFiles" => {
                let result = self.list_files(None).await?;
                to_value(result).map(Some)
            }
            "codeinput.listOwners" => {
                let result = self.list_owners(None).await?;
                to_value(result).map(Some)
            }
            "codeinput.listTags" => {
                let result = self.list_tags(None).await?;
                to_value(result).map(Some)
            }
            _ => Err(tower_lsp::jsonrpc::Error::method_not_found()),
        }
    }
}