-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include theorem environments in completion
- Loading branch information
Showing
4 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ pub mod include; | |
pub mod kernel; | ||
pub mod label; | ||
pub mod symbol; | ||
pub mod theorem; | ||
pub mod tikz; | ||
pub mod user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::completion::factory::{self, LatexComponentId}; | ||
use crate::completion::latex::combinators; | ||
use crate::feature::{FeatureProvider, FeatureRequest}; | ||
use crate::syntax::SyntaxTree; | ||
use futures_boxed::boxed; | ||
use lsp_types::{CompletionItem, CompletionParams, TextEdit}; | ||
|
||
#[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
pub struct LatexTheoremEnvironmentCompletionProvider; | ||
|
||
impl FeatureProvider for LatexTheoremEnvironmentCompletionProvider { | ||
type Params = CompletionParams; | ||
type Output = Vec<CompletionItem>; | ||
|
||
#[boxed] | ||
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output { | ||
combinators::environment(request, async move |_, name_range| { | ||
let mut items = Vec::new(); | ||
for document in request.related_documents() { | ||
if let SyntaxTree::Latex(tree) = &document.tree { | ||
for theorem in &tree.theorem_definitions { | ||
let name = theorem.name().text().to_owned(); | ||
let text_edit = TextEdit::new(name_range, name.clone().into()); | ||
let item = factory::environment( | ||
request, | ||
name.into(), | ||
text_edit, | ||
&LatexComponentId::User, | ||
); | ||
items.push(item); | ||
} | ||
} | ||
} | ||
items | ||
}) | ||
.await | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::feature::{test_feature, FeatureSpec}; | ||
use lsp_types::{Position, Range}; | ||
use std::borrow::Cow; | ||
|
||
#[test] | ||
fn test() { | ||
let items = test_feature( | ||
LatexTheoremEnvironmentCompletionProvider, | ||
FeatureSpec { | ||
files: vec![FeatureSpec::file( | ||
"foo.tex", | ||
"\\newtheorem{theorem}{Theorem}\n\\begin{th}", | ||
)], | ||
main_file: "foo.tex", | ||
position: Position::new(1, 8), | ||
..FeatureSpec::default() | ||
}, | ||
); | ||
assert_eq!(items.len(), 1); | ||
assert_eq!(items[0].label, Cow::from("theorem")); | ||
assert_eq!( | ||
items[0].text_edit.as_ref().map(|edit| edit.range), | ||
Some(Range::new_simple(1, 7, 1, 9)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters