Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update clojure-mode and add kibit-mode
- Loading branch information
Showing
11 changed files
with
335 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/pom.xml | ||
*jar | ||
/lib | ||
/classes | ||
/native | ||
/.lein-failures | ||
/checkouts | ||
/.lein-deps-sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (C) 2012 Alex Redington | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
obtaining a copy of this software and associated documentation | ||
files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following | ||
conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# kibit-mode | ||
|
||
kibit-mode is a combination of a thin wrapper around the excellent | ||
[kibit](https://github.com/jonase/kibit) tool, and a minor for Clojure | ||
buffers that will help you find ways to make your Clojure code more | ||
idiomatic. | ||
|
||
## Requirements | ||
|
||
* [Emacs](http://www.gnu.org/software/emacs/) 24.0 or greater | ||
* [Clojure](http://clojure.org) 1.4 or greater | ||
* [Leiningen](https://github.com/technomancy/leiningen) 1.6.2 or greater | ||
* [clojure-mode](https://github.com/technomancy/clojure-mode) 1.11.5 or greater | ||
|
||
## Configuration | ||
|
||
I use el-get to manage my Emacs config. To add kibit-mode, I added the | ||
following to my package definitions: | ||
|
||
``` | ||
(:name kibit-mode | ||
:type git | ||
:url "https://github.com/aredington/kibit-mode.git" | ||
:after (lambda () | ||
(require 'kibit-mode) | ||
(add-hook 'clojure-mode-hook 'kibit-mode))) | ||
``` | ||
|
||
The important bits are to get kibit-mode.el on your load-path, require | ||
it, and add it as a hook to clojure-mode. Hopefully this works for you | ||
if you also use el-get, if you do not use el-get you are on your own. | ||
|
||
## Usage | ||
|
||
In an open Clojure buffer, hit C-c C-n (you can use the mnemonic that | ||
this **C**ompilation tool will help you catch **N**oob mistakes) to | ||
open a compilation buffer that will tell you where you can replace | ||
code with a terser, more idiomatic expression of the same semantics. | ||
|
||
The compilation buffer will output formatted filename and line number | ||
indicators of kibits suggestions, like follows: | ||
|
||
``` | ||
/Users/alex/projects/pi/src/pi/core.clj:5: | ||
Replace | ||
(if (even? x) x nil) | ||
with | ||
(when (even? x) x) | ||
``` | ||
|
||
You can jump immediately to the suggestion from the compilation buffer | ||
by hitting enter. If you do not have any code that kibit thinks it can | ||
improve, then it will exit happily and the compilation step will | ||
report success. | ||
|
||
## Reverse Double Secret Next Level Maneuver | ||
|
||
If you add the following to your emacs config: | ||
|
||
``` | ||
(add-hook 'clojure-mode-hook 'flymake-mode-on) | ||
``` | ||
|
||
Then kibit-mode will be registered as a flymake checker and check your | ||
code for you as you write. It will highlight the line which starts the | ||
form relevant to kibit's suggestion. This part is pretty ugly and | ||
hacked together with a shell script, be warned. | ||
|
||
## License | ||
|
||
Copyright (C) 2012 Alex Redington | ||
|
||
Distributed under the MIT License |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Setting basedir on flymake tasks doesn't actually get set beyond a | ||
# logging statement, so we hijack the CWD using this script to farm | ||
# out the job to leiningen | ||
|
||
# normalize $0 on certain BSDs | ||
if [ "$(dirname "$0")" = "." ]; then | ||
SCRIPT="$(which $(basename "$0"))" | ||
else | ||
SCRIPT="$0" | ||
fi | ||
|
||
cd "$(dirname "$(dirname "$SCRIPT")")" | ||
lein run -m kibit-mode.flymake $1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
;;; kibit-mode.el --- Enhance clojure-mode with Kibit analysis | ||
|
||
;; Copyright (C) 2012 Alex Redington <http://www.holychao.com> | ||
;; Authors: Alex Redington | ||
;; Created: 2012 | ||
;; Version: 0.1 | ||
;; Keywords: clojure kibit | ||
;; Package-Requires: ((clojure-mode "1.11.5") | ||
;; (mode-compile "2.29")) | ||
|
||
;;; Commentary: | ||
;; | ||
;; This file is NOT part of GNU Emacs. | ||
;; | ||
;; Copyright (C) 2012 Alex Redington | ||
|
||
;; Permission is hereby granted, free of charge, to any person | ||
;; obtaining a copy of this software and associated documentation | ||
;; files (the "Software"), to deal in the Software without | ||
;; restriction, including without limitation the rights to use, | ||
;; copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
;; copies of the Software, and to permit persons to whom the | ||
;; Software is furnished to do so, subject to the following | ||
;; conditions: | ||
|
||
;; The above copyright notice and this permission notice shall be | ||
;; included in all copies or substantial portions of the Software. | ||
|
||
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
;; OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
;;; Documentation: | ||
;; | ||
;; This minor mode acts as a compilation mechanism for interactively replacing | ||
;; clojure s-expressions by their more idiomatic representations. It provides | ||
;; the following capabilities: | ||
;; | ||
;; * Run a check over the currently open file (bound to `\C-c \C-n`). This will | ||
;; open a compilation mode buffer showing the kibit replacements. | ||
;; | ||
;; * Implement a suggested replacement (bound to `r`). This will destroy the | ||
;; extant formatting when the replacement is inserted | ||
|
||
;;; Dependencies: | ||
;; This minor mode depends on `mode-compile` and `clojure-mode`. | ||
|
||
;;; Change Log: | ||
;; | ||
;; 0.1 - First cut of kibit-mode | ||
|
||
;;; Code: | ||
|
||
(require 'clojure-mode) | ||
(require 'compile) | ||
|
||
(defconst kibit-mode-keymap (make-sparse-keymap) "Keymap used in kibit mode") | ||
|
||
(define-key kibit-mode-keymap (kbd "C-c C-n") 'kibit-check) | ||
|
||
(defgroup kibit-mode nil | ||
"Kibit minor mode.") | ||
|
||
(eval-and-compile | ||
(defvar kibit-mode-path | ||
(let ((path (or (locate-library "kibit-mode") load-file-name))) | ||
(and path (file-name-directory path))) | ||
"Directory containing the kibit-mode package. | ||
This is used to execute the supporting kibit analysis execution environment. | ||
The default value is automatically computed from the location of the | ||
Emacs Lisp package.")) | ||
|
||
|
||
;;;###autoload | ||
(define-minor-mode kibit-mode | ||
"Minor mode for kibit compilation support" | ||
:lighter " kibit" | ||
:keymap kibit-mode-keymap) | ||
|
||
(defun kibit-check () | ||
"Runs the current file through kibit check" | ||
(interactive) | ||
(let ((default-directory kibit-mode-path)) | ||
(compile (concat "lein run -m kibit-mode.core " | ||
(buffer-file-name))))) | ||
|
||
(add-to-list 'compilation-error-regexp-alist-alist | ||
'(kibit-mode "\\([0-9A-Za-z_./\:-]+\\.clj\\):\\([0-9]+\\):" 1 2)) | ||
(add-to-list 'compilation-error-regexp-alist 'kibit-mode) | ||
|
||
(require 'flymake) | ||
(defun flymake-kibit-init () | ||
(flymake-simple-make-init-impl | ||
'flymake-create-temp-with-folder-structure nil nil | ||
buffer-file-name | ||
'flymake-get-kibit-cmdline)) | ||
|
||
(defun flymake-get-kibit-cmdline (source base-dir) | ||
(list (concat kibit-mode-path "bin/kibit-flymake.sh") (list source) kibit-mode-path)) | ||
|
||
(push '(".+\\.clj$" flymake-kibit-init) flymake-allowed-file-name-masks) | ||
(push '(".+\\.cljs$" flymake-kibit-init) flymake-allowed-file-name-masks) | ||
|
||
(push '("\\(.*\\):\\([0-9]+\\): \\(ERROR: .* CORRECTION: .*\\)" | ||
1 2 nil 3) | ||
flymake-err-line-patterns) | ||
|
||
(provide 'kibit-mode) | ||
;;; kibit-mode.el ends here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(defproject kibit-mode "0.0.1" | ||
:description "A kibit compiler for Clojure files" | ||
:dependencies [[org.clojure/clojure "1.4.0"] | ||
[jonase/kibit "0.0.3"]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
(ns kibit-mode.core | ||
(:require [kibit.check :as c] | ||
[clojure.java.io :as io] | ||
[clojure.string :as s])) | ||
|
||
(defn report-error | ||
"Given a kibit simplification, print the line number and normalized | ||
form of the expr and the replacement" | ||
[file {:keys [expr alt line] :as simplify-map}] | ||
(println (str file | ||
":" | ||
line | ||
":\n Replace\n " | ||
(pr-str expr) | ||
"\n with\n " | ||
(pr-str alt) | ||
))) | ||
|
||
(defn check-file | ||
[file reporter] | ||
(with-open [reader (io/reader file)] | ||
(let [errors (c/check-reader reader)] | ||
(doseq [simplify-map errors] | ||
(reporter file simplify-map)) | ||
errors))) | ||
|
||
(defn -main | ||
[file] | ||
(when-not (empty? (check-file file report-error)) | ||
(System/exit 1))) |
Oops, something went wrong.