Skip to content

Commit

Permalink
Passthrough node now respects indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lread committed Oct 3, 2019
1 parent aa4b946 commit 08b5071
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/fipp/engine.cljc
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(ns fipp.engine
"See: Oleg Kiselyov, Simon Peyton-Jones, and Amr Sabry
Lazy v. Yield: Incremental, Linear Pretty-printing"
(:require [clojure.string :as s]
[fipp.deque :as deque])
(:require [fipp.deque :as deque])
#?(:clj (:import (java.io Writer))))


Expand Down Expand Up @@ -175,26 +174,25 @@
([] (rf))
([res] (rf res))
([res {:keys [op right] :as node}]
(let [indent (peek @tab-stops)]
(let [indent (peek @tab-stops)
format-text (fn [text width]
(let [res* (if (zero? @column)
(do (vswap! column + indent)
(rf res (apply str (repeat indent \space))))
res)]
(vswap! column + width)
(rf res* text)))]
(case op
:text
(let [text (:text node)
res* (if (zero? @column)
(do (vswap! column + indent)
(rf res (apply str (repeat indent \space))))
res)]
(vswap! column + (count text))
(rf res* text))
(let [text (:text node)]
(format-text text (count text)))

:escaped
(let [text (:text node)
res* (if (zero? @column)
(do (vswap! column + indent)
(rf res (apply str (repeat indent \space))))
res)]
(vswap! column inc)
(rf res* text))
(format-text (:text node) 1)

:pass
(rf res (:text node))
(format-text (:text node) 0)

:line
(if (zero? @fits)
(do
Expand Down
51 changes: 51 additions & 0 deletions test/fipp/engine_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,57 @@
(is (= (ppstr [:group (repeat 5 [:span [:escaped "a"] :line])] 10)
(str (apply str (repeat 5 "a ")) "\n")))))

(deftest passthrough-node-test
;; reminder to reader: :pass nodes are to be used with non-visible characters such as ANSI escape codes
(testing "indentation without :pass (as baseline for comparison with subsequent tests)"
(is (= (ppstr [:group
"AA"
[:align
:line
"BB"
:line
"CC"
[:align
:line
"DD"]]] 6)
(str "AA\n"
" BB\n"
" CC\n"
" DD\n"))))
(testing ":pass nodes have a width of zero and respect indentation"
(is (= (ppstr [:group
[:span [:pass "<"] "AA" [:pass ">"]]
[:align
:line
[:span [:pass "<"] "BB" [:pass ">>"]]
:line
[:span [:pass "<<<<<<<<<<"] "CC" [:pass ">>>"]]
[:align
:line
[:span [:pass "<"] "DD" [:pass ">"]]]]] 6)
(str "<AA>\n"
" <BB>>\n"
" <<<<<<<<<<CC>>>\n"
" <DD>\n"))))
(testing ":pass nodes can be used at to affect indent whitespace by placing them before newline"
(is (= (ppstr [:group
"AA"
[:align
[:pass "<"]
:line
"BB"
[:pass "<"]
:line
"CC"
[:align
[:pass "<"]
:line
[:span "DD"]]]] 6)
(str "AA<\n"
" BB<\n"
" CC<\n"
" DD\n")))) )

(deftest terminate-test
(is (= (ppstr [:group "a" [:line "-" ";"] "b"] 1000)
"a-b\n"))
Expand Down

0 comments on commit 08b5071

Please sign in to comment.