Skip to content

Commit

Permalink
feat: add at cursor
Browse files Browse the repository at this point in the history
resolves #184
  • Loading branch information
lilopkins committed Apr 27, 2024
1 parent 92243d0 commit 304d25b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
2 changes: 1 addition & 1 deletion testangel/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Default for Action {
author: String::new(),
visible: true,
group: String::new(),
script: "--: param Integer Example Parameter\n--: return Text Some value to return\nfunction run_action(x)\n -- Your action can be built here!\n return 'Hello, world!'\nend\n".to_string(),
script: "--: param Integer Example Parameter\n--: return Text Some value to return\nfunction run_action(x)\n \n return 'Hello, world!'\nend\n".to_string(),
required_instructions: Vec::new(),
}
}
Expand Down
59 changes: 52 additions & 7 deletions testangel/src/ui/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ impl Component for ActionsModel {

let loc = if instruction.outputs().is_empty() {
format!(
"\n--> {}.{}({})",
"{}.{}({})",
engine.lua_name,
instruction.lua_name(),
params
Expand All @@ -604,7 +604,7 @@ impl Component for ActionsModel {
let _ = returns.pop();

format!(
"\n-- local {} = {}.{}({})",
"local {} = {}.{}({})",
returns,
engine.lua_name,
instruction.lua_name(),
Expand All @@ -613,11 +613,56 @@ impl Component for ActionsModel {
};
// Append LoC
let buffer = self.source_view.buffer();
let mut script = buffer
.text(&buffer.start_iter(), &buffer.end_iter(), false)
.to_string();
script.push_str(&loc);
buffer.set_text(&script);
let text = buffer.text(&buffer.start_iter(), &buffer.end_iter(), false).to_string();
let mut newline_after = true;

// Decide if cursor needs moving down a line (or into function)
let cursor_pos = buffer.cursor_position();
log::debug!("Inserting step, cursor pos: {} (text len: {})", cursor_pos, text.len());
if cursor_pos == 0 || cursor_pos == text.len() as i32 {
// Move cursor into function
log::debug!("Offsetting cursor into function");
for (i, l) in text.lines().enumerate() {
if l.contains("function run_action") {
log::debug!("Function on line {}", i);
if let Some(text_iter) = buffer.iter_at_line_offset(i as i32 + 1, 2) {
buffer.place_cursor(&text_iter);
}
}
}
} else {
// If line is not empty, add new line
let mut line_starts_at = 0;
let mut line_ends_at = text.len();
let mut line_num = 0;

for (idx, c) in text.char_indices() {
if c == '\n' {
if idx < cursor_pos as usize {
line_starts_at = idx + 1;
line_num += 1;
} else {
line_ends_at = idx;
break;
}
}
}

// Move cursor to end and insert newline if needed
let line = &text[line_starts_at..line_ends_at];
log::debug!("cursor on line: {:?}", line);
if !line.trim().is_empty() {
// Offset cursor to end of line
log::debug!("Moving cursor to end of line {} (pos {})", line_num, line.len());
if let Some(iter) = buffer.iter_at_line_offset(line_num, line.len() as i32) {
buffer.place_cursor(&iter);
buffer.insert_at_cursor("\n ");
newline_after = false;
}
}
}

buffer.insert_interactive_at_cursor(&format!("{}{}", loc, if newline_after { "\n " } else { "" }), true);

self.needs_saving = true;
}
Expand Down

0 comments on commit 304d25b

Please sign in to comment.