oak_lsp/
types.rs

1use oak_core::language::UniversalElementRole;
2use serde::{Deserialize, Serialize};
3
4pub use core::range::Range;
5pub use oak_folding::{FoldingRange, FoldingRangeKind};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8pub struct Position {
9    pub line: u32,
10    pub character: u32,
11}
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
14pub struct LspRange {
15    pub start: Position,
16    pub end: Position,
17}
18
19/// Represents a location inside a resource.
20/// Can be either byte-based (Rust model) or line/column based (LSP model).
21#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
22#[serde(bound(serialize = "R: Serialize", deserialize = "R: Deserialize<'de>"))]
23pub struct Location<R = Range<usize>> {
24    pub uri: String,
25    pub range: R,
26}
27
28/// A specialized location type for byte-based ranges.
29#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
30pub struct LocationRange {
31    pub uri: String,
32    #[serde(with = "oak_core::serde_range")]
33    pub range: Range<usize>,
34}
35
36impl From<LocationRange> for Location<Range<usize>> {
37    fn from(loc: LocationRange) -> Self {
38        Self { uri: loc.uri, range: loc.range }
39    }
40}
41
42impl From<Location<Range<usize>>> for LocationRange {
43    fn from(loc: Location<Range<usize>>) -> Self {
44        Self { uri: loc.uri, range: loc.range }
45    }
46}
47
48/// A document highlight is a range inside a text document which deserves
49/// special attention. Usually a document highlight is visualized by changing
50/// the background color of its range.
51#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
52pub enum DocumentHighlightKind {
53    Text = 1,
54    Read = 2,
55    Write = 3,
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
59pub struct DocumentHighlight {
60    pub range: LspRange,
61    pub kind: Option<DocumentHighlightKind>,
62}
63
64/// Represents a color in RGBA space.
65#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
66pub struct Color {
67    pub red: f32,
68    pub green: f32,
69    pub blue: f32,
70    pub alpha: f32,
71}
72
73/// Represents a color range from a document.
74#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
75pub struct ColorInformation {
76    pub range: LspRange,
77    pub color: Color,
78}
79
80/// Represents hover information.
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct Hover {
83    /// The hover's content as a markdown string.
84    pub contents: String,
85    /// An optional span to which this hover applies.
86    #[serde(with = "oak_core::serde_range::option")]
87    pub range: Option<Range<usize>>,
88}
89
90/// Represents an item in the document structure (e.g., in an outline or breadcrumbs).
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct StructureItem {
93    /// The name of this item (e.g., function name, class name).
94    pub name: String,
95    /// More detail about this item (e.g., function signature, type).
96    pub detail: Option<String>,
97    /// The universal role of this element.
98    pub role: UniversalElementRole,
99    /// The symbol kind.
100    pub kind: SymbolKind,
101    /// The range of the entire element in the source code.
102    #[serde(with = "oak_core::serde_range")]
103    pub range: Range<usize>,
104    /// The range that should be selected when clicking on this item.
105    /// Usually the range of the identifier.
106    #[serde(with = "oak_core::serde_range")]
107    pub selection_range: Range<usize>,
108    /// Whether this item is deprecated.
109    pub deprecated: bool,
110    /// Nested structure items (e.g., methods within a class).
111    pub children: Vec<StructureItem>,
112}
113
114/// Parameters for the `initialize` request.
115#[derive(Debug, Clone, Serialize, Deserialize, Default)]
116pub struct InitializeParams {
117    pub root_uri: Option<String>,
118    pub workspace_folders: Vec<WorkspaceFolder>,
119}
120
121/// A workspace folder.
122#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct WorkspaceFolder {
124    pub uri: String,
125    pub name: String,
126}
127
128/// Represents a symbol kind.
129#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
130pub enum SymbolKind {
131    File = 1,
132    Module = 2,
133    Namespace = 3,
134    Package = 4,
135    Class = 5,
136    Method = 6,
137    Property = 7,
138    Field = 8,
139    Constructor = 9,
140    Enum = 10,
141    Interface = 11,
142    Function = 12,
143    Variable = 13,
144    Constant = 14,
145    String = 15,
146    Number = 16,
147    Boolean = 17,
148    Array = 18,
149    Object = 19,
150    Key = 20,
151    Null = 21,
152    EnumMember = 22,
153    Struct = 23,
154    Event = 24,
155    Operator = 25,
156    TypeParameter = 26,
157}
158
159/// Represents a workspace symbol.
160#[derive(Debug, Clone, Serialize, Deserialize)]
161pub struct WorkspaceSymbol {
162    /// The name of the symbol.
163    pub name: String,
164    /// The kind of the symbol.
165    pub kind: SymbolKind,
166    /// The location of the symbol.
167    pub location: LocationRange,
168    /// The name of the container this symbol is in.
169    pub container_name: Option<String>,
170}
171
172/// Represents information about a symbol (e.g., function, variable, class).
173#[derive(Debug, Clone, Serialize, Deserialize)]
174pub struct SymbolInformation {
175    /// The name of the symbol.
176    pub name: String,
177    /// The kind of the symbol.
178    pub kind: SymbolKind,
179    /// The location of the symbol.
180    pub location: LocationRange,
181    /// The name of the container this symbol is in.
182    pub container_name: Option<String>,
183}
184
185/// Represents a change to the workspace.
186#[derive(Debug, Clone, Default, Serialize, Deserialize)]
187pub struct WorkspaceEdit {
188    /// The changes to the workspace.
189    pub changes: std::collections::HashMap<String, Vec<TextEdit>>,
190}
191
192/// Represents a text edit.
193#[derive(Debug, Clone, Serialize, Deserialize)]
194pub struct TextEdit {
195    /// The range of the text edit.
196    #[serde(with = "oak_core::serde_range")]
197    pub range: Range<usize>,
198    /// The new text.
199    pub new_text: String,
200}
201
202/// Represents a completion item.
203#[derive(Debug, Clone, Serialize, Deserialize)]
204pub struct CompletionItem {
205    /// The label of the completion item.
206    pub label: String,
207    /// The kind of the completion item.
208    pub kind: Option<CompletionItemKind>,
209    /// A human-readable string with additional information about this item.
210    pub detail: Option<String>,
211    /// A human-readable string that contains documentation about this item.
212    pub documentation: Option<String>,
213    /// The text that should be inserted when selecting this completion item.
214    pub insert_text: Option<String>,
215}
216
217/// Represents a completion item kind.
218#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
219pub enum CompletionItemKind {
220    Text = 1,
221    Method = 2,
222    Function = 3,
223    Constructor = 4,
224    Field = 5,
225    Variable = 6,
226    Class = 7,
227    Interface = 8,
228    Module = 9,
229    Property = 10,
230    Unit = 11,
231    Value = 12,
232    Enum = 13,
233    Keyword = 14,
234    Snippet = 15,
235    Color = 16,
236    File = 17,
237    Reference = 18,
238    Folder = 19,
239    EnumMember = 20,
240    Constant = 21,
241    Struct = 22,
242    Event = 23,
243    Operator = 24,
244    TypeParameter = 25,
245}
246
247/// Represents a diagnostic.
248#[derive(Debug, Clone, Serialize, Deserialize)]
249pub struct Diagnostic {
250    /// The range of the diagnostic.
251    #[serde(with = "oak_core::serde_range")]
252    pub range: Range<usize>,
253    /// The severity of the diagnostic.
254    pub severity: Option<DiagnosticSeverity>,
255    /// The diagnostic's code.
256    pub code: Option<String>,
257    /// The source of the diagnostic.
258    pub source: Option<String>,
259    /// The diagnostic's message.
260    pub message: String,
261}
262
263/// Represents a diagnostic severity.
264#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
265pub enum DiagnosticSeverity {
266    Error = 1,
267    Warning = 2,
268    Information = 3,
269    Hint = 4,
270}