protobuf-lsp 0.1.4

A Language Server Protocol implementation for Protocol Buffers (protobuf)
Documentation
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
use crate::workspace::WorkspaceManager;
use tower_lsp::lsp_types::*;

/// Token types supported by our semantic tokens provider.
pub const TOKEN_TYPES: &[SemanticTokenType] = &[
    SemanticTokenType::TYPE,           // 0: message names
    SemanticTokenType::ENUM,           // 1: enum names
    SemanticTokenType::ENUM_MEMBER,    // 2: enum values
    SemanticTokenType::INTERFACE,      // 3: service names
    SemanticTokenType::METHOD,         // 4: rpc method names
    SemanticTokenType::PROPERTY,       // 5: field names
    SemanticTokenType::KEYWORD,        // 6: keywords
    SemanticTokenType::NAMESPACE,      // 7: package names
    SemanticTokenType::STRING,         // 8: string literals
    SemanticTokenType::NUMBER,         // 9: number literals
    SemanticTokenType::COMMENT,        // 10: comments
];

/// Token modifiers (none used currently, but required by the protocol).
pub const TOKEN_MODIFIERS: &[SemanticTokenModifier] = &[
    SemanticTokenModifier::DECLARATION,
    SemanticTokenModifier::DEFINITION,
];

const TOKEN_TYPE: u32 = 0;
const TOKEN_ENUM: u32 = 1;
const TOKEN_ENUM_MEMBER: u32 = 2;
const TOKEN_INTERFACE: u32 = 3;
const TOKEN_METHOD: u32 = 4;
const TOKEN_PROPERTY: u32 = 5;
const TOKEN_KEYWORD: u32 = 6;
const TOKEN_NAMESPACE: u32 = 7;
const TOKEN_STRING: u32 = 8;
const TOKEN_NUMBER: u32 = 9;
const TOKEN_COMMENT: u32 = 10;

/// Provide full semantic tokens for a proto file.
pub fn provide_semantic_tokens_full(
    params: SemanticTokensParams,
    workspace: &WorkspaceManager,
    content: Option<&str>,
) -> Option<SemanticTokensResult> {
    let uri = &params.text_document.uri;
    let _proto = workspace.get_file(uri)?;
    let content = content?;

    let mut tokens: Vec<RawToken> = Vec::new();

    for (line_num, line) in content.lines().enumerate() {
        let line_num = line_num as u32;
        let trimmed = line.trim();

        // Comments
        if trimmed.starts_with("//") {
            let start = line.find("//").unwrap_or(0) as u32;
            tokens.push(RawToken {
                line: line_num,
                start,
                length: (line.len() as u32).saturating_sub(start),
                token_type: TOKEN_COMMENT,
                token_modifiers: 0,
            });
            continue;
        }

        // Block comment start (simplified — single-line only)
        if trimmed.starts_with("/*") {
            let start = line.find("/*").unwrap_or(0) as u32;
            tokens.push(RawToken {
                line: line_num,
                start,
                length: (line.len() as u32).saturating_sub(start),
                token_type: TOKEN_COMMENT,
                token_modifiers: 0,
            });
            continue;
        }

        // Tokenize keywords and other elements on this line
        tokenize_line(line, line_num, trimmed, &mut tokens);
    }

    // Sort tokens by position
    tokens.sort_by(|a, b| a.line.cmp(&b.line).then(a.start.cmp(&b.start)));

    // Convert to delta-encoded SemanticTokens
    let data = encode_tokens(&tokens);

    Some(SemanticTokensResult::Tokens(SemanticTokens {
        result_id: None,
        data,
    }))
}

struct RawToken {
    line: u32,
    start: u32,
    length: u32,
    token_type: u32,
    token_modifiers: u32,
}

