Skip to content

Commit

Permalink
Support for ecmascript functions with empty body
Browse files Browse the repository at this point in the history
  • Loading branch information
Artawower committed Oct 1, 2021
1 parent 321593d commit b424a51
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ Disable semicolon for ecmascript
#+END_SRC


** TODO Plans [11/13]
** TODO Plans [12/13]
+ [X] Typescript support
+ [X] Javascript support
+ [X] Python support
+ [X] Golang support
+ [X] Add support for multiple logger
+ [X] Add additional function for print only first logger from list.
+ [ ] Add support for ecmascript functions without body like =function func() { }=
+ [X] Add support for ecmascript functions without body like =function func() { }=
+ [X] Improve insert position finding for ecmascript
+ [ ] Improve insert position finding for golang
+ [X] Improve space/tab count calculation before inserted line. Use ==newline-and-indent==
Expand Down
13 changes: 13 additions & 0 deletions test.el
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,17 @@
(should (equal (line-number-at-pos) 7))) 0)
)

(ert-deftest test-ecmascript-empty-body-p ()
"Test that turbo-log--ecmascript-empty-body-p should find func or method."
(should (equal t (turbo-log--ecmascript-empty-body-p "function myAwesomeFunc() { }")))
(should (equal t (turbo-log--ecmascript-empty-body-p "protected myAwesomeFunc() {}")))
(should (equal t (turbo-log--ecmascript-empty-body-p "public myAwesomeFunc(a: string) {}")))
(should (equal t (turbo-log--ecmascript-empty-body-p "private some_bad_function(bad_arg, anotherArg) {}")))
(should (equal t (turbo-log--ecmascript-empty-body-p "some_bad_function(bad_arg, anotherArg) {}")))
(should (equal nil (turbo-log--ecmascript-empty-body-p "someObject {}")))
(should (equal nil (turbo-log--ecmascript-empty-body-p "someObject { }")))
(should (equal nil (turbo-log--ecmascript-empty-body-p "anotherobject { b: 4, }")))
(should (equal nil (turbo-log--ecmascript-empty-body-p "anotherobject { b: 'some-string' }")))
(should (equal nil (turbo-log--ecmascript-empty-body-p "myf {awesomeKey:'some-string'}"))))

;;; test.el ends here
22 changes: 18 additions & 4 deletions turbo-log.el
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
;; Author: Artur Yaroshenko <[email protected]>
;; URL: https://github.com/Artawower/turbo-log
;; Package-Requires: ((emacs "24.4"))
;; Version: 0.9.1
;; Version: 0.10.0

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -167,6 +167,9 @@ When MULTIPLE-LOGGERS-P is nil will choose first logger from list."
(line-number-at-pos)
(+ (line-number-at-pos) 1)))

(defun turbo-log--ecmascript-empty-body-p (line-text)
"Return t if LINE-TEXT is func like function hello() { }."
(not (eq (string-match "\\(function\\|public\\|protected\\|private\\)?[[:blank:]]?[a-zA-Z0-9_]+\([a-zA-Z0-9_:,[:blank:]]*\)[[:blank:]]*{[[:blank:]]*}" line-text) nil)))

(defun turbo-log--ecmascript-print (current-line-number formatted-selected-text prev-line-text multiple-logger-p)
"Console log for ecmascript, js/ts modes.
Expand All @@ -176,7 +179,8 @@ FORMATTED-SELECTED-TEXT - formatted text without space at start position
PREV-LINE-TEXT - text from previous line
MULTIPLE-LOGGER-P - should guess list of available loggers?"

(let* ((insert-line-number (turbo-log--ecmascript-find-insert-pos current-line-number prev-line-text))
(let* ((is-empty-body (turbo-log--ecmascript-empty-body-p (turbo-log--get-line-text current-line-number)))
(insert-line-number (turbo-log--ecmascript-find-insert-pos current-line-number prev-line-text))
(insert-line-space-count (turbo-log--calculate-space-count (turbo-log--get-line-text insert-line-number)))
(meta-info (turbo-log--format-meta-info current-line-number))
(normalized-code (turbo-log--ecmascript-normilize-code formatted-selected-text))
Expand All @@ -189,7 +193,16 @@ MULTIPLE-LOGGER-P - should guess list of available loggers?"
normalized-code ")"
(if (plist-get turbo-log--ecmascript-configs :include-semicolon) ";"))))

(turbo-log--insert-with-indent insert-line-number turbo-log--message)))
(if is-empty-body
(progn
(turbo-log--goto-line (- current-line-number 1))
(beginning-of-line)
(search-forward-regexp "}[[:blank:]]*")
(replace-match "")
(turbo-log--insert-with-indent current-line-number turbo-log--message)
(turbo-log--insert-with-indent (+ current-line-number 1) "}")
(indent-according-to-mode))
(turbo-log--insert-with-indent insert-line-number turbo-log--message))))

(defun turbo-log--python-find-insert-pos (current-line-number text)
"Find insert position for python mode from CURRENT-LINE-NUMBER TEXT."
Expand Down Expand Up @@ -367,10 +380,11 @@ LOG-TYPE can be 'commented 'uncommented 'both."
(defun turbo-log-print-immediately ()
"Log selected region for current major mode without ask a logger from list."
(interactive)
(save-excursion
(let* ((logger-list (turbo-log--choose-mode))
(logger (cdr logger-list)))
(if logger
(turbo-log--handle-logger logger))))
(turbo-log--handle-logger logger)))))

;;;###autoload
(defun turbo-log-comment-all-logs ()
Expand Down

0 comments on commit b424a51

Please sign in to comment.