cargo-port 0.2.1

A TUI for inspecting and managing Rust projects
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use std::collections::HashSet;
use std::path::Path;

use ratatui::style::Color;
use tui_pane::title_color;

pub use super::constants::FINDER_COLUMN_COUNT;
pub use super::constants::FINDER_HEADERS;
use crate::ci;
use crate::ci::OwnerRepo;
use crate::project::AbsolutePath;
use crate::project::CheckoutInfo;
use crate::project::ExampleGroup;
use crate::project::Package;
use crate::project::ProjectEntry;
use crate::project::ProjectFields;
use crate::project::ProjectType;
use crate::project::RootItem;
use crate::project::RustProject;
use crate::project::VendoredPackage;
use crate::project::Visibility;
use crate::project::Workspace;
use crate::tui::constants::TARGET_KIND_BENCH_LABEL;
use crate::tui::constants::TARGET_KIND_BIN_LABEL;
use crate::tui::constants::TARGET_KIND_EXAMPLE_LABEL;
use crate::tui::panes::RunTargetKind;
use crate::tui::project_list::ProjectList;

/// A searchable item in the universal finder.
#[derive(Clone)]
pub struct FinderItem {
    /// Display name shown in the results list.
    pub display_name:  String,
    /// Search tokens derived from visible fields and path segments.
    pub search_tokens: Vec<String>,
    /// What kind of item this is.
    pub kind:          FinderKind,
    /// Path of the project this item belongs to (for navigation).
    pub project_path:  AbsolutePath,
    /// For targets: the cargo target name (used with --example/--bench).
    pub target_name:   Option<String>,
    /// Parent project display name (shown dimmed for non-project items).
    pub parent_label:  String,
    /// Git branch, if known. Distinguishes worktrees.
    pub branch:        String,
    /// Directory name (last path component).
    pub dir:           String,
    pub pr_target:     Option<PullRequestTarget>,
}

#[derive(Clone)]
pub struct PullRequestTarget {
    pub owner_repo: OwnerRepo,
    pub number:     u32,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum FinderKind {
    Project,
    Binary,
    Example,
    Bench,
    PullRequest,
}

impl FinderKind {
    pub const fn label(self) -> &'static str {
        match self {
            Self::Project => "project",
            Self::Binary => TARGET_KIND_BIN_LABEL,
            Self::Example => TARGET_KIND_EXAMPLE_LABEL,
            Self::Bench => TARGET_KIND_BENCH_LABEL,
            Self::PullRequest => "pr",
        }
    }

    pub fn color(self) -> Color {
        match self {
            Self::Project | Self::PullRequest => title_color(),
            Self::Binary => RunTargetKind::Binary.color(),
            Self::Example => RunTargetKind::Example.color(),
            Self::Bench => RunTargetKind::Bench.color(),
        }
    }
}

/// Build a flat index of all searchable items from the project list.
/// Uses the tree structure so workspace members inherit the branch
/// from their workspace root (members don't have their own `.git`).
/// Only `Visible` entries are indexed: `Deleted` (gone from disk) and
/// `Dismissed` ones are skipped so the finder only returns live,
/// navigable targets. Returns `(items, col_widths)` where `col_widths` is
/// the max display width of each column across the entire index.
pub fn build_finder_index(
    entries: &ProjectList,
) -> (Vec<FinderItem>, [usize; FINDER_COLUMN_COUNT]) {
    let mut items = Vec::new();
    let mut seen_pull_requests = HashSet::new();

    for entry in entries {
        if entry.root_item.visibility() != Visibility::Visible {
            continue;
        }
        match &entry.root_item {
            RootItem::Rust(RustProject::Workspace(ws)) => {
                add_workspace_items(&mut items, ws);
            },
            RootItem::Rust(RustProject::Package(pkg)) => {
                add_package_items(&mut items, pkg);
            },
            RootItem::NonRust(nr) => {
                let dp = nr.display_path().into_string();
                let abs = nr.path();
                let branch = branch_for(nr.git_info());
                let root_name = nr.root_directory_name().into_string();
                let context = TypedProjectContext {
                    project_name: &root_name,
                    cargo_name:   None,
                    abs_path:     abs,
                    display_path: &dp,
                    branch:       &branch,
                };
                add_project_items_from_typed(&mut items, &context, &[], &[], &[]);
            },
            RootItem::Worktrees(group) => {
                let primary_path = group.primary.path().clone();
                let mut emit = |entry: &RustProject| match entry {
                    RustProject::Workspace(ws) => add_workspace_items(&mut items, ws),
                    RustProject::Package(pkg) => add_package_items(&mut items, pkg),
                };
                if group.primary.visibility() == Visibility::Visible {
                    emit(&group.primary);
                }
                for l in &group.linked {
                    if l.path() == &primary_path {
                        continue;
                    }
                    if l.visibility() != Visibility::Visible {
                        continue;
                    }
                    emit(l);
                }
            },
        }
        add_pull_request_items(entries, &mut items, &mut seen_pull_requests, entry);
    }

    // Pre-compute column widths from the full index
    let mut col_widths: [usize; FINDER_COLUMN_COUNT] = FINDER_HEADERS.map(str::len);
    for item in &items {
        col_widths[0] = col_widths[0].max(item.display_name.len());
        col_widths[1] = col_widths[1].max(if item.kind == FinderKind::Project {
            0
        } else {
            item.parent_label.len()
        });
        col_widths[2] = col_widths[2].max(item.branch.len());
        col_widths[3] = col_widths[3].max(item.dir.len());
        col_widths[4] = col_widths[4].max(item.kind.label().len());
    }

    (items, col_widths)
}

