Skip to main content

diffr_plugin_sdk/
types.rs

1//! The records of `wit/plugin.wit`'s `types` interface. They are generated
2//! from the WIT itself by [`crate::bindings`], so the contract has one
3//! definition of each record and nothing to keep in step: a plugin receives
4//! and returns exactly these, natively and in a component.
5//!
6//! This module adds only what the WIT cannot say: [`ROOT`], the id that names
7//! the file rather than a region, and the impls below.
8pub use crate::bindings::diffr::plugin::types::{
9    Annotation, Cut, FileEntry, FileRef, FileSides, FileStatus, Kind, Leaf, Move, Position,
10    QuerySource, Range, Region, Side, Source, SourceSides, Span, Visibility,
11};
12
13use crate::tree::Pairing;
14
15/// The id that names the file itself rather than a region.
16pub const ROOT: u32 = 0;
17
18impl FileEntry {
19    /// The sides the file has, as the pairing the rest of the SDK speaks.
20    pub fn sides(&self) -> Pairing<&FileRef> {
21        match &self.file {
22            FileSides::Both((lhs, rhs)) => Pairing::Both { lhs, rhs },
23            FileSides::LeftOnly(lhs) => Pairing::LeftOnly { lhs },
24            FileSides::RightOnly(rhs) => Pairing::RightOnly { rhs },
25        }
26    }
27
28    /// The side the file is named and shown by: the after side, or the
29    /// before side for a deleted file.
30    pub fn side(&self) -> &FileRef {
31        match &self.file {
32            FileSides::Both((_, rhs)) | FileSides::RightOnly(rhs) => rhs,
33            FileSides::LeftOnly(lhs) => lhs,
34        }
35    }
36
37    /// [`FileEntry::side`]'s path, the path to show the file under.
38    pub fn path(&self) -> &str {
39        &self.side().path
40    }
41}
42
43impl Range {
44    /// The lines this range touches, half-open. A range ending at column
45    /// zero does not touch its end line.
46    pub fn lines(&self) -> std::ops::Range<u32> {
47        let end = if self.end.column == 0 {
48            self.end.line
49        } else {
50            self.end.line + 1
51        };
52        self.start.line..end
53    }
54}
55
56/// An open region with no label, the state every region starts in.
57impl Default for Visibility {
58    fn default() -> Self {
59        Self {
60            collapsed: false,
61            label: String::new(),
62        }
63    }
64}