我正在寻找一个编辑器/命令行程序,可以正确地(IMHO)缩进Clojure代码,将第三个和后续的元素与第二个元素对齐:
(let [foo (fun arg1
arg2)
bar (doto (Baz.)
(.setProp abc123))
[v1 v2] (fun2)
[v3 v4] (fun3)]
(xyzzy 42))我尝试过使用"Align Clojure forms by the second element“(不使用第二个元素对齐)、VimClojure、emacs (弄乱了[v3 v4]部件的缩进)、Netbeans中的Enclojure (不使用第二个元素对齐)和Eclipse中的逆时针(不使用第二个元素对齐)的Enclojure for Eclipse。在每种情况下,我都使用最新版本的软件。
如有必要,我可以在编辑器外部(在命令行上)运行美化器。
以下是Aquamacs的输出。注意"cascading“(make-day-panel)调用:
(defn make-mainframe [build-number project-version]
"Create the mainframe.
@param build-number the current build number
@param project-version the Maven project version
@returns the reified JFrame"
(let [[^JToggleButton day-sheets-button
^JToggleButton summaries-button]
(let [day-sheets-button
(doto (JToggleButton. "Day Sheets")
(.setToolTipText
"Switch to the Day Sheets view"))
summaries-button
(doto (JToggleButton. "Summaries")
(.setToolTipText "Switch to the Summaries view"))]
(doto (ButtonGroup.)
(.add day-sheets-button)
(.add summaries-button))
(.setSelected day-sheets-button true)
[day-sheets-button summaries-button])
[^JPanel monday-panel
monday-button
monday-vacation-check
monday-arrive-text-field
monday-depart-text-field] (make-day-panel "Monday")
[^JPanel tuesday-panel
tuesday-button
tuesday-vacation-check
tuesday-arrive-text-field
tuesday-depart-text-field] (make-day-panel "Tuesday")
[^JPanel wednesday-panel
wednesday-button
wednesday-vacation-check
wednesday-arrive-text-field
wednesday-depart-text-field] (make-day-panel "Wednesday")
[^JPanel thursday-panel
thursday-button
thursday-vacation-check
thursday-arrive-text-field
thursday-depart-text-field] (make-day-panel "Thursday")
[^JPanel friday-panel
friday-button
friday-vacation-check
friday-arrive-text-field
friday-depart-text-field] (make-day-panel "Friday")
[^JPanel saturday-panel
saturday-button
saturday-vacation-check
saturday-arrive-text-field
saturday-depart-text-field] (make-day-panel "Saturday")
[^JPanel sunday-panel
sunday-button
sunday-vacation-check
sunday-arrive-text-field
sunday-depart-text-field] (make-day-panel "Sunday")
#_ [week-selector-panel week-selector-combobox week-selector-today-button]
#_ (make-week-selector)
north-panel
(let [^JPanel panel (JPanel.)]
(doto panel
(.setLayout (BoxLayout. panel BoxLayout/X_AXIS))
(.add day-sheets-button)
(.add (Box/createHorizontalStrut 5))
(.add summaries-button)
(.add (Box/createHorizontalGlue))
#_ (.add week-selector-panel)))
^JToggleButton mail-button
(make-button "Mail Report"
(make-icon "email-32x32-plain.png")
"Generate and send a weekly report for the currently selected week")
^JToggleButton report-button
(make-button "Report"
(make-icon "history2-32x32-plain.png")
"Generate a weekly report for the currently selected week")
day-sheet-panel
(let [panel (JPanel. (BorderLayout.))
north-panel (grid-bag-container (JPanel. (GridBagLayout.))
{:insets (Insets. 2 2 2 2)}
[[(doto (JPanel. (GridLayout. 1 0))
(.add report-button)
(.add mail-button))]
[(Box/createHorizontalGlue)
:weightx 1.0 :fill :HORIZONTAL ]
[(doto (JPanel. (GridLayout. 1 0))
(.add monday-panel)
(.add tuesday-panel)
(.add wednesday-panel)
(.add thursday-panel)
(.add friday-panel)
(.add saturday-panel)
(.add sunday-panel))]])]
(doto panel
(.add north-panel BorderLayout/NORTH)
(.setBorder (BorderFactory/createRaisedBevelBorder))))
summaries-panel (let [panel (JPanel.)]
(doto panel))
card-panel (let [panel (JPanel. (CardLayout.))]
(doto panel
(.add day-sheet-panel day-sheet-panel-key)
(.add summaries-panel summaries-panel-key)))
main-panel (doto (JPanel. (BorderLayout.))
(.setBorder
(BorderFactory/createEmptyBorder default-border default-border
default-border default-border))
(.add north-panel BorderLayout/NORTH)
(.add card-panel))
frame (doto (JFrame. (str "SpareTime (version " project-version ", build "
build-number ")"))
(.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(.add main-panel))]
(reify mainview-methods
(frame-add-component-listener [_ listener] (.addComponentListener frame listener))
(frame-center [_] (.setLocationRelativeTo frame nil))
(frame-get-location [_] (.getLocation frame))
(frame-get-size [_] (.getSize frame))
(frame-set-location [_ x y] (.setLocation frame x y))
(frame-set-size [_ width height] (.setSize frame (Dimension. width height)))
(frame-set-visible [_ visible] (.setVisible frame visible)))))发布于 2011-10-19 02:09:20
据我所知,如果你在与缩进续行相同的行上开始一个全新的性别匹配,那么Emacs缩进并不理解发生了什么,这就是上面的let所做的事情。您可以看到类似的问题
(if (< 1 (- 3
1) 3
4))当然,一开始这样写就不是很好的风格,所以也许Emacs是可以原谅的。正如您所提到的,像这样的代码更可能出现在Clojure的let或cond中,而且它的风格也不错--但是Emacs缩进是为其他lisps设计的,这要求每一对缩进都要用括号括起来:
(let [([x
y
z] (foo))
([a
b
c] (bar))]
...)正如您所看到的,这缩进得很好。因此,我认为在某种程度上,有人必须找出如何更新Emacs缩进器的这种情况,这将比让其他编辑器达到Emacs的水平要容易得多。也许那个人应该是你,如果它困扰着你--我想,开源的魔力和负担。
发布于 2011-10-18 23:28:15
据我所知,当前的clojure-mode缩进方式和你想要的一样,除了它将doto作为特殊的处理(也就是说,它从第三种形式开始缩进,以使“目标”更加突出)。
在我的emacs24上使用marmalade的clojure-mode的indent-sexp输出:
(let [foo (fun arg1
arg2)
bar (doto (Baz.)
(.setProp abc123))
[v1 v2] (fun2)
[v3 v4] (fun3)]
(xyzzy 42))我认为要么是你的clojure模式坏了,要么是aquamacs变得不一样了,又变得困难了。我非常推荐在OSX上使用标准的Gnu emacs --只要你将meta映射到除了命令之外的其他东西,你就可以在标准emacs中使用大多数简单的标准osx键组合( command -c,command-v,command-s等)。
附录:大代码块输出(注意make-day-panel调用的正确位置):
(defn make-mainframe [build-number project-version]
"Create the mainframe.
@param build-number the current build number
@param project-version the Maven project version
@returns the reified JFrame"
(let [[^JToggleButton day-sheets-button
^JToggleButton summaries-button]
(let [day-sheets-button
(doto (JToggleButton. "Day Sheets")
(.setToolTipText
"Switch to the Day Sheets view"))
summaries-button
(doto (JToggleButton. "Summaries")
(.setToolTipText "Switch to the Summaries view"))]
(doto (ButtonGroup.)
(.add day-sheets-button)
(.add summaries-button))
(.setSelected day-sheets-button true)
[day-sheets-button summaries-button])
[^JPanel monday-panel
monday-button
monday-vacation-check
monday-arrive-text-field
monday-depart-text-field] (make-day-panel "Monday")
[^JPanel tuesday-panel
tuesday-button
tuesday-vacation-check
tuesday-arrive-text-field
tuesday-depart-text-field] (make-day-panel "Tuesday")
[^JPanel wednesday-panel
wednesday-button
wednesday-vacation-check
wednesday-arrive-text-field
wednesday-depart-text-field] (make-day-panel "Wednesday")
[^JPanel thursday-panel
thursday-button
thursday-vacation-check
thursday-arrive-text-field
thursday-depart-text-field] (make-day-panel "Thursday")
[^JPanel friday-panel
friday-button
friday-vacation-check
friday-arrive-text-field
friday-depart-text-field] (make-day-panel "Friday")
[^JPanel saturday-panel
saturday-button
saturday-vacation-check
saturday-arrive-text-field
saturday-depart-text-field] (make-day-panel "Saturday")
[^JPanel sunday-panel
sunday-button
sunday-vacation-check
sunday-arrive-text-field
sunday-depart-text-field] (make-day-panel "Sunday")
#_ [week-selector-panel week-selector-combobox week-selector-today-button]
#_ (make-week-selector)
north-panel
(let [^JPanel panel (JPanel.)]
(doto panel
(.setLayout (BoxLayout. panel BoxLayout/X_AXIS))
(.add day-sheets-button)
(.add (Box/createHorizontalStrut 5))
(.add summaries-button)
(.add (Box/createHorizontalGlue))
#_ (.add week-selector-panel)))
^JToggleButton mail-button
(make-button "Mail Report"
(make-icon "email-32x32-plain.png")
"Generate and send a weekly report for the currently selected week")
^JToggleButton report-button
(make-button "Report"
(make-icon "history2-32x32-plain.png")
"Generate a weekly report for the currently selected week")
day-sheet-panel
(let [panel (JPanel. (BorderLayout.))
north-panel (grid-bag-container (JPanel. (GridBagLayout.))
{:insets (Insets. 2 2 2 2)}
[[(doto (JPanel. (GridLayout. 1 0))
(.add report-button)
(.add mail-button))]
[(Box/createHorizontalGlue)
:weightx 1.0 :fill :HORIZONTAL ]
[(doto (JPanel. (GridLayout. 1 0))
(.add monday-panel)
(.add tuesday-panel)
(.add wednesday-panel)
(.add thursday-panel)
(.add friday-panel)
(.add saturday-panel)
(.add sunday-panel))]])]
(doto panel
(.add north-panel BorderLayout/NORTH)
(.setBorder (BorderFactory/createRaisedBevelBorder))))
summaries-panel (let [panel (JPanel.)]
(doto panel))
card-panel (let [panel (JPanel. (CardLayout.))]
(doto panel
(.add day-sheet-panel day-sheet-panel-key)
(.add summaries-panel summaries-panel-key)))
main-panel (doto (JPanel. (BorderLayout.))
(.setBorder
(BorderFactory/createEmptyBorder default-border default-border
default-border default-border))
(.add north-panel BorderLayout/NORTH)
(.add card-panel))
frame (doto (JFrame. (str "SpareTime (version " project-version ", build "
build-number ")"))
(.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(.add main-panel))]
(reify mainview-methods
(frame-add-component-listener [_ listener] (.addComponentListener frame listener))
(frame-center [_] (.setLocationRelativeTo frame nil))
(frame-get-location [_] (.getLocation frame))
(frame-get-size [_] (.getSize frame))
(frame-set-location [_ x y] (.setLocation frame x y))
(frame-set-size [_ width height] (.setSize frame (Dimension. width height)))
(frame-set-visible [_ visible] (.setVisible frame visible)))))发布于 2011-10-19 14:18:24
当您从lispwords中删除doto时,VimClojure将按照您想要的方式进行缩进(如果我正确理解了您的示例)。
setlocal lispwords-=dotohttps://stackoverflow.com/questions/7809333
复制相似问题