Skip to content

Rust: skip private items when extracting library files #19581

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
60 changes: 58 additions & 2 deletions rust/extractor/src/translate/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ra_ap_ide_db::RootDatabase;
use ra_ap_ide_db::line_index::{LineCol, LineIndex};
use ra_ap_parser::SyntaxKind;
use ra_ap_span::TextSize;
use ra_ap_syntax::ast::{Const, Fn, HasName, Param, Static};
use ra_ap_syntax::ast::{Const, Fn, HasName, HasVisibility, Param, Static};
use ra_ap_syntax::{
AstNode, NodeOrToken, SyntaxElementChildren, SyntaxError, SyntaxNode, SyntaxToken, TextRange,
ast,
Expand Down Expand Up @@ -272,7 +272,7 @@ impl<'a> Translator<'a> {
) {
for child in children {
if let NodeOrToken::Token(token) = child {
if token.kind() == SyntaxKind::COMMENT {
if token.kind() == SyntaxKind::COMMENT && self.source_kind == SourceKind::Source {
let label = self.trap.emit(generated::Comment {
id: TrapId::Star,
parent: parent_label,
Expand Down Expand Up @@ -655,6 +655,9 @@ impl<'a> Translator<'a> {
pub(crate) fn should_be_excluded(&self, item: &impl ast::AstNode) -> bool {
if self.source_kind == SourceKind::Library {
let syntax = item.syntax();
if syntax.kind() == SyntaxKind::TOKEN_TREE {
return true;
}
if syntax
.parent()
.and_then(Fn::cast)
Expand Down Expand Up @@ -687,6 +690,59 @@ impl<'a> Translator<'a> {
{
return true;
}

if ast::AnyHasVisibility::cast(syntax.clone())
.and_then(|x| x.visibility())
.is_none()
{
match syntax.kind() {
SyntaxKind::ENUM
| SyntaxKind::STATIC
| SyntaxKind::STRUCT
| SyntaxKind::TRAIT
| SyntaxKind::TRAIT_ALIAS
| SyntaxKind::UNION => return true,
SyntaxKind::CONST | SyntaxKind::FN | SyntaxKind::TYPE_ALIAS => {
// check for enclosing source file
if syntax.parent().and_then(ast::SourceFile::cast).is_some() {
return true;
}
// check for enclosing module
if syntax
.parent()
.and_then(ast::ItemList::cast)
.and_then(|x| x.syntax().parent())
.and_then(ast::Module::cast)
.is_some()
{
return true;
}
// check for enclosing extern block
if syntax
.parent()
.and_then(ast::ExternItemList::cast)
.is_some()
{
return true;
}
// check for enclosing block expr
if syntax.parent().and_then(ast::BlockExpr::cast).is_some() {
return true;
}
// check for enclosing non-trait impl
if syntax
.parent()
.and_then(ast::AssocItemList::cast)
.and_then(|x| x.syntax().parent())
.and_then(ast::Impl::cast)
.is_some_and(|imp| imp.trait_().is_none())
{
return true;
}
}
_ => {}
}
}
}
false
}
Expand Down