Skip to content

Add leetcode-ignore-region to ignore lines. #123

New issue

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

By clicking “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? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ You can save solution by setting `leetcode-save-solutions`:
(setq leetcode-directory "~/leetcode")
```

If you want to have some code before your solution, which may make auto-completion work better but shouldn't been submitted, you can customize `leetcode-ignore-region` to ignore lines in your code. For example, suppose you have a `common.h` and some custom definations, and `leetcode-ignore-region` is set to `'("" "// start code")`, only code below `// start code` will be submitted:

```c++
#include "common.h"
... your custom definations ...
// start code

class Solution {
......
```

# Work with Org Mode

`leetcode-show-problem-by-slug` will let you put to org files with a link in this format to show the question after the *leetcode* buffer is load like [elisp:(leetcode-show-problem-by-slug (leetcode--slugify-title "ZigZag Conversion"))]
Expand Down
40 changes: 36 additions & 4 deletions leetcode.el
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
(defcustom leetcode-prefer-language "python3"
"LeetCode programming language.
c, cpp, csharp, golang, java, javascript, typescript, kotlin, php, python,
python3, ruby, rust, scala, swift."
python3, racket, ruby, rust, scala, swift."
:group 'leetcode
:type 'string)

Expand All @@ -123,6 +123,14 @@ mysql, mssql, oraclesql."
:group 'leetcode
:type 'boolean)

(defcustom leetcode-ignore-region '("" "")
"If it's '(MARK1 MARK2), and none of them is \"\", ignore code between lines
starting with \"MARK1\" and \"MARK2\" when submitting code. If only MARK1 is
\"\", ignore code before MARK2. If only MARK2 is \"\", ignore code from MARK1.
If both are \"\", ignore nothing."
:group 'leetcode
:type '(list string string))

(cl-defstruct leetcode-user
"A LeetCode User.
The object with following attributes:
Expand Down Expand Up @@ -194,7 +202,7 @@ Default is programming language.")
("mysql" . ".sql") ("mssql" . ".sql") ("oraclesql" . ".sql"))
"LeetCode programming language suffixes.
c, cpp, csharp, golang, java, javascript, typescript, kotlin, php, python,
python3, ruby, rust, scala, swift, mysql, mssql, oraclesql.")
python3, racket, ruby, rust, scala, swift, mysql, mssql, oraclesql.")

(defvar leetcode--filter-regex nil "Filter rows by regex.")
(defvar leetcode--filter-tag nil "Filter rows by tag.")
Expand Down Expand Up @@ -800,6 +808,30 @@ see: https://github.com/skeeto/emacs-aio/issues/3."
(buffer-substring-no-properties
(point-min) (point-max))))

(defun leetcode--code-buffer-content (buf)
"Get code content without text properties of BUF, taking `leetcode-ignore-region'
into consideration."
(let ((code (leetcode--buffer-content buf)))
(if (not (equal leetcode-ignore-region '("" "")))
(let* ((lines (string-lines code))
(mark-start (car leetcode-ignore-region))
(mark-end (car (cdr leetcode-ignore-region)))
(result '())
(ignore (equal mark-start "")))
(dolist (line lines result)
(cond ((and (not (equal mark-start "")) (string-prefix-p mark-start line))
(setq ignore t)
(push "" result))
((and (not (equal mark-end "")) (string-prefix-p mark-end line))
(setq ignore nil)
(push "" result))
(ignore
(push "" result))
((not ignore)
(push line result))))
(string-join (reverse result) "\n"))
(code))))

(defun leetcode--get-slug-title (code-buf)
"Get slug title before try or submit with CODE-BUF.
LeetCode require slug-title as the request parameters."
Expand All @@ -819,7 +851,7 @@ LeetCode require slug-title as the request parameters."
(backend-id (leetcode-problem-backend-id problem))
(testcase-buf (get-buffer (leetcode--testcase-buffer-name problem-id)))
(result (aio-await (leetcode--api-try backend-id slug-title
(leetcode--buffer-content code-buf)
(leetcode--code-buffer-content code-buf)
(leetcode--buffer-content testcase-buf)))))
(if-let ((error-info (plist-get (car result) :error)))
(progn
Expand Down Expand Up @@ -997,7 +1029,7 @@ STATUS_CODE has following possible value:
(interactive)
(leetcode--loading-mode t)
(let* ((code-buf (current-buffer))
(code (leetcode--buffer-content code-buf))
(code (leetcode--code-buffer-content code-buf))
(slug-title (leetcode--get-slug-title code-buf))
(problem (leetcode--get-problem slug-title))
(problem-id (leetcode-problem-id problem))
Expand Down