Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
987c7315
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
987c7315
编写于
6月 20, 2022
作者:
G
Guillaume Gomez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Integrate `generate_macro_def_id_path` into `href_with_root_path`
上级
f4db07ed
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
72 addition
and
68 deletion
+72
-68
src/librustdoc/html/format.rs
src/librustdoc/html/format.rs
+70
-1
src/librustdoc/html/highlight.rs
src/librustdoc/html/highlight.rs
+2
-67
未找到文件。
src/librustdoc/html/format.rs
浏览文件 @
987c7315
...
...
@@ -8,14 +8,16 @@
use
std
::
borrow
::
Cow
;
use
std
::
cell
::
Cell
;
use
std
::
fmt
;
use
std
::
iter
;
use
std
::
iter
::{
self
,
once
}
;
use
rustc_ast
as
ast
;
use
rustc_attr
::{
ConstStability
,
StabilityLevel
};
use
rustc_data_structures
::
captures
::
Captures
;
use
rustc_data_structures
::
fx
::
FxHashSet
;
use
rustc_hir
as
hir
;
use
rustc_hir
::
def
::
DefKind
;
use
rustc_hir
::
def_id
::
DefId
;
use
rustc_metadata
::
creader
::{
CStore
,
LoadedMacro
};
use
rustc_middle
::
ty
;
use
rustc_middle
::
ty
::
DefIdTree
;
use
rustc_middle
::
ty
::
TyCtxt
;
...
...
@@ -557,6 +559,71 @@ pub(crate) fn join_with_double_colon(syms: &[Symbol]) -> String {
s
}
/// This function is to get the external macro path because they are not in the cache used in
/// `href_with_root_path`.
fn
generate_macro_def_id_path
(
def_id
:
DefId
,
cx
:
&
Context
<
'_
>
,
root_path
:
Option
<&
str
>
,
)
->
(
String
,
ItemType
,
Vec
<
Symbol
>
)
{
let
tcx
=
cx
.shared.tcx
;
let
crate_name
=
tcx
.crate_name
(
def_id
.krate
)
.to_string
();
let
cache
=
cx
.cache
();
let
fqp
:
Vec
<
Symbol
>
=
tcx
.def_path
(
def_id
)
.data
.into_iter
()
.filter_map
(|
elem
|
{
// extern blocks (and a few others things) have an empty name.
match
elem
.data
.get_opt_name
()
{
Some
(
s
)
if
!
s
.is_empty
()
=>
Some
(
s
),
_
=>
None
,
}
})
.collect
();
let
relative
=
fqp
.iter
()
.map
(|
elem
|
elem
.to_string
());
// Check to see if it is a macro 2.0 or built-in macro.
// More information in <https://rust-lang.github.io/rfcs/1584-macros.html>.
let
is_macro_2
=
match
CStore
::
from_tcx
(
tcx
)
.load_macro_untracked
(
def_id
,
tcx
.sess
)
{
LoadedMacro
::
MacroDef
(
def
,
_
)
=>
{
// If `ast_def.macro_rules` is `true`, then it's not a macro 2.0.
matches!
(
&
def
.kind
,
ast
::
ItemKind
::
MacroDef
(
ast_def
)
if
!
ast_def
.macro_rules
)
}
_
=>
false
,
};
let
mut
path
=
if
is_macro_2
{
once
(
crate_name
.clone
())
.chain
(
relative
)
.collect
()
}
else
{
vec!
[
crate_name
.clone
(),
relative
.last
()
.unwrap
()]
};
if
path
.len
()
<
2
{
// The minimum we can have is the crate name followed by the macro name. If shorter, then
// it means that that `relative` was empty, which is an error.
panic!
(
"macro path cannot be empty!"
);
}
if
let
Some
(
last
)
=
path
.last_mut
()
{
*
last
=
format!
(
"macro.{}.html"
,
last
);
}
let
url
=
match
cache
.extern_locations
[
&
def_id
.krate
]
{
ExternalLocation
::
Remote
(
ref
s
)
=>
{
// `ExternalLocation::Remote` always end with a `/`.
format!
(
"{}{}"
,
s
,
path
.join
(
"/"
))
}
ExternalLocation
::
Local
=>
{
// `root_path` always end with a `/`.
format!
(
"{}{}/{}"
,
root_path
.unwrap_or
(
""
),
crate_name
,
path
.join
(
"/"
))
}
ExternalLocation
::
Unknown
=>
{
panic!
(
"crate {} not in cache when linkifying macros"
,
crate_name
)
}
};
(
url
,
ItemType
::
Macro
,
fqp
)
}
pub
(
crate
)
fn
href_with_root_path
(
did
:
DefId
,
cx
:
&
Context
<
'_
>
,
...
...
@@ -612,6 +679,8 @@ fn to_module_fqp(shortty: ItemType, fqp: &[Symbol]) -> &[Symbol] {
ExternalLocation
::
Unknown
=>
return
Err
(
HrefError
::
DocumentationNotBuilt
),
},
)
}
else
if
matches!
(
def_kind
,
DefKind
::
Macro
(
_
))
{
return
Ok
(
generate_macro_def_id_path
(
did
,
cx
,
root_path
));
}
else
{
return
Err
(
HrefError
::
NotInExternalCache
);
}
...
...
src/librustdoc/html/highlight.rs
浏览文件 @
987c7315
...
...
@@ -5,19 +5,15 @@
//!
//! Use the `render_with_highlighting` to highlight some rust code.
use
crate
::
clean
::
{
ExternalLocation
,
PrimitiveType
}
;
use
crate
::
clean
::
PrimitiveType
;
use
crate
::
html
::
escape
::
Escape
;
use
crate
::
html
::
render
::
Context
;
use
std
::
collections
::
VecDeque
;
use
std
::
fmt
::{
Display
,
Write
};
use
std
::
iter
::
once
;
use
rustc_ast
as
ast
;
use
rustc_data_structures
::
fx
::
FxHashMap
;
use
rustc_hir
::
def_id
::
DefId
;
use
rustc_lexer
::{
LiteralKind
,
TokenKind
};
use
rustc_metadata
::
creader
::{
CStore
,
LoadedMacro
};
use
rustc_span
::
edition
::
Edition
;
use
rustc_span
::
symbol
::
Symbol
;
use
rustc_span
::{
BytePos
,
Span
,
DUMMY_SP
};
...
...
@@ -784,17 +780,8 @@ fn string_without_closing_tag<T: Display>(
.map
(|
s
|
format!
(
"{}{}"
,
href_context
.root_path
,
s
)),
LinkFromSrc
::
External
(
def_id
)
=>
{
format
::
href_with_root_path
(
*
def_id
,
context
,
Some
(
href_context
.root_path
))
.map
(|(
url
,
_
,
_
)|
url
)
.or_else
(|
e
|
{
if
e
==
format
::
HrefError
::
NotInExternalCache
&&
matches!
(
klass
,
Class
::
Macro
(
_
))
{
Ok
(
generate_macro_def_id_path
(
href_context
,
*
def_id
))
}
else
{
Err
(
e
)
}
})
.ok
()
.map
(|(
url
,
_
,
_
)|
url
)
}
LinkFromSrc
::
Primitive
(
prim
)
=>
format
::
href_with_root_path
(
PrimitiveType
::
primitive_locations
(
context
.tcx
())[
prim
],
...
...
@@ -814,57 +801,5 @@ fn string_without_closing_tag<T: Display>(
Some
(
"</span>"
)
}
/// This function is to get the external macro path because they are not in the cache used in
/// `href_with_root_path`.
fn
generate_macro_def_id_path
(
href_context
:
&
HrefContext
<
'_
,
'_
,
'_
>
,
def_id
:
DefId
)
->
String
{
let
tcx
=
href_context
.context.shared.tcx
;
let
crate_name
=
tcx
.crate_name
(
def_id
.krate
)
.to_string
();
let
cache
=
href_context
.context
.cache
();
let
relative
=
tcx
.def_path
(
def_id
)
.data
.into_iter
()
.filter_map
(|
elem
|
{
// extern blocks have an empty name
let
s
=
elem
.data
.to_string
();
if
!
s
.is_empty
()
{
Some
(
s
)
}
else
{
None
}
});
// Check to see if it is a macro 2.0 or built-in macro.
// More information in <https://rust-lang.github.io/rfcs/1584-macros.html>.
let
is_macro_2
=
match
CStore
::
from_tcx
(
tcx
)
.load_macro_untracked
(
def_id
,
tcx
.sess
)
{
LoadedMacro
::
MacroDef
(
def
,
_
)
=>
{
// If `ast_def.macro_rules` is `true`, then it's not a macro 2.0.
matches!
(
&
def
.kind
,
ast
::
ItemKind
::
MacroDef
(
ast_def
)
if
!
ast_def
.macro_rules
)
}
_
=>
false
,
};
let
mut
path
=
if
is_macro_2
{
once
(
crate_name
.clone
())
.chain
(
relative
)
.collect
()
}
else
{
vec!
[
crate_name
.clone
(),
relative
.last
()
.unwrap
()]
};
if
path
.len
()
<
2
{
// The minimum we can have is the crate name followed by the macro name. If shorter, then
// it means that that `relative` was empty, which is an error.
panic!
(
"macro path cannot be empty!"
);
}
if
let
Some
(
last
)
=
path
.last_mut
()
{
*
last
=
format!
(
"macro.{}.html"
,
last
);
}
match
cache
.extern_locations
[
&
def_id
.krate
]
{
ExternalLocation
::
Remote
(
ref
s
)
=>
{
// `ExternalLocation::Remote` always end with a `/`.
format!
(
"{}{}"
,
s
,
path
.join
(
"/"
))
}
ExternalLocation
::
Local
=>
{
// `href_context.root_path` always end with a `/`.
format!
(
"{}{}/{}"
,
href_context
.root_path
,
crate_name
,
path
.join
(
"/"
))
}
ExternalLocation
::
Unknown
=>
{
panic!
(
"crate {} not in cache when linkifying macros"
,
crate_name
)
}
}
}
#[cfg(test)]
mod
tests
;
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录