fn add_pull_request_items(
    entries: &ProjectList,
    items: &mut Vec<FinderItem>,
    seen: &mut HashSet<(OwnerRepo, u32)>,
    entry: &ProjectEntry,
) {
    let Some(url) = entries.fetch_url_for(entry.root_item.path()) else {
        return;
    };
    if ci::parse_owner_repo(&url).is_none() {
        return;
    }
    let Some(info) = entry.git_repo.as_ref().and_then(|repo| repo.pr_data.info()) else {
        return;
    };
    let project_label = entry.root_item.root_directory_name().into_string();
    let dir = entry.root_item.display_path().into_string();
    for pull_request in &info.open {
        if !seen.insert((info.owner_repo.clone(), pull_request.number)) {
            continue;
        }
        let display_name = format!("#{} {}", pull_request.number, pull_request.title);
        let branch = pull_request.branch_label(&info.default_branch);
        let number = pull_request.number.to_string();
        let state_label = pull_request.state.label();
        let head_owner = pull_request.head_owner.as_deref().unwrap_or("");
        let head_repo = pull_request.head_repo.as_deref().unwrap_or("");
        items.push(FinderItem {
            search_tokens: build_search_tokens(&[
                &number,
                &pull_request.title,
                &branch,
                &pull_request.head,
                &pull_request.base,
                state_label,
                head_owner,
                head_repo,
                &info.viewer_login,
                FinderKind::PullRequest.label(),
            ]),
            display_name,
            kind: FinderKind::PullRequest,
            project_path: entry.root_item.path().clone(),
            target_name: None,
            parent_label: project_label.clone(),
            branch,
            dir: dir.clone(),
            pr_target: Some(PullRequestTarget {
                owner_repo: info.owner_repo.clone(),
                number:     pull_request.number,
            }),
        });
    }
}

fn branch_for(git_info: Option<&CheckoutInfo>) -> String {
    git_info.map_or_else(String::new, |g| g.head.display_label())
}

fn add_workspace_items(items: &mut Vec<FinderItem>, ws: &Workspace) {
    let root_path = ws.display_path().into_string();
    let root_abs_path = ws.path();
    let root_branch = branch_for(ws.git_info());
    let cargo = &ws.cargo;
    let root_name = ws.root_directory_name().into_string();
    let cargo_name = ws.package_name().into_string();
    let cargo_name = (cargo_name != root_name).then_some(cargo_name);
    let root_context = TypedProjectContext {
        project_name: &root_name,
        cargo_name:   cargo_name.as_deref(),
        abs_path:     root_abs_path,
        display_path: &root_path,
        branch:       &root_branch,
    };

    add_project_items_from_typed(
        items,
        &root_context,
        cargo.types(),
        cargo.examples(),
        cargo.benches(),
    );

    for group in ws.groups() {
        for member in group.members() {
            let member_cargo = &member.cargo;
            let member_display_path = member.display_path();
            let member_abs_path = member.path();
            let member_name = member.package_name().into_string();
            let member_context = TypedProjectContext {
                project_name: &member_name,
                cargo_name:   None,
                abs_path:     member_abs_path,
                display_path: member_display_path.as_str(),
                branch:       &root_branch,
            };
            add_project_items_from_typed(
                items,
                &member_context,
                member_cargo.types(),
                member_cargo.examples(),
                member_cargo.benches(),
            );
            for vendored in member.vendored() {
                add_vendored_items_typed(items, vendored, &member_name);
            }
        }
    }

    let ws_package_name = ws.package_name().into_string();
    for vendored in ws.vendored() {
        add_vendored_items_typed(items, vendored, &ws_package_name);
    }
}

fn add_package_items(items: &mut Vec<FinderItem>, pkg: &Package) {
    let root_path = pkg.display_path().into_string();
    let root_abs_path = pkg.path();
    let root_branch = branch_for(pkg.git_info());
    let cargo = &pkg.cargo;
    let root_name = pkg.root_directory_name().into_string();
    let pkg_name = pkg.package_name().into_string();
    let cargo_name = (pkg_name != root_name).then_some(pkg_name);
    let root_context = TypedProjectContext {
        project_name: &root_name,
        cargo_name:   cargo_name.as_deref(),
        abs_path:     root_abs_path,
        display_path: &root_path,
        branch:       &root_branch,
    };

    add_project_items_from_typed(
        items,
        &root_context,
        cargo.types(),
        cargo.examples(),
        cargo.benches(),
    );

    let pkg_parent_name = pkg.package_name().into_string();
    for vendored in pkg.vendored() {
        add_vendored_items_typed(items, vendored, &pkg_parent_name);
    }
}

fn add_vendored_items_typed(
    items: &mut Vec<FinderItem>,
    project: &VendoredPackage,
    parent_name: &str,
) {
    let project_name = project.package_name().into_string();
    let dir = project.display_path().into_string();
    let project_path: AbsolutePath = project.path().clone();
    let branch = String::new();
    let display_name = format!("{project_name} (vendored)");

    items.push(FinderItem {
        search_tokens: build_search_tokens(&[
            &display_name,
            &project_name,
            parent_name,
            &dir,
            "vendored",
            FinderKind::Project.label(),
        ]),
        display_name,
        kind: FinderKind::Project,
        project_path: project_path.clone(),
        target_name: None,
        parent_label: parent_name.to_string(),
        branch: branch.clone(),
        dir: dir.clone(),
        pr_target: None,
    });

    let cargo = &project.cargo;

    if cargo.types().contains(&ProjectType::Binary) {
        let kind = FinderKind::Binary;
        items.push(FinderItem {
            search_tokens: build_search_tokens(&[
                &project_name,
                &project_name,
                parent_name,
                &dir,
                "vendored",
                kind.label(),
            ]),
            display_name: project_name.clone(),
            kind,
            project_path: project_path.clone(),
            target_name: Some(project_name.clone()),
            parent_label: project_name.clone(),
            branch: branch.clone(),
            dir: dir.clone(),
            pr_target: None,
        });
    }

    for group in cargo.examples() {
        for name in &group.names {
            let display = if group.category.is_empty() {
                name.clone()
            } else {
                format!("{}/{name}", group.category)
            };
            let kind = FinderKind::Example;
            items.push(FinderItem {
                search_tokens: build_search_tokens(&[
                    &display,
                    &project_name,
                    parent_name,
                    &dir,
                    "vendored",
                    kind.label(),
                ]),
                display_name: display,
                kind,
                project_path: project_path.clone(),
                target_name: Some(name.clone()),
                parent_label: project_name.clone(),
                branch: branch.clone(),
                dir: dir.clone(),
                pr_target: None,
            });
        }
    }

    for name in cargo.benches() {
        let kind = FinderKind::Bench;
        items.push(FinderItem {
            search_tokens: build_search_tokens(&[
                name,
                &project_name,
                parent_name,
                &dir,
                "vendored",
                kind.label(),
            ]),
            display_name: name.clone(),
            kind,
            project_path: project_path.clone(),
            target_name: Some(name.clone()),
            parent_label: project_name.clone(),
            branch: branch.clone(),
            dir: dir.clone(),
            pr_target: None,
        });
    }
}

fn add_project_items_from_typed(
    items: &mut Vec<FinderItem>,
    context: &TypedProjectContext<'_>,
    types: &[ProjectType],
    examples: &[ExampleGroup],
    benches: &[String],
) {
    let project_name = context.project_name.to_string();
    let cargo_name = context.cargo_name.map(str::to_string);
    let branch = context.branch.to_string();
    let dir = context.display_path.to_string();

    // Build base token fields shared by all rows. Cargo name is included so
    // all targets remain findable by Cargo name when the directory differs.
    let base_fields: Vec<&str> = [&project_name as &str, &dir, &branch]
        .into_iter()
        .chain(cargo_name.as_deref())
        .collect();

    // The project itself
    let kind = FinderKind::Project;
    let mut project_tokens = base_fields.clone();
    project_tokens.push(kind.label());
    items.push(FinderItem {
        search_tokens: build_search_tokens(&project_tokens),
        display_name: project_name.clone(),
        kind,
        project_path: context.abs_path.into(),
        target_name: None,
        parent_label: String::new(),
        branch: branch.clone(),
        dir: dir.clone(),
        pr_target: None,
    });

    // Binary
    if types.contains(&ProjectType::Binary) {
        let kind = FinderKind::Binary;
        let mut tokens = base_fields.clone();
        tokens.push(kind.label());
        items.push(FinderItem {
            search_tokens: build_search_tokens(&tokens),
            display_name: project_name.clone(),
            kind,
            project_path: context.abs_path.into(),
            target_name: Some(project_name.clone()),
            parent_label: project_name.clone(),
            branch: branch.clone(),
            dir: dir.clone(),
            pr_target: None,
        });
    }

    // Examples (with category prefix)
    for group in examples {
        for name in &group.names {
            let display = if group.category.is_empty() {
                name.clone()
            } else {
                format!("{}/{name}", group.category)
            };
            let kind = FinderKind::Example;
            let mut tokens = vec![display.as_str()];
            tokens.extend_from_slice(&base_fields);
            tokens.push(kind.label());
            items.push(FinderItem {
                search_tokens: build_search_tokens(&tokens),
                display_name: display,
                kind,
                project_path: context.abs_path.into(),
                target_name: Some(name.clone()),
                parent_label: project_name.clone(),
                branch: branch.clone(),
                dir: dir.clone(),
                pr_target: None,
            });
        }
    }

    // Benches
    for name in benches {
        let kind = FinderKind::Bench;
        let mut tokens = vec![name.as_str()];
        tokens.extend_from_slice(&base_fields);
        tokens.push(kind.label());
        items.push(FinderItem {
            search_tokens: build_search_tokens(&tokens),
            display_name: name.clone(),
            kind,
            project_path: context.abs_path.into(),
            target_name: Some(name.clone()),
            parent_label: project_name.clone(),
            branch: branch.clone(),
            dir: dir.clone(),
            pr_target: None,
        });
    }
}

struct TypedProjectContext<'a> {
    project_name: &'a str,
    /// Cargo package name when it differs from `project_name`. Included in
    /// search tokens so root-level Rust items remain findable by Cargo name.
    cargo_name:   Option<&'a str>,
    abs_path:     &'a Path,
    display_path: &'a str,
    branch:       &'a str,
}

pub fn build_search_tokens(fields: &[&str]) -> Vec<String> {
    let mut tokens = Vec::new();
    for field in fields {
        for segment in field
            .split(|ch: char| ch.is_whitespace() || matches!(ch, '/' | '\\'))
            .filter(|segment| !segment.is_empty())
        {
            push_search_token(&mut tokens, segment);
            for fragment in segment.split(|ch: char| !ch.is_alphanumeric()) {
                push_search_token(&mut tokens, fragment);
            }
        }
    }
    tokens
}

fn push_search_token(tokens: &mut Vec<String>, token: &str) {
    if token.is_empty() || !token.chars().any(char::is_alphanumeric) {
        return;
    }
    if tokens.iter().any(|existing| existing == token) {
        return;
    }
    tokens.push(token.to_string());
}