Skip to content

Commit

Permalink
fix #406
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarrus1 committed Jul 23, 2024
1 parent 9049795 commit e84aa7a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixed

- Fixed generic events not being included (see #411).
- Fixed missing IntelliSense for some invalid syntax (see #406).

## [0.13.2]

Expand Down
25 changes: 15 additions & 10 deletions crates/hir-def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,25 +193,30 @@ impl ExprCollector<'_> {
for init in expr.children_by_field_name("initialization", &mut expr.walk()) {
initialization.push(self.collect_expr(init));
}
let condition = expr.child_by_field_name("condition")?;
let iteration = expr.child_by_field_name("iteration")?;
let body = expr.child_by_field_name("body")?;
let for_loop = Expr::Loop {
initialization: initialization.into_boxed_slice(),
condition: self.collect_expr(condition),
iteration: self.maybe_collect_expr(iteration),
body: self.collect_expr(body),
condition: expr
.child_by_field_name("condition")
.map(|it| self.collect_expr(it)),
iteration: expr
.child_by_field_name("iteration")
.and_then(|it| self.maybe_collect_expr(it)),
body: expr
.child_by_field_name("body")
.map(|it| self.collect_expr(it)),
};
Some(self.alloc_expr(for_loop, NodePtr::from(&expr)))
}
TSKind::while_statement | TSKind::do_while_statement => {
let condition = expr.child_by_field_name("condition")?;
let body = expr.child_by_field_name("body")?;
let loop_expr = Expr::Loop {
initialization: Default::default(),
condition: self.collect_expr(condition),
condition: expr
.child_by_field_name("condition")
.map(|it| self.collect_expr(it)),
iteration: None,
body: self.collect_expr(body),
body: expr
.child_by_field_name("body")
.map(|it| self.collect_expr(it)),
};
Some(self.alloc_expr(loop_expr, NodePtr::from(&expr)))
}
Expand Down
4 changes: 3 additions & 1 deletion crates/hir-def/src/body/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope
for init in initialization.iter() {
compute_expr_scopes(*init, body, scopes, scope);
}
compute_expr_scopes(*loop_body, body, scopes, scope);
if let Some(loop_body) = loop_body {
compute_expr_scopes(*loop_body, body, scopes, scope);
}
}
Expr::Condition {
then_branch,
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ pub enum Expr {
},
Loop {
initialization: Box<[ExprId]>,
condition: ExprId,
condition: Option<ExprId>,
iteration: Option<ExprId>,
body: ExprId,
body: Option<ExprId>,
},
Switch {
condition: ExprId,
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ impl InferenceContext<'_> {
for init in initialization.iter() {
self.infer_expr(init);
}
self.infer_expr(condition);
condition.map(|idx| self.infer_expr(&idx));
if let Some(iteration) = iteration {
self.infer_expr(iteration);
}
self.infer_expr(body);
body.map(|idx| self.infer_expr(&idx));
None
}
Expr::Condition {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
source: crates/sourcepawn-studio/tests/text_document/goto_definition/statements.rs
assertion_line: 68
expression: "goto_definition(r#\"\n%! main.sp\nenum struct Foo {\n int bar;\n void Get(int foo) {}\n}\n\nint main() {\n Foo foo;\n for (int i = 1; --i >= 0;) {\n foo.Get(1);\n |\n ^\n }\n foo.Get(1);\n}\n\"#)"
---
[
{
"originSelectionRange": {
"start": {
"line": 8,
"character": 12
},
"end": {
"line": 8,
"character": 15
}
},
"targetUri": "file:///main.sp",
"targetRange": {
"start": {
"line": 2,
"character": 4
},
"end": {
"line": 2,
"character": 24
}
},
"targetSelectionRange": {
"start": {
"line": 2,
"character": 9
},
"end": {
"line": 2,
"character": 12
}
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,26 @@ void foo() {
"#,
));
}

#[test]
fn for_loop_5() {
assert_json_snapshot!(goto_definition(
r#"
%! main.sp
enum struct Foo {
int bar;
void Get(int foo) {}
}
int main() {
Foo foo;
for (int i = 1; --i >= 0;) {
foo.Get(1);
|
^
}
foo.Get(1);
}
"#,
));
}

0 comments on commit e84aa7a

Please sign in to comment.