ClojureScript

Atom

如何安裝 Atom 套件

您可以按下 Command + , (command + 逗號) 或在 Windows 上按下 Control + , (control + 逗號) 來開啟您的 Atom 設定。從那裡,您可以前往「安裝」標籤,並搜尋您想要安裝的套件名稱。如果您偏好使用命令列,您也可以使用 apm 來安裝套件,使用方式如下:

$ apm install [package-name]

Parinfer

與 Java 和 Javascript 不同,ClojureScript 的慣例是將結尾分隔符號放在同一行,而不是放在新的一行。為了幫助管理這個問題,Shaun LeBron 的 Parinfer 可以根據程式碼的縮排,自動平衡您的結尾分隔符號。Chris Oakman 的 Atom Parinfer 套件可以在這裡找到:https://atom.io/packages/parinfer

改進的新行自動縮排

Atom 預設情況下,對於 Lisp 方言的新行不會正確地自動縮排。您可以透過安裝 Jon Spalding 的 lisp-paredit Atom 套件來解決這個問題。如果您已經在使用 Parinfer,並且只想使用 lisp-paredit 套件進行新行縮排,您可以變更 lisp-paredit 的設定為

  • 已啟用:勾選

  • 嚴格:取消勾選

  • 快捷鍵啟用:取消勾選

…​並使用以下快捷鍵更新您的 keymap.cson 檔案

# Provides proper indentation when enter is pressed
'atom-text-editor[data-grammar~="clojure"]':
  'enter': 'lisp-paredit:newline'

# Override newline back to original when in Proto-REPL console
# GitHub Issue: https://github.com/jonspalding/lisp-paredit/issues/26
'ink-console atom-text-editor[data-grammar~="clojure"]':
  'enter': 'editor:newline'
keymap.cson 檔案位於主選單的「Atom > 快捷鍵…」。

Joker Linter

Roman Bataev 的 Joker linter 可以讓您知道何時在程式碼中犯了錯誤。Ryan De La Torre 的 Atom Joker 套件可以在這裡找到:https://atom.io/packages/linter-joker

要使用此功能,您還需要在您的機器上安裝 Joker。您可以從這裡下載,或透過 Homebrew 安裝,使用方式如下:

brew install candid82/brew/joker

Proto REPL

Jason Gilman 的 Proto REPL 可讓您從編輯器評估 ClojureScript 程式碼。

使用 Figwheel 設定 Proto REPL

如果您正在使用 Leiningen 和 Figwheel,將 Proto REPL 連接到應用程式的最快方法是從 Proto REPL 中啟動 Figwheel。要做到這一點,首先將 figwheel-sidecarpiggiebackproto-repl 合併到您的 project.clj 中的開發相依性

:profiles {:dev {:dependencies [[figwheel-sidecar "0.5.9"]
                                [com.cemerick/piggieback "0.2.1"]
                                [proto-repl "0.3.1"]]

接下來,在您的 project.clj 中為 ClojureScript 新增 piggieback 的 nREPL 中介軟體

:repl-options {:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]}

現在開啟 Proto REPL。您可以按下 Command + Shift + P (或在 Windows 上按下 Control + Shift + P) 來開啟命令面板,然後使用命令面板搜尋 Proto REPL: Toggle 命令。

啟動 Proto REPL 後,您應該會在 REPL 指示的底部看到訊息「在 [您的專案路徑] 中使用 lein 啟動 REPL」。如果一切順利,您應該可以在 Proto REPL 中執行以下命令來啟動 Figwheel 並啟動 ClojureScript REPL

(do (use 'figwheel-sidecar.repl-api) (start-figwheel!) (cljs-repl))

在瀏覽器中開啟您在本機託管的網頁,然後嘗試在 Proto REPL 中輸入一些測試程式碼,例如 (js/alert "Hello from Proto REPL!") 來驗證一切是否正常運作。(上面的命令應該會導致瀏覽器中出現警示)。

將命令儲存到 Proto REPL

如果您不想每次都輸入啟動 Figwheel 的命令,您可以將自訂命令儲存到您的 Atom init.js/init.coffee 檔案

atom.commands.add('atom-text-editor', 'Start Figwheel with CLJS REPL', function () {
  protoRepl.executeCode(`
    (when
      (try
        (require 'figwheel-sidecar.repl-api)
        (resolve 'figwheel-sidecar.repl-api/start-figwheel!)
        (catch Throwable _))
      (eval '(do (figwheel-sidecar.repl-api/start-figwheel!)
                 (figwheel-sidecar.repl-api/cljs-repl))))
  `);
});

有關擴充 Proto REPL 的更多文件可以在這裡找到:https://github.com/jasongilman/proto-repl/blob/master/extending_proto_repl.md