Skip to content

Commit 9ade014

Browse files
committed
Add gas estimation at point
Works for all functions except from constructor. Cursor needs to be on the function name. It's bound to "C-c C-g". User can change this by modifying the keymap as is standard with emacs packages.
1 parent 07714e3 commit 9ade014

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

solidity-mode.el

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ Possible values are:
9898
:safe #'symbolp)
9999

100100
(defvar solidity-mode-map
101-
(let ((map (make-keymap)))
101+
(let ((map (make-sparse-keymap)))
102102
(define-key map "\C-j" 'newline-and-indent)
103+
(define-key map (kbd "C-c C-g") 'solidity-estimate-gas-at-point)
103104
map)
104105
"Keymap for solidity major mode.")
105106

@@ -462,6 +463,57 @@ Highlight the 1st result."
462463
st)
463464
"Syntax table for the solidity language.")
464465

466+
467+
(defun solidity--re-matches (regexp string count)
468+
"Get a list of all REGEXP match results in a STRING.
469+
470+
COUNT is the parenthentical subexpression for which to return matches.
471+
If your provide 0 for COUNT then the entire regex match is returned."
472+
(save-match-data
473+
(let ((pos 0)
474+
matches)
475+
(while (string-match regexp string pos)
476+
(message "GOT MATCH")
477+
(push (match-string count string) matches)
478+
(setq pos (match-end 0)))
479+
matches)))
480+
481+
(defun solidity--handle-gasestimate-finish (process event)
482+
"Handle all events from the solc gas estimation PROCESS.
483+
EVENT is isgnored."
484+
(when (memq (process-status process) '(signal exit))
485+
(let* ((buffer (process-buffer process))
486+
(funcname (process-get process 'solidity-gasestimate-for-function))
487+
(output (with-current-buffer buffer (buffer-string)))
488+
(matches (solidity--re-matches (format "%s(.*?):.*?\\([0-9]+\\|infinite\\)" funcname) output 1))
489+
(result (car matches)))
490+
(kill-buffer buffer)
491+
(if result
492+
(message "Gas for '%s' is %s" funcname result)
493+
(message "No gas estimate found for '%s'" funcname)))))
494+
495+
496+
(defun solidity--start-gasestimate (func)
497+
"Start a gas estimation subprocess for FUNC.
498+
499+
Does not currently work for constructors."
500+
(let* ((command (format "%s --gas %s" solidity-solc-path (buffer-file-name)))
501+
(process-name (format "solidity-command-%s" command))
502+
(process (start-process-shell-command
503+
process-name
504+
(format "*%s*" process-name)
505+
command)))
506+
(set-process-query-on-exit-flag process nil)
507+
(set-process-sentinel process 'solidity--handle-gasestimate-finish)
508+
(process-put process 'solidity-gasestimate-for-function func)))
509+
510+
(defun solidity-estimate-gas-at-point ()
511+
"Estimate gas of the function at point.
512+
513+
Cursor must be at the function's name. Does not currently work for constructors."
514+
(interactive)
515+
(solidity--start-gasestimate (thing-at-point 'word 'no-properties)))
516+
465517
;;;###autoload
466518
(define-derived-mode solidity-mode c-mode "solidity"
467519
"Major mode for editing solidity language buffers."
@@ -488,6 +540,10 @@ Highlight the 1st result."
488540
(set (make-local-variable 'comment-multi-line) t)
489541
(set (make-local-variable 'comment-line-break-function)
490542
'c-indent-new-comment-line)
543+
544+
;; set keymap
545+
(use-local-map solidity-mode-map)
546+
;; set hooks
491547
(run-hooks 'solidity-mode-hook))
492548

493549
;;; --- interface with flycheck if existing ---

0 commit comments

Comments
 (0)