v2 / vlib / v / pref / line_info.v
70 lines · 65 sloc · 1.91 KB · 2f8717723fcf2ffae2120ba249c9b6f3b37a2f47
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
3module pref
4
5// Method copy from vls/lsp.v
6pub enum Method {
7 unknown @['unknown']
8 initialize @['initialize']
9 initialized @['initialized']
10 did_open @['textDocument/didOpen']
11 did_change @['textDocument/didChange']
12 definition @['textDocument/definition']
13 completion @['textDocument/completion']
14 signature_help @['textDocument/signatureHelp']
15 hover @['textDocument/hover']
16 set_trace @['$/setTrace']
17 cancel_request @['$/cancelRequest']
18 shutdown @['shutdown']
19 exit @['exit']
20}
21
22pub struct LineInfo {
23pub mut:
24 method Method
25 path string // same, but stores the path being parsed
26 line_nr int // a quick single file run when called with v -line-info (contains line nr to inspect)
27 col int
28 vars_printed map[string]bool // to avoid dups
29}
30
31fn (mut p Preferences) parse_line_info(line string) {
32 format_err := 'wrong format, use `-line-info "file.v:24:7"'
33 vals := line.split(':')
34 if vals.len < 3 {
35 eprintln(format_err)
36 return
37 }
38 file_name := vals[..vals.len - 2].join(':')
39 line_nr := vals[vals.len - 2].int()
40
41 if (!file_name.ends_with('.v') && !file_name.ends_with('.vv')) || line_nr == -1 {
42 eprintln(format_err)
43 return
44 }
45
46 // Third value is column
47 third := vals[vals.len - 1]
48 mut col := 0
49 method := if third.starts_with('fn^') {
50 col = third[3..].int() - 1
51 Method.signature_help
52 } else if third.starts_with('gd^') {
53 col = third[3..].int() - 1
54 Method.definition
55 } else if third.starts_with('hv^') {
56 col = third[3..].int() - 1
57 Method.hover
58 } else if third[0].is_digit() {
59 col = third.int() - 1
60 Method.completion
61 } else {
62 Method.unknown
63 }
64 p.linfo = LineInfo{
65 method: method
66 line_nr: line_nr - 1
67 path: file_name
68 col: col
69 }
70}
71