rs-runec 0.7.0

Compiler, linter, and language server for RuneScript, ClientScript, and GlobalScript, with ClientScript decompilation
Documentation
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use super::{CodeAction, Location, SymbolKind, TextEdit, index};
use crate::diagnostics::{Applicability, Diagnostic};
use crate::find_testscript_line;
use crate::lexer::Lexer;
use crate::parser::{Parser, Statement};
use crate::project::{Document, Project, SourceId};
use crate::token::Kind;
use crate::types::Type;
use std::collections::HashSet;
use std::ops::Range;

fn distance(left: &str, right: &str) -> usize {
    let mut row: Vec<usize> = (0..=right.len()).collect();
    for (i, a) in left.bytes().enumerate() {
        let mut diagonal = row[0];
        row[0] = i + 1;
        for (j, b) in right.bytes().enumerate() {
            let old = row[j + 1];
            row[j + 1] = (row[j + 1] + 1)
                .min(row[j] + 1)
                .min(diagonal + usize::from(a != b));
            diagonal = old;
        }
    }
    row[right.len()]
}

fn insertion(document: &Document, offset: usize) -> usize {
    find_testscript_line(&document.text)
        .and_then(|line| document.lines.line(line.saturating_sub(1)))
        .map(|line| line.start)
        .filter(|marker| offset < *marker)
        .unwrap_or(document.text.len())
}

impl Project {
    pub fn quick_fixes(&self, source: SourceId, diagnostics: &[Diagnostic]) -> Vec<CodeAction> {
        if diagnostics.is_empty() {
            return Vec::new();
        }
        let mut actions = self.code_actions(diagnostics);
        let Some(document) = self.document(source) else {
            return actions;
        };
        let index = self.symbol_index();
        for diagnostic in diagnostics {
            if diagnostic.message.contains("from production code") {
                continue;
            }
            let Some(line) = document.lines.line(diagnostic.line.saturating_sub(1)) else {
                continue;
            };
            if diagnostic.message.starts_with("Type mismatch:") {
                let types = diagnostic.message.split('\'').collect::<Vec<_>>();
                if let (Some(given), Some(expected)) = (types.get(1), types.get(3)) {
                    for (declared, inferred) in [(given, expected), (expected, given)] {
                        if Type::from_name(inferred).is_none() {
                            continue;
                        }
                        if let Some(range) = document.tokens.iter().zip(&document.spans).find_map(
                            |(token, range)| {
                                (token.kind == Kind::Def
                                    && line.contains(&range.start)
                                    && document.text[range.clone()] == format!("def_{declared}"))
                                .then_some(range.clone())
                            },
                        ) {
                            actions.push(CodeAction {
                                title: format!("Change declared type to 'def_{inferred}'"),
                                kind: "quickfix",
                                edits: vec![TextEdit {
                                    location: Location { source, range },
                                    replacement: format!("def_{inferred}"),
                                }],
                                applicability: Applicability::MaybeIncorrect,
                            });
                        }
                    }
                }
            }
            let Some(raw) = diagnostic.message.split('\'').nth(1) else {
                continue;
            };
            let name = raw.trim_start_matches(['~', '@', '^', '%', '$']);
            let Some(range) = document
                .spans
                .iter()
                .find(|range| {
                    line.contains(&range.start) && document.text[(*range).clone()] == *name
                })
                .cloned()
            else {
                continue;
            };
            if diagnostic.message.contains("bare value")
                && let Some(symbol) = index
                    .symbols
                    .iter()
                    .find(|symbol| symbol.kind == SymbolKind::Command && symbol.name == name)
            {
                let args = symbol
                    .parameters
                    .iter()
                    .map(|(_, ty)| match ty {
                        Type::String => "\"\"",
                        Type::Boolean => "false",
                        Type::Long => "0L",
                        _ => "0",
                    })
                    .collect::<Vec<_>>()
                    .join(", ");
                actions.push(CodeAction {
                    title: format!("Call '{name}' with arguments"),
                    kind: "quickfix",
                    edits: vec![TextEdit {
                        location: Location {
                            source,
                            range: range.clone(),
                        },
                        replacement: format!("{name}({args})"),
                    }],
                    applicability: Applicability::HasPlaceholders,
                });
                continue;
            }
            let kind = if diagnostic.message.contains("constant") {
                Some(SymbolKind::Constant)
            } else if diagnostic.message.contains("proc") {
                Some(SymbolKind::Procedure)
            } else if diagnostic.message.contains("command") {
                Some(SymbolKind::Command)
            } else if diagnostic.message.contains("label") {
                Some(SymbolKind::Trigger)
            } else {
                None
            };
            if diagnostic.message.contains("resolve") || diagnostic.message.contains("Unknown") {
                let mut candidates = index
                    .symbols
                    .iter()
                    .filter(|symbol| {
                        kind.is_none_or(|kind| symbol.kind == kind)
                            && symbol.name.len().abs_diff(name.len()) <= 3
                            && index::visible(symbol, document, range.start)
                    })
                    .map(|symbol| (distance(name, &symbol.name), &symbol.name))
                    .filter(|(distance, _)| *distance > 0 && *distance <= 3)
                    .collect::<Vec<_>>();
                candidates.sort();
                candidates.dedup();
                for (_, candidate) in candidates.into_iter().take(3) {
                    actions.push(CodeAction {
                        title: format!("Change to '{candidate}'"),
                        kind: "quickfix",
                        edits: vec![TextEdit {
                            location: Location {
                                source,
                                range: range.clone(),
                            },
                            replacement: candidate.clone(),
                        }],
                        applicability: Applicability::MaybeIncorrect,
                    });
                }
                if matches!(kind, Some(SymbolKind::Procedure | SymbolKind::Trigger)) {
                    let trigger = if kind == Some(SymbolKind::Procedure) {
                        "proc"
                    } else {
                        "label"
                    };
                    let insertion = insertion(document, range.start);
                    actions.push(CodeAction {
                        title: format!("Create [{trigger},{name}]"),
                        kind: "quickfix",
                        edits: vec![TextEdit {
                            location: Location {
                                source,
                                range: insertion..insertion,
                            },
                            replacement: format!("\n\n[{trigger},{name}]\nreturn;\n"),
                        }],
                        applicability: Applicability::MaybeIncorrect,
                    });
                }
                if diagnostic.message.contains("symbol") {
                    actions.push(CodeAction {
                        title: format!("Quote '{name}' as a string"),
                        kind: "quickfix",
                        edits: vec![TextEdit {
                            location: Location {
                                source,
                                range: range.clone(),
                            },
                            replacement: format!("\"{name}\""),
                        }],
                        applicability: Applicability::MaybeIncorrect,
                    });
                }
            }
        }
        actions
    }

