Skip to content

Commit

Permalink
Clean: Ported and refactored docstring detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ekaschalk committed May 15, 2019
1 parent c07f4d4 commit dde9c9d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
25 changes: 23 additions & 2 deletions hy-base.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,28 @@
(require 'dash-functional)
(require 's)

;;; Syntax State Aliases
;;; Syntax State Methods
;;;; Alias `syntax-ppss' and `parse-partial-sexp'

;; Alias `parse-partial-sexp' and `syntax-ppss' components
(defun hy--syntax->inner-char (syntax)
"Get innermost char of SYNTAX."
(nth 1 syntax))

(defun hy--syntax->string-start (syntax)
"Return start of STATE that is in a string."
(nth 8 syntax))

(defun hy--goto-inner-sexp (syntax)
"Goto innermost sexp of SYNTAX."
(-some-> syntax hy--syntax->inner-char 1+ goto-char))

(defun hy--syntax->inner-symbol (syntax)
"Get innermost sexp of SYNTAX."
(save-excursion
(when (hy--goto-inner-sexp syntax)
(thing-at-point 'symbol))))

;;;; Old

(defun hy--sexp-inermost-char (state)
"Return innermost char of syntax STATE."
Expand All @@ -53,6 +72,8 @@
"Return start of syntax STATE that is in a string."
(nth 8 state))

;;;; Methods

(defun hy--prior-sexp? (state)
"Is there a prior sexp from syntax STATE?"
(number-or-marker-p (hy--start-of-last-sexp state)))
Expand Down
28 changes: 26 additions & 2 deletions hy-font-lock.el
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

;; Font lock definitions and setup for `hy-mode'.

;; Font locks are organized and exposed at the end via `hy-font-lock-kwds'
;; Font locks are organized and exposed at the end: `hy-font-lock-kwds'
;; Also implements docstring detection: `hy-font-lock-syntactic-face-function'

;;; Code:

Expand Down Expand Up @@ -519,8 +520,32 @@
'(1 font-lock-comment-face t))
"Support for higlighting #_(form) the form as a comment.")

;;; Syntactic Face Function
;;;; Utilities

(defun hy-font-lock--string-is-module-docstring? (syntax)
"Is string SYNTAX specifically a module docstring?"
(= 1 (hy--syntax->string-start syntax)))

(defun hy-font-lock--string-is-function-docstring? (syntax)
"Is string SYNTAX specifically a function docstring?"
(-when-let (inner-symbol (hy--syntax->inner-symbol syntax))
(unless (s-equals? inner-symbol "defmethod")
(s-matches? (rx "def"
(not blank))
inner-symbol))))

;;; Font Lock Keywords

(defun hy-font-lock-syntactic-face-function (syntax)
"Return syntactic face function for synatax STATE."
(if (hy--in-string? syntax)
(if (or (hy-font-lock--string-is-module-docstring? syntax)
(hy-font-lock--string-is-function-docstring? syntax))
font-lock-doc-face
font-lock-string-face)
font-lock-comment-face))

(defconst hy-font-lock-kwds
(list hy-font-lock--kwds-builtins
hy-font-lock--kwds-class
Expand All @@ -544,7 +569,6 @@
;; Optional kwds
(when hy-font-lock-highlight-percent-args?
hy-font-lock--kwds-anonymous-funcs))

"All Hy font lock keywords.")

;;; Provide:
Expand Down
24 changes: 0 additions & 24 deletions hy-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -279,30 +279,6 @@ and determined by `font-lock-mode' internals when making an edit to a buffer."
(put-text-property (match-end 1) (1+ (match-end 1))
'syntax-table (string-to-syntax "|")))))

;;; Font Lock Syntactics

(defun hy--string-in-doc-position? (state)
"Is STATE within a docstring?"
(if (= 1 (hy--start-of-string state)) ; Identify module docstring
t
(-when-let* ((first-sexp (hy--sexp-inermost-char state))
(function (save-excursion
(goto-char (1+ first-sexp))
(thing-at-point 'symbol))))
(and (s-matches? (rx "def" (not blank)) function)
(not (s-matches? (rx "defmethod") function))))))

(defun hy-font-lock-syntactic-face-function (state)
"Return syntactic face function for the position represented by STATE.
STATE is a `parse-partial-sexp' state, and the returned function is the
Lisp font lock syntactic face function. String is shorthand for either
a string or comment."
(if (hy--in-string? state)
(if (hy--string-in-doc-position? state)
font-lock-doc-face
font-lock-string-face)
font-lock-comment-face))

;;; Shell Integration
;;;; Configuration

Expand Down

0 comments on commit dde9c9d

Please sign in to comment.