Skip to content

Commit 9081c6f

Browse files
authored
👌 Improve viewcode (#15)
1 parent a4bb857 commit 9081c6f

File tree

9 files changed

+18
-16
lines changed

9 files changed

+18
-16
lines changed

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ license = "MIT"
1212

1313
[workspace.dependencies]
1414
anyhow = "1.0.82"
15+
dunce = "1.0.4"
1516
toml = "0.8.12"
1617
insta = { version = "1.38.0", features = ["yaml"] }
1718
pyo3 = "0.21.2"

‎crates/analyzer/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ readme = "../../README.md"
1414

1515
[dependencies]
1616
anyhow.workspace = true
17+
dunce.workspace = true
1718
quote.workspace = true
1819
serde.workspace = true
1920
serde_json.workspace = true

‎crates/analyzer/src/analyze/crate_.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use super::{Enum, Module, Struct};
66

77
pub fn analyze_crate(path: &str) -> Result<AnalysisResult> {
88
// make the path absolute
9+
// TODO we use dunce to canonicalize the path because otherwise there is issues with python's os.path.relpath on windows, but maybe we should fix this on the Python side
910
let path =
10-
std::fs::canonicalize(path).context(format!("Error resolving crate path: {}", path))?;
11+
dunce::canonicalize(path).context(format!("Error resolving crate path: {}", path))?;
1112
// check the path is a directory
1213
if !path.is_dir() {
1314
return Err(anyhow::anyhow!(format!(

‎docs/index.rst‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ Add `sphinx_rust` to your `conf.py`, and specifiy the paths to the Rust crates y
4646
"../path/to/crate",
4747
...
4848
]
49-
rust_viewcode = True # Optional: add "View Source" links
5049
...
5150
5251
Now add a `toctree` in your `index.rst`, to point towards the generated documentation for each crate

‎python/sphinx_rust/config.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ def add_configs(app: Sphinx) -> None:
2929
"""Add the configuration values for the Rust domain."""
3030
app.add_config_value("rust_crates", [], "env")
3131
app.add_config_value("rust_doc_formats", {}, "env")
32-
app.add_config_value("rust_viewcode", False, "env")
32+
app.add_config_value("rust_viewcode", True, "env")

‎python/sphinx_rust/directives/_core.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ def create_source_xref(
190190
*,
191191
warn_dangling: bool = False,
192192
text: str | None = None,
193+
classes: list[str] | None = None,
193194
) -> addnodes.pending_xref:
194195
"""Create a cross-reference node to the source-code of a rust object.
195196
@@ -203,6 +204,7 @@ def create_source_xref(
203204
"refexplicit": True,
204205
"refwarn": warn_dangling,
205206
"reftarget": f"rust-code:{full_name}",
207+
"classes": classes or [],
206208
}
207209
ref = addnodes.pending_xref(full_name, **options)
208210
text = full_name.split("::")[-1] if text is None else text

‎python/sphinx_rust/directives/crate.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,11 @@ def run(self) -> list[nodes.Node]:
7575
self.rust_domain.note_object(crate.name, "crate", node_id, signature)
7676

7777
if self.rust_config.rust_viewcode and crate_mod and crate_mod.file:
78-
root += nodes.paragraph(
79-
"",
80-
"",
81-
create_source_xref(
82-
self.env.docname, crate_mod.path_str, text="[View Source]"
83-
),
78+
signature += create_source_xref(
79+
self.env.docname,
80+
crate_mod.path_str,
81+
text="[source]",
82+
classes=["viewcode-link"],
8483
)
8584

8685
if crate_mod and crate_mod.docstring:

‎python/sphinx_rust/directives/module.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@ def run(self) -> list[nodes.Node]:
6464
self.rust_domain.note_object(module.path_str, "module", node_id, signature)
6565

6666
if self.rust_config.rust_viewcode and module and module.file:
67-
root += nodes.paragraph(
68-
"",
69-
"",
70-
create_source_xref(
71-
self.env.docname, module.path_str, text="[View Source]"
72-
),
67+
signature += create_source_xref(
68+
self.env.docname,
69+
module.path_str,
70+
text="[source]",
71+
classes=["viewcode-link"],
7372
)
7473

7574
if module.docstring:

‎python/sphinx_rust/domain.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def create_code_pages(crate_name: str, srcdir: Path, cache: Path) -> None:
246246
code_folder = srcdir.joinpath("api", "crates", crate_name, "code")
247247
code_folder.mkdir(exist_ok=True, parents=True)
248248
for full_name, file_path in modules:
249-
rel_path = os.path.relpath(file_path, code_folder)
249+
rel_path = os.path.relpath(Path(file_path), code_folder)
250250
# note, this is available only in Python 3.12+
251251
# rel_path = Path(file_path).relative_to(code_folder, walk_up=True)
252252
# TODO only write the file if it doesn't exist or is different

0 commit comments

Comments
 (0)