aboutsummaryrefslogtreecommitdiff
path: root/src/server/inlay_hints.odin
blob: fb989db95e4b340cf01bdfe4864feea2486da651 (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
package server 

import "core:odin/ast"
import "core:fmt"

import "shared:common"

get_inlay_hints :: proc(document: ^Document, symbols: map[uintptr]SymbolAndNode) -> ([]InlayHint, bool) {
	hints := make([dynamic]InlayHint, context.temp_allocator)

	ast_context := make_ast_context(document.ast, document.imports, document.package_name, document.uri.uri, document.fullpath)

	Visit_Data :: struct {
		calls: [dynamic]^ast.Node,
	}

	data := Visit_Data {
		calls = make([dynamic]^ast.Node, context.temp_allocator),
	}

	visit :: proc(visitor: ^ast.Visitor, node: ^ast.Node) -> ^ast.Visitor {
		if node == nil || visitor == nil {
			return nil
		}

		data := cast(^Visit_Data)visitor.data

		if call, ok := node.derived.(^ast.Call_Expr); ok {
			append(&data.calls, node)
		}

		return visitor
	}

	visitor := ast.Visitor {
		data = &data,
		visit = visit,
	}

	for decl in document.ast.decls {
		ast.walk(&visitor, decl)
	}

	loop: for node_call in &data.calls {
		symbol_arg_count := 0

		call := node_call.derived.(^ast.Call_Expr)

		for arg in call.args {
			if _, ok := arg.derived.(^ast.Field); ok {
				continue loop
			}
		}

		if symbol_and_node, ok := symbols[cast(uintptr)node_call]; ok {
			if symbol_call, ok := symbol_and_node.symbol.value.(SymbolProcedureValue); ok {
				for arg in symbol_call.arg_types {
					for name in arg.names {
						if symbol_arg_count >= len(call.args) {
							continue loop
						}

						if ident, ok := name.derived.(^ast.Ident); ok {
							hint := InlayHint {
								kind = "parameter",
								label = fmt.tprintf("%v = ", ident.name),
								range = common.get_token_range(call.args[symbol_arg_count], string(document.text)),
							}
							append(&hints, hint)
						} 
						symbol_arg_count += 1
					}
				}
			}
		}
	}

	return hints[:], true
}