Last active
July 26, 2016 07:09
-
-
Save ibrahima/e3d1227d545538251e9119fcf8241e00 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 flycheck-fix-css-property-sort-order () | |
"Fixes SCSS-lint property sort order errors in the current buffer." | |
(interactive) | |
(cl-loop for error in flycheck-current-errors | |
do | |
(fix-css-property-order error)) | |
) | |
(defun correct-property-order (err) | |
"Extracts correct CSS property order out of ERR." | |
(let* ((err-msg (flycheck-error-message err)) | |
(sort-order-substr (s-chop-prefix "Properties should be ordered " err-msg)) | |
(correct-order-list ) | |
) | |
(s-split ", " sort-order-substr) | |
) | |
) | |
(defun property-order-error? (err) | |
"True if the flycheck error ERR is an scss property order error." | |
(let ((err-msg (flycheck-error-message err))) | |
(s-starts-with? "Properties should be ordered" err-msg) | |
) | |
) | |
(defun get-current-line () | |
"Gets the current line contents, stripped of whitespace." | |
(s-trim | |
(buffer-substring-no-properties | |
(line-beginning-position) | |
(line-end-position)))) | |
(defun current-line-starts-with? (needle) | |
(s-starts-with? needle (get-current-line))) | |
(defun fix-css-property-order (err) | |
"Fixes SCSS-lint's property order error marked by ERR." | |
;;; for property in correct order | |
;;; find line that sets that property, below this line | |
;;; move it up to the current line | |
;;; go on to next line in file and property in list | |
(if (property-order-error? err) | |
(let* ((correct-order (correct-property-order err)) | |
(num-properties (length correct-order)) | |
(lineno (flycheck-error-line err)) | |
) | |
(save-excursion | |
(goto-char (point-min)) | |
(forward-line (- lineno 1)) ; Go to error line | |
(cl-loop for property in correct-order | |
do | |
(push-mark) ; Save current line to mark | |
(save-excursion | |
(cl-loop until (current-line-starts-with? property) do (forward-line)) ; Find the line that starts with property | |
(ignore-errors (transpose-lines 0)) | |
) | |
(pop-mark) | |
(forward-line) | |
)) | |
))) | |
(defcustom flycheck-fix-error-functions nil | |
"Functions to attempt to fix errors. | |
Each function in this hook must accept a single argument: A | |
Flycheck error to fix. The function should, if applicable, | |
attempt to fix the error, or else return nil if it cannot. | |
All functions in this hook are called in order of appearance, | |
until a function returns non-nil. Thus, a function in this hook | |
may return nil, to allow for further processing of the error, or | |
any non-nil value, to indicate that the error was fully fixed | |
and inhibit any further processing. | |
Note that these functions should only ever be called | |
interactively; Flycheck will not attempt to fix errors unsolicited. | |
This variable is an abnormal hook. See Info | |
node `(elisp)Hooks'." | |
:group 'flycheck | |
:type 'hook | |
:package-version '(flycheck . "0.24") | |
:risky t) | |
(add-hook 'flycheck-fix-error-functions 'fix-css-property-order) | |
(defun flycheck-try-fix-errors-at-point () | |
"Attempts to fix errors at point using the list of fixer functions." | |
(interactive) | |
(let ((err (car-safe (flycheck-overlay-errors-at (point))))) | |
(if err | |
(run-hook-with-args-until-success 'flycheck-fix-error-functions err) | |
(message "No errors at point to fix!")))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment