summaryrefslogtreecommitdiff
path: root/support/texlab/crates/texlab/src/util/from_proto.rs
blob: e05d317dfe03c4a9730ea638949b0d49b4803e9f (plain)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use base_db::{FeatureParams, Workspace};
use completion::CompletionParams;
use definition::DefinitionParams;
use highlights::HighlightParams;
use hover::HoverParams;
use inlay_hints::InlayHintParams;
use references::ReferenceParams;
use rename::RenameParams;
use rowan::TextSize;

use crate::features::completion::ResolveInfo;

use super::{line_index_ext::LineIndexExt, ClientFlags};

pub fn client_flags(
    capabilities: lsp_types::ClientCapabilities,
    info: Option<lsp_types::ClientInfo>,
) -> ClientFlags {
    let hierarchical_document_symbols = capabilities
        .text_document
        .as_ref()
        .and_then(|cap| cap.document_symbol.as_ref())
        .and_then(|cap| cap.hierarchical_document_symbol_support)
        .unwrap_or(false);

    let completion_markdown = capabilities
        .text_document
        .as_ref()
        .and_then(|cap| cap.completion.as_ref())
        .and_then(|cap| cap.completion_item.as_ref())
        .and_then(|cap| cap.documentation_format.as_ref())
        .map_or(false, |formats| {
            formats.contains(&lsp_types::MarkupKind::Markdown)
        });

    let completion_snippets = capabilities
        .text_document
        .as_ref()
        .and_then(|cap| cap.completion.as_ref())
        .and_then(|cap| cap.completion_item.as_ref())
        .and_then(|cap| cap.snippet_support)
        .unwrap_or(false);

    let completion_kinds = capabilities
        .text_document
        .as_ref()
        .and_then(|cap| cap.completion.as_ref())
        .and_then(|cap| cap.completion_item_kind.as_ref())
        .and_then(|cap| cap.value_set.clone())
        .unwrap_or_default();

    let completion_always_incomplete = info.map_or(false, |info| info.name == "Visual Studio Code");

    let hover_markdown = capabilities
        .text_document
        .as_ref()
        .and_then(|cap| cap.hover.as_ref())
        .and_then(|cap| cap.content_format.as_ref())
        .map_or(false, |formats| {
            formats.contains(&lsp_types::MarkupKind::Markdown)
        });

    let configuration_pull = capabilities
        .workspace
        .as_ref()
        .and_then(|cap| cap.configuration)
        .unwrap_or(false);

    let configuration_push = capabilities
        .workspace
        .as_ref()
        .and_then(|cap| cap.did_change_configuration)
        .and_then(|cap| cap.dynamic_registration)
        .unwrap_or(false);

    let definition_link = capabilities
        .text_document
        .as_ref()
        .and_then(|cap| cap.definition)
        .and_then(|cap| cap.link_support)
        .unwrap_or(false);

    let folding_custom_kinds = capabilities
        .text_document
        .as_ref()
        .and_then(|cap| cap.folding_range.as_ref())
        .and_then(|cap| cap.folding_range_kind.as_ref())
        .and_then(|cap| cap.value_set.as_ref())
        .is_some();

    let progress = capabilities
        .window
        .as_ref()
        .and_then(|cap| cap.work_done_progress)
        .unwrap_or(false);

    ClientFlags {
        hierarchical_document_symbols,
        completion_markdown,
        completion_snippets,
        completion_kinds,
        completion_always_incomplete,
        hover_markdown,
        configuration_pull,
        configuration_push,
        definition_link,
        folding_custom_kinds,
        progress,
    }
}

pub fn rename_params<'a>(
    workspace: &'a Workspace,
    params: lsp_types::TextDocumentPositionParams,
) -> Option<RenameParams<'a>> {
    let (feature, offset) =
        feature_params_offset(workspace, params.text_document, params.position)?;

    Some(RenameParams { feature, offset })
}

pub fn hover_params<'a>(
    workspace: &'a Workspace,
    params: lsp_types::HoverParams,
) -> Option<HoverParams<'a>> {
    let (feature, offset) = feature_params_offset(
        workspace,
        params.text_document_position_params.text_document,
        params.text_document_position_params.position,
    )?;

    Some(HoverParams { feature, offset })
}

pub fn inlay_hint_params<'a>(
    workspace: &'a Workspace,
    params: lsp_types::InlayHintParams,
) -> Option<InlayHintParams> {
    let feature = feature_params(workspace, params.text_document)?;
    let range = feature.document.line_index.offset_lsp_range(params.range)?;
    Some(InlayHintParams { feature, range })
}

pub fn highlight_params<'a>(
    workspace: &'a Workspace,
    params: lsp_types::DocumentHighlightParams,
) -> Option<HighlightParams<'a>> {
    let (feature, offset) = feature_params_offset(
        workspace,
        params.text_document_position_params.text_document,
        params.text_document_position_params.position,
    )?;

    Some(HighlightParams { feature, offset })
}

pub fn definition_params<'a>(
    workspace: &'a Workspace,
    params: lsp_types::GotoDefinitionParams,
) -> Option<DefinitionParams<'a>> {
    let (feature, offset) = feature_params_offset(
        workspace,
        params.text_document_position_params.text_document,
        params.text_document_position_params.position,
    )?;

    Some(DefinitionParams { feature, offset })
}

pub fn completion_params<'a>(
    workspace: &'a Workspace,
    params: lsp_types::CompletionParams,
) -> Option<CompletionParams<'a>> {
    let (feature, offset) = feature_params_offset(
        workspace,
        params.text_document_position.text_document,
        params.text_document_position.position,
    )?;

    Some(CompletionParams { feature, offset })
}

pub fn reference_params<'a>(
    workspace: &'a Workspace,
    params: lsp_types::ReferenceParams,
) -> Option<ReferenceParams<'a>> {
    let (feature, offset) = feature_params_offset(
        workspace,
        params.text_document_position.text_document,
        params.text_document_position.position,
    )?;

    let include_declaration = params.context.include_declaration;
    Some(ReferenceParams {
        feature,
        offset,
        include_declaration,
    })
}

pub fn feature_params<'a>(
    workspace: &'a Workspace,
    text_document: lsp_types::TextDocumentIdentifier,
) -> Option<FeatureParams<'a>> {
    let document = workspace.lookup(&text_document.uri)?;
    Some(FeatureParams::new(workspace, document))
}

pub fn feature_params_offset<'a>(
    workspace: &'a Workspace,
    text_document: lsp_types::TextDocumentIdentifier,
    position: lsp_types::Position,
) -> Option<(FeatureParams<'a>, TextSize)> {
    let feature = feature_params(workspace, text_document)?;
    let offset = feature.document.line_index.offset_lsp(position)?;
    Some((feature, offset))
}

pub fn completion_resolve_info(item: &mut lsp_types::CompletionItem) -> Option<ResolveInfo> {
    item.data
        .take()
        .and_then(|data| serde_json::from_value(data).ok())
}