Skip to content

Commit 68d269c

Browse files
authored
feat: inject code before and after leetcode boilerplate (#132)
* Add code templates * chore: cleanup messages * Add "inject_before" and "inject_after" to replace code templates * docs: Update README.md for `inject_before` and `inject_after`
1 parent fc4029f commit 68d269c

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ SUBCOMMANDS:
6868

6969
## Example
7070

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

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

187187
<br>
188188

189+
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:
190+
191+
```toml
192+
[code]
193+
inject_before = ["#include<bits/stdc++.h", "using namespace std;"]
194+
inject_after = ["int main() {\n Solution solution;\n\n}"]
195+
```
196+
189197
#### 1. <kbd>pick</kbd>
190198

191199
```sh

src/cache/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ impl Cache {
356356
) -> Result<VerifyResult, Error> {
357357
trace!("Exec problem filter —— Test or Submit");
358358
let (json, [url, refer]) = self.pre_run_code(run.clone(), rfid, test_case).await?;
359-
trace!("Pre run code result {:#?}, {}, {}", json, url, refer);
359+
trace!("Pre-run code result {:#?}, {}, {}", json, url, refer);
360360

361361
let text = self
362362
.0
@@ -367,7 +367,7 @@ impl Cache {
367367
.await?;
368368

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

373373
trace!("Run code result {:#?}", run_res);

src/cmds/edit.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ impl Command for EditCommand {
9393
file_code.write_all(p_desc_comment.as_bytes())?;
9494
file_code.write_all(question_desc.as_bytes())?;
9595
}
96+
if let Some(inject_before) = &conf.code.inject_before {
97+
for line in inject_before {
98+
file_code.write_all((line.to_string() + "\n").as_bytes())?;
99+
}
100+
}
96101
if conf.code.edit_code_marker {
97102
file_code.write_all(
98103
(conf.code.comment_leading.clone()
@@ -112,6 +117,11 @@ impl Command for EditCommand {
112117
.as_bytes(),
113118
)?;
114119
}
120+
if let Some(inject_after) = &conf.code.inject_after {
121+
for line in inject_after {
122+
file_code.write_all((line.to_string() + "\n").as_bytes())?;
123+
}
124+
}
115125

116126
if test_flag {
117127
let mut file_tests = File::create(&test_path)?;

src/config/code.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ pub struct Code {
2222
pub start_marker: String,
2323
#[serde(default, skip_serializing)]
2424
pub end_marker: String,
25+
#[serde(rename(serialize = "inject_before"), alias = "inject_before", default)]
26+
pub inject_before: Option<Vec<String>>,
27+
#[serde(rename(serialize = "inject_after"), alias = "inject_after", default)]
28+
pub inject_after: Option<Vec<String>>,
2529
#[serde(default, skip_serializing)]
2630
pub comment_problem_desc: bool,
2731
#[serde(default, skip_serializing)]
@@ -43,6 +47,8 @@ impl Default for Code {
4347
edit_code_marker: false,
4448
start_marker: "".into(),
4549
end_marker: "".into(),
50+
inject_before: None,
51+
inject_after: None,
4652
comment_problem_desc: false,
4753
comment_leading: "".into(),
4854
test: true,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy