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
// Rust guideline compliant 2025-10-17
//! Shared helpers for deriving the in-scope identifier of a Go import.
//!
//! Go imports are slash-separated paths and support *semantic import
//! versioning* (a path ending in `/vN`, e.g. `github.com/jackc/pgx/v5`). For
//! such a path the package identifier that code actually references is the
//! segment *before* the `/vN` (`pgx`), not the literal last segment (`v5`).
//!
//! Two subsystems must agree on this derivation:
//! - `unused_imports` (`mcp::tools::handlers::analysis`) — to find whether the
//! import's identifier appears at a call site (#149 Bug 2).
//! - the reference resolver (`resolution::resolver`) — to map a selector
//! qualifier (`foojobs`) back to the import path so same-named packages don't
//! collide (#149 Bug 1).
//!
//! Keeping the logic here guarantees both stay consistent.
/// True if `seg` is a Go semantic-import-versioning segment: `v` followed by
/// one or more ASCII digits (`v2`, `v5`, `v11`). A segment that merely *starts*
/// with `v` (`revision`, `view`) is not a version segment.
/// Returns the package identifier a bare (un-aliased) Go import path brings
/// into scope: the last path segment, except when that segment is a `/vN`
/// version marker, in which case the *preceding* segment is used.
///
/// - `net/url` -> `url`
/// - `github.com/golang-jwt/jwt/v5` -> `jwt`
/// - `github.com/jackc/pgx/v5` -> `pgx`
/// - `example.com/m/internal/foo/revision` -> `revision` (only `^v\d+$` triggers)
///
/// Returns `None` for an empty path.
/// Returns the identifier a Go import brings into scope, accounting for an
/// explicit alias encoded as `<path> as <alias>` (the convention used by the
/// Go extractor's Use nodes).
///
/// - `net/url` -> `url`
/// - `github.com/jackc/pgx/v5` -> `pgx`
/// - `github.com/jackc/pgx/v5 as pgxv5` -> `pgxv5`
///
/// Returns `None` when the derived identifier would be empty.