Skip to content
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

document symbols numbering as in toc #1251

Merged
merged 2 commits into from
Oct 26, 2024
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
36 changes: 36 additions & 0 deletions crates/base-db/src/semantics/auxiliary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use syntax::latex::{self, HasCurly};
#[derive(Debug, Clone, Default)]
pub struct Semantics {
pub label_numbers: FxHashMap<String, String>,
pub section_numbers: FxHashMap<String, String>,
}

impl Semantics {
Expand All @@ -18,6 +19,9 @@ impl Semantics {
if let Some(label_number) = latex::LabelNumber::cast(node.clone()) {
self.process_label_number(&label_number);
}
if let Some(toc_line) = latex::TocContentsLine::cast(node.clone()) {
self.process_toc_line(&toc_line);
}
}

fn process_label_number(&mut self, label_number: &latex::LabelNumber) -> Option<()> {
Expand All @@ -40,4 +44,36 @@ impl Semantics {
self.label_numbers.insert(name, text);
Some(())
}

fn process_toc_line(&mut self, toc_line: &latex::TocContentsLine) -> Option<()> {
let unit = toc_line.unit().and_then(|u| u.content_text())?.to_string();

if [
"part",
"chapter",
"section",
"subsection",
"subsubsection",
"paragraph",
"subparagraph",
]
.contains(&unit.as_str())
{
let line = toc_line.line()?;
let name = line.syntax().children().find_map(|child| {
latex::Text::cast(child.clone())?;
Some(child)
})?;
let name = name.to_string();

let num_line = line
.syntax()
.children()
.find_map(|child| latex::TocNumberLine::cast(child.clone()))?;
let number = num_line.number()?;
let number = number.content_text()?.replace(['{', '}'], "");
self.section_numbers.insert(name, number);
}
Some(())
}
}
34 changes: 34 additions & 0 deletions crates/parser/src/latex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ impl<'a> Parser<'a> {
CommandName::VerbatimBlock => self.verbatim_block(),
CommandName::GraphicsPath => self.graphics_path(),
CommandName::BibItem => self.bibitem(),
CommandName::TocContentsLine => self.toc_contents_line(),
CommandName::TocNumberLine => self.toc_number_line(),
},
}
}
Expand Down Expand Up @@ -1261,6 +1263,38 @@ impl<'a> Parser<'a> {

self.builder.finish_node();
}

fn toc_contents_line(&mut self) {
self.builder.start_node(TOC_CONTENTS_LINE.into());
self.eat();
self.trivia();

if self.lexer.peek() == Some(Token::LCurly) {
self.curly_group();
}

if self.lexer.peek() == Some(Token::LCurly) {
self.curly_group();
}

if self.lexer.peek() == Some(Token::LCurly) {
self.curly_group();
}

self.builder.finish_node();
}

fn toc_number_line(&mut self) {
self.builder.start_node(TOC_NUMBER_LINE.into());
self.eat();
self.trivia();

if self.lexer.peek() == Some(Token::LCurly) {
self.curly_group();
}

self.builder.finish_node();
}
}