    pub fn extract_proc(
        &self,
        source: SourceId,
        range: Range<usize>,
        name: &str,
    ) -> Result<CodeAction, String> {
        let document = self.document(source).ok_or("Unknown source")?;
        if range.is_empty() || document.config.is_some() {
            return Err("Select statements inside a script".into());
        }
        let text = document
            .text
            .get(range.clone())
            .ok_or("Invalid selection")?;
        let index = self.symbol_index();
        if index
            .symbols()
            .any(|symbol| symbol.container.is_none() && symbol.name == name)
        {
            return Err("A script with this name already exists".into());
        }
        if name.is_empty()
            || !name
                .bytes()
                .enumerate()
                .all(|(i, b)| b == b'_' || b.is_ascii_alphabetic() || (i > 0 && b.is_ascii_digit()))
        {
            return Err("Invalid procedure name".into());
        }
        let enclosing = index
            .symbols()
            .find(|symbol| {
                symbol.container.is_none()
                    && symbol.kind != SymbolKind::Command
                    && symbol.extent.as_ref().is_some_and(|extent| {
                        extent.source == source
                            && extent.range.start < range.start
                            && range.end <= extent.range.end
                    })
            })
            .ok_or("Selection must stay inside one script")?;
        let selected = document
            .tokens
            .iter()
            .zip(&document.spans)
            .filter(|(_, span)| range.start <= span.start && span.start < range.end)
            .collect::<Vec<_>>();
        if selected
            .iter()
            .any(|(token, _)| token.kind == Kind::JumpCall)
        {
            return Err("A jump cannot be extracted into a returning procedure".into());
        }
        let mut params = Vec::new();
        let mut outputs = Vec::new();
        let mut seen = HashSet::new();
        for reference in index
            .references_in(source)
            .filter(|r| range.contains(&r.location.range.start))
        {
            let symbol = index.symbol(reference.symbol);
            if symbol.container.is_none() || !seen.insert(reference.symbol) {
                continue;
            }
            let defined_inside = symbol
                .definition
                .as_ref()
                .is_some_and(|definition| range.contains(&definition.range.start));
            if !defined_inside {
                params.push(symbol);
            }
            let used_after = index
                .references_in(source)
                .any(|r| r.symbol == reference.symbol && r.location.range.start >= range.end);
            let assigned = index
                .references_in(source)
                .filter(|r| r.symbol == reference.symbol && range.contains(&r.location.range.start))
                .any(|r| {
                    document
                        .spans
                        .iter()
                        .position(|span| *span == r.location.range)
                        .is_some_and(|i| {
                            document
                                .tokens
                                .iter()
                                .skip(i + 1)
                                .find(|token| {
                                    !matches!(
                                        token.kind,
                                        Kind::Comma | Kind::LocalVar | Kind::Identifier
                                    )
                                })
                                .is_some_and(|token| token.kind == Kind::Equals)
                        })
                });
            if used_after && (defined_inside || assigned) {
                outputs.push((symbol, defined_inside));
            }
        }
        let has_return = selected.iter().any(|(token, _)| token.kind == Kind::Return);
        let probe = format!("[proc,{name}]\n{text}");
        let tokens = Lexer::new(&probe, &document.label)
            .tokenize()
            .map_err(|_| "Select complete statements")?;
        let (parsed, errors) = Parser::new(tokens, &document.label).parse_recovering();
        if !errors.is_empty() || parsed.scripts.len() != 1 {
            return Err("Select complete statements".into());
        }
        if has_return
            && !parsed.scripts[0]
                .body
                .last()
                .is_some_and(|statement| matches!(statement, Statement::Return { .. }))
        {
            return Err("A selection with returns must end with an unconditional return".into());
        }
        if params.iter().any(|symbol| {
            symbol.definition.as_ref().is_some_and(|definition| {
                document
                    .spans
                    .iter()
                    .position(|span| *span == definition.range)
                    .is_some_and(|i| {
                        document
                            .tokens
                            .get(i + 1)
                            .is_some_and(|token| token.kind == Kind::LParen)
                    })
            })
        }) {
            return Err("An array local cannot be passed as a scalar parameter".into());
        }
        let parameters = params
            .iter()
            .map(|symbol| {
                format!(
                    "{} ${}",
                    symbol.value_type.unwrap_or(Type::Int).name(),
                    symbol.name
                )
            })
            .collect::<Vec<_>>()
            .join(", ");
        let arguments = params
            .iter()
            .map(|symbol| format!("${}", symbol.name))
            .collect::<Vec<_>>()
            .join(", ");
        let returns = if has_return {
            enclosing
                .returns
                .iter()
                .map(|ty| ty.name())
                .collect::<Vec<_>>()
                .join(", ")
        } else {
            outputs
                .iter()
                .map(|(symbol, _)| symbol.value_type.unwrap_or(Type::Int).name())
                .collect::<Vec<_>>()
                .join(", ")
        };
        let values = outputs
            .iter()
            .map(|(symbol, _)| format!("${}", symbol.name))
            .collect::<Vec<_>>()
            .join(", ");
        let mut replacement = String::new();
        for (symbol, new) in outputs.iter().filter(|_| !has_return) {
            if *new {
                replacement.push_str(&format!(
                    "def_{} ${};\n",
                    symbol.value_type.unwrap_or(Type::Int).name(),
                    symbol.name
                ));
            }
        }
        if has_return && !enclosing.returns.is_empty() {
            replacement.push_str("return(");
        } else if !has_return && !outputs.is_empty() {
            replacement.push_str(&format!("{values} = "));
        }
        replacement.push_str(&format!("~{name}({arguments})"));
        replacement.push_str(if has_return && !enclosing.returns.is_empty() {
            ");"
        } else {
            ";"
        });
        if has_return && enclosing.returns.is_empty() {
            replacement.push_str("\nreturn;");
        }
        let body = format!(
            "\n\n[proc,{name}]({parameters}){}\n{}\n{}\n",
            if returns.is_empty() {
                String::new()
            } else {
                format!("({returns})")
            },
            text.trim(),
            if has_return {
                String::new()
            } else if outputs.is_empty() {
                "return;".into()
            } else {
                format!("return({values});")
            }
        );
        let insertion = insertion(document, range.start);
        Ok(CodeAction {
            title: format!("Extract procedure '{name}'"),
            kind: "refactor.extract",
            edits: vec![
                TextEdit {
                    location: Location { source, range },
                    replacement,
                },
                TextEdit {
                    location: Location {
                        source,
                        range: insertion..insertion,
                    },
                    replacement: body,
                },
            ],
            applicability: Applicability::MaybeIncorrect,
        })
    }
}