44
55; ; Author: Lefteris Karapetsas <lefteris@refu.co>
66; ; Keywords: languages
7- ; ; Version: 0.1.5
7+ ; ; Version: 0.1.6
88
99; ; This program is free software; you can redistribute it and/or modify
1010; ; it under the terms of the GNU General Public License as published by
@@ -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,63 @@ Highlight the 1st result."
462463<
8000
div class="diff-text-inner"> 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+ (push (match-string count string) matches)
477+ (setq pos (match-end 0 )))
478+ matches)))
479+
480+ (defun solidity--handle-gasestimate-finish (process event )
481+ " Handle all events from the solc gas estimation PROCESS.
482+ EVENT is isgnored."
483+ (when (memq (process-status process) '(signal exit))
484+ (let* ((buffer (process-buffer process))
485+ (funcname (process-get process 'solidity-gasestimate-for-function ))
486+ (filename (process-get process 'solidity-gasestimate-for-filename ))
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+ (let* ((clearfilename (file-name-nondirectory filename))
493+ (ctor-matches (solidity--re-matches (format " =.*?%s :%s .*?= " clearfilename funcname) output 0 )))
494+ (if ctor-matches
495+ (message " Gas for '%s ' constructor is %s " funcname (car (solidity--re-matches " construction:
496+ .*?=.*?\\ ([0-9]+\\ |infinite\\ )" output 1 )))
497+ (message " No gas estimate found for '%s ' " funcname)))))))
498+
499+
500+ (defun solidity--start-gasestimate (func )
501+ " Start a gas estimation subprocess for FUNC.
502+
503+ Does not currently work for constructors."
504+ (let* ((filename (buffer-file-name ))
505+ (command (format " %s --gas %s " solidity-solc-path filename))
506+ (process-name (format " solidity-command-%s " command))
507+ (process (start-process-shell-command
508+ process-name
509+ (format " *%s * " process-name)
510+ command)))
511+ (set-process-query-on-exit-flag process nil )
512+ (set-process-sentinel process 'solidity--handle-gasestimate-finish )
513+ (process-put process 'solidity-gasestimate-for-function func)
514+ (process-put process 'solidity-gasestimate-for-filename filename)))
515+
516+ (defun solidity-estimate-gas-at-point ()
517+ " Estimate gas of the function at point.
518+
519+ Cursor must be at the function's name. Does not currently work for constructors."
520+ (interactive )
521+ (solidity--start-gasestimate (thing-at-point 'word 'no-properties )))
522+
465523;;;### autoload
466524(define-derived-mode solidity-mode c-mode " solidity"
467525 " Major mode for editing solidity language buffers."
@@ -488,6 +546,10 @@ Highlight the 1st result."
488546 (set (make-local-variable 'comment-multi-line ) t )
489547 (set (make-local-variable 'comment-line-break-function )
490548 'c-indent-new-comment-line )
549+
550+ ; ; set keymap
551+ (use-local-map solidity-mode-map)
552+ ; ; set hooks
491553 (run-hooks 'solidity-mode-hook ))
492554
493555; ;; --- interface with flycheck if existing ---
0 commit comments