Skip to content

Commit

Permalink
Rename BAML class (#1518)
Browse files Browse the repository at this point in the history
<!-- ELLIPSIS_HIDDEN -->


> [!IMPORTANT]
> Add class renaming support to BAML language server with new methods
for locating class definitions and handling rename requests.
> 
>   - **Behavior**:
> - Adds `find_class_locations()` to `IRHelper` in `mod.rs` to locate
class definitions and references.
> - Updates `WasmRuntime` in `mod.rs` to include
`search_for_class_locations()` and `is_valid_class()`.
> - Implements class renaming in `server.ts` with `textDocument/rename`
request, supporting only classes.
>   - **Misc**:
> - Adds `SymbolLocation` struct in `mod.rs` for tracking symbol
positions.
> - Updates `getCurrentOpenedFile()` in `get-open-file.ts` for better
file handling.
>     - Minor logging and error handling improvements in `server.ts`.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup>
for bb4826c. It will automatically
update as commits are pushed.</sup>


<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
antoniosarosi authored Feb 24, 2025
1 parent 3510139 commit 339068a
Show file tree
Hide file tree
Showing 11 changed files with 380 additions and 87 deletions.
72 changes: 72 additions & 0 deletions engine/baml-lib/baml-core/src/ir/ir_helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ mod error_utils;
pub mod scope_diagnostics;
mod to_baml_arg;

use std::collections::HashSet;

use indexmap::IndexMap;
use internal_baml_diagnostics::Span;
use internal_baml_schema_ast::ast::{WithIdentifier, WithSpan};
use itertools::Itertools;

use self::scope_diagnostics::ScopeStack;
Expand All @@ -14,6 +18,7 @@ use crate::{
TypeAlias,
},
};

use anyhow::Result;
use baml_types::{
BamlMap, BamlValue, BamlValueWithMeta, Constraint, ConstraintLevel, FieldType, LiteralValue,
Expand Down Expand Up @@ -50,6 +55,9 @@ pub trait IRHelper {
function: &'a FunctionWalker<'a>,
test_name: &str,
) -> Result<TestCaseWalker<'a>>;

fn find_class_locations(&self, type_name: &str) -> Vec<Span>;

fn check_function_params<'a>(
&'a self,
function: &'a FunctionWalker<'a>,
Expand Down Expand Up @@ -233,6 +241,70 @@ impl IRHelper for IntermediateRepr {
}
}

fn find_class_locations(&self, class_name: &str) -> Vec<Span> {
let mut locations = vec![];

for cls in self.walk_classes() {
// First look for the definition of the class.
if cls.name() == class_name {
locations.push(
cls.item
.attributes
.identifier_span
.as_ref()
.unwrap()
.to_owned(),
);
}

// After that we'll find all the references to this class in the
// fields of other classes.
for field in cls.walk_fields() {
field
.elem()
.r#type
.attributes
.symbol_spans
.iter()
.for_each(|(name, spans)| {
if name == class_name {
locations.extend(spans.iter().cloned());
}
});
}
}

// Now do the same for type aliases.
for alias in self.walk_type_aliases() {
alias
.elem()
.r#type
.attributes
.symbol_spans
.iter()
.for_each(|(name, spans)| {
if name == class_name {
locations.extend(spans.iter().cloned());
}
});
}

// Then find function inputs and outputs pointing to this class.
for func in self.walk_functions() {
func.item
.attributes
.symbol_spans
.iter()
.for_each(|(name, spans)| {
if name == class_name {
locations.extend(spans.iter().cloned());
}
});
}

locations
}

fn check_function_params<'a>(
&'a self,
function: &'a FunctionWalker<'a>,
Expand Down
Loading

0 comments on commit 339068a

Please sign in to comment.