summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsko Nõmm <ano@ano.ee>2022-05-01 19:52:41 +0300
committerAsko Nõmm <ano@ano.ee>2022-05-01 19:52:41 +0300
commit64fe2ecd153e6414cf33fc887604a31ee36e7952 (patch)
tree95439aa35bad656b36beede18a0ae424e2ce516b
parente8cb3717a66bf2bf3e5be815ad2c5311311c969e (diff)
Implement newline corrector for list block.
-rw-r--r--src/clarktown/correctors.clj9
-rw-r--r--src/clarktown/correctors/list_block.clj36
-rw-r--r--test/clarktown/correctors/list_block_test.clj66
3 files changed, 108 insertions, 3 deletions
diff --git a/src/clarktown/correctors.clj b/src/clarktown/correctors.clj
index e251595..1a61f2d 100644
--- a/src/clarktown/correctors.clj
+++ b/src/clarktown/correctors.clj
@@ -1,7 +1,8 @@
(ns clarktown.correctors
(:require
[clarktown.correctors.code-block :as code-block]
- [clarktown.correctors.atx-heading-block :as atx-heading-block]))
+ [clarktown.correctors.atx-heading-block :as atx-heading-block]
+ [clarktown.correctors.list-block :as list-block]))
(def
@@ -9,10 +10,12 @@
default-block-separation-correctors
{:newline-above
[code-block/empty-line-above?
- atx-heading-block/empty-line-above?]
+ atx-heading-block/empty-line-above?
+ list-block/empty-line-above?]
:newline-below
[code-block/empty-line-below?
- atx-heading-block/empty-line-below?]})
+ atx-heading-block/empty-line-below?
+ list-block/empty-line-below?]})
(def
diff --git a/src/clarktown/correctors/list_block.clj b/src/clarktown/correctors/list_block.clj
new file mode 100644
index 0000000..cf84bb1
--- /dev/null
+++ b/src/clarktown/correctors/list_block.clj
@@ -0,0 +1,36 @@
+(ns clarktown.correctors.list-block
+ (:require
+ [clojure.string :as string]
+ [clarktown.matchers.list-block :as matcher]))
+
+
+(defn empty-line-above?
+ "Determines whether there's a need for an empty new line
+ above the `line` at the current `index`. In the list block
+ case that's when the above line is not a newline and is not
+ a list block line."
+ [lines line index]
+ (and (matcher/match? line)
+ (> index 0)
+ (nil? (-> (nth lines (- index 1))
+ string/trim
+ matcher/match?))
+ (not (= (-> (nth lines (- index 1))
+ string/trim)
+ ""))))
+
+
+(defn empty-line-below?
+ "Determines whether there's a need for an empty new line
+ above the `line` at the current `index`. In the list block
+ case that's when the below line is not a newline and is not
+ a list block line."
+ [lines line index]
+ (and (matcher/match? line)
+ (> (- (count lines) 1) index)
+ (nil? (-> (nth lines (+ index 1))
+ string/trim
+ matcher/match?))
+ (not (= (-> (nth lines (+ index 1))
+ string/trim)
+ ""))))
diff --git a/test/clarktown/correctors/list_block_test.clj b/test/clarktown/correctors/list_block_test.clj
new file mode 100644
index 0000000..d435caf
--- /dev/null
+++ b/test/clarktown/correctors/list_block_test.clj
@@ -0,0 +1,66 @@
+(ns clarktown.correctors.list-block-test
+ (:require
+ [clojure.test :refer [deftest testing is]]
+ [clarktown.correctors.list-block :as corrector]))
+
+
+(deftest line-block-corrector-test
+ (testing "Empty line above"
+ (let [line "1. Hello"
+ lines ["Some text goes here" line]
+ index 1]
+ (is (true? (corrector/empty-line-above? lines line index)))))
+
+ (testing "Empty line above II"
+ (let [line "* Hello"
+ lines ["Some text goes here" line]
+ index 1]
+ (is (true? (corrector/empty-line-above? lines line index)))))
+
+ (testing "No empty line above"
+ (let [line "1. Hello"
+ lines ["Some text goes here" "\n" line]
+ index 2]
+ (is (false? (corrector/empty-line-above? lines line index)))))
+
+ (testing "No empty line above II"
+ (let [line "* Hello"
+ lines ["Some text goes here" "\n" line]
+ index 2]
+ (is (false? (corrector/empty-line-above? lines line index)))))
+
+ (testing "No empty line above III"
+ (let [line "* Hello"
+ lines ["Some text here" "* Asd" line]
+ index 2]
+ (is (false? (corrector/empty-line-above? lines line index)))))
+
+ (testing "Empty line below"
+ (let [line "1. Hello"
+ lines [line "Some text goes here"]
+ index 0]
+ (is (true? (corrector/empty-line-below? lines line index)))))
+
+ (testing "Empty line below II"
+ (let [line "* Hello"
+ lines [line "Some text goes here"]
+ index 0]
+ (is (true? (corrector/empty-line-below? lines line index)))))
+
+ (testing "No empty line below"
+ (let [line "1. Hello"
+ lines [line "\n" "Some text goes here"]
+ index 0]
+ (is (false? (corrector/empty-line-below? lines line index)))))
+
+ (testing "No empty line below II"
+ (let [line "* Hello"
+ lines [line "\n" "Some text goes here"]
+ index 0]
+ (is (false? (corrector/empty-line-below? lines line index)))))
+
+ (testing "No empty line below III"
+ (let [line "* Hello"
+ lines [line "* Asd" "Some text goes here"]
+ index 0]
+ (is (false? (corrector/empty-line-below? lines line index))))))