Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use crate::{backend::Backend, buffer::Cell};
use crossterm::{
    cursor::{CursorShape, Hide, MoveTo, SetCursorShape, Show},
    execute, queue,
    style::{
        Attribute as CAttribute, Color as CColor, Print, SetAttribute, SetBackgroundColor,
        SetForegroundColor,
    },
    terminal::{self, Clear, ClearType},
};
use helix_view::graphics::{Color, CursorKind, Modifier, Rect};
use std::io::{self, Write};

pub struct CrosstermBackend<W: Write> {
    buffer: W,
}

impl<W> CrosstermBackend<W>
where
    W: Write,
{
    pub fn new(buffer: W) -> CrosstermBackend<W> {
        CrosstermBackend { buffer }
    }
}

impl<W> Write for CrosstermBackend<W>
where
    W: Write,
{
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.buffer.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.buffer.flush()
    }
}

impl<W> Backend for CrosstermBackend<W>
where
    W: Write,
{
    fn draw<'a, I>(&mut self, content: I) -> io::Result<()>
    where
        I: Iterator<Item = (u16, u16, &'a Cell)>,
    {
        let mut fg = Color::Reset;
        let mut bg = Color::Reset;
        let mut modifier = Modifier::empty();
        let mut last_pos: Option<(u16, u16)> = None;
        for (x, y, cell) in content {
            // Move the cursor if the previous location was not (x - 1, y)
            if !matches!(last_pos, Some(p) if x == p.0 + 1 && y == p.1) {
                map_error(queue!(self.buffer, MoveTo(x, y)))?;
            }
            last_pos = Some((x, y));
            if cell.modifier != modifier {
                let diff = ModifierDiff {
                    from: modifier,
                    to: cell.modifier,
                };
                diff.queue(&mut self.buffer)?;
                modifier = cell.modifier;
            }
            if cell.fg != fg {
                let color = CColor::from(cell.fg);
                map_error(queue!(self.buffer, SetForegroundColor(color)))?;
                fg = cell.fg;
            }
            if cell.bg != bg {
                let color = CColor::from(cell.bg);
                map_error(queue!(self.buffer, SetBackgroundColor(color)))?;
                bg = cell.bg;
            }

            map_error(queue!(self.buffer, Print(&cell.symbol)))?;
        }

        map_error(queue!(
            self.buffer,
            SetForegroundColor(CColor::Reset),
            SetBackgroundColor(CColor::Reset),
            SetAttribute(CAttribute::Reset)
        ))
    }

    fn hide_cursor(&mut self) -> io::Result<()> {
        map_error(execute!(self.buffer, Hide))
    }

    fn show_cursor(&mut self, kind: CursorKind) -> io::Result<()> {
        let shape = match kind {
            CursorKind::Block => CursorShape::Block,
            CursorKind::Bar => CursorShape::Line,
            CursorKind::Underline => CursorShape::UnderScore,
            CursorKind::Hidden => unreachable!(),
        };
        map_error(execute!(self.buffer, Show, SetCursorShape(shape)))
    }

    fn get_cursor(&mut self) -> io::Result<(u16, u16)> {
        crossterm::cursor::position()
            .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))
    }

    fn set_cursor(&mut self, x: u16, y: u16) -> io::Result<()> {
        map_error(execute!(self.buffer, MoveTo(x, y)))
    }

    fn clear(&mut self) -> io::Result<()> {
        map_error(execute!(self.buffer, Clear(ClearType::All)))
    }

    fn size(&self) -> io::Result<Rect> {
        let (width, height) =
            terminal::size().map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;

        Ok(Rect::new(0, 0, width, height))
    }

    fn flush(&mut self) -> io::Result<()> {
        self.buffer.flush()
    }
}

fn map_error(error: crossterm::Result<()>) -> io::Result<()> {
    error.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))
}

#[derive(Debug)]
struct ModifierDiff {
    pub from: Modifier,
    pub to: Modifier,
}

impl ModifierDiff {
    fn queue<W>(&self, mut w: W) -> io::Result<()>
    where
        W: io::Write,
    {
        //use crossterm::Attribute;
        let removed = self.from - self.to;
        if removed.contains(Modifier::REVERSED) {
            map_error(queue!(w, SetAttribute(CAttribute::NoReverse)))?;
        }
        if removed.contains(Modifier::BOLD) {
            map_error(queue!(w, SetAttribute(CAttribute::NormalIntensity)))?;
            if self.to.contains(Modifier::DIM) {
                map_error(queue!(w, SetAttribute(CAttribute::Dim)))?;
            }
        }
        if removed.contains(Modifier::ITALIC) {
            map_error(queue!(w, SetAttribute(CAttribute::NoItalic)))?;
        }
        if removed.contains(Modifier::UNDERLINED) {
            map_error(queue!(w, SetAttribute(CAttribute::NoUnderline)))?;
        }
        if removed.contains(Modifier::DIM) {
            map_error(queue!(w, SetAttribute(CAttribute::NormalIntensity)))?;
        }
        if removed.contains(Modifier::CROSSED_OUT) {
            map_error(queue!(w, SetAttribute(CAttribute::NotCrossedOut)))?;
        }
        if removed.contains(Modifier::SLOW_BLINK) || removed.contains(Modifier::RAPID_BLINK) {
            map_error(queue!(w, SetAttribute(CAttribute::NoBlink)))?;
        }

        let added = self.to - self.from;
        if added.contains(Modifier::REVERSED) {
            map_error(queue!(w, SetAttribute(CAttribute::Reverse)))?;
        }
        if added.contains(Modifier::BOLD) {
            map_error(queue!(w, SetAttribute(CAttribute::Bold)))?;
        }
        if added.contains(Modifier::ITALIC) {
            map_error(queue!(w, SetAttribute(CAttribute::Italic)))?;
        }
        if added.contains(Modifier::UNDERLINED) {
            map_error(queue!(w, SetAttribute(CAttribute::Underlined)))?;
        }
        if added.contains(Modifier::DIM) {
            map_error(queue!(w, SetAttribute(CAttribute::Dim)))?;
        }
        if added.contains(Modifier::CROSSED_OUT) {
            map_error(queue!(w, SetAttribute(CAttribute::CrossedOut)))?;
        }
        if added.contains(Modifier::SLOW_BLINK) {
            map_error(queue!(w, SetAttribute(CAttribute::SlowBlink)))?;
        }
        if added.contains(Modifier::RAPID_BLINK) {
            map_error(queue!(w, SetAttribute(CAttribute::RapidBlink)))?;
        }

        Ok(())
    }
}
ated: bool, /// The full range of the definition not including leading/trailing whitespace but everything else, e.g comments and code. /// The range must be included in fullRange. full_range: Range, /// Optional detail information for the definition. #[serde(skip_serializing_if = "Option::is_none")] detail: Option<String>, } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct ReferenceTag { text: String, } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct UnknownTag { text: String, } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] #[serde(tag = "type")] pub enum RangeTag { Definition(DefinitionTag), Declaration(DeclarationTag), Reference(ReferenceTag), Unknown(UnknownTag), } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] #[serde(tag = "label")] pub enum Vertex { MetaData(MetaData), /// <https://github.com/Microsoft/language-server-protocol/blob/master/indexFormat/specification.md#the-project-vertex> Project(Project), Document(Document), /// <https://github.com/Microsoft/language-server-protocol/blob/master/indexFormat/specification.md#ranges> Range { #[serde(flatten)] range: Range, #[serde(skip_serializing_if = "Option::is_none")] tag: Option<RangeTag>, }, /// <https://github.com/Microsoft/language-server-protocol/blob/master/indexFormat/specification.md#result-set> ResultSet(ResultSet), Moniker(crate::Moniker), PackageInformation(PackageInformation), #[serde(rename = "$event")] Event(Event), DefinitionResult, DeclarationResult, TypeDefinitionResult, ReferenceResult, ImplementationResult, FoldingRangeResult { result: Vec<crate::FoldingRange>, }, HoverResult { result: crate::Hover, }, DocumentSymbolResult { result: DocumentSymbolOrRangeBasedVec, }, DocumentLinkResult { result: Vec<crate::DocumentLink>, }, DiagnosticResult { result: Vec<crate::Diagnostic>, }, } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub enum EventKind { Begin, End, } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub enum EventScope { Document, Project, } #[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct Event { pub kind: EventKind, pub scope: EventScope, pub data: Id, } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] #[serde(tag = "label")] pub enum Edge { Contains(EdgeDataMultiIn), Moniker(EdgeData), NextMoniker(EdgeData), Next(EdgeData), PackageInformation(EdgeData), Item(Item), // Methods #[serde(rename = "textDocument/definition")] Definition(EdgeData), #[serde(rename = "textDocument/declaration")] Declaration(EdgeData), #[serde(rename = "textDocument/hover")] Hover(EdgeData), #[serde(rename = "textDocument/references")] References(EdgeData), #[serde(rename = "textDocument/implementation")] Implementation(EdgeData), #[serde(rename = "textDocument/typeDefinition")] TypeDefinition(EdgeData), #[serde(rename = "textDocument/foldingRange")] FoldingRange(EdgeData), #[serde(rename = "textDocument/documentLink")] DocumentLink(EdgeData), #[serde(rename = "textDocument/documentSymbol")] DocumentSymbol(EdgeData), #[serde(rename = "textDocument/diagnostic")] Diagnostic(EdgeData), } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct EdgeData { pub in_v: Id, pub out_v: Id, } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct EdgeDataMultiIn { pub in_vs: Vec<Id>, pub out_v: Id, } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(untagged)] pub enum DefinitionResultType { Scalar(LocationOrRangeId), Array(LocationOrRangeId), } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub enum ItemKind { Declarations, Definitions, References, ReferenceResults, ImplementationResults, } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Item { pub document: Id, #[serde(skip_serializing_if = "Option::is_none")] pub property: Option<ItemKind>, #[serde(flatten)] pub edge_data: EdgeDataMultiIn, } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Document { pub uri: Url, pub language_id: String, } /// <https://github.com/Microsoft/language-server-protocol/blob/master/indexFormat/specification.md#result-set> #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct ResultSet { #[serde(skip_serializing_if = "Option::is_none")] pub key: Option<String>, } /// <https://github.com/Microsoft/language-server-protocol/blob/master/indexFormat/specification.md#the-project-vertex> #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Project { #[serde(skip_serializing_if = "Option::is_none")] pub resource: Option<Url>, #[serde(skip_serializing_if = "Option::is_none")] pub content: Option<String>, pub kind: String, } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct MetaData { /// The version of the LSIF format using semver notation. See <https://semver.org/>. Please note /// the version numbers starting with 0 don't adhere to semver and adopters have to assume /// that each new version is breaking. pub version: String, /// The project root (in form of an URI) used to compute this dump. pub project_root: Url, /// The string encoding used to compute line and character values in /// positions and ranges. pub position_encoding: Encoding, /// Information about the tool that created the dump #[serde(skip_serializing_if = "Option::is_none")] pub tool_info: Option<ToolInfo>, } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Repository { pub r#type: String, pub url: String, #[serde(skip_serializing_if = "Option::is_none")] pub commit_id: Option<String>, } #[derive(Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct PackageInformation { pub name: String, pub manager: String, #[serde(skip_serializing_if = "Option::is_none")] pub uri: Option<Url>, #[serde(skip_serializing_if = "Option::is_none")] pub content: Option<String>, #[serde(skip_serializing_if = "Option::is_none")] pub repository: Option<Repository>, #[serde(skip_serializing_if = "Option::is_none")] pub version: Option<String>, }