From 82767c9896de5c9c91ec4bc115b7dbfe55dfd37a Mon Sep 17 00:00:00 2001 From: doty Date: Wed, 14 Nov 2018 16:21:40 -0800 Subject: [PATCH] Key stuff Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags: --- .emacs.d/core.el | 21 +++++--- .emacs.d/custom.el | 2 +- site-lisp/blacken.el | 124 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 8 deletions(-) create mode 100644 site-lisp/blacken.el diff --git a/.emacs.d/core.el b/.emacs.d/core.el index 75b133b..485953f 100644 --- a/.emacs.d/core.el +++ b/.emacs.d/core.el @@ -318,7 +318,8 @@ (define-key c-mode-base-map "\C-m" 'c-context-line-break) (unless is-fb-environment (set-fill-column 120)) - (local-set-key "}" 'indent-on-closing-bracket)) + ;; (local-set-key "}" 'indent-on-closing-bracket) + ) (add-hook 'c-mode-common-hook 'my-c-common-hook) @@ -620,22 +621,28 @@ :modes python-mode) (add-to-list 'flycheck-checkers 'python-fb-flake8) -(when is-fb-environment - (add-hook 'python-mode-hook - (lambda () - (flycheck-select-checker `python-fb-flake8))) - ) - (global-flycheck-mode) ;; ================================================================= ;; Python Support ;; ================================================================= (autoload 'python-mode "python-mode" "Python editing mode." t) +(autoload 'blacken-mode "blacken" "Automatically run black before saving." t) (add-to-list 'auto-mode-alist '("\\.py$" . python-mode)) (add-to-list 'interpreter-mode-alist '("python" . python-mode)) +(defun my-python-mode-hook () + "My hook for `python-mode`." + (when is-fb-environment + (flycheck-select-checker `python-fb-flake8)) + (unless (and (buffer-file-name) + (string-match-p "TARGETS" (buffer-file-name))) + (blacken-mode))) + +(add-hook 'python-mode-hook 'my-python-mode-hook) + + ;; ================================================================= ;; JavaScript Support ;; ================================================================= diff --git a/.emacs.d/custom.el b/.emacs.d/custom.el index 8a57278..6fd8027 100644 --- a/.emacs.d/custom.el +++ b/.emacs.d/custom.el @@ -50,7 +50,7 @@ ((sequence "TODO" "|" "DONE" "ABANDONED" "DEFERRED")))) '(package-selected-packages (quote - (lsp-ui company-lsp cquery mustache-mode clang-format projectile dash-functional mocha add-node-modules-path rjsx-mode xref-js2 js2-refactor company omnisharp geiser cider clojure-mode graphviz-dot-mode multi-term xterm-color thrift markdown-mode tuareg merlin ag use-package flycheck dockerfile-mode js2-mode web-mode zencoding-mode tss switch-window python-mode paredit magit lua-mode go-mode go-autocomplete exec-path-from-shell csharp-mode color-theme-solarized color-theme-monokai auto-complete auto-complete-nxml flymake flyspell json-mode popup ruby-mode company-jedi tide ahg elm-mode monky))) + (wgrep lsp-ui company-lsp cquery mustache-mode clang-format projectile dash-functional mocha add-node-modules-path rjsx-mode xref-js2 js2-refactor company omnisharp geiser cider clojure-mode graphviz-dot-mode multi-term xterm-color thrift markdown-mode tuareg merlin ag use-package flycheck dockerfile-mode js2-mode web-mode zencoding-mode tss switch-window python-mode paredit magit lua-mode go-mode go-autocomplete exec-path-from-shell csharp-mode color-theme-solarized color-theme-monokai auto-complete auto-complete-nxml flymake flyspell json-mode popup ruby-mode company-jedi tide ahg elm-mode monky))) '(reb-re-syntax (quote string)) '(rmail-mail-new-frame t) '(safe-local-variable-values diff --git a/site-lisp/blacken.el b/site-lisp/blacken.el new file mode 100644 index 0000000..5120cab --- /dev/null +++ b/site-lisp/blacken.el @@ -0,0 +1,124 @@ +;;; blacken.el --- Reformat python buffers using the "black" formatter + +;; Copyright (C) 2018 Artem Malyshev + +;; Author: Artem Malyshev +;; Homepage: https://github.com/proofit404/blacken +;; Version: 0.0.1 +;; Package-Requires: ((emacs "25.2")) + +;; This file is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published +;; by the Free Software Foundation; either version 3, or (at your +;; option) any later version. +;; +;; This file is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. +;; +;; For a full copy of the GNU General Public License +;; see . + +;;; Commentary: +;; +;; Blacken uses black to format a Python buffer. It can be called +;; explicitly on a certain buffer, but more conveniently, a minor-mode +;; 'blacken-mode' is provided that turns on automatically running +;; black on a buffer before saving. +;; +;; Installation: +;; +;; Add blacken.el to your load-path. +;; +;; To automatically format all Python buffers before saving, add the +;; function blacken-mode to python-mode-hook: +;; +;; (add-hook 'python-mode-hook 'blacken-mode) +;; +;;; Code: + + +(defgroup blacken nil + "Reformat Python code with \"black\"." + :group 'python) + +(defcustom blacken-executable "black" + "Name of the executable to run." + :type 'string) + +(defcustom blacken-line-length nil + "Line length to enforce." + :type 'number + :safe 'numberp) + +(defun blacken-call-bin (input-buffer output-buffer error-buffer) + "Call process black. + +Send INPUT-BUFFER content to the process stdin. Saving the +output to OUTPUT-BUFFER. Saving process stderr to ERROR-BUFFER. +Return black process the exit code." + (with-current-buffer input-buffer + (let ((process (make-process :name "blacken" + :command `(,blacken-executable ,@(blacken-call-args)) + :buffer output-buffer + :stderr error-buffer + :noquery t + :sentinel (lambda (process event))))) + (set-process-query-on-exit-flag (get-buffer-process error-buffer) nil) + (set-process-sentinel (get-buffer-process error-buffer) (lambda (process event))) + (save-restriction + (widen) + (process-send-region process (point-min) (point-max))) + (process-send-eof process) + (accept-process-output process nil nil t) + (while (process-live-p process) + (accept-process-output process nil nil t)) + (process-exit-status process)))) + +(defun blacken-call-args () + "Build black process call arguments." + (append + (when blacken-line-length + (list "--line-length" (number-to-string blacken-line-length))) + '("-"))) + +;;;###autoload +(defun blacken-buffer (&optional display) + "Try to blacken the current buffer. + +Show black output, if black exit abnormally and DISPLAY is t." + (interactive (list t)) + (let* ((original-buffer (current-buffer)) + (original-point (point)) + (original-window-pos (window-start)) + (tmpbuf (get-buffer-create "*blacken*")) + (errbuf (get-buffer-create "*blacken-error*"))) + ;; This buffer can be left after previous black invocation. It + ;; can contain error message of the previous run. + (dolist (buf (list tmpbuf errbuf)) + (with-current-buffer buf + (erase-buffer))) + (condition-case err + (if (not (zerop (blacken-call-bin original-buffer tmpbuf errbuf))) + (error "Black failed, see %s buffer for details" (buffer-name errbuf)) + (with-current-buffer tmpbuf + (copy-to-buffer original-buffer (point-min) (point-max))) + (mapc 'kill-buffer (list tmpbuf errbuf)) + (goto-char original-point) + (set-window-start (selected-window) original-window-pos)) + (error (message "%s" (error-message-string err)) + (when display + (pop-to-buffer errbuf)))))) + +;;;###autoload +(define-minor-mode blacken-mode + "Automatically run black before saving." + :lighter " Black" + (if blacken-mode + (add-hook 'before-save-hook 'blacken-buffer nil t) + (remove-hook 'before-save-hook 'blacken-buffer t))) + +(provide 'blacken) + +;;; blacken.el ends here