sync_lsp/workspace/symbol.rs
1//! Implementation of the `workspace/symbol` request.
2//!
3//! # Usage
4//! In the language server protocol symbols are defined as any kind of symbol used in a
5//! programming language. These symbols are commonly used resolve their specific
6//! location in a source file. They can either be queried for a specific file or,
7//! like in this case, for the whole workspace using [`Server::on_symbol`].
8
9use crate::TypeProvider;
10use crate::text_document::Location;
11use crate::{Server, connection::Endpoint};
12use crate::connection::Callback;
13use serde::{Deserialize, Serialize};
14use serde_repr::Serialize_repr;
15
16#[derive(Default, Clone)]
17pub(crate) struct SymbolOptions;
18
19#[derive(Deserialize)]
20#[serde(rename_all = "camelCase")]
21struct WorkspaceSymbolParams {
22 query: String
23}
24
25/// Denotes any kind of symbol used in a programming language, for example variables, functions, classes, interfaces etc.
26#[derive(Serialize, Debug)]
27#[serde(rename_all = "camelCase")]
28pub struct SymbolInformation {
29 /// The name of this symbol as it should appear in a user interface.
30 pub name: String,
31 /// A variant of [`SymbolKind`]
32 pub kind: SymbolKind,
33 /// The location of this symbol in the source code. Optionally this location
34 /// may contain more than just the symbol itself, like visibility modifiers.
35 pub location: Location,
36 /// A optional name of another symbol containing this one.
37 /// For example the name of a class containing this symbol as a method.
38 pub container_name: Option<String>
39}
40
41/// This enum contain various kinds of symbols, which are mainly used
42/// in the user interface and attached to [`SymbolInformation`]
43#[repr(i32)]
44#[derive(Serialize_repr, Debug)]
45pub enum SymbolKind {
46 File = 1,
47 Module = 2,
48 Namespace = 3,
49 Package = 4,
50 Class = 5,
51 Method = 6,
52 Property = 7,
53 Field = 8,
54 Constructor = 9,
55 Enum = 10,
56 Interface = 11,
57 Function = 12,
58 Variable = 13,
59 Constant = 14,
60 String = 15,
61 Number = 16,
62 Boolean = 17,
63 Array = 18,
64}
65
66impl SymbolOptions {
67
68 pub(crate) const METHOD: &'static str = "workspace/symbol";
69
70 pub(super) fn endpoint<T: TypeProvider>() -> Endpoint<T, SymbolOptions> {
71 Endpoint::new(Callback::request(|_, _: WorkspaceSymbolParams| Vec::<SymbolInformation>::new()))
72 }
73}
74
75impl<T: TypeProvider> Server<T> {
76
77 /// Sets the callback that will be used to [resolve symbols](self) in a workspace.
78 ///
79 /// # Argument
80 /// * `callback` - A callback which is called with the following parameters as soon as the corresponding request is received:
81 /// * The server instance receiving the response.
82 /// * A possibly empty query string that is used to filter the symbols.
83
84 pub fn on_symbol(&mut self, callback: fn(&mut Server<T>, String) -> Vec<SymbolInformation>) {
85 self.workspace.symbol.set_callback(Callback::request(move |server, params: WorkspaceSymbolParams| {
86 callback(server, params.query)
87 }))
88 }
89}