-
Notifications
You must be signed in to change notification settings - Fork 111
/
types.rs
284 lines (244 loc) · 7.23 KB
/
types.rs
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use jsonrpc_core::{Call, Output, Params};
use lsp_types::{DiagnosticSeverity, Position, Range, SemanticTokenModifier};
use serde::{de::Error as SerdeError, Deserialize, Deserializer, Serialize};
use serde_json::Value;
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::Display;
use std::io::Error;
pub enum Void {}
// Configuration
#[derive(Clone, Deserialize, Debug)]
pub struct Config {
pub language: HashMap<String, LanguageConfig>,
#[serde(default)]
pub server: ServerConfig,
#[serde(default)]
pub verbosity: u8,
#[serde(default)]
pub snippet_support: bool,
#[serde(default, deserialize_with = "deserialize_semantic_tokens")]
pub semantic_tokens: Vec<SemanticTokenConfig>,
}
#[derive(Clone, Deserialize, Debug)]
pub struct DynamicConfig {
#[serde(default)]
pub language: HashMap<String, DynamicLanguageConfig>,
}
#[derive(Clone, Deserialize, Debug)]
pub struct ServerConfig {
#[serde(default)]
pub session: String,
#[serde(default)]
pub timeout: u64,
}
#[derive(Clone, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct LanguageConfig {
pub filetypes: Vec<String>,
pub roots: Vec<String>,
pub command: String,
#[serde(default)]
pub args: Vec<String>,
pub settings_section: Option<String>,
pub settings: Option<Value>,
#[serde(default = "default_offset_encoding")]
pub offset_encoding: OffsetEncoding,
pub workaround_server_sends_plaintext_labeled_as_markdown: Option<bool>,
}
#[derive(Clone, Deserialize, Debug)]
pub struct DynamicLanguageConfig {
pub settings: Option<Value>,
}
impl Default for ServerConfig {
fn default() -> Self {
ServerConfig {
session: String::new(),
timeout: 0,
}
}
}
fn default_offset_encoding() -> OffsetEncoding {
OffsetEncoding::Utf16
}
#[derive(Clone, Deserialize, Debug)]
pub struct SemanticTokenConfig {
pub token: String,
pub face: String,
#[serde(default)]
pub modifiers: Vec<SemanticTokenModifier>,
}
fn deserialize_semantic_tokens<'de, D>(
deserializer: D,
) -> Result<Vec<SemanticTokenConfig>, D::Error>
where
D: Deserializer<'de>,
{
HashMap::<String, String>::deserialize(deserializer).map(|xs| {
xs.into_iter().map(|(k, face)| {
let mut token_mods = k.split('+').map(String::from);
let token = token_mods.next().unwrap_or_default();
let modifiers = token_mods.map(SemanticTokenModifier::from).collect::<Vec<_>>();
SemanticTokenConfig {
token, face, modifiers
}
}).collect()
}).map_err(|e| {
D::Error::custom(e.to_string() + "\nSee https://github.com/kak-lsp/kak-lsp#semantic-tokens for the new configuration syntax for semantic tokens\n")
})
}
// Editor
#[derive(Clone, Debug, Deserialize)]
pub struct EditorMeta {
pub session: String,
pub client: Option<String>,
pub buffile: String,
pub filetype: String,
pub version: i32,
pub fifo: Option<String>,
}
pub type EditorParams = toml::Value;
#[derive(Clone, Debug, Deserialize)]
pub struct EditorRequest {
#[serde(flatten)]
pub meta: EditorMeta,
pub method: String,
pub params: EditorParams,
pub ranges: Option<Vec<Range>>,
}
#[derive(Deserialize)]
pub struct EditorResponse {
pub meta: EditorMeta,
pub command: Cow<'static, str>,
}
pub type SessionId = String;
pub type LanguageId = String;
pub type RootPath = String;
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub struct Route {
pub session: SessionId,
pub language: LanguageId,
pub root: RootPath,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct EditorCompletion {
pub offset: u32,
}
#[derive(Deserialize, Debug)]
pub struct TextDocumentDidOpenParams {
pub draft: String,
}
#[derive(Deserialize, Debug)]
pub struct TextDocumentDidChangeParams {
pub draft: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct TextDocumentCompletionParams {
pub position: KakounePosition,
pub completion: EditorCompletion,
pub have_kakoune_feature_filtertext: bool,
}
#[derive(Clone, Copy, Deserialize, Debug)]
pub struct PositionParams {
pub position: KakounePosition,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CallHierarchyParams {
pub position: KakounePosition,
pub incoming_or_outgoing: bool,
}
#[derive(Clone, Copy, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CodeActionsParams {
pub position: KakounePosition,
pub perform_code_action: bool,
}
#[derive(Clone, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct NextOrPrevSymbolParams {
pub position: KakounePosition,
/// Match any of these kinds of symbols, or any symbol if empty.
pub symbol_kinds: Vec<String>,
/// If true then searches forward ("next")
/// otherwise searches backward ("previous")
pub search_next: bool,
/// If true, don't navigate to the next/previous symbol but show its hover
/// otherwise goto the next/previous symbol
#[serde(default)]
pub hover: bool,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TextDocumentRenameParams {
pub position: KakounePosition,
pub new_name: String,
}
// Language Server
// XXX serde(untagged) ?
#[derive(Debug)]
pub enum ServerMessage {
Request(Call),
Response(Output),
}
pub trait IntoParams {
fn into_params(self) -> Result<Params, Error>;
}
impl<T> IntoParams for T
where
T: Serialize,
{
fn into_params(self) -> Result<Params, Error> {
let json_value = serde_json::to_value(self)?;
let params = match json_value {
Value::Null => Params::None,
Value::Bool(_) | Value::Number(_) | Value::String(_) => Params::Array(vec![json_value]),
Value::Array(vec) => Params::Array(vec),
Value::Object(map) => Params::Map(map),
};
Ok(params)
}
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct KakounePosition {
pub line: u32,
pub column: u32, // in bytes, not chars!!!
}
#[derive(Clone, Debug)]
pub struct HoverModal {
pub context: String,
pub do_after: String,
}
#[derive(Debug, PartialEq)]
pub struct KakouneRange {
pub start: KakounePosition,
pub end: KakounePosition,
}
impl Display for KakounePosition {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}.{}", self.line, self.column)
}
}
impl Display for KakouneRange {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{},{}", self.start, self.end)
}
}
/// Represents how language server interprets LSP's `Position.character`
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum OffsetEncoding {
/// UTF-8 code units aka bytes
#[serde(rename = "utf-8")]
Utf8,
/// UTF-16 code units
#[serde(rename = "utf-16")]
Utf16,
}
// An intermediate representation of the diagnostics on a line, for use with inlay diagnostics
pub struct LineDiagnostics {
pub range_end: Position,
pub symbols: String,
pub text: String,
pub text_face: &'static str,
pub text_severity: Option<DiagnosticSeverity>,
}