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
|
#+private file
package server
import "core:fmt"
import "core:log"
import "core:odin/ast"
import "core:odin/tokenizer"
import path "core:path/slashpath"
import "core:strings"
import "src:common"
/*
* The general idea behind inverting if statements is to allow
* if statements to be inverted without changing their behavior.
* The examples of these changes are provided in the tests.
* We should be careful to only allow this code action when it is safe to do so.
* So for now, we only support only one level of if statements without else-if chains.
*/
@(private="package")
add_invert_if_action :: proc(
document: ^Document,
position: common.AbsolutePosition,
uri: string,
actions: ^[dynamic]CodeAction,
) {
if_stmt := find_if_stmt_at_position(document.ast.decls[:], position)
if if_stmt == nil {
return
}
new_text, ok := generate_inverted_if(document, if_stmt)
if !ok {
return
}
range := common.get_token_range(if_stmt^, document.ast.src)
textEdits := make([dynamic]TextEdit, context.temp_allocator)
append(&textEdits, TextEdit{range = range, newText = new_text})
workspaceEdit: WorkspaceEdit
workspaceEdit.changes = make(map[string][]TextEdit, 0, context.temp_allocator)
workspaceEdit.changes[uri] = textEdits[:]
append(
actions,
CodeAction {
kind = "refactor.more",
isPreferred = false,
title = "Invert if",
edit = workspaceEdit,
},
)
}
// Find the innermost if statement that contains the given position
// This will NOT return else-if statements, only top-level if statements
// Also will not return an if statement if the position is in its else clause
find_if_stmt_at_position :: proc(stmts: []^ast.Stmt, position: common.AbsolutePosition) -> ^ast.If_Stmt {
for stmt in stmts {
if stmt == nil {
continue
}
if result := find_if_stmt_in_node(stmt, position, false); result != nil {
return result
}
}
return nil
}
find_if_stmt_in_node :: proc(node: ^ast.Node, position: common.AbsolutePosition, in_else_clause: bool) -> ^ast.If_Stmt {
if node == nil {
return nil
}
if !(node.pos.offset <= position && position <= node.end.offset) {
return nil
}
#partial switch n in node.derived {
case ^ast.If_Stmt:
// First check if position is in the else clause
if n.else_stmt != nil && position_in_node(n.else_stmt, position) {
// Position is in the else clause - look for nested ifs inside it
// but mark that we're in an else clause
if nested := find_if_stmt_in_node(n.else_stmt, position, true); nested != nil {
return nested
}
// Position is in else clause but not on a valid nested if
// Don't return the current if statement
return nil
}
if n.body != nil && position_in_node(n.body, position) {
if nested := find_if_stmt_in_node(n.body, position, false); nested != nil {
return nested
}
// Position is inside the body but no nested if found
// Don't return the current if statement
return nil
}
// Position is in the condition/init part or we're the closest if
// Only return this if statement if we're NOT in an else clause
// (i.e., this is not an else-if)
if !in_else_clause {
return n
}
return nil
case ^ast.Block_Stmt:
for stmt in n.stmts {
if result := find_if_stmt_in_node(stmt, position, false); result != nil {
return result
}
}
case ^ast.Proc_Lit:
if n.body != nil {
return find_if_stmt_in_node(n.body, position, false)
}
case ^ast.Value_Decl:
for value in n.values {
if result := find_if_stmt_in_node(value, position, false); result != nil {
return result
}
}
case ^ast.For_Stmt:
if n.body != nil {
return find_if_stmt_in_node(n.body, position, false)
}
case ^ast.Range_Stmt:
if n.body != nil {
return find_if_stmt_in_node(n.body, position, false)
}
case ^ast.Switch_Stmt:
if n.body != nil {
return find_if_stmt_in_node(n.body, position, false)
}
case ^ast.Type_Switch_Stmt:
if n.body != nil {
return find_if_stmt_in_node(n.body, position, false)
}
case ^ast.Case_Clause:
for stmt in n.body {
if result := find_if_stmt_in_node(stmt, position, false); result != nil {
return result
}
}
case ^ast.When_Stmt:
if n.body != nil {
if result := find_if_stmt_in_node(n.body, position, false); result != nil {
return result
}
}
if n.else_stmt != nil {
if result := find_if_stmt_in_node(n.else_stmt, position, false); result != nil {
return result
}
}
case ^ast.Defer_Stmt:
if n.stmt != nil {
return find_if_stmt_in_node(n.stmt, position, false)
}
}
return nil
}
// Generate the inverted if statement text
generate_inverted_if :: proc(document: ^Document, if_stmt: ^ast.If_Stmt) -> (string, bool) {
src := document.ast.src
indent := get_line_indentation(src, if_stmt.pos.offset)
sb := strings.builder_make(context.temp_allocator)
if if_stmt.label != nil {
label_text := src[if_stmt.label.pos.offset:if_stmt.label.end.offset]
strings.write_string(&sb, label_text)
strings.write_string(&sb, ": ")
}
strings.write_string(&sb, "if ")
if if_stmt.init != nil {
init_text := src[if_stmt.init.pos.offset:if_stmt.init.end.offset]
strings.write_string(&sb, init_text)
strings.write_string(&sb, "; ")
}
if if_stmt.cond != nil {
inverted_cond, ok := invert_condition(src, if_stmt.cond)
if !ok {
return "", false
}
strings.write_string(&sb, inverted_cond)
}
strings.write_string(&sb, " ")
// Now we need to swap the bodies
if if_stmt.else_stmt != nil {
else_body_text := get_block_body_text(src, if_stmt.else_stmt, indent)
then_body_text := get_block_body_text(src, if_stmt.body, indent)
strings.write_string(&sb, "{\n")
strings.write_string(&sb, else_body_text)
strings.write_string(&sb, indent)
strings.write_string(&sb, "} else {\n")
strings.write_string(&sb, then_body_text)
strings.write_string(&sb, indent)
strings.write_string(&sb, "}")
} else {
then_body_text := get_block_body_text(src, if_stmt.body, indent)
strings.write_string(&sb, "{\n")
strings.write_string(&sb, indent)
strings.write_string(&sb, "} else {\n")
strings.write_string(&sb, then_body_text)
strings.write_string(&sb, indent)
strings.write_string(&sb, "}")
}
return strings.to_string(sb), true
}
// Get the indentation (leading whitespace) of the line containing the given offset
get_line_indentation :: proc(src: string, offset: int) -> string {
line_start := offset
for line_start > 0 && src[line_start - 1] != '\n' {
line_start -= 1
}
indent_end := line_start
for indent_end < len(src) && (src[indent_end] == ' ' || src[indent_end] == '\t') {
indent_end += 1
}
return src[line_start:indent_end]
}
// Extract the body text from a block statement (without the braces)
get_block_body_text :: proc(src: string, stmt: ^ast.Stmt, base_indent: string) -> string {
if stmt == nil {
return ""
}
#partial switch block in stmt.derived {
case ^ast.Block_Stmt:
if len(block.stmts) == 0 {
return ""
}
sb := strings.builder_make(context.temp_allocator)
for s in block.stmts {
if s == nil {
continue
}
stmt_indent := get_line_indentation(src, s.pos.offset)
stmt_text := src[s.pos.offset:s.end.offset]
strings.write_string(&sb, stmt_indent)
strings.write_string(&sb, stmt_text)
strings.write_string(&sb, "\n")
}
return strings.to_string(sb)
case ^ast.If_Stmt:
// This is an else-if, need to handle it recursively
if_text, ok := generate_inverted_if_for_else(src, block, base_indent)
if ok {
return if_text
}
}
// Fallback: just return the statement text
stmt_text := src[stmt.pos.offset:stmt.end.offset]
return fmt.tprintf("%s%s\n", base_indent, stmt_text)
}
// For else-if chains, we don't invert them, just preserve
generate_inverted_if_for_else :: proc(src: string, if_stmt: ^ast.If_Stmt, base_indent: string) -> (string, bool) {
stmt_indent := get_line_indentation(src, if_stmt.pos.offset)
stmt_text := src[if_stmt.pos.offset:if_stmt.end.offset]
return fmt.tprintf("%s%s\n", stmt_indent, stmt_text), true
}
// Invert a condition expression
invert_condition :: proc(src: string, cond: ^ast.Expr) -> (string, bool) {
if cond == nil {
return "", false
}
#partial switch c in cond.derived {
case ^ast.Binary_Expr:
inverted_op, can_invert := get_inverted_operator(c.op.kind)
if can_invert {
left_text := src[c.left.pos.offset:c.left.end.offset]
right_text := src[c.right.pos.offset:c.right.end.offset]
return fmt.tprintf("%s %s %s", left_text, inverted_op, right_text), true
}
if c.op.kind == .Cmp_And || c.op.kind == .Cmp_Or {
// Just wrap with !()
cond_text := src[cond.pos.offset:cond.end.offset]
return fmt.tprintf("!(%s)", cond_text), true
}
case ^ast.Unary_Expr:
// If it's already negated with !, remove the negation
if c.op.kind == .Not {
inner_text := src[c.expr.pos.offset:c.expr.end.offset]
return inner_text, true
}
case ^ast.Paren_Expr:
inner_inverted, ok := invert_condition(src, c.expr)
if ok {
if needs_parentheses(inner_inverted) {
return fmt.tprintf("(%s)", inner_inverted), true
}
return inner_inverted, true
}
}
// Default: wrap the whole condition with !()
cond_text := src[cond.pos.offset:cond.end.offset]
if is_simple_expr(cond) {
return fmt.tprintf("!%s", cond_text), true
}
return fmt.tprintf("!(%s)", cond_text), true
}
// Check if an expression is simple (identifier, call, or already parenthesized)
is_simple_expr :: proc(expr: ^ast.Expr) -> bool {
if expr == nil {
return false
}
#partial switch e in expr.derived {
case ^ast.Ident, ^ast.Paren_Expr, ^ast.Call_Expr, ^ast.Selector_Expr, ^ast.Index_Expr:
return true
}
return false
}
// Check if a string needs parentheses (simple heuristic)
needs_parentheses :: proc(s: string) -> bool {
// If it starts with ! and is not wrapped in parens, it might need them
// This is a simple heuristic
return strings.contains(s, " && ") || strings.contains(s, " || ")
}
// Get the inverted comparison operator
get_inverted_operator :: proc(op: tokenizer.Token_Kind) -> (string, bool) {
#partial switch op {
case .Cmp_Eq:
return "!=", true
case .Not_Eq:
return "==", true
case .Lt:
return ">=", true
case .Lt_Eq:
return ">", true
case .Gt:
return "<=", true
case .Gt_Eq:
return "<", true
}
return "", false
}
|