Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.8 KiB

4 years ago
  1. ;;; haskell-sort-imports.el --- Sort the list of Haskell imports at the point alphabetically -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2010 Chris Done
  3. ;; Author: Chris Done <chrisdone@gmail.com>
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software: you can redistribute it and/or
  6. ;; modify it under the terms of the GNU General Public License as
  7. ;; published by the Free Software Foundation, either version 3 of
  8. ;; the License, or (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be
  10. ;; useful, but WITHOUT ANY WARRANTY; without even the implied
  11. ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. ;; PURPOSE. See the GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public
  14. ;; License along with this program. If not, see
  15. ;; <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; If the region is active it sorts the imports within the
  18. ;; region.
  19. ;; This will align and sort the columns of the current import
  20. ;; list. It's more or less the coolest thing on the planet.
  21. ;;; Code:
  22. (require 'cl-lib)
  23. (defvar haskell-sort-imports-regexp
  24. (concat "^import[ ]+"
  25. "\\(qualified \\)?"
  26. "[ ]*\\(\"[^\"]*\" \\)?"
  27. "[ ]*\\([A-Za-z0-9_.']*.*\\)"))
  28. ;;;###autoload
  29. (defun haskell-sort-imports ()
  30. "Sort the import list at point. It sorts the current group
  31. i.e. an import list separated by blank lines on either side.
  32. If the region is active, it will restrict the imports to sort
  33. within that region."
  34. (interactive)
  35. (when (haskell-sort-imports-at-import)
  36. (let* ((points (haskell-sort-imports-decl-points))
  37. (current-string (buffer-substring-no-properties (car points)
  38. (cdr points)))
  39. (current-offset (- (point) (car points))))
  40. (if (region-active-p)
  41. (progn (goto-char (region-beginning))
  42. (haskell-sort-imports-goto-import-start))
  43. (haskell-sort-imports-goto-group-start))
  44. (let* ((start (point))
  45. (imports (haskell-sort-imports-collect-imports))
  46. (sorted (sort (cl-copy-list imports)
  47. (lambda (a b)
  48. (string< (haskell-sort-imports-normalize a)
  49. (haskell-sort-imports-normalize b))))))
  50. (when (not (equal imports sorted))
  51. (delete-region start (point))
  52. (mapc (lambda (import) (insert import "\n")) sorted))
  53. (goto-char start)
  54. (when (search-forward current-string nil t 1)
  55. (forward-char (- (length current-string)))
  56. (forward-char current-offset))))))
  57. (defun haskell-sort-imports-normalize (i)
  58. "Normalize an import, if possible, so that it can be sorted."
  59. (if (string-match haskell-sort-imports-regexp
  60. i)
  61. (match-string 3 i)
  62. i))
  63. (defun haskell-sort-imports-collect-imports ()
  64. (let ((imports (list)))
  65. (while (looking-at "import")
  66. (let* ((points (haskell-sort-imports-decl-points))
  67. (string (buffer-substring-no-properties (car points)
  68. (cdr points))))
  69. (goto-char (min (1+ (cdr points))
  70. (point-max)))
  71. (setq imports (cons string imports))))
  72. (reverse (delq nil (delete-dups imports)))))
  73. (defun haskell-sort-imports-goto-group-start ()
  74. "Go to the start of the import group."
  75. (or (and (search-backward "\n\n" nil t 1)
  76. (goto-char (+ 2 (line-end-position))))
  77. (when (search-backward-regexp "^module " nil t 1)
  78. (goto-char (1+ (line-end-position))))
  79. (goto-char (point-min))))
  80. (defun haskell-sort-imports-at-import ()
  81. "Are we at an import?"
  82. (save-excursion
  83. (haskell-sort-imports-goto-import-start)
  84. (looking-at "import")))
  85. (defun haskell-sort-imports-goto-import-start ()
  86. "Go to the start of the import."
  87. (goto-char (car (haskell-sort-imports-decl-points))))
  88. (defun haskell-sort-imports-decl-points ()
  89. "Get the points of the declaration."
  90. (save-excursion
  91. (let ((start (or (progn (goto-char (line-end-position))
  92. (search-backward-regexp "^[^ \n]" nil t 1)
  93. (unless (or (looking-at "^-}$")
  94. (looking-at "^{-$"))
  95. (point)))
  96. 0))
  97. (end (progn (goto-char (1+ (point)))
  98. (or (when (search-forward-regexp "[\n]+[^ \n]" nil t 1)
  99. (forward-char -1)
  100. (search-backward-regexp "[^\n ]" nil t)
  101. (line-end-position))
  102. (when (search-forward-regexp "\n" nil t 1)
  103. (1- (point)))
  104. (point-max)))))
  105. (cons start end))))
  106. (provide 'haskell-sort-imports)
  107. ;;; haskell-sort-imports.el ends here