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
|
package server
import "base:runtime"
import "core:log"
import "core:mem"
import "core:odin/ast"
import "core:strings"
import "src:common"
get_rename :: proc(document: ^Document, new_text: string, position: common.Position) -> (WorkspaceEdit, bool) {
ast_context := make_ast_context(
document.ast,
document.imports,
document.package_name,
document.uri.uri,
document.fullpath,
context.temp_allocator,
)
position_context, ok := get_document_position_context(document, position, .Hover)
if !ok {
log.warn("Failed to get position context")
return {}, false
}
ast_context.position_hint = position_context.hint
get_globals(document.ast, &ast_context)
ast_context.current_package = ast_context.document_package
if position_context.function != nil {
get_locals(document.ast, position_context.function, &ast_context, &position_context)
}
locations, ok2 := resolve_references(document, &ast_context, &position_context)
changes := make(map[string][dynamic]TextEdit, 0, context.temp_allocator)
for location in locations {
edits: ^[dynamic]TextEdit
if edits = &changes[location.uri]; edits == nil {
changes[strings.clone(location.uri, context.temp_allocator)] = make(
[dynamic]TextEdit,
context.temp_allocator,
)
edits = &changes[location.uri]
}
append(edits, TextEdit{newText = new_text, range = location.range})
}
workspace: WorkspaceEdit
workspace.changes = make(map[string][]TextEdit, len(changes), context.temp_allocator)
for k, v in changes {
workspace.changes[k] = v[:]
}
return workspace, true
}
get_prepare_rename :: proc(document: ^Document, position: common.Position) -> (common.Range, bool) {
ast_context := make_ast_context(
document.ast,
document.imports,
document.package_name,
document.uri.uri,
document.fullpath,
context.temp_allocator,
)
position_context, ok := get_document_position_context(document, position, .Hover)
if !ok {
log.warn("Failed to get position context")
return {}, false
}
ast_context.position_hint = position_context.hint
get_globals(document.ast, &ast_context)
ast_context.current_package = ast_context.document_package
if position_context.function != nil {
get_locals(document.ast, position_context.function, &ast_context, &position_context)
}
symbol, ok2 := prepare_rename(document, &ast_context, &position_context)
return symbol.range, ok2
}
get_struct_field_type_position :: proc(
ast_context: ^AstContext, position_context: ^DocumentPositionContext, node: ^ast.Expr
) -> (Symbol, bool) {
#partial switch v in node.derived {
case ^ast.Ident:
symbol := Symbol {
range = common.get_token_range(node, ast_context.file.src),
}
return symbol, true
case ^ast.Selector_Expr:
symbol := Symbol {
range = common.get_token_range(v.field, ast_context.file.src),
}
return symbol, true
case ^ast.Pointer_Type:
return get_struct_field_type_position(ast_context, position_context, v.elem)
}
return {}, false
}
// For preparing the rename, we want to position of the token within the current file,
// not the position of the declaration
prepare_rename :: proc(
document: ^Document,
ast_context: ^AstContext,
position_context: ^DocumentPositionContext,
) -> (
symbol: Symbol,
ok: bool,
) {
ok = false
pkg := ""
if position_context.struct_type != nil {
found := false
done_struct: for field in position_context.struct_type.fields.list {
for name in field.names {
if position_in_node(name, position_context.position) {
symbol = Symbol {
range = common.get_token_range(name, ast_context.file.src),
}
found = true
break done_struct
}
}
if position_in_node(field.type, position_context.position) {
node := get_desired_expr(field.type, position_context.position)
symbol, ok = get_struct_field_type_position(ast_context, position_context, node)
if !ok {
return
}
found = true
break done_struct
}
}
if !found {
return
}
} else if position_context.enum_type != nil {
found := false
done_enum: for field in position_context.enum_type.fields {
if ident, ok := field.derived.(^ast.Ident); ok {
if position_in_node(ident, position_context.position) {
symbol = Symbol {
range = common.get_token_range(ident, ast_context.file.src),
}
found = true
break done_enum
}
} else if value, ok := field.derived.(^ast.Field_Value); ok {
if position_in_node(value.field, position_context.position) {
symbol = Symbol {
range = common.get_token_range(value.field, ast_context.file.src),
}
found = true
break done_enum
} else if position_in_node(value.value, position_context.position) {
symbol = Symbol {
range = common.get_token_range(value.value, ast_context.file.src),
}
found = true
break done_enum
}
}
}
if !found {
return
}
} else if position_context.bitset_type != nil {
if position_in_node(position_context.bitset_type.elem, position_context.position) {
symbol = Symbol {
range = common.get_token_range(position_context.bitset_type.elem, ast_context.file.src)
}
return symbol, true
}
return
} else if position_context.union_type != nil {
found := false
for variant in position_context.union_type.variants {
if position_in_node(variant, position_context.position) {
symbol = Symbol {
range = common.get_token_range(variant, ast_context.file.src),
}
found = true
break
}
}
if !found {
return
}
} else if position_context.implicit {
range := common.get_token_range(position_context.implicit_selector_expr, ast_context.file.src)
// Skip the `.`
range.start.character += 1
symbol = Symbol{
range = range,
}
} else if position_context.field_value != nil &&
position_context.comp_lit != nil &&
!is_expr_basic_lit(position_context.field_value.field) &&
position_in_node(position_context.field_value.field, position_context.position) {
symbol = Symbol {
range = common.get_token_range(position_context.field_value.field, ast_context.file.src)
}
} else if position_context.selector_expr != nil {
if position_in_node(position_context.selector, position_context.position) &&
position_context.identifier != nil {
ident := position_context.identifier.derived.(^ast.Ident)
symbol, ok = resolve_location_identifier(ast_context, ident^)
if !ok {
return
}
} else {
symbol, ok = resolve_location_selector(ast_context, position_context.selector_expr)
if selector, ok := position_context.selector_expr.derived.(^ast.Selector_Expr); ok {
symbol.range = common.get_token_range(selector.field.expr_base, ast_context.file.src)
}
}
} else if position_context.identifier != nil {
ident := position_context.identifier.derived.(^ast.Ident)
symbol = Symbol {
range = common.get_token_range(position_context.identifier^, ast_context.file.src)
}
} else {
return
}
return symbol, true
}
|