Skip to content

Commit

Permalink
fix: Append enum bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Dustin Blackman committed Nov 9, 2023
1 parent e466672 commit 2047204
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 53 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
All notable changes to this project will be documented in this file.

## [0.2.0] - 2023-11-09
[v0.1.0...v0.2.0](https://github.com/dustinblackman/oatmeal/compare/v0.1.0...v0.2.0)

[v0.1.0...v0.2.0](https://github.com/dustinblackman/oatmeal/compare/v0.1.0...v0.2.0)

### ⛰️ Features
### ⛰️ Features

- Release v0.2.0 - ([ba865fc](https://github.com/dustinblackman/oatmeal/commit/ba865fcba3608b444b764dc6e76fb925b114666f))
- Add arguments to copy command - ([bb8eecc](https://github.com/dustinblackman/oatmeal/commit/bb8eeccae3ca0398fe7a454f945dc96de3c1d3ca))
Expand All @@ -31,10 +31,10 @@ All notable changes to this project will be documented in this file.
- Toml lint - ([8466a81](https://github.com/dustinblackman/oatmeal/commit/8466a815bdd9aebaae23d38de389d17d730e5292))

## [0.1.0] - 2023-11-08
[...v0.1.0](https://github.com/dustinblackman/oatmeal/compare/...v0.1.0)

[...v0.1.0](https://github.com/dustinblackman/oatmeal/compare/...v0.1.0)

### ⛰️ Features
### ⛰️ Features

- Release v0.1.0 - ([f1a38b2](https://github.com/dustinblackman/oatmeal/commit/f1a38b22c4768c894e506bcf66b39e30ec1d5afe))
- Init - ([f2216d9](https://github.com/dustinblackman/oatmeal/commit/f2216d9aa15827f644c3991bfc6cac4bfcedcb91))
Expand Down
5 changes: 1 addition & 4 deletions src/domain/models/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ use async_trait::async_trait;
pub enum AcceptType {
/// Append in editor where the cursor was last.
Append,
/// Copy to clipboard
Copy,
/// Replace selected code in editor.
Replace,
}

impl fmt::Display for AcceptType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AcceptType::Append => return write!(f, "copy"),
AcceptType::Copy => return write!(f, "replace"),
AcceptType::Append => return write!(f, "append"),
AcceptType::Replace => return write!(f, "replace"),
}
}
Expand Down
35 changes: 14 additions & 21 deletions src/domain/services/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,7 @@ async fn accept_codeblock(
context: Option<EditorContext>,
codeblock: String,
accept_type: AcceptType,
tx: &mpsc::UnboundedSender<Action>,
) -> Result<()> {
if accept_type == AcceptType::Copy {
ClipboardService::set(codeblock)?;

tx.send(Action::MessageEvent(Message::new(
Author::Oatmeal,
"Copied code block to clipboard.",
)))?;
return Ok(());
}

let editor_name = Config::get(ConfigKey::Editor);
let editor = EditorManager::get(&editor_name)?;

Expand All @@ -146,15 +135,19 @@ async fn accept_codeblock(
}

fn copy_messages(messages: Vec<Message>, tx: &mpsc::UnboundedSender<Action>) -> Result<()> {
let formatted = messages
.iter()
.map(|message| {
return format!("{}: {}", message.author_formatted, message.text);
})
.collect::<Vec<String>>()
.join("\n\n");

ClipboardService::set(formatted)?;
if messages.len() == 1 {
ClipboardService::set(messages[0].text.to_string())?;
} else {
let formatted = messages
.iter()
.map(|message| {
return format!("{}: {}", message.author_formatted, message.text);
})
.collect::<Vec<String>>()
.join("\n\n");

ClipboardService::set(formatted)?;
}

tx.send(Action::MessageEvent(Message::new(
Author::Oatmeal,
Expand Down Expand Up @@ -190,7 +183,7 @@ impl ActionsService {

match event.unwrap() {
Action::AcceptCodeBlock(context, codeblock, accept_type) => {
accept_codeblock(context, codeblock, accept_type, &tx).await?;
accept_codeblock(context, codeblock, accept_type).await?;
}
Action::CopyMessages(messages) => {
copy_messages(messages, &tx)?;
Expand Down
27 changes: 15 additions & 12 deletions src/domain/services/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,7 @@ impl<'a> AppState<'a> {
|| command.is_replace_code_block()
|| command.is_copy_code_block()
{
let mut accept_type = AcceptType::Append;
if command.is_replace_code_block() {
accept_type = AcceptType::Replace;
} else if command.is_copy_code_block() {
accept_type = AcceptType::Copy;
}
should_continue = true;

let codeblocks_res = self.codeblocks.blocks_from_slash_commands(&command);
if let Err(err) = codeblocks_res.as_ref() {
Expand All @@ -163,20 +158,28 @@ impl<'a> AppState<'a> {
),
));

should_continue = true;
return Ok((should_break, should_continue));
}

if command.is_copy_code_block() {
tx.send(Action::CopyMessages(vec![Message::new(
Author::Model,
&codeblocks_res.unwrap(),
)]))?;
self.waiting_for_backend = true;
return Ok((should_break, should_continue));
}

let mut accept_type = AcceptType::Append;
if command.is_replace_code_block() {
accept_type = AcceptType::Replace;
}

tx.send(Action::AcceptCodeBlock(
self.editor_context.clone(),
codeblocks_res.unwrap(),
accept_type,
))?;

should_continue = true;
if command.is_copy_code_block() {
self.waiting_for_backend = true;
}
}

if command.is_copy_chat() {
Expand Down
10 changes: 5 additions & 5 deletions src/domain/services/app_state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ mod handle_slash_commands {
Action::AcceptCodeBlock(_context, codeblock, accept_type) => {
assert_eq!(accept_type, AcceptType::Append);
insta_snapshot(|| {
insta::assert_yaml_snapshot!(codeblock);
insta::assert_toml_snapshot!(codeblock);
})
}
_ => bail!("Wrong enum"),
Expand Down Expand Up @@ -95,7 +95,7 @@ mod handle_slash_commands {
Action::AcceptCodeBlock(_context, codeblock, accept_type) => {
assert_eq!(accept_type, AcceptType::Replace);
insta_snapshot(|| {
insta::assert_yaml_snapshot!(codeblock);
insta::assert_toml_snapshot!(codeblock);
})
}
_ => bail!("Wrong enum"),
Expand All @@ -120,10 +120,10 @@ mod handle_slash_commands {

let event = rx.blocking_recv().unwrap();
match event {
Action::AcceptCodeBlock(_context, codeblock, accept_type) => {
assert_eq!(accept_type, AcceptType::Copy);
Action::CopyMessages(messages) => {
assert_eq!(messages[0].author, Author::Model);
insta_snapshot(|| {
insta::assert_yaml_snapshot!(codeblock);
insta::assert_toml_snapshot!(messages[0].text);
})
}
_ => bail!("Wrong enum"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
source: src/domain/services/app_state_test.rs
expression: codeblock
---
"fn print_numbers() {\n for i in 0..=0 {\n println!(\"{i}\");\n }\n}"

'''
fn print_numbers() {
for i in 0..=0 {
println!("{i}");
}
}'''
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
---
source: src/domain/services/app_state_test.rs
expression: codeblock
expression: "messages[0].text"
---
"fn print_numbers() {\n for i in 0..=0 {\n println!(\"{i}\");\n }\n}"

'''
fn print_numbers() {
for i in 0..=0 {
println!("{i}");
}
}'''
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
source: src/domain/services/app_state_test.rs
expression: codeblock
---
"fn print_numbers() {\n for i in 0..=0 {\n println!(\"{i}\");\n }\n}"

'''
fn print_numbers() {
for i in 0..=0 {
println!("{i}");
}
}'''

0 comments on commit 2047204

Please sign in to comment.