diff options
Diffstat (limited to 'support/texlab/src/features/completion/citation.rs')
-rw-r--r-- | support/texlab/src/features/completion/citation.rs | 209 |
1 files changed, 14 insertions, 195 deletions
diff --git a/support/texlab/src/features/completion/citation.rs b/support/texlab/src/features/completion/citation.rs index 92b21f0ffc..e6436689d7 100644 --- a/support/texlab/src/features/completion/citation.rs +++ b/support/texlab/src/features/completion/citation.rs @@ -42,7 +42,7 @@ pub fn complete_citations<'a>( .children() .filter_map(bibtex::Entry::cast) { - if let Some(item) = make_item(document, entry, range) { + if let Some(item) = make_item(document, &entry, range) { items.push(item); } } @@ -61,7 +61,9 @@ fn check_citation(context: &CursorContext<CompletionParams>) -> Option<()> { fn check_acronym(context: &CursorContext<CompletionParams>) -> Option<()> { let token = context.cursor.as_latex()?; - let pair = token.ancestors().find_map(latex::KeyValuePair::cast)?; + let pair = token + .parent_ancestors() + .find_map(latex::KeyValuePair::cast)?; if pair.key()?.to_string() != "cite" { return None; } @@ -70,16 +72,18 @@ fn check_acronym(context: &CursorContext<CompletionParams>) -> Option<()> { Some(()) } -fn make_item( - document: &Document, - entry: bibtex::Entry, +fn make_item<'a>( + document: &'a Document, + entry: &bibtex::Entry, range: TextRange, -) -> Option<InternalCompletionItem> { +) -> Option<InternalCompletionItem<'a>> { let key = entry.name_token()?.to_string(); let ty = LANGUAGE_DATA .find_entry_type(&entry.type_token()?.text()[1..]) - .map(|ty| Structure::Entry(ty.category)) - .unwrap_or_else(|| Structure::Entry(BibtexEntryTypeCategory::Misc)); + .map_or_else( + || Structure::Entry(BibtexEntryTypeCategory::Misc), + |ty| Structure::Entry(ty.category), + ); let entry_code = entry.syntax().text().to_string(); let text = format!( @@ -88,8 +92,8 @@ fn make_item( WHITESPACE_REGEX .replace_all( &entry_code - .replace('{', "") - .replace('}', "") + .replace('{', " ") + .replace('}', " ") .replace(',', " ") .replace('=', " "), " " @@ -109,188 +113,3 @@ fn make_item( } static WHITESPACE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new("\\s+").unwrap()); - -#[cfg(test)] -mod tests { - use crate::features::testing::FeatureTester; - - use super::*; - - #[test] - fn test_empty_latex_document() { - let request = FeatureTester::builder() - .files(vec![("main.tex", "")]) - .main("main.tex") - .line(0) - .character(0) - .build() - .completion(); - - let context = CursorContext::new(request); - let mut actual_items = Vec::new(); - complete_citations(&context, &mut actual_items); - - assert!(actual_items.is_empty()); - } - - #[test] - fn test_empty_bibtex_document() { - let request = FeatureTester::builder() - .files(vec![("main.bib", "")]) - .main("main.bib") - .line(0) - .character(0) - .build() - .completion(); - - let context = CursorContext::new(request); - let mut actual_items = Vec::new(); - complete_citations(&context, &mut actual_items); - - assert!(actual_items.is_empty()); - } - - #[test] - fn test_latex_simple() { - let request = FeatureTester::builder() - .files(vec![ - ("main.tex", "\\addbibresource{main.bib}\n\\cite{}"), - ("main.bib", "@article{foo,}"), - ]) - .main("main.tex") - .line(1) - .character(6) - .build() - .completion(); - - let context = CursorContext::new(request); - let mut actual_items = Vec::new(); - complete_citations(&context, &mut actual_items); - - assert!(!actual_items.is_empty()); - for item in actual_items { - assert_eq!(item.range, TextRange::new(32.into(), 32.into())); - } - } - - #[test] - fn test_latex_two_words() { - let request = FeatureTester::builder() - .files(vec![ - ("main.tex", "\\addbibresource{main.bib}\n\\cite{foo}"), - ("main.bib", "@article{foo bar,}"), - ]) - .main("main.tex") - .line(1) - .character(6) - .build() - .completion(); - - let context = CursorContext::new(request); - let mut actual_items = Vec::new(); - complete_citations(&context, &mut actual_items); - - assert!(!actual_items.is_empty()); - for item in actual_items { - assert_eq!(item.range, TextRange::new(32.into(), 35.into())); - } - } - - #[test] - fn test_latex_open_brace() { - let request = FeatureTester::builder() - .files(vec![ - ("main.tex", "\\addbibresource{main.bib}\n\\cite{"), - ("main.bib", "@article{foo,}"), - ]) - .main("main.tex") - .line(1) - .character(6) - .build() - .completion(); - - let context = CursorContext::new(request); - let mut actual_items = Vec::new(); - complete_citations(&context, &mut actual_items); - - assert!(!actual_items.is_empty()); - for item in actual_items { - assert_eq!(item.range, TextRange::new(32.into(), 32.into())); - } - } - - #[test] - fn test_latex_open_brace_second() { - let request = FeatureTester::builder() - .files(vec![ - ("main.tex", "\\addbibresource{main.bib}\n\\cite{foo,a"), - ("main.bib", "@article{foo,}"), - ]) - .main("main.tex") - .line(1) - .character(10) - .build() - .completion(); - - let context = CursorContext::new(request); - let mut actual_items = Vec::new(); - complete_citations(&context, &mut actual_items); - - assert!(!actual_items.is_empty()); - for item in actual_items { - assert_eq!(item.range, TextRange::new(36.into(), 37.into())); - } - } - - #[test] - fn test_latex_acronym() { - let request = FeatureTester::builder() - .files(vec![ - ( - "main.tex", - "\\addbibresource{main.bib}\n\\DeclareAcronym{foo}{cite={\n}}", - ), - ("main.bib", "@article{foo,}"), - ]) - .main("main.tex") - .line(2) - .character(0) - .build() - .completion(); - - let context = CursorContext::new(request); - let mut actual_items = Vec::new(); - complete_citations(&context, &mut actual_items); - - assert!(!actual_items.is_empty()); - for item in actual_items { - assert_eq!(item.range, TextRange::new(54.into(), 54.into())); - } - } - - #[test] - fn test_latex_acronym_two_words() { - let request = FeatureTester::builder() - .files(vec![ - ( - "main.tex", - "\\addbibresource{main.bib}\n\\DeclareAcronym{foo}{cite={\na b}}", - ), - ("main.bib", "@article{foo,}"), - ]) - .main("main.tex") - .line(2) - .character(0) - .build() - .completion(); - - let context = CursorContext::new(request); - let mut actual_items = Vec::new(); - complete_citations(&context, &mut actual_items); - - assert!(!actual_items.is_empty()); - for item in actual_items { - assert_eq!(item.range, TextRange::new(54.into(), 57.into())); - } - } -} |