fn tokenize_line(line: &str, line_num: u32, trimmed: &str, tokens: &mut Vec<RawToken>) {
    // Keywords at the beginning of lines
    // Check for keyword-led lines and tokenize accordingly
    if trimmed.starts_with("syntax") {
        add_keyword_token(line, "syntax", line_num, tokens);
        add_string_literals(line, line_num, tokens);
    } else if trimmed.starts_with("package") {
        add_keyword_token(line, "package", line_num, tokens);
        // Package name
        if let Some(name) = extract_after_keyword(trimmed, "package") {
            let name = name.trim_end_matches(';').trim();
            if !name.is_empty() {
                if let Some(pos) = line.find(name) {
                    tokens.push(RawToken {
                        line: line_num,
                        start: pos as u32,
                        length: name.len() as u32,
                        token_type: TOKEN_NAMESPACE,
                        token_modifiers: 0,
                    });
                }
            }
        }
    } else if trimmed.starts_with("import") {
        add_keyword_token(line, "import", line_num, tokens);
        add_string_literals(line, line_num, tokens);
    } else if trimmed.starts_with("message") {
        add_keyword_token(line, "message", line_num, tokens);
        if let Some(name) = extract_after_keyword(trimmed, "message") {
            let name = name.trim_end_matches(|c: char| c == '{' || c.is_whitespace());
            if !name.is_empty() {
                if let Some(pos) = line.find(name) {
                    tokens.push(RawToken {
                        line: line_num,
                        start: pos as u32,
                        length: name.len() as u32,
                        token_type: TOKEN_TYPE,
                        token_modifiers: 0,
                    });
                }
            }
        }
    } else if trimmed.starts_with("enum") {
        add_keyword_token(line, "enum", line_num, tokens);
        if let Some(name) = extract_after_keyword(trimmed, "enum") {
            let name = name.trim_end_matches(|c: char| c == '{' || c.is_whitespace());
            if !name.is_empty() {
                if let Some(pos) = line.find(name) {
                    tokens.push(RawToken {
                        line: line_num,
                        start: pos as u32,
                        length: name.len() as u32,
                        token_type: TOKEN_ENUM,
                        token_modifiers: 0,
                    });
                }
            }
        }
    } else if trimmed.starts_with("service") {
        add_keyword_token(line, "service", line_num, tokens);
        if let Some(name) = extract_after_keyword(trimmed, "service") {
            let name = name.trim_end_matches(|c: char| c == '{' || c.is_whitespace());
            if !name.is_empty() {
                if let Some(pos) = line.find(name) {
                    tokens.push(RawToken {
                        line: line_num,
                        start: pos as u32,
                        length: name.len() as u32,
                        token_type: TOKEN_INTERFACE,
                        token_modifiers: 0,
                    });
                }
            }
        }
    } else if trimmed.starts_with("rpc") {
        tokenize_rpc_line(line, line_num, trimmed, tokens);
    } else if trimmed.starts_with("repeated")
        || trimmed.starts_with("optional")
        || trimmed.starts_with("required")
    {
        // Field with label
        let kw = if trimmed.starts_with("repeated") {
            "repeated"
        } else if trimmed.starts_with("optional") {
            "optional"
        } else {
            "required"
        };
        add_keyword_token(line, kw, line_num, tokens);
        tokenize_field_after_label(line, line_num, trimmed, kw, tokens);
    } else if trimmed.starts_with("oneof") {
        add_keyword_token(line, "oneof", line_num, tokens);
    } else if trimmed.starts_with("map") {
        add_keyword_token(line, "map", line_num, tokens);
        tokenize_field_parts(line, line_num, tokens);
    } else if trimmed.starts_with("reserved") {
        add_keyword_token(line, "reserved", line_num, tokens);
        add_number_literals(line, line_num, tokens);
    } else {
        // Could be a field definition (type name = number;) or an enum value (NAME = number;)
        tokenize_field_or_enum_value(line, line_num, trimmed, tokens);
    }
}

fn tokenize_rpc_line(line: &str, line_num: u32, trimmed: &str, tokens: &mut Vec<RawToken>) {
    add_keyword_token(line, "rpc", line_num, tokens);

    // Find method name
    if let Some(after_rpc) = trimmed.strip_prefix("rpc ") {
        let after_rpc = after_rpc.trim_start();
        if let Some(name_end) = after_rpc.find(|c: char| !c.is_ascii_alphanumeric() && c != '_') {
            let method_name = &after_rpc[..name_end];
            if !method_name.is_empty() {
                if let Some(pos) = line.find(method_name) {
                    tokens.push(RawToken {
                        line: line_num,
                        start: pos as u32,
                        length: method_name.len() as u32,
                        token_type: TOKEN_METHOD,
                        token_modifiers: 0,
                    });
                }
            }
        }
    }

    // Look for 'returns' keyword
    if let Some(pos) = line.find("returns") {
        tokens.push(RawToken {
            line: line_num,
            start: pos as u32,
            length: 7,
            token_type: TOKEN_KEYWORD,
            token_modifiers: 0,
        });
    }

    // Look for 'stream' keywords
    let mut search_start = 0;
    while let Some(pos) = line[search_start..].find("stream") {
        let abs_pos = search_start + pos;
        // Verify it's a whole word
        let before_ok = abs_pos == 0
            || !line.as_bytes()[abs_pos - 1].is_ascii_alphanumeric();
        let after_ok = abs_pos + 6 >= line.len()
            || !line.as_bytes()[abs_pos + 6].is_ascii_alphanumeric();
        if before_ok && after_ok {
            tokens.push(RawToken {
                line: line_num,
                start: abs_pos as u32,
                length: 6,
                token_type: TOKEN_KEYWORD,
                token_modifiers: 0,
            });
        }
        search_start = abs_pos + 6;
    }

    // Type references in parentheses
    for cap in find_paren_contents(line) {
        let type_name = cap.trim();
        let type_name = type_name.strip_prefix("stream ").unwrap_or(type_name).trim();
        if !type_name.is_empty() {
            if let Some(pos) = line.rfind(type_name) {
                tokens.push(RawToken {
                    line: line_num,
                    start: pos as u32,
                    length: type_name.len() as u32,
                    token_type: TOKEN_TYPE,
                    token_modifiers: 0,
                });
            }
        }
    }
}

