Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions crates/rue-ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ ast_nodes!(
DebugStmt,
PathExpr,
PathSegment,
LeadingPathSeparator,
StructInitializerExpr,
StructInitializerField,
LiteralExpr,
Expand Down Expand Up @@ -365,10 +364,11 @@ impl AstImportPath {
}

impl AstImportPathSegment {
pub fn initial_separator(&self) -> Option<AstLeadingPathSeparator> {
pub fn separator(&self) -> Option<SyntaxToken> {
self.syntax()
.children()
.find_map(AstLeadingPathSeparator::cast)
.children_with_tokens()
.filter_map(SyntaxElement::into_token)
.find(|token| token.kind() == T![::])
}

pub fn name(&self) -> Option<SyntaxToken> {
Expand Down Expand Up @@ -507,10 +507,17 @@ impl AstPathExpr {
}

impl AstPathSegment {
pub fn initial_separator(&self) -> Option<AstLeadingPathSeparator> {
pub fn separator(&self) -> Option<SyntaxToken> {
self.syntax()
.children()
.find_map(AstLeadingPathSeparator::cast)
.children_with_tokens()
.filter_map(SyntaxElement::into_token)
.find(|token| token.kind() == T![::])
.filter(|token| {
let Some(name) = self.name() else {
return true;
};
token.text_range().start() < name.text_range().start()
})
}

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

pub fn dot(&self) -> Option<SyntaxToken> {
self.syntax()
.children_with_tokens()
.filter_map(SyntaxElement::into_token)
.find(|token| token.kind() == T![.])
}

pub fn field(&self) -> Option<SyntaxToken> {
self.syntax()
.children_with_tokens()
Expand Down
12 changes: 6 additions & 6 deletions crates/rue-compiler/src/compile/binding/struct_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use rue_diagnostic::DiagnosticKind;
use rue_hir::{BindingSymbol, Declaration, Hir, Symbol, SymbolId, SymbolPath, Value};

use crate::{
Compiler, CompletionContext, Field, FieldResult, SyntaxField, SyntaxItem, SyntaxItemKind,
compile_field, create_binding, create_binding_for_identifier,
Compiler, CompletionContext, Field, FieldResult, SyntaxField, SyntaxItemKind, compile_field,
create_binding, create_binding_for_identifier,
};

pub fn create_struct_binding(
Expand Down Expand Up @@ -70,21 +70,21 @@ pub fn create_struct_binding(

ctx.pop_declaration();

ctx.syntax_map_mut().add_item(SyntaxItem::new(
ctx.add_syntax(
SyntaxItemKind::FieldReference(SyntaxField {
name: name.text().to_string(),
container: ty,
ty: field_type,
}),
name.text_range(),
));
);
}

ctx.syntax_map_mut().add_item(SyntaxItem::new(
ctx.add_syntax(
SyntaxItemKind::CompletionContext(CompletionContext::StructFields {
ty,
specified_fields: Some(specified_fields),
}),
struct_binding.syntax().text_range(),
));
);
}
11 changes: 5 additions & 6 deletions crates/rue-compiler/src/compile/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use rue_hir::{
use rue_types::{Type, TypeId, Union};

use crate::{
Compiler, CompletionContext, SyntaxItem, SyntaxItemKind, compile_expr, compile_type,
create_binding,
Compiler, CompletionContext, SyntaxItemKind, compile_expr, compile_type, create_binding,
};

pub fn compile_block(
Expand All @@ -18,10 +17,10 @@ pub fn compile_block(
expected_type: Option<TypeId>,
require_return: bool,
) -> Value {
ctx.syntax_map_mut().add_item(SyntaxItem::new(
ctx.add_syntax(
SyntaxItemKind::CompletionContext(CompletionContext::Expression),
block.syntax().text_range(),
));
);

let index = ctx.mapping_checkpoint();

Expand All @@ -37,10 +36,10 @@ pub fn compile_block(
for stmt in block.items() {
let stmt = match stmt {
AstStmtOrExpr::Stmt(stmt) => {
ctx.syntax_map_mut().add_item(SyntaxItem::new(
ctx.add_syntax(
SyntaxItemKind::CompletionContext(CompletionContext::Statement),
stmt.syntax().text_range(),
));
);

stmt
}
Expand Down
6 changes: 3 additions & 3 deletions crates/rue-compiler/src/compile/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ use rue_ast::{AstExpr, AstNode};
use rue_hir::Value;
use rue_types::TypeId;

use crate::{Compiler, CompletionContext, SyntaxItem, SyntaxItemKind, compile_block};
use crate::{Compiler, CompletionContext, SyntaxItemKind, compile_block};

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

match expr {
AstExpr::PathExpr(expr) => compile_path_expr(ctx, expr),
Expand Down
22 changes: 12 additions & 10 deletions crates/rue-compiler/src/compile/expr/field_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,30 @@ use rue_diagnostic::DiagnosticKind;
use rue_hir::Value;

use crate::{
Compiler, CompletionContext, Field, FieldResult, SyntaxField, SyntaxItem, SyntaxItemKind,
compile_expr, compile_field,
Compiler, CompletionContext, Field, FieldResult, SyntaxField, SyntaxItemKind, compile_expr,
compile_field,
};

pub fn compile_field_access_expr(ctx: &mut Compiler, access: &AstFieldAccessExpr) -> Value {
let mut start = access.syntax().text_range().start();

let expr = if let Some(expr) = access.expr() {
start = expr.syntax().text_range().end();
compile_expr(ctx, &expr, None)
} else {
debug!("Unresolved field access expr");
ctx.builtins().unresolved.clone()
};

ctx.syntax_map_mut().add_item(SyntaxItem::new(
let Some(dot) = access.dot() else {
debug!("Unresolved field access dot");
return ctx.builtins().unresolved.clone();
};

ctx.add_syntax(
SyntaxItemKind::CompletionContext(CompletionContext::StructFields {
ty: expr.ty,
specified_fields: None,
}),
TextRange::new(start, access.syntax().text_range().end()),
));
TextRange::new(dot.text_range().end(), access.syntax().text_range().end()),
);

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

match compile_field(ctx, expr.clone(), &Field::Named(name.text())) {
FieldResult::Value(value) => {
ctx.syntax_map_mut().add_item(SyntaxItem::new(
ctx.add_syntax(
SyntaxItemKind::FieldReference(SyntaxField {
name: name.text().to_string(),
container: expr.ty,
ty: value.ty,
}),
name.text_range(),
));
);
value
}
FieldResult::Unknown => {
Expand Down
2 changes: 1 addition & 1 deletion crates/rue-compiler/src/compile/expr/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{Compiler, PathKind, PathResult, compile_path};

pub fn compile_path_expr(ctx: &mut Compiler, path: &AstPathExpr) -> Value {
let PathResult::Symbol(symbol, override_type, _) =
compile_path(ctx, path.syntax(), path.segments(), PathKind::Symbol)
compile_path(ctx, path.syntax(), path.segments(), PathKind::Symbol, true)
else {
debug!("Unresolved path expr");
return ctx.builtins().unresolved.clone();
Expand Down
24 changes: 14 additions & 10 deletions crates/rue-compiler/src/compile/expr/struct_initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use rue_hir::{Hir, SymbolPath, Value};
use rue_types::{Pair, Type, Union};

use crate::{
Compiler, CompletionContext, PathKind, PathResult, SyntaxField, SyntaxItem, SyntaxItemKind,
compile_expr, compile_path,
Compiler, CompletionContext, PathKind, PathResult, SyntaxField, SyntaxItemKind, compile_expr,
compile_path,
};

pub fn compile_struct_initializer_expr(
Expand All @@ -17,7 +17,7 @@ pub fn compile_struct_initializer_expr(
) -> Value {
let ty = if let Some(path) = expr.path()
&& let PathResult::Type(ty, _) =
compile_path(ctx, path.syntax(), path.segments(), PathKind::Type)
compile_path(ctx, path.syntax(), path.segments(), PathKind::Type, true)
{
ty
} else {
Expand All @@ -27,7 +27,7 @@ pub fn compile_struct_initializer_expr(

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

ctx.syntax_map_mut().add_item(SyntaxItem::new(
ctx.add_syntax(
SyntaxItemKind::CompletionContext(CompletionContext::StructFields {
ty: semantic,
specified_fields: Some(
Expand All @@ -38,7 +38,7 @@ pub fn compile_struct_initializer_expr(
),
}),
expr.syntax().text_range(),
));
);

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

if let PathResult::Symbol(symbol, override_type, _) =
compile_path(ctx, field.syntax(), [name].into_iter(), PathKind::Symbol)
{
if let PathResult::Symbol(symbol, override_type, _) = compile_path(
ctx,
field.syntax(),
[name].into_iter(),
PathKind::Symbol,
false,
) {
let ty = ctx.symbol_type(symbol);

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

ctx.syntax_map_mut().add_item(SyntaxItem::new(
ctx.add_syntax(
SyntaxItemKind::FieldInitializer(SyntaxField {
name: name.text().to_string(),
container: semantic,
Expand All @@ -134,7 +138,7 @@ pub fn compile_struct_initializer_expr(
.unwrap_or(value.ty),
}),
name.text_range(),
));
);

if struct_type.fields.contains(name.text()) {
if let Some(expected_type) = expected_field_types.get(name.text()) {
Expand Down
Loading