-
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.
Refactor "textDocument/documentHighlight" request
- Loading branch information
Showing
9 changed files
with
363 additions
and
45 deletions.
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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use crate::{ | ||
feature::{FeatureProvider, FeatureRequest}, | ||
protocol::{DocumentHighlight, DocumentHighlightKind, RangeExt, TextDocumentPositionParams}, | ||
syntax::{latex, LatexLabelKind, SyntaxNode}, | ||
workspace::DocumentContent, | ||
}; | ||
use futures_boxed::boxed; | ||
|
||
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)] | ||
pub struct LatexLabelHighlightProvider; | ||
|
||
impl FeatureProvider for LatexLabelHighlightProvider { | ||
type Params = TextDocumentPositionParams; | ||
type Output = Vec<DocumentHighlight>; | ||
|
||
#[boxed] | ||
async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output { | ||
let mut highlights = Vec::new(); | ||
if let DocumentContent::Latex(table) = &req.current().content { | ||
if let Some(name) = table | ||
.labels | ||
.iter() | ||
.flat_map(|label| label.names(&table.tree)) | ||
.find(|label| label.range().contains(req.params.position)) | ||
.map(latex::Token::text) | ||
{ | ||
for label_group in &table.labels { | ||
for label in label_group.names(&table.tree) { | ||
if label.text() == name { | ||
let kind = match label_group.kind { | ||
LatexLabelKind::Definition => DocumentHighlightKind::Write, | ||
LatexLabelKind::Reference(_) => DocumentHighlightKind::Read, | ||
}; | ||
|
||
let highlight = DocumentHighlight { | ||
range: label.range(), | ||
kind: Some(kind), | ||
}; | ||
highlights.push(highlight); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
highlights | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::{feature::FeatureTester, protocol::Range}; | ||
use indoc::indoc; | ||
|
||
#[tokio::test] | ||
async fn has_label() { | ||
let actual_highlights = FeatureTester::new() | ||
.file( | ||
"foo.tex", | ||
indoc!( | ||
r#" | ||
\label{foo} | ||
\ref{foo} | ||
"# | ||
), | ||
) | ||
.main("foo.tex") | ||
.position(0, 7) | ||
.test_position(LatexLabelHighlightProvider) | ||
.await; | ||
|
||
let expected_highlights = vec![ | ||
DocumentHighlight { | ||
range: Range::new_simple(0, 7, 0, 10), | ||
kind: Some(DocumentHighlightKind::Write), | ||
}, | ||
DocumentHighlight { | ||
range: Range::new_simple(1, 5, 1, 8), | ||
kind: Some(DocumentHighlightKind::Read), | ||
}, | ||
]; | ||
|
||
assert_eq!(actual_highlights, expected_highlights); | ||
} | ||
|
||
#[tokio::test] | ||
async fn no_label_latex() { | ||
let actual_highlights = FeatureTester::new() | ||
.file("foo.tex", "") | ||
.main("foo.tex") | ||
.position(0, 0) | ||
.test_position(LatexLabelHighlightProvider) | ||
.await; | ||
|
||
assert!(actual_highlights.is_empty()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn no_label_bibtex() { | ||
let actual_highlights = FeatureTester::new() | ||
.file("foo.bib", "") | ||
.main("foo.bib") | ||
.position(0, 0) | ||
.test_position(LatexLabelHighlightProvider) | ||
.await; | ||
|
||
assert!(actual_highlights.is_empty()); | ||
} | ||
} |
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,39 @@ | ||
mod latex_label; | ||
|
||
use self::latex_label::LatexLabelHighlightProvider; | ||
use crate::{ | ||
feature::{ConcatProvider, FeatureProvider, FeatureRequest}, | ||
protocol::{DocumentHighlight, TextDocumentPositionParams}, | ||
}; | ||
use futures_boxed::boxed; | ||
|
||
pub struct HighlightProvider { | ||
provider: ConcatProvider<TextDocumentPositionParams, DocumentHighlight>, | ||
} | ||
|
||
impl HighlightProvider { | ||
pub fn new() -> Self { | ||
Self { | ||
provider: ConcatProvider::new(vec![Box::new(LatexLabelHighlightProvider)]), | ||
} | ||
} | ||
} | ||
|
||
impl Default for HighlightProvider { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl FeatureProvider for HighlightProvider { | ||
type Params = TextDocumentPositionParams; | ||
type Output = Vec<DocumentHighlight>; | ||
|
||
#[boxed] | ||
async fn execute<'a>( | ||
&'a self, | ||
request: &'a FeatureRequest<TextDocumentPositionParams>, | ||
) -> Vec<DocumentHighlight> { | ||
self.provider.execute(request).await | ||
} | ||
} |
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
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
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,69 @@ | ||
use indoc::indoc; | ||
use texlab::{ | ||
protocol::*, | ||
test::{TestBedBuilder, PULL_CAPABILITIES}, | ||
}; | ||
|
||
#[tokio::test] | ||
async fn empty_document() { | ||
let mut test_bed = TestBedBuilder::new().file("main.tex", "").build().await; | ||
test_bed.spawn(); | ||
test_bed.initialize(PULL_CAPABILITIES.clone()).await; | ||
test_bed.open("main.tex").await; | ||
|
||
let actual_highlights = test_bed.document_highlight("main.tex", 0, 0).await.unwrap(); | ||
|
||
test_bed.shutdown().await; | ||
|
||
assert!(actual_highlights.is_empty()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn label() { | ||
let mut test_bed = TestBedBuilder::new() | ||
.file( | ||
"main.tex", | ||
indoc!( | ||
r#" | ||
\label{foo} | ||
\ref{foo} | ||
"# | ||
), | ||
) | ||
.build() | ||
.await; | ||
test_bed.spawn(); | ||
test_bed.initialize(PULL_CAPABILITIES.clone()).await; | ||
test_bed.open("main.tex").await; | ||
|
||
let actual_highlights = test_bed.document_highlight("main.tex", 0, 7).await.unwrap(); | ||
|
||
let expected_highlights = vec![ | ||
DocumentHighlight { | ||
range: Range::new_simple(0, 7, 0, 10), | ||
kind: Some(DocumentHighlightKind::Write), | ||
}, | ||
DocumentHighlight { | ||
range: Range::new_simple(1, 5, 1, 8), | ||
kind: Some(DocumentHighlightKind::Read), | ||
}, | ||
]; | ||
|
||
test_bed.shutdown().await; | ||
|
||
assert_eq!(actual_highlights, expected_highlights); | ||
} | ||
|
||
#[tokio::test] | ||
async fn unknown_file() { | ||
let mut test_bed = TestBedBuilder::new().build().await; | ||
|
||
test_bed.spawn(); | ||
test_bed.initialize(PULL_CAPABILITIES.clone()).await; | ||
|
||
let actual_highlights = test_bed.document_highlight("main.tex", 0, 0).await; | ||
|
||
test_bed.shutdown().await; | ||
|
||
assert_eq!(actual_highlights, None); | ||
} |
Oops, something went wrong.