|
| 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 |
0 commit comments