Skip to content

Commit d0ff4de

Browse files
authored
Merge pull request #26 from ssmolkin1/master
Integrated company-solidity for autocompletion
2 parents 5504957 + 0002886 commit d0ff4de

File tree

4 files changed

+176
-4
lines changed

4 files changed

+176
-4
lines changed

README.org

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,29 @@ how they are chained. Its value can be either =t=, =error=, =warning= or =info=
117117
of the solc checker after which solium will not run. If =t= is given solium will always run. The default is =warning=, so
118118
if anything over than a warning is found in solc solium will not run.
119119

120+
** [Optional] Autocompletion
121+
[[https://github.com/ssmolkin1/company-solidity][company-solidity]], a simple [[http://company-mode.github.io/][company-mode]] back-end for Solidity, has now been fully integrated into solidity-mode. You no longer need to install company-solidity separately from solidity-mode, nor should you do so. To take advantage of the autocompletion features, you must have the [[http://company-mode.github.io/][company-mode]] package installed. The autocompletion features will be enabled automatically if you have company-mode installed.
122+
123+
*** What it does
124+
Give completion suggestions for Solidity keywords, global variables, and address methods.
125+
126+
*** What it isn't
127+
Smart. The completion suggestions are *not context dependent*.
128+
129+
*** Something to watch out for
130+
=company-mode= treats =.= as the end of a word, and will cut off compeletion suggestions when you type a =.=. So, when you've typed =msg= you will get =msg.sender=, =msg.value= etc. as completion suggestions. However, as soon as you type =msg.=, the suggestions will disappear.
131+
132+
*** Local Variables
133+
If you want autocomplete suggestions to include local variables, in addition to Solidity keywords, add the following to your =init.el=:
134+
135+
#+BEGIN_SRC emacs-lisp
136+
(add-hook 'solidity-mode-hook
137+
(lambda ()
138+
(set (make-local-variable 'company-backends)
139+
(append '((company-solidity company-capf company-dabbrev-code))
140+
company-backends))))
141+
#+END_SRC
142+
120143
* Commands
121144

122145
** Gas estimate of function under point
@@ -127,9 +150,7 @@ This will call =solidity-estimate-gas-at-point= and provide a max gas estimate,
127150
if possible, for the function.
128151
* Features
129152
+ Syntax highlighting
153+
+ Autocompletion
130154
+ Indentation
131155
+ On the fly syntax checking with flycheck
132156
+ Gas estimation for function under point
133-
134-
* Autocompletion
135-
For autocompletion, you can install [[https://github.com/ssmolkin1/company-solidity][company-solidity]], a [[http://company-mode.github.io/][company-mode]] back-end for Solidity.

changelog.MD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
The changelog starts from version 0.1.4 as too much was added in each version before that.
44

5+
## Version 0.1.9
6+
7+
- Integrated [company-solidity](https://github.com/ssmolkin1/company-solidity) into solidity-mode, providing autocompletion out of the box if the user has [company-mode](http://company-mode.github.io) installed.
8+
59
## Version 0.1.8
610

711
- Bugfix for [issue 8](https://github.com/ethereum/emacs-solidity/issues/8). Now if flycheck is not installed

company-solidity.el

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
;;; company-solidity.el --- Company-mode back-end for solidity-mode
2+
3+
;; Copyright (C) 2018 Samuel Smolkin
4+
5+
;; Author: Samuel Smolkin <[email protected]>
6+
;; URL: https://github.com/ethereum/emacs-solidity
7+
;; Keywords: solidity, completion, company
8+
;; Version: 2.0.0
9+
;; Package-Requires: ((company "0.9.0") (cl-lib "0.5.0"))
10+
11+
;; This program is free software; you can redistribute it and/or modify
12+
;; it under the terms of the GNU General Public License as published by
13+
;; the Free Software Foundation, either version 3 of the License, or
14+
;; (at your option) any later version.
15+
16+
;; This program is distributed in the hope that it will be useful,
17+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
;; GNU General Public License for more details.
20+
21+
;; You should have received a copy of the GNU General Public License
22+
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
24+
;;; Commentary:
25+
26+
;; This package provides a simple company-mode back-end for auto-completing Solidity keywords when working in solidity-mode.
27+
28+
;;; Code:
29+
30+
(require 'cl-lib)
31+
(require 'company)
32+
33+
;; Additional completion targets whcih are nore part of solidty-mode syntax-highlighting keywords lists:
34+
35+
(defconst company-solidity-additional-math
36+
'("addmod"
37+
"mulmod"))
38+
39+
(defconst company-solidity-additional-hashing
40+
'("keccak256"
41+
"sha256"
42+
"sha3"
43+
"ripemd160"
44+
"ecrecover"))
45+
46+
(defconst company-solidity-additional-block-methods
47+
'("block.blockhash"
48+
"block.coinbase"
49+
"block.difficulty"
50+
"block.gaslimit"
51+
"block.number"
52+
"block.timestamp"
53+
"now"))
54+
55+
(defconst company-solidity-additional-msg-methods
56+
'("msg.data"
57+
"msg.gas"
58+
"msg.sender"
59+
"msg.sig"
60+
"msg.value"
61+
"gasleft"))
62+
63+
(defconst company-solidity-additional-tx-methods
64+
'("tx.gasprice"
65+
"tx.origin"))
66+
67+
(defconst company-solidity-additional-address-methods
68+
'("balance"
69+
"transfer"
70+
"send"
71+
"call"
72+
"callcode"
73+
"delegatecall"))
74+
75+
(defconst company-solidity-additional-contracts
76+
'("super"
77+
"selfdestruct"
78+
"suicide"))
79+
80+
(defconst company-solidity-additional-modifiers
81+
'("payable"))
82+
83+
(defconst company-solidity-additional-pragma
84+
'("solidity"))
85+
86+
(defconst company-solidity-additional-types
87+
'("fixed"
88+
"ufixed"
89+
"hex"))
90+
91+
(defconst company-solidity-additional-function-methods
92+
'("selector"))
93+
94+
;; defvar symbols taken from solidity-mode.el to avoid reference warnings
95+
(defvar solidity-keywords)
96+
(defvar solidity-constants)
97+
(defvar solidity-variable-modifier)
98+
(defvar solidity-builtin-types)
99+
(defvar solidity-builtin-constructs)
100+
101+
;; Completion targets taken from solidity-mode syntax-highlighting keywords lists, plus additional targets above.
102+
(defconst company-solidity-keywords
103+
(append
104+
solidity-keywords
105+
solidity-constants
106+
solidity-variable-modifier
107+
solidity-builtin-types
108+
solidity-builtin-constructs
109+
company-solidity-additional-math
110+
company-solidity-additional-hashing
111+
company-solidity-additional-block-methods
112+
company-solidity-additional-msg-methods
113+
company-solidity-additional-tx-methods
114+
company-solidity-additional-address-methods
115+
company-solidity-additional-contracts
116+
company-solidity-additional-modifiers
117+
company-solidity-additional-pragma
118+
company-solidity-additional-types
119+
company-solidity-additional-function-methods))
120+
121+
;;;###autoload
122+
(defun company-solidity (command &optional arg &rest ignored)
123+
"Autocompletion for solidity with company mode.
124+
125+
Argument COMMAND `company-backend` functions.
126+
Optional argument ARG the completion target prefix.
127+
Optional argument IGNORED Additional arguments are ingnored."
128+
(interactive (list 'interactive))
129+
(set (make-local-variable 'company-minimum-prefix-length) 2)
130+
(cl-case command
131+
(interactive (company-begin-backend 'company-solidity))
132+
(prefix (and (eq major-mode 'solidity-mode)
133+
(company-grab-symbol)))
134+
(candidates
135+
(cl-remove-if-not
136+
(lambda (c) (string-prefix-p arg c))
137+
company-solidity-keywords))))
138+
139+
(add-to-list 'company-backends 'company-solidity)
140+
141+
(provide 'company-solidity)
142+
143+
;;; company-solidity.el ends here

solidity-mode.el

Lines changed: 5 additions & 1 deletion
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.8
7+
;; Version: 0.1.9
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
@@ -530,5 +530,9 @@ Cursor must be at the function's name. Does not currently work for constructors
530530
(when (require 'flycheck nil 'noerror)
531531
(require 'solidity-flycheck))
532532

533+
;;; --- autcompletion back-end for company-mode, loads if company mode is installed ---
534+
(when (require 'company nil 'noerror)
535+
(require 'company-solidity))
536+
533537
(provide 'solidity-mode)
534538
;;; solidity-mode.el ends here

0 commit comments

Comments
 (0)