use crate::domain::{LineNumber, RepoPath};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SearchHit {
path: RepoPath,
line: Option<LineNumber>,
preview: String,
}
impl SearchHit {
#[must_use]
pub fn file(path: RepoPath) -> Self {
Self {
path,
line: None,
preview: String::new(),
}
}
#[must_use]
pub fn content(path: RepoPath, line: LineNumber, preview: String) -> Self {
Self {
path,
line: Some(line),
preview,
}
}
#[must_use]
pub fn path(&self) -> &RepoPath {
&self.path
}
#[must_use]
pub fn line(&self) -> Option<LineNumber> {
self.line
}
#[must_use]
pub fn preview(&self) -> &str {
&self.preview
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TextFileDocument {
lines: Vec<String>,
state: TextFileState,
}
#[derive(Clone, Debug, Eq, PartialEq)]
enum TextFileState {
Exact(String),
DisplayOnly,
Truncated,
}
impl TextFileDocument {
#[must_use]
pub fn exact(source: String) -> Self {
Self {
lines: source.lines().map(ToOwned::to_owned).collect(),
state: TextFileState::Exact(source),
}
}
#[must_use]
pub fn display_only(lines: Vec<String>) -> Self {
Self {
lines,
state: TextFileState::DisplayOnly,
}
}
#[must_use]
pub fn truncated(lines: Vec<String>) -> Self {
Self {
lines,
state: TextFileState::Truncated,
}
}
#[must_use]
pub fn lines(&self) -> &[String] {
&self.lines
}
#[must_use]
pub fn source(&self) -> Option<&str> {
match &self.state {
TextFileState::Exact(source) => Some(source),
TextFileState::DisplayOnly | TextFileState::Truncated => None,
}
}
#[must_use]
pub fn is_truncated(&self) -> bool {
matches!(self.state, TextFileState::Truncated)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum FileDocument {
Text(TextFileDocument),
Binary {
summary: String,
},
Symlink {
target: String,
},
Unavailable {
summary: String,
},
}
impl FileDocument {
#[must_use]
pub fn exact_text(source: impl Into<String>) -> Self {
Self::Text(TextFileDocument::exact(source.into()))
}
#[must_use]
pub fn display_text(lines: Vec<String>) -> Self {
Self::Text(TextFileDocument::display_only(lines))
}
#[must_use]
pub fn truncated_text(lines: Vec<String>) -> Self {
Self::Text(TextFileDocument::truncated(lines))
}
#[must_use]
pub fn lines(&self) -> &[String] {
match self {
Self::Text(document) => document.lines(),
Self::Binary { .. } | Self::Symlink { .. } | Self::Unavailable { .. } => &[],
}
}
#[must_use]
pub fn source(&self) -> Option<&str> {
match self {
Self::Text(document) => document.source(),
Self::Binary { .. } | Self::Symlink { .. } | Self::Unavailable { .. } => None,
}
}
#[must_use]
pub fn message(&self) -> Option<&str> {
match self {
Self::Binary { summary } => Some(summary),
Self::Symlink { target } => Some(target),
Self::Unavailable { summary } => Some(summary),
Self::Text(_) => None,
}
}
#[must_use]
pub fn is_truncated(&self) -> bool {
matches!(self, Self::Text(document) if document.is_truncated())
}
}
#[cfg(test)]
mod tests {
use super::{FileDocument, TextFileDocument};
#[test]
fn incomplete_or_lossy_text_is_not_an_lsp_document() {
for document in [
FileDocument::Text(TextFileDocument::truncated(vec!["partial".to_owned()])),
FileDocument::Text(TextFileDocument::display_only(vec![
"replacement �".to_owned(),
])),
] {
assert!(document.source().is_none());
}
}
}