Skip to content

Commit a5a07df

Browse files
authored
Merge pull request #75 from xch-dev/lsp-improvements
More LSP improvements
2 parents c968b65 + 15aeabe commit a5a07df

File tree

24 files changed

+702
-326
lines changed

24 files changed

+702
-326
lines changed

crates/rue-ast/src/lib.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ ast_nodes!(
8686
DebugStmt,
8787
PathExpr,
8888
PathSegment,
89-
LeadingPathSeparator,
9089
StructInitializerExpr,
9190
StructInitializerField,
9291
LiteralExpr,
@@ -365,10 +364,11 @@ impl AstImportPath {
365364
}
366365

367366
impl AstImportPathSegment {
368-
pub fn initial_separator(&self) -> Option<AstLeadingPathSeparator> {
367+
pub fn separator(&self) -> Option<SyntaxToken> {
369368
self.syntax()
370-
.children()
371-
.find_map(AstLeadingPathSeparator::cast)
369+
.children_with_tokens()
370+
.filter_map(SyntaxElement::into_token)
371+
.find(|token| token.kind() == T![::])
372372
}
373373

374374
pub fn name(&self) -> Option<SyntaxToken> {
@@ -507,10 +507,17 @@ impl AstPathExpr {
507507
}
508508

509509
impl AstPathSegment {
510-
pub fn initial_separator(&self) -> Option<AstLeadingPathSeparator> {
510+
pub fn separator(&self) -> Option<SyntaxToken> {
511511
self.syntax()
512-
.children()
513-
.find_map(AstLeadingPathSeparator::cast)
512+
.children_with_tokens()
513+
.filter_map(SyntaxElement::into_token)
514+
.find(|token| token.kind() == T![::])
515+
.filter(|token| {
516+
let Some(name) = self.name() else {
517+
return true;
518+
};
519+
token.text_range().start() < name.text_range().start()
520+
})
514521
}
515522

516523
pub fn name(&self) -> Option<SyntaxToken> {
@@ -680,6 +687,13 @@ impl AstFieldAccessExpr {
680687
self.syntax().children().find_map(AstExpr::cast)
681688
}
682689

690+
pub fn dot(&self) -> Option<SyntaxToken> {
691+
self.syntax()
692+
.children_with_tokens()
693+
.filter_map(SyntaxElement::into_token)
694+
.find(|token| token.kind() == T![.])
695+
}
696+
683697
pub fn field(&self) -> Option<SyntaxToken> {
684698
self.syntax()
685699
.children_with_tokens()

crates/rue-compiler/src/compile/binding/struct_binding.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use rue_diagnostic::DiagnosticKind;
66
use rue_hir::{BindingSymbol, Declaration, Hir, Symbol, SymbolId, SymbolPath, Value};
77

88
use crate::{
9-
Compiler, CompletionContext, Field, FieldResult, SyntaxField, SyntaxItem, SyntaxItemKind,
10-
compile_field, create_binding, create_binding_for_identifier,
9+
Compiler, CompletionContext, Field, FieldResult, SyntaxField, SyntaxItemKind, compile_field,
10+
create_binding, create_binding_for_identifier,
1111
};
1212

1313
pub fn create_struct_binding(
@@ -70,21 +70,21 @@ pub fn create_struct_binding(
7070

7171
ctx.pop_declaration();
7272

73-
ctx.syntax_map_mut().add_item(SyntaxItem::new(
73+
ctx.add_syntax(
7474
SyntaxItemKind::FieldReference(SyntaxField {
7575
name: name.text().to_string(),
7676
container: ty,
7777
ty: field_type,
7878
}),
7979
name.text_range(),
80-
));
80+
);
8181
}
8282

83-
ctx.syntax_map_mut().add_item(SyntaxItem::new(
83+
ctx.add_syntax(
8484
SyntaxItemKind::CompletionContext(CompletionContext::StructFields {
8585
ty,
8686
specified_fields: Some(specified_fields),
8787
}),
8888
struct_binding.syntax().text_range(),
89-
));
89+
);
9090
}

crates/rue-compiler/src/compile/block.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use rue_hir::{
77
use rue_types::{Type, TypeId, Union};
88

99
use crate::{
10-
Compiler, CompletionContext, SyntaxItem, SyntaxItemKind, compile_expr, compile_type,
11-
create_binding,
10+
Compiler, CompletionContext, SyntaxItemKind, compile_expr, compile_type, create_binding,
1211
};
1312

1413
pub fn compile_block(
@@ -18,10 +17,10 @@ pub fn compile_block(
1817
expected_type: Option<TypeId>,
1918
require_return: bool,
2019
) -> Value {
21-
ctx.syntax_map_mut().add_item(SyntaxItem::new(
20+
ctx.add_syntax(
2221
SyntaxItemKind::CompletionContext(CompletionContext::Expression),
2322
block.syntax().text_range(),
24-
));
23+
);
2524

2625
let index = ctx.mapping_checkpoint();
2726

@@ -37,10 +36,10 @@ pub fn compile_block(
3736
for stmt in block.items() {
3837
let stmt = match stmt {
3938
AstStmtOrExpr::Stmt(stmt) => {
40-
ctx.syntax_map_mut().add_item(SyntaxItem::new(
39+
ctx.add_syntax(
4140
SyntaxItemKind::CompletionContext(CompletionContext::Statement),
4241
stmt.syntax().text_range(),
43-
));
42+
);
4443

4544
stmt
4645
}

crates/rue-compiler/src/compile/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ use rue_ast::{AstExpr, AstNode};
3232
use rue_hir::Value;
3333
use rue_types::TypeId;
3434

35-
use crate::{Compiler, CompletionContext, SyntaxItem, SyntaxItemKind, compile_block};
35+
use crate::{Compiler, CompletionContext, SyntaxItemKind, compile_block};
3636

3737
pub fn compile_expr(ctx: &mut Compiler, expr: &AstExpr, expected_type: Option<TypeId>) -> Value {
38-
ctx.syntax_map_mut().add_item(SyntaxItem::new(
38+
ctx.add_syntax(
3939
SyntaxItemKind::CompletionContext(CompletionContext::Expression),
4040
expr.syntax().text_range(),
41-
));
41+
);
4242

4343
match expr {
4444
AstExpr::PathExpr(expr) => compile_path_expr(ctx, expr),

crates/rue-compiler/src/compile/expr/field_access.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,30 @@ use rue_diagnostic::DiagnosticKind;
55
use rue_hir::Value;
66

77
use crate::{
8-
Compiler, CompletionContext, Field, FieldResult, SyntaxField, SyntaxItem, SyntaxItemKind,
9-
compile_expr, compile_field,
8+
Compiler, CompletionContext, Field, FieldResult, SyntaxField, SyntaxItemKind, compile_expr,
9+
compile_field,
1010
};
1111

1212
pub fn compile_field_access_expr(ctx: &mut Compiler, access: &AstFieldAccessExpr) -> Value {
13-
let mut start = access.syntax().text_range().start();
14-
1513
let expr = if let Some(expr) = access.expr() {
16-
start = expr.syntax().text_range().end();
1714
compile_expr(ctx, &expr, None)
1815
} else {
1916
debug!("Unresolved field access expr");
2017
ctx.builtins().unresolved.clone()
2118
};
2219

23-
ctx.syntax_map_mut().add_item(SyntaxItem::new(
20+
let Some(dot) = access.dot() else {
21+
debug!("Unresolved field access dot");
22+
return ctx.builtins().unresolved.clone();
23+
};
24+
25+
ctx.add_syntax(
2426
SyntaxItemKind::CompletionContext(CompletionContext::StructFields {
2527
ty: expr.ty,
2628
specified_fields: None,
2729
}),
28-
TextRange::new(start, access.syntax().text_range().end()),
29-
));
30+
TextRange::new(dot.text_range().end(), access.syntax().text_range().end()),
31+
);
3032

3133
let Some(name) = access.field() else {
3234
debug!("Unresolved field access name");
@@ -35,14 +37,14 @@ pub fn compile_field_access_expr(ctx: &mut Compiler, access: &AstFieldAccessExpr
3537

3638
match compile_field(ctx, expr.clone(), &Field::Named(name.text())) {
3739
FieldResult::Value(value) => {
38-
ctx.syntax_map_mut().add_item(SyntaxItem::new(
40+
ctx.add_syntax(
3941
SyntaxItemKind::FieldReference(SyntaxField {
4042
name: name.text().to_string(),
4143
container: expr.ty,
4244
ty: value.ty,
4345
}),
4446
name.text_range(),
45-
));
47+
);
4648
value
4749
}
4850
FieldResult::Unknown => {

crates/rue-compiler/src/compile/expr/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{Compiler, PathKind, PathResult, compile_path};
66

77
pub fn compile_path_expr(ctx: &mut Compiler, path: &AstPathExpr) -> Value {
88
let PathResult::Symbol(symbol, override_type, _) =
9-
compile_path(ctx, path.syntax(), path.segments(), PathKind::Symbol)
9+
compile_path(ctx, path.syntax(), path.segments(), PathKind::Symbol, true)
1010
else {
1111
debug!("Unresolved path expr");
1212
return ctx.builtins().unresolved.clone();

crates/rue-compiler/src/compile/expr/struct_initializer.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use rue_hir::{Hir, SymbolPath, Value};
77
use rue_types::{Pair, Type, Union};
88

99
use crate::{
10-
Compiler, CompletionContext, PathKind, PathResult, SyntaxField, SyntaxItem, SyntaxItemKind,
11-
compile_expr, compile_path,
10+
Compiler, CompletionContext, PathKind, PathResult, SyntaxField, SyntaxItemKind, compile_expr,
11+
compile_path,
1212
};
1313

1414
pub fn compile_struct_initializer_expr(
@@ -17,7 +17,7 @@ pub fn compile_struct_initializer_expr(
1717
) -> Value {
1818
let ty = if let Some(path) = expr.path()
1919
&& let PathResult::Type(ty, _) =
20-
compile_path(ctx, path.syntax(), path.segments(), PathKind::Type)
20+
compile_path(ctx, path.syntax(), path.segments(), PathKind::Type, true)
2121
{
2222
ty
2323
} else {
@@ -27,7 +27,7 @@ pub fn compile_struct_initializer_expr(
2727

2828
let semantic = rue_types::unwrap_semantic(ctx.types_mut(), ty, true);
2929

30-
ctx.syntax_map_mut().add_item(SyntaxItem::new(
30+
ctx.add_syntax(
3131
SyntaxItemKind::CompletionContext(CompletionContext::StructFields {
3232
ty: semantic,
3333
specified_fields: Some(
@@ -38,7 +38,7 @@ pub fn compile_struct_initializer_expr(
3838
),
3939
}),
4040
expr.syntax().text_range(),
41-
));
41+
);
4242

4343
let Type::Struct(struct_type) = ctx.ty(semantic).clone() else {
4444
debug!("Unresolved struct initializer due to non struct type");
@@ -98,9 +98,13 @@ pub fn compile_struct_initializer_expr(
9898
continue;
9999
};
100100

101-
if let PathResult::Symbol(symbol, override_type, _) =
102-
compile_path(ctx, field.syntax(), [name].into_iter(), PathKind::Symbol)
103-
{
101+
if let PathResult::Symbol(symbol, override_type, _) = compile_path(
102+
ctx,
103+
field.syntax(),
104+
[name].into_iter(),
105+
PathKind::Symbol,
106+
false,
107+
) {
104108
let ty = ctx.symbol_type(symbol);
105109

106110
let mut value = Value::new(ctx.alloc_hir(Hir::Reference(symbol)), ty)
@@ -124,7 +128,7 @@ pub fn compile_struct_initializer_expr(
124128
continue;
125129
};
126130

127-
ctx.syntax_map_mut().add_item(SyntaxItem::new(
131+
ctx.add_syntax(
128132
SyntaxItemKind::FieldInitializer(SyntaxField {
129133
name: name.text().to_string(),
130134
container: semantic,
@@ -134,7 +138,7 @@ pub fn compile_struct_initializer_expr(
134138
.unwrap_or(value.ty),
135139
}),
136140
name.text_range(),
137-
));
141+
);
138142

139143
if struct_type.fields.contains(name.text()) {
140144
if let Some(expected_type) = expected_field_types.get(name.text()) {

0 commit comments

Comments
 (0)