Skip to content

Commit

Permalink
feat: improve function hover text
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestrew committed Dec 31, 2023
1 parent 2615917 commit 858f284
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/eval/env/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ fn function_definition_hover() {
.pos_value(&Position::new(14, 9))
.expect("obj should be some");

assert_eq!(value.obj, Object::Function(2, Box::new(Object::Unknown)));
assert_eq!(
value.obj,
Object::Function(vec!["x".into(), "y".into()], Box::new(Object::Unknown))
);
assert_eq!(
value.ident_rng,
Range::new(Position::new(14, 4), Position::new(14, 13))
Expand Down
8 changes: 5 additions & 3 deletions src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ impl<'source> Eval {
Ok(body) => {
let child_env = Env::new_child(self.env.clone(), body);

let mut param_names = Vec::new();
for param in params {
match param {
Ok(Expression::Identifier(ident_expr)) => {
Expand All @@ -299,14 +300,15 @@ impl<'source> Eval {
Arc::clone(&ident_expr.name),
);
child_env.insert_store(value);
param_names.push(Arc::clone(&ident_expr.name));
}
Ok(_) => unreachable!(),
Err(err) => diags.push(err.clone().into()),
}
}

let (body_obj, body_diags) = Self::eval_block_stmt(body, child_env);
obj = Object::Function(params.len(), Box::new(body_obj));
obj = Object::Function(param_names, Box::new(body_obj));
diags.extend(body_diags);
}
Err(err) => diags.push(err.clone().into()),
Expand All @@ -331,8 +333,8 @@ impl<'source> Eval {
let arg_count;
let ret_type;
match func_obj {
Object::Function(count, r_type) => {
arg_count = count;
Object::Function(args, r_type) => {
arg_count = args.len();
ret_type = *r_type;
}
Object::Builtin(func) => {
Expand Down
7 changes: 4 additions & 3 deletions src/eval/object.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use tower_lsp::lsp_types::{CompletionItem, CompletionItemKind};

use crate::ast::Call;
Expand All @@ -10,7 +12,7 @@ pub enum Object {
Int,
Bool,
String,
Function(usize, Box<Object>),
Function(Vec<Arc<str>>, Box<Object>),
Builtin(Builtin),
Array,
Hash,
Expand All @@ -35,8 +37,7 @@ impl Object {

pub fn hover_content(&self) -> String {
match self {
Object::Function(arg_count, ret_type) => {
let args: Vec<String> = (0..*arg_count).map(|i| format!("arg{i}")).collect();
Object::Function(args, ret_type) => {
format!("fn({}) -> {}", args.join(", "), ret_type.typename())
}
Object::Builtin(func) => func.hover_content(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: "format!(\"{:#?}\\n\\n========================\\n\\n{:#?}\", env, di
---
Environment {
store: [
Value("foo", Function(0, Int), (0,4)->(0,7), (0,0)->(0,29)),
Value("foo", Function([], Int), (0,4)->(0,7), (0,0)->(0,29)),
],
refs: [
Pos("foo", "(0,4)->(0,7)"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: "format!(\"{:#?}\\n\\n========================\\n\\n{:#?}\", env, di
---
Environment {
store: [
Value("add", Function(2, Unknown), (0,4)->(0,7), (0,0)->(0,36)),
Value("add", Function(["x", "y"], Unknown), (0,4)->(0,7), (0,0)->(0,36)),
],
refs: [
Pos("add", "(0,4)->(0,7)"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: "format!(\"{:#?}\\n\\n========================\\n\\n{:#?}\", env, di
---
Environment {
store: [
Value("add", Function(2, Unknown), (1,4)->(1,7), (1,0)->(1,36)),
Value("add", Function(["x", "y"], Unknown), (1,4)->(1,7), (1,0)->(1,36)),
Value("x", Unknown, (3,4)->(3,5), (3,0)->(3,18)),
],
refs: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: "format!(\"{:#?}\\n\\n========================\\n\\n{:#?}\", env, di
---
Environment {
store: [
Value("add", Function(2, Unknown), (1,4)->(1,7), (1,0)->(4,2)),
Value("add", Function(["x", "y"], Unknown), (1,4)->(1,7), (1,0)->(4,2)),
],
refs: [
Pos("add", "(1,4)->(1,7)"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: "format!(\"{:#?}\\n\\n========================\\n\\n{:#?}\", env, di
---
Environment {
store: [
Value("f", Function(0, Nil), (1,4)->(1,5), (1,0)->(1,17)),
Value("f", Function([], Nil), (1,4)->(1,5), (1,0)->(1,17)),
Value("a", Nil, (2,4)->(2,5), (2,0)->(2,12)),
],
refs: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ expression: "format!(\"{:#?}\\n\\n========================\\n\\n{:#?}\", env, di
---
Environment {
store: [
Value("add", Function(2, Function(0, Unknown)), (1,4)->(1,7), (1,0)->(7,2)),
Value("mult", Function(0, Unknown), (9,4)->(9,8), (9,0)->(9,21)),
Value("add", Function(["x", "y"], Function([], Unknown)), (1,4)->(1,7), (1,0)->(7,2)),
Value("mult", Function([], Unknown), (9,4)->(9,8), (9,0)->(9,21)),
],
refs: [
Pos("add", "(1,4)->(1,7)"),
Expand Down

0 comments on commit 858f284

Please sign in to comment.