diff --git a/README.md b/README.md index 982ccd8..dbf6d4c 100644 --- a/README.md +++ b/README.md @@ -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"))] diff --git a/leetcode.el b/leetcode.el index f916a74..095f151 100644 --- a/leetcode.el +++ b/leetcode.el @@ -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) @@ -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: @@ -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.") @@ -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." @@ -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 @@ -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))