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

import "core:odin/parser"
import "core:odin/ast"
import "core:odin/tokenizer"
import "core:fmt"
import "core:log"
import "core:strings"
import path "core:path/slashpath"
import "core:mem"
import "core:strconv"
import "core:path/filepath"
import "core:sort"
import "core:slice"
import "core:os"

import "shared:common"

get_definition_location :: proc(document: ^Document, position: common.Position) -> ([]common.Location, bool) {
	locations := make([dynamic]common.Location, context.temp_allocator)

	location: common.Location

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

	uri: string

	position_context, ok := get_document_position_context(document, position, .Definition)

	if !ok {
		log.warn("Failed to get position context")
		return {}, false
	}

	get_globals(document.ast, &ast_context)

	if position_context.function != nil {
		get_locals(document.ast, position_context.function, &ast_context, &position_context)
	}

	if position_context.selector != nil {
		//if the base selector is the client wants to go to.
		if base, ok := position_context.selector.derived.(^ast.Ident); ok && position_context.identifier != nil {
			ident := position_context.identifier.derived.(^ast.Ident)

			if ident.name == base.name {
				if resolved, ok := resolve_location_identifier(&ast_context, ident^); ok {
					location.range = resolved.range

					if resolved.uri == "" {
						location.uri = document.uri.uri
					} else {
						location.uri = resolved.uri
					}

					append(&locations, location)

					return locations[:], true
				} else {
					return {}, false
				}
			}
		}

		//otherwise it's the field the client wants to go to.

		selector: Symbol

		ast_context.use_locals = true
		ast_context.use_globals = true
		ast_context.current_package = ast_context.document_package

		selector, ok = resolve_type_expression(&ast_context, position_context.selector)

		if !ok {
			return {}, false
		}

		field: string

		if position_context.field != nil {
			#partial switch v in position_context.field.derived {
			case ^ast.Ident:
				field = v.name
			}
		}

		uri = selector.uri

		#partial switch v in selector.value {
		case SymbolEnumValue:
			location.range = selector.range
		case SymbolStructValue:
			for name, i in v.names {
				if strings.compare(name, field) == 0 {
					location.range = common.get_token_range(v.types[i]^, document.ast.src)
				}
			}
		case SymbolPackageValue:
			if symbol, ok := lookup(field, selector.pkg); ok {
				location.range = symbol.range
				uri = symbol.uri
			} else {
				return {}, false
			}
		}

		if !ok {
			return {}, false
		}
	} else if position_context.identifier != nil {
		if resolved, ok := resolve_location_identifier(&ast_context, position_context.identifier.derived.(^ast.Ident)^); ok {
			location.range = resolved.range
			uri = resolved.uri
		} else {
			return {}, false
		}
	} else {
		return {}, false
	}

	//if the symbol is generated by the ast we don't set the uri.
	if uri == "" {
		location.uri = document.uri.uri
	} else {
		location.uri = uri
	}

	append(&locations, location)

	return locations[:], true
}