Skip to content

Commit a2f722c

Browse files
authored
Merge pull request #19 from ethereum/gas_estimation
Gas estimation
2 parents 07714e3 + adaae56 commit a2f722c

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

README.org

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ Regardless of where you installed solidity mode from, you need to require the pa
3030
#+END_SRC
3131
(append that line to your =~/.emacs= file)
3232

33+
** Keymap
34+
You can modify the default keybindings of the solidity mode keymap by adding
35+
a new key combination for each command you want to change. For example
36+
37+
#+BEGIN_SRC lisp
38+
(define-key map (kbd "C-c C-g") 'solidity-estimate-gas-at-point)
39+
#+END_SRC
40+
3341
** Interface with linters
3442
*** Provide path to solc binary
3543
The =solc= binary is assumed to be part of the PATH. Wherever that is not the case you would have to manually
@@ -100,8 +108,16 @@ how they are chained. Its value can be either =t=, =error=, =warning= or =info=
100108
of the solc checker after which solium will not run. If =t= is given solium will always run. The default is =warning=, so
101109
if anything over than a warning is found in solc solium will not run.
102110

111+
* Commands
112+
113+
** Gas estimate of function under point
114+
You can get an estimate of the function under the cursor, by placing the curson
115+
on top of the function name and typing =C-c C-g=.
116+
117+
This will call =solidity-estimate-gas-at-point= and provide a max gas estimate,
118+
if possible, for the function.
103119
* Features
104120
+ Syntax highlighting
105121
+ Indentation
106122
+ On the fly syntax checking with flycheck
107-
More features are planned, which would interface with the solidity libraries.
123+
+ Gas estimation for function under point

changelog.MD

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Emacs Solidity Mode Changelog
2+
3+
The changelog starts from version 0.1.4 as too much was added in each version before that.
4+
5+
## Version 0.1.6
6+
7+
- Add gas estimation code. User facing function is `solidity-estimate-gas-at-point`.
8+
9+
## Version 0.1.5
10+
11+
- Add ability to chain flycheck checkers for solidity.
12+
- Add `solidity-flycheck-chaining-error-level` so that user can customize
13+
the level at which chaining will happen.
14+
15+
## Version 0.1.4
16+
17+
- Integrate with the solium linter
18+
- Allow specification of solium config file via `flycheck-solidity-solium-soliumrcfile`
19+

solidity-mode.el

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
;; Author: Lefteris Karapetsas <[email protected]>
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
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

Comments
 (0)