Created
April 16, 2018 10:08
-
-
Save knusbaum/a0934a7a6de986420e683704a38bf58f to your computer and use it in GitHub Desktop.
emacs java functions
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
(defun find-dead-imports () | |
(interactive) | |
(make-local-variable 'import-overlays) | |
(if (> (length import-overlays) 0) | |
(progn ; Remove the overlays if there are any | |
(mapcar #'delete-overlay import-overlays) | |
(setq import-overlays '())) | |
(let ((point-save (point)) | |
(matches '()) | |
(jump-point nil)) | |
(goto-char 1) | |
(condition-case err | |
; Collect all the imports | |
(while | |
(add-to-list 'matches | |
(progn | |
(re-search-forward "^\s*import\s.*\\.\\([^*;]*\\);" nil) | |
(match-string-no-properties 1)))) | |
(error nil)) | |
; Set the 'jump-point' to the end of the list of imports, where we will begin searching. | |
(setq jump-point (point)) | |
(let ((find-match ; Finds all the matches for imports in the program text. | |
(lambda (string-to-find) | |
(with-temp-message string-to-find | |
(goto-char jump-point) | |
(condition-case e | |
(re-search-forward (concat "[^0-9a-zA-Z_]" string-to-find "[^0-9a-zA-Z_]") nil) | |
(error ; The class was not found to be used in this file. Let's mark it. | |
(progn | |
(goto-char 1) | |
(condition-case nil | |
(while t ; Mark all instances of the import (in case there are duplicates | |
; I think the 'find-duplicates' function actually takes care of this | |
; but oh, well. | |
(re-search-forward (concat "\\(^\s*import\s.*\\." string-to-find ";\\)") nil) | |
(let ((overlay (make-overlay (match-beginning 0) (match-end 0)))) | |
(overlay-put overlay 'face '(:foreground "red")) | |
(add-to-list 'import-overlays overlay))) | |
(error nil)))))))) | |
(find-duplicates ; Finds and marks all duplicate imports. | |
(lambda (import) | |
(with-temp-message import | |
(goto-char 0) | |
(let ((full-import (concat "^\s*import\s.*\\." import ";"))) | |
(condition-case nil | |
(progn | |
(re-search-forward full-import) ; Skip the first one (obviously) | |
(while t | |
(re-search-forward full-import) | |
(let ((overlay (make-overlay (match-beginning 0) (match-end 0)))) | |
(overlay-put overlay 'face '(:foreground "red")) | |
(add-to-list 'import-overlays overlay)))) | |
(error nil))))))) | |
(mapcar find-match matches) | |
(mapcar find-duplicates matches)) | |
(goto-char point-save)))) | |
(defun sort-imports () | |
(interactive) | |
(let ((point-save (point)) | |
(begin-imports 1) | |
(end-imports 1)) | |
(unwind-protect | |
(progn | |
(goto-char 1) | |
(re-search-forward "^import.*$") | |
(setq begin-imports (match-beginning 0)) | |
(goto-char (point-max)) | |
(re-search-backward "^import.*$") | |
(setq end-imports (match-end 0)) | |
(sort-lines nil begin-imports end-imports) | |
(flush-lines "^$" begin-imports end-imports)) | |
(goto-char point-save)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment