branch: elpa/haskell-ts-mode commit 7bca83318c5bea05c7a299f219110e1f3470b310 Author: Pranshu Sharma <pran...@bauherren.ovh> Commit: Pranshu Sharma <pran...@bauherren.ovh>
Added option to use formatter --- README.md | 36 ++++++++++++++---------------------- haskell-ts-mode.el | 54 ++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 62 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index a6306b9aa7..8521a8030b 100644 --- a/README.md +++ b/README.md @@ -115,34 +115,32 @@ development: ## Use a formatter (e.g. hindent, ormolu/formolu) -Put the following code in your init file to bind `C-c C-f` to format -the code in the selected region using +`C-c C-f` in a haskell formats the current region is it is active, or +the current function. + +The default formater is [ormolu](https://hackage.haskell.org/package/ormolu). You can adjust -this to use another formatter. +`haskell-ts-format-command` this to use another formatter. -``` -(defun format-haskell (start end) - (interactive "r") - (let ((file (or buffer-file-name (error "Need to be visiting file"))) - (text (buffer-substring-no-properties start end))) - (shell-command-on-region start end (concat "ormolu --stdin-input-file " file) nil t) - (message "Formatted the code"))) - -(with-eval-after-load 'haskell-ts-mode - (define-key haskell-ts-mode-map (kbd "C-c C-f") 'format-haskell)) -``` ## Pretify Symbols mode `prettify-symbols-mode` can be used to replace common symbols with unicode alternatives. + +Turning on `prettify-symbols-mode` does stuff like turn `->` to +`→`. If you want to prettify words, set `haskell-ts-prettify-words` to +non-nil. This will do stuff like prettify `forall` into `∀` and +`elem` to `∈`. + (add-hook 'haskell-ts-mode 'prettify-symbols-mode) ## Adjusting font lock level -Set `haskell-ts-font-lock-level` accordingly. Default value is 4, so if -you suffer from contagious dehydration, you can lower it. +Set `haskell-ts-font-lock-level` accordingly. The default and most +highest value is 4. You are against vibrancy, you can lower it to +match your dreariness. ## Language server @@ -157,12 +155,6 @@ to your `init.el`: (add-to-list 'eglot-server-programs '(haskell-ts-mode . ("haskell-language-server-wrapper" "--lsp")))) -## Prettify sybmols mode - -Turning on `prettify-symbols-mode` does stuff like turn `->` to `→`. If you -want to prettify words, set `haskell-ts-prettify-words` to non-nil. -This will do stuff like prettify `forall` into `∀` and `elem` to `∈`. - # TODO - Support for M-x align, so that calling it will align all the 'equal' diff --git a/haskell-ts-mode.el b/haskell-ts-mode.el index 2627e8e1c4..466fa7a3fe 100644 --- a/haskell-ts-mode.el +++ b/haskell-ts-mode.el @@ -70,6 +70,12 @@ This will concat `haskell-ts-prettify-words-alist' to `prettify-symbols-alist' in `haskell-ts-mode'." :type 'boolean) +(defcustom haskell-ts-format-command "ormolu --stdin-input-file %s" + "The command used to call the formatter. The input is given as the +standard input. This string is passed to `format', with the one +argument being the `buffer-file-name'." + :type 'string) + (defvar haskell-ts-font-lock-feature-list `((comment str pragma parens) (type definition function args module import operator) @@ -431,7 +437,8 @@ when `haskell-ts-prettify-words' is non-nil.") (defvar-keymap haskell-ts-mode-map :doc "Keymap for haskell-ts-mode." "C-c C-c" #'haskell-ts-compile-region-and-go - "C-c C-r" #'run-haskell) + "C-c C-r" #'run-haskell + "C-c C-f" #'haskell-ts-format) ;;;###autoload (define-derived-mode haskell-ts-mode prog-mode "haskell ts mode" @@ -535,15 +542,15 @@ when `haskell-ts-prettify-words' is non-nil.") (defun haskell-ts-defun-name (node) (treesit-node-text (treesit-node-child node 0))) -(defun haskell-ts-compile-region-and-go () +(defun haskell-ts-compile-region-and-go (start end) "Compile the text from START to END in the haskell proc. If region is not active, reload the whole file." - (interactive) + (interactive (if (region-active-p) + (list (region-beginning) (region-end)) + (list (point-min) (point-max)))) (let ((hs (haskell-ts-haskell-session))) (if (region-active-p) - (let ((str (buffer-substring-no-properties - (region-beginning) - (region-end)))) + (let ((str (buffer-substring-no-properties start end))) (comint-send-string hs ":{\n") (comint-send-string hs @@ -554,6 +561,41 @@ If region is not active, reload the whole file." (comint-send-string hs "\n:}\n")) (comint-send-string hs ":r\n")))) +(defun haskell-ts-current-function-bound () + "Get start and end point of current funciton." + (let (start end) + (save-excursion + (mark-defun) + (setq start (region-beginning)) + (setq end (region-end)) + (deactivate-mark)) + (list start end))) + +(defun haskell-ts-format (start end) + "Format haskell code. + +If region is active, format the code using the comand specified in +`haskell-ts-format-command'. Otherwise, format the current function." + (interactive + (if (region-active-p) + (list (region-beginning) (region-end)) + (haskell-ts-current-function-bound))) + (let ((file (or buffer-file-name (error "Need to be visiting a file"))) + (ra (region-active-p))) + (save-excursion + (goto-char start) + (while (looking-at "[ \t]*$") + (goto-char (line-beginning-position 2))) + (setq start (point))) + (shell-command-on-region start + end + (format haskell-ts-format-command file) + nil + t) + (message "Formatted succesefully.") + (unless ra + (pulse-momentary-highlight-region (region-beginning) (region-end))))) + ;;;###autoload (defun run-haskell () "Run an inferior Haskell process."