Skip to content

Fix Forgejo repo/view-source links #4706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@

([Amjad Mohamed](https://github.com/andho))

- Docs generator now strips trailing slashes from Gitea/Forgejo hosts so sidebar "Repository" and "View Source" links never include `//`, and single-line “View Source” anchors emit `#Lx` instead of `#Lx-x`. ([Aayush Tripathi](https://github.com/aayush-tripathi))

### Language server

### Formatter
Expand Down
6 changes: 5 additions & 1 deletion compiler-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,11 @@ impl Repository {
}
Repository::Gitea {
repo, user, host, ..
} => Some(format!("{host}/{user}/{repo}")),
} => {
let string_host = host.to_string();
let cleaned_host = string_host.trim_end_matches('/');
Some(format!("{cleaned_host}/{user}/{repo}"))
}
Repository::Custom { url } => Some(url.clone()),
Repository::None => None,
}
Expand Down
24 changes: 16 additions & 8 deletions compiler-core/src/docs/source_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ impl SourceLinker {
)),
Repository::Gitea {
user, repo, host, ..
} => Some((
format!(
"{host}/{user}/{repo}/src/tag/v{}/{}#L",
project_config.version, path_in_repo
),
"-".into(),
)),
} => {
let string_host = host.to_string();
let cleaned_host = string_host.trim_end_matches('/');
Some((
format!(
"{cleaned_host}/{user}/{repo}/src/tag/v{}/{}#L",
project_config.version, path_in_repo
),
"-".into(),
))
}
Repository::Custom { .. } | Repository::None => None,
};

Expand All @@ -90,7 +94,11 @@ impl SourceLinker {
Some((base, line_sep)) => {
let start_line = self.line_numbers.line_number(span.start);
let end_line = self.line_numbers.line_number(span.end);
format!("{base}{start_line}{line_sep}{end_line}")
if start_line == end_line {
format!("{base}{start_line}")
} else {
format!("{base}{start_line}{line_sep}{end_line}")
}
}

None => "".into(),
Expand Down
25 changes: 21 additions & 4 deletions compiler-core/src/docs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{
use camino::Utf8PathBuf;
use ecow::EcoString;
use hexpm::version::Version;
use http::Uri;
use itertools::Itertools;
use serde_json::to_string as serde_to_string;

Expand Down Expand Up @@ -626,7 +627,7 @@ fn source_link_for_github_repository() {
let modules = vec![("app.gleam", "pub type Wibble = Int")];
assert!(
compile(config, modules)
.contains("https://github.com/wibble/wobble/blob/v0.1.0/src/app.gleam#L1-L1")
.contains("https://github.com/wibble/wobble/blob/v0.1.0/src/app.gleam#L1")
);
}

Expand All @@ -641,9 +642,11 @@ fn source_link_for_github_repository_with_path() {
};

let modules = vec![("app.gleam", "pub type Wibble = Int")];
assert!(compile(config, modules).contains(
"https://github.com/wibble/wobble/blob/v0.1.0/path/to/package/src/app.gleam#L1-L1"
));
assert!(
compile(config, modules).contains(
"https://github.com/wibble/wobble/blob/v0.1.0/path/to/package/src/app.gleam#L1"
)
);
}

#[test]
Expand Down Expand Up @@ -1162,3 +1165,17 @@ pub type Wibble {
NONE
);
}
#[test]
fn gitea_repository_url_has_no_double_slash() {
let repo = Repository::Gitea {
host: "https://code.example.org/".parse::<Uri>().unwrap(),
user: "person".into(),
repo: "forgejo_bug".into(),
path: None,
};

assert_eq!(
repo.url().unwrap(),
"https://code.example.org/person/forgejo_bug"
);
}
Loading