pub fn parse_latex(text: &str, config: &SyntaxConfig) -> GreenNode {
Expand Down
2 changes: 2 additions & 0 deletions crates/parser/src/latex/lexer/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ pub fn classify(name: &str, config: &SyntaxConfig) -> CommandName {
"fi" => CommandName::EndBlockComment,
"verb" => CommandName::VerbatimBlock,
"bibitem" => CommandName::BibItem,
"contentsline" => CommandName::TocContentsLine,
"numberline" => CommandName::TocNumberLine,

_ if config.citation_commands.contains(name) => CommandName::Citation,
_ if config.label_definition_commands.contains(name) => CommandName::LabelDefinition,
Expand Down
2 changes: 2 additions & 0 deletions crates/parser/src/latex/lexer/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ pub enum CommandName {
EndBlockComment,
VerbatimBlock,
BibItem,
TocContentsLine,
TocNumberLine,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
Expand Down
72 changes: 36 additions & 36 deletions crates/symbols/src/document/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,44 +281,44 @@ fn test_section() {
check(
&fixture,
expect![[r#"
[
Symbol {
name: "Foo",
kind: Section,
label: None,
full_range: 43..56,
selection_range: 43..56,
children: [],
},
Symbol {
name: "2 Bar",
kind: Section,
label: Some(
Span(
"sec:bar",
71..86,
[
Symbol {
name: "1 Foo",
kind: Section,
label: None,
full_range: 43..56,
selection_range: 43..56,
children: [],
},
Symbol {
name: "2 Bar",
kind: Section,
label: Some(
Span(
"sec:bar",
71..86,
),
),
),
full_range: 58..119,
selection_range: 71..86,
children: [
Symbol {
name: "Baz",
kind: Section,
label: Some(
Span(
"sec:baz",
104..119,
full_range: 58..119,
selection_range: 71..86,
children: [
Symbol {
name: "Baz",
kind: Section,
label: Some(
Span(
"sec:baz",
104..119,
),
),
),
full_range: 88..119,
selection_range: 104..119,
children: [],
},
],
},
]
"#]],
full_range: 88..119,
selection_range: 104..119,
children: [],
},
],
},
]
"#]],
);
}

Expand Down
25 changes: 16 additions & 9 deletions crates/symbols/src/document/tex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,14 @@ impl<'a> SymbolBuilder<'a> {
let group_text = group.content_text()?;
let kind = SymbolKind::Section;

let symbol = match self.find_label(section.syntax()) {
Some(label) => {
let name = match self.find_label_number(&label.text) {
Some(number) => format!("{number} {group_text}"),
None => group_text,
};
let name = match self.find_section_number(&group_text) {
Some(number) => format!("{number} {group_text}"),
None => group_text,
};

Symbol::new_label(name, kind, range, label)
}
None => Symbol::new_simple(group_text, kind, range, range),
let symbol = match self.find_label(section.syntax()) {
Some(label) => Symbol::new_label(name, kind, range, label),
None => Symbol::new_simple(name, kind, range, range)
};

Some(symbol)
Expand Down Expand Up @@ -247,6 +245,15 @@ impl<'a> SymbolBuilder<'a> {
Some(Span { text, range })
}

fn find_section_number(&self, name: &str) -> Option<&str> {
self.project
.documents
.iter()
.filter_map(|document| document.data.as_aux())
.find_map(|data| data.semantics.section_numbers.get(name))
.map(String::as_str)
}

fn find_label_number(&self, name: &str) -> Option<&str> {
self.project
.documents
Expand Down
28 changes: 28 additions & 0 deletions crates/syntax/src/latex/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,3 +762,31 @@ impl BibItem {
self.syntax().children().find_map(CurlyGroupWord::cast)
}
}

cst_node!(TocContentsLine, TOC_CONTENTS_LINE);

impl TocContentsLine {
pub fn command(&self) -> Option<SyntaxToken> {
self.syntax().first_token()
}

pub fn unit(&self) -> Option<CurlyGroup> {
self.syntax().children().find_map(CurlyGroup::cast)
}

pub fn line(&self) -> Option<CurlyGroup> {
self.syntax().children().filter_map(CurlyGroup::cast).nth(1)
}
}

cst_node!(TocNumberLine, TOC_NUMBER_LINE);

impl TocNumberLine {
pub fn command(&self) -> Option<SyntaxToken> {
self.syntax().first_token()
}

pub fn number(&self) -> Option<CurlyGroup> {
self.syntax().children().find_map(CurlyGroup::cast)
}
}
2 changes: 2 additions & 0 deletions crates/syntax/src/latex/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pub enum SyntaxKind {
GRAPHICS_PATH,
BLOCK_COMMENT,
BIBITEM,
TOC_CONTENTS_LINE,
TOC_NUMBER_LINE,
ROOT,
}

Expand Down