Skip to content

feat: inject code before and after leetcode boilerplate #132

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Merged
merged 4 commits into from
Sep 8, 2023
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ SUBCOMMANDS:

## Example

For example, given this config (could be found at `~/.leetcode/leetcode.toml`):
To configure leetcode-cli, create a file at `~/.leetcode/leetcode.toml`):

```toml
[code]
Expand Down Expand Up @@ -186,6 +186,14 @@ pub fn three_sum(nums: Vec<i32>) -> Vec<Vec<i32>> {

<br>

Some linting tools/lsps will throw errors unless the necessary libraries are imported. leetcode-cli can generate this boilerplate automatically if the `inject_before` key is set. Similarly, if you want to test out your code locally, you can automate that with `inject_after`. For c++ this might look something like:

```toml
[code]
inject_before = ["#include<bits/stdc++.h", "using namespace std;"]
inject_after = ["int main() {\n Solution solution;\n\n}"]
```

#### 1. <kbd>pick</kbd>

```sh
Expand Down
4 changes: 2 additions & 2 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ impl Cache {
) -> Result<VerifyResult, Error> {
trace!("Exec problem filter —— Test or Submit");
let (json, [url, refer]) = self.pre_run_code(run.clone(), rfid, test_case).await?;
trace!("Pre run code result {:#?}, {}, {}", json, url, refer);
trace!("Pre-run code result {:#?}, {}, {}", json, url, refer);

let text = self
.0
Expand All @@ -367,7 +367,7 @@ impl Cache {
.await?;

let run_res: RunCode = serde_json::from_str(&text).map_err(|e| {
anyhow!("json error: {e}, plz double check your session and csrf config.")
anyhow!("JSON error: {e}, please double check your session and csrf config.")
})?;

trace!("Run code result {:#?}", run_res);
Expand Down
10 changes: 10 additions & 0 deletions src/cmds/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ impl Command for EditCommand {
file_code.write_all(p_desc_comment.as_bytes())?;
file_code.write_all(question_desc.as_bytes())?;
}
if let Some(inject_before) = &conf.code.inject_before {
for line in inject_before {
file_code.write_all((line.to_string() + "\n").as_bytes())?;
}
}
if conf.code.edit_code_marker {
file_code.write_all(
(conf.code.comment_leading.clone()
Expand All @@ -112,6 +117,11 @@ impl Command for EditCommand {
.as_bytes(),
)?;
}
if let Some(inject_after) = &conf.code.inject_after {
for line in inject_after {
file_code.write_all((line.to_string() + "\n").as_bytes())?;
}
}

if test_flag {
let mut file_tests = File::create(&test_path)?;
Expand Down
6 changes: 6 additions & 0 deletions src/config/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ pub struct Code {
pub start_marker: String,
#[serde(default, skip_serializing)]
pub end_marker: String,
#[serde(rename(serialize = "inject_before"), alias = "inject_before", default)]
pub inject_before: Option<Vec<String>>,
#[serde(rename(serialize = "inject_after"), alias = "inject_after", default)]
pub inject_after: Option<Vec<String>>,
#[serde(default, skip_serializing)]
pub comment_problem_desc: bool,
#[serde(default, skip_serializing)]
Expand All @@ -43,6 +47,8 @@ impl Default for Code {
edit_code_marker: false,
start_marker: "".into(),
end_marker: "".into(),
inject_before: None,
inject_after: None,
comment_problem_desc: false,
comment_leading: "".into(),
test: true,
Expand Down