-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show full path when hovering over includes
- Loading branch information
Showing
2 changed files
with
105 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use crate::range::RangeExt; | ||
use crate::syntax::*; | ||
use crate::workspace::*; | ||
use futures_boxed::boxed; | ||
use lsp_types::*; | ||
|
||
#[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
pub struct LatexIncludeHoverProvider; | ||
|
||
impl FeatureProvider for LatexIncludeHoverProvider { | ||
type Params = TextDocumentPositionParams; | ||
type Output = Option<Hover>; | ||
|
||
#[boxed] | ||
async fn execute<'a>( | ||
&'a self, | ||
request: &'a FeatureRequest<TextDocumentPositionParams>, | ||
) -> Option<Hover> { | ||
let (range, targets) = Self::find_include(request)?; | ||
for target in targets { | ||
if let Some(document) = request.workspace().find(&target) { | ||
let path = document.uri.to_file_path().ok()?; | ||
return Some(Hover { | ||
range: Some(range), | ||
contents: HoverContents::Markup(MarkupContent { | ||
kind: MarkupKind::PlainText, | ||
value: path.to_string_lossy().into_owned(), | ||
}), | ||
}); | ||
} | ||
} | ||
None | ||
} | ||
} | ||
|
||
impl LatexIncludeHoverProvider { | ||
fn find_include( | ||
request: &FeatureRequest<TextDocumentPositionParams>, | ||
) -> Option<(Range, &[Uri])> { | ||
if let SyntaxTree::Latex(tree) = &request.document().tree { | ||
for include in &tree.includes { | ||
for (i, path) in include.paths().iter().enumerate() { | ||
let range = path.range(); | ||
if range.contains(request.params.position) { | ||
return Some((range, &include.all_targets[i])); | ||
} | ||
} | ||
} | ||
} | ||
None | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::range::RangeExt; | ||
|
||
#[test] | ||
fn test_multiple_paths() { | ||
let hover = test_feature( | ||
LatexIncludeHoverProvider, | ||
FeatureSpec { | ||
files: vec![ | ||
FeatureSpec::file("foo.tex", "\\include{bar, baz}"), | ||
FeatureSpec::file("bar.tex", ""), | ||
FeatureSpec::file("baz.tex", ""), | ||
], | ||
main_file: "foo.tex", | ||
position: Position::new(0, 16), | ||
..FeatureSpec::default() | ||
}, | ||
); | ||
|
||
assert_eq!( | ||
hover, | ||
Some(Hover { | ||
contents: HoverContents::Markup(MarkupContent { | ||
kind: MarkupKind::PlainText, | ||
value: FeatureSpec::uri("baz.tex").to_file_path().unwrap().to_string_lossy().into_owned(), | ||
}), | ||
range: Some(Range::new_simple(0, 14, 0, 17)), | ||
}) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_empty() { | ||
let hover = test_feature( | ||
LatexIncludeHoverProvider, | ||
FeatureSpec { | ||
files: vec![ | ||
FeatureSpec::file("foo.tex", "") | ||
], | ||
main_file: "foo.tex", | ||
position: Position::new(0, 0), | ||
..FeatureSpec::default() | ||
}, | ||
); | ||
assert_eq!(hover, None); | ||
} | ||
} |
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