Skip to main content

rust_analyzer/cli/lsif/
vendored.rs

1//! Types of Language Server Index Format (LSIF). LSIF is a standard format
2//! for language servers or other programming tools to dump their knowledge
3//! about a workspace.
4//!
5//! Based on <https://microsoft.github.io/language-server-protocol/specifications/lsif/0.6.0/specification/>
6
7#![allow(unused, unreachable_pub, reason = "implementing the full spec is fine")]
8
9use lsp_types::{Range, Uri};
10use serde::{Deserialize, Serialize};
11
12pub type Id = lsp_types::Code;
13
14#[derive(Debug, PartialEq, Serialize, Deserialize)]
15#[serde(untagged)]
16pub enum LocationOrRangeId {
17    Location(lsp_types::Location),
18    RangeId(Id),
19}
20
21#[derive(Debug, PartialEq, Serialize, Deserialize)]
22#[serde(rename_all = "camelCase")]
23pub struct Entry {
24    pub id: Id,
25    #[serde(flatten)]
26    pub data: Element,
27}
28
29#[derive(Debug, PartialEq, Serialize, Deserialize)]
30#[serde(rename_all = "camelCase")]
31#[serde(tag = "type")]
32pub enum Element {
33    Vertex(Vertex),
34    Edge(Edge),
35}
36
37#[derive(Debug, PartialEq, Serialize, Deserialize)]
38pub struct ToolInfo {
39    pub name: String,
40    #[serde(default = "Default::default")]
41    #[serde(skip_serializing_if = "Vec::is_empty")]
42    pub args: Vec<String>,
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub version: Option<String>,
45}
46
47#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Copy)]
48pub enum Encoding {
49    /// Currently only 'utf-16' is supported due to the limitations in LSP.
50    #[serde(rename = "utf-16")]
51    Utf16,
52}
53
54#[derive(Debug, PartialEq, Serialize, Deserialize)]
55pub struct RangeBasedDocumentSymbol {
56    pub id: Id,
57    #[serde(default = "Default::default")]
58    #[serde(skip_serializing_if = "Vec::is_empty")]
59    pub children: Vec<RangeBasedDocumentSymbol>,
60}
61
62#[derive(Debug, PartialEq, Serialize, Deserialize)]
63#[serde(rename_all = "camelCase")]
64#[serde(untagged)]
65pub enum DocumentSymbolOrRangeBasedVec {
66    DocumentSymbol(Vec<lsp_types::DocumentSymbol>),
67    RangeBased(Vec<RangeBasedDocumentSymbol>),
68}
69
70#[derive(Debug, PartialEq, Serialize, Deserialize)]
71#[serde(rename_all = "camelCase")]
72pub struct DefinitionTag {
73    /// The text covered by the range
74    text: String,
75    /// The symbol kind.
76    kind: lsp_types::SymbolKind,
77    /// Indicates if this symbol is deprecated.
78    #[serde(default)]
79    #[serde(skip_serializing_if = "std::ops::Not::not")]
80    deprecated: bool,
81    /// The full range of the definition not including leading/trailing whitespace but everything else, e.g comments and code.
82    /// The range must be included in fullRange.
83    full_range: Range,
84    /// Optional detail information for the definition.
85    #[serde(skip_serializing_if = "Option::is_none")]
86    detail: Option<String>,
87}
88
89#[derive(Debug, PartialEq, Serialize, Deserialize)]
90#[serde(rename_all = "camelCase")]
91pub struct DeclarationTag {
92    /// The text covered by the range
93    text: String,
94    /// The symbol kind.
95    kind: lsp_types::SymbolKind,
96    /// Indicates if this symbol is deprecated.
97    #[serde(default)]
98    deprecated: bool,
99    /// The full range of the definition not including leading/trailing whitespace but everything else, e.g comments and code.
100    /// The range must be included in fullRange.
101    full_range: Range,
102    /// Optional detail information for the definition.
103    #[serde(skip_serializing_if = "Option::is_none")]
104    detail: Option<String>,
105}
106
107#[derive(Debug, PartialEq, Serialize, Deserialize)]
108#[serde(rename_all = "camelCase")]
109pub struct ReferenceTag {
110    text: String,
111}
112
113#[derive(Debug, PartialEq, Serialize, Deserialize)]
114#[serde(rename_all = "camelCase")]
115pub struct UnknownTag {
116    text: String,
117}
118
119#[derive(Debug, PartialEq, Serialize, Deserialize)]
120#[serde(rename_all = "camelCase")]
121#[serde(tag = "type")]
122pub enum RangeTag {
123    Definition(DefinitionTag),
124    Declaration(DeclarationTag),
125    Reference(ReferenceTag),
126    Unknown(UnknownTag),
127}
128
129#[derive(Debug, PartialEq, Serialize, Deserialize)]
130#[serde(rename_all = "camelCase")]
131#[serde(tag = "label")]
132pub enum Vertex {
133    MetaData(MetaData),
134    /// <https://github.com/Microsoft/language-server-protocol/blob/master/indexFormat/specification.md#the-project-vertex>
135    Project(Project),
136    Document(Document),
137    /// <https://github.com/Microsoft/language-server-protocol/blob/master/indexFormat/specification.md#ranges>
138    Range {
139        #[serde(flatten)]
140        range: Range,
141        #[serde(skip_serializing_if = "Option::is_none")]
142        tag: Option<RangeTag>,
143    },
144    /// <https://github.com/Microsoft/language-server-protocol/blob/master/indexFormat/specification.md#result-set>
145    ResultSet(ResultSet),
146    Moniker(lsp_types::Moniker),
147    PackageInformation(PackageInformation),
148
149    #[serde(rename = "$event")]
150    Event(Event),
151
152    DefinitionResult,
153    DeclarationResult,
154    TypeDefinitionResult,
155    ReferenceResult,
156    ImplementationResult,
157    FoldingRangeResult {
158        result: Vec<lsp_types::FoldingRange>,
159    },
160    HoverResult {
161        result: lsp_types::Hover,
162    },
163    DocumentSymbolResult {
164        result: DocumentSymbolOrRangeBasedVec,
165    },
166    DocumentLinkResult {
167        result: Vec<lsp_types::DocumentLink>,
168    },
169    DiagnosticResult {
170        result: Vec<lsp_types::Diagnostic>,
171    },
172}
173
174#[derive(Debug, PartialEq, Serialize, Deserialize)]
175#[serde(rename_all = "camelCase")]
176pub enum EventKind {
177    Begin,
178    End,
179}
180
181#[derive(Debug, PartialEq, Serialize, Deserialize)]
182#[serde(rename_all = "camelCase")]
183pub enum EventScope {
184    Document,
185    Project,
186}
187
188#[derive(Debug, PartialEq, Serialize, Deserialize)]
189pub struct Event {
190    pub kind: EventKind,
191    pub scope: EventScope,
192    pub data: Id,
193}
194
195#[derive(Debug, PartialEq, Serialize, Deserialize)]
196#[serde(rename_all = "camelCase")]
197#[serde(tag = "label")]
198pub enum Edge {
199    Contains(EdgeDataMultiIn),
200    Moniker(EdgeData),
201    NextMoniker(EdgeData),
202    Next(EdgeData),
203    PackageInformation(EdgeData),
204    Item(Item),
205
206    // Methods
207    #[serde(rename = "textDocument/definition")]
208    Definition(EdgeData),
209    #[serde(rename = "textDocument/declaration")]
210    Declaration(EdgeData),
211    #[serde(rename = "textDocument/hover")]
212    Hover(EdgeData),
213    #[serde(rename = "textDocument/references")]
214    References(EdgeData),
215    #[serde(rename = "textDocument/implementation")]
216    Implementation(EdgeData),
217    #[serde(rename = "textDocument/typeDefinition")]
218    TypeDefinition(EdgeData),
219    #[serde(rename = "textDocument/foldingRange")]
220    FoldingRange(EdgeData),
221    #[serde(rename = "textDocument/documentLink")]
222    DocumentLink(EdgeData),
223    #[serde(rename = "textDocument/documentSymbol")]
224    DocumentSymbol(EdgeData),
225    #[serde(rename = "textDocument/diagnostic")]
226    Diagnostic(EdgeData),
227}
228
229#[derive(Debug, PartialEq, Serialize, Deserialize)]
230#[serde(rename_all = "camelCase")]
231pub struct EdgeData {
232    pub in_v: Id,
233    pub out_v: Id,
234}
235
236#[derive(Debug, PartialEq, Serialize, Deserialize)]
237#[serde(rename_all = "camelCase")]
238pub struct EdgeDataMultiIn {
239    pub in_vs: Vec<Id>,
240    pub out_v: Id,
241}
242
243#[derive(Debug, PartialEq, Serialize, Deserialize)]
244#[serde(untagged)]
245pub enum DefinitionResultType {
246    Scalar(LocationOrRangeId),
247    Array(LocationOrRangeId),
248}
249
250#[derive(Debug, PartialEq, Serialize, Deserialize)]
251#[serde(rename_all = "camelCase")]
252pub enum ItemKind {
253    Declarations,
254    Definitions,
255    References,
256    ReferenceResults,
257    ImplementationResults,
258}
259
260#[derive(Debug, PartialEq, Serialize, Deserialize)]
261#[serde(rename_all = "camelCase")]
262pub struct Item {
263    pub document: Id,
264    #[serde(skip_serializing_if = "Option::is_none")]
265    pub property: Option<ItemKind>,
266    #[serde(flatten)]
267    pub edge_data: EdgeDataMultiIn,
268}
269
270#[derive(Debug, PartialEq, Serialize, Deserialize)]
271#[serde(rename_all = "camelCase")]
272pub struct Document {
273    pub uri: Uri,
274    pub language_id: String,
275}
276
277/// <https://github.com/Microsoft/language-server-protocol/blob/master/indexFormat/specification.md#result-set>
278#[derive(Debug, PartialEq, Serialize, Deserialize)]
279#[serde(rename_all = "camelCase")]
280pub struct ResultSet {
281    #[serde(skip_serializing_if = "Option::is_none")]
282    pub key: Option<String>,
283}
284
285/// <https://github.com/Microsoft/language-server-protocol/blob/master/indexFormat/specification.md#the-project-vertex>
286#[derive(Debug, PartialEq, Serialize, Deserialize)]
287#[serde(rename_all = "camelCase")]
288pub struct Project {
289    #[serde(skip_serializing_if = "Option::is_none")]
290    pub resource: Option<Uri>,
291    #[serde(skip_serializing_if = "Option::is_none")]
292    pub content: Option<String>,
293    pub kind: String,
294}
295
296#[derive(Debug, PartialEq, Serialize, Deserialize)]
297#[serde(rename_all = "camelCase")]
298pub struct MetaData {
299    /// The version of the LSIF format using semver notation. See <https://semver.org/>. Please note
300    /// the version numbers starting with 0 don't adhere to semver and adopters have to assume
301    /// that each new version is breaking.
302    pub version: String,
303
304    /// The project root (in form of an URI) used to compute this dump.
305    pub project_root: Uri,
306
307    /// The string encoding used to compute line and character values in
308    /// positions and ranges.
309    pub position_encoding: Encoding,
310
311    /// Information about the tool that created the dump
312    #[serde(skip_serializing_if = "Option::is_none")]
313    pub tool_info: Option<ToolInfo>,
314}
315
316#[derive(Debug, PartialEq, Serialize, Deserialize)]
317#[serde(rename_all = "camelCase")]
318pub struct Repository {
319    pub r#type: String,
320    pub url: String,
321    #[serde(skip_serializing_if = "Option::is_none")]
322    pub commit_id: Option<String>,
323}
324
325#[derive(Debug, PartialEq, Serialize, Deserialize)]
326#[serde(rename_all = "camelCase")]
327pub struct PackageInformation {
328    pub name: String,
329    pub manager: String,
330    #[serde(skip_serializing_if = "Option::is_none")]
331    pub uri: Option<Uri>,
332    #[serde(skip_serializing_if = "Option::is_none")]
333    pub content: Option<String>,
334    #[serde(skip_serializing_if = "Option::is_none")]
335    pub repository: Option<Repository>,
336    #[serde(skip_serializing_if = "Option::is_none")]
337    pub version: Option<String>,
338}