fn tokenize_field_after_label(
    line: &str,
    line_num: u32,
    trimmed: &str,
    keyword: &str,
    tokens: &mut Vec<RawToken>,
) {
    if let Some(rest) = trimmed.strip_prefix(keyword) {
        let rest = rest.trim_start();
        // rest should be "type_name field_name = number;"
        tokenize_field_definition(line, line_num, rest, tokens);
    }
}

fn tokenize_field_definition(line: &str, line_num: u32, field_str: &str, tokens: &mut Vec<RawToken>) {
    let parts: Vec<&str> = field_str.split_whitespace().collect();
    if parts.len() >= 2 {
        let type_name = parts[0];
        let field_name = parts[1];

        // Type reference (if not builtin)
        if !is_builtin_type(type_name) && !type_name.starts_with("map") {
            if let Some(pos) = line.find(type_name) {
                tokens.push(RawToken {
                    line: line_num,
                    start: pos as u32,
                    length: type_name.len() as u32,
                    token_type: TOKEN_TYPE,
                    token_modifiers: 0,
                });
            }
        }

        // Field name
        let field_name_clean = field_name.trim_end_matches(|c: char| c == '=' || c.is_whitespace());
        if !field_name_clean.is_empty() {
            // Find the field name position after the type
            if let Some(type_pos) = line.find(type_name) {
                let after_type = type_pos + type_name.len();
                if let Some(rel_pos) = line[after_type..].find(field_name_clean) {
                    let abs_pos = after_type + rel_pos;
                    tokens.push(RawToken {
                        line: line_num,
                        start: abs_pos as u32,
                        length: field_name_clean.len() as u32,
                        token_type: TOKEN_PROPERTY,
                        token_modifiers: 0,
                    });
                }
            }
        }

        // Number literal
        add_number_literals(line, line_num, tokens);
    }
}

fn tokenize_field_or_enum_value(line: &str, line_num: u32, trimmed: &str, tokens: &mut Vec<RawToken>) {
    if trimmed.is_empty() || trimmed == "}" || trimmed == "{" {
        return;
    }

    // Check if it looks like an enum value: NAME = number;
    if trimmed.contains('=') {
        let parts: Vec<&str> = trimmed.splitn(2, '=').collect();
        if parts.len() == 2 {
            let left = parts[0].trim();
            let right = parts[1].trim().trim_end_matches(';').trim();

            // If left is all uppercase or starts with uppercase, and right is a number → enum value
            if !left.is_empty() && right.chars().all(|c| c.is_ascii_digit() || c == '-') {
                // Could be an enum value or a field
                // Heuristic: if no space in left (single word), likely enum value or simple field
                if !left.contains(' ') {
                    // Enum value
                    if let Some(pos) = line.find(left) {
                        tokens.push(RawToken {
                            line: line_num,
                            start: pos as u32,
                            length: left.len() as u32,
                            token_type: TOKEN_ENUM_MEMBER,
                            token_modifiers: 0,
                        });
                    }
                    add_number_literals(line, line_num, tokens);
                    return;
                }
            }
        }
    }

    // Field definition: type field_name = number;
    tokenize_field_definition(line, line_num, trimmed, tokens);
}

fn tokenize_field_parts(line: &str, line_num: u32, tokens: &mut Vec<RawToken>) {
    // For map fields, find the field name and number
    if let Some(gt_pos) = line.find('>') {
        let rest = &line[gt_pos + 1..];
        let rest = rest.trim_start();
        if let Some(eq_pos) = rest.find('=') {
            let field_name = rest[..eq_pos].trim();
            if !field_name.is_empty() {
                let abs_pos = gt_pos + 1 + (rest.len() - rest.trim_start().len());
                if let Some(name_pos) = line[abs_pos..].find(field_name) {
                    tokens.push(RawToken {
                        line: line_num,
                        start: (abs_pos + name_pos) as u32,
                        length: field_name.len() as u32,
                        token_type: TOKEN_PROPERTY,
                        token_modifiers: 0,
                    });
                }
            }
        }
    }
    add_number_literals(line, line_num, tokens);
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn add_keyword_token(line: &str, keyword: &str, line_num: u32, tokens: &mut Vec<RawToken>) {
    if let Some(pos) = line.find(keyword) {
        tokens.push(RawToken {
            line: line_num,
            start: pos as u32,
            length: keyword.len() as u32,
            token_type: TOKEN_KEYWORD,
            token_modifiers: 0,
        });
    }
}

fn add_string_literals(line: &str, line_num: u32, tokens: &mut Vec<RawToken>) {
    let mut in_string = false;
    let mut string_start = 0;
    for (i, ch) in line.char_indices() {
        if ch == '"' {
            if in_string {
                tokens.push(RawToken {
                    line: line_num,
                    start: string_start as u32,
                    length: (i - string_start + 1) as u32,
                    token_type: TOKEN_STRING,
                    token_modifiers: 0,
                });
                in_string = false;
            } else {
                string_start = i;
                in_string = true;
            }
        }
    }
}

fn add_number_literals(line: &str, line_num: u32, tokens: &mut Vec<RawToken>) {
    // Find number after '='
    if let Some(eq_pos) = line.find('=') {
        let after_eq = &line[eq_pos + 1..];
        let trimmed = after_eq.trim_start();
        let offset = after_eq.len() - trimmed.len();
        let num_len = trimmed
            .find(|c: char| !c.is_ascii_digit() && c != '-')
            .unwrap_or(trimmed.len());
        if num_len > 0 {
            let abs_start = eq_pos + 1 + offset;
            tokens.push(RawToken {
                line: line_num,
                start: abs_start as u32,
                length: num_len as u32,
                token_type: TOKEN_NUMBER,
                token_modifiers: 0,
            });
        }
    }
}

fn extract_after_keyword<'a>(line: &'a str, keyword: &str) -> Option<&'a str> {
    line.strip_prefix(keyword).map(|s| s.trim_start())
}

fn find_paren_contents(line: &str) -> Vec<&str> {
    let mut results = Vec::new();
    let mut search_from = 0;
    while let Some(open) = line[search_from..].find('(') {
        let abs_open = search_from + open;
        if let Some(close) = line[abs_open..].find(')') {
            let content = &line[abs_open + 1..abs_open + close];
            results.push(content);
            search_from = abs_open + close + 1;
        } else {
            break;
        }
    }
    results
}

fn is_builtin_type(t: &str) -> bool {
    matches!(
        t,
        "double"
            | "float"
            | "int32"
            | "int64"
            | "uint32"
            | "uint64"
            | "sint32"
            | "sint64"
            | "fixed32"
            | "fixed64"
            | "sfixed32"
            | "sfixed64"
            | "bool"
            | "string"
            | "bytes"
    )
}

fn encode_tokens(raw: &[RawToken]) -> Vec<SemanticToken> {
    let mut result = Vec::with_capacity(raw.len());
    let mut prev_line = 0u32;
    let mut prev_start = 0u32;

    for token in raw {
        let delta_line = token.line - prev_line;
        let delta_start = if delta_line == 0 {
            token.start - prev_start
        } else {
            token.start
        };

        result.push(SemanticToken {
            delta_line,
            delta_start,
            length: token.length,
            token_type: token.token_type,
            token_modifiers_bitset: token.token_modifiers,
        });

        prev_line = token.line;
        prev_start = token.start;
    }

    result
}

/// Build the `SemanticTokensLegend` used in capabilities.
pub fn semantic_tokens_legend() -> SemanticTokensLegend {
    SemanticTokensLegend {
        token_types: TOKEN_TYPES.to_vec(),
        token_modifiers: TOKEN_MODIFIERS.to_vec(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_encode_tokens() {
        let raw = vec![
            RawToken { line: 0, start: 0, length: 6, token_type: TOKEN_KEYWORD, token_modifiers: 0 },
            RawToken { line: 0, start: 9, length: 8, token_type: TOKEN_STRING, token_modifiers: 0 },
            RawToken { line: 2, start: 0, length: 7, token_type: TOKEN_KEYWORD, token_modifiers: 0 },
        ];
        let encoded = encode_tokens(&raw);
        assert_eq!(encoded.len(), 3);
        assert_eq!(encoded[0].delta_line, 0);
        assert_eq!(encoded[0].delta_start, 0);
        assert_eq!(encoded[1].delta_line, 0);
        assert_eq!(encoded[1].delta_start, 9);
        assert_eq!(encoded[2].delta_line, 2);
        assert_eq!(encoded[2].delta_start, 0);
    }

    #[test]
    fn test_find_paren_contents() {
        let result = find_paren_contents("rpc GetUser(GetUserRequest) returns (GetUserResponse);");
        assert_eq!(result, vec!["GetUserRequest", "GetUserResponse"]);
    }

    #[test]
    fn test_is_builtin_type() {
        assert!(is_builtin_type("string"));
        assert!(is_builtin_type("int32"));
        assert!(!is_builtin_type("UserMessage"));
    }
}