Skip to content

Commit a1bc9ca

Browse files
authored
Merge pull request ethereum#17 from ethereum/integrate_solium
Intergrate with Solium linter
2 parents bfca76b + cd13dce commit a1bc9ca

File tree

2 files changed

+113
-11
lines changed

2 files changed

+113
-11
lines changed

README.org

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,65 @@ 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-
** Provide path to solc binary
34-
The ~solc~ binary is assumed to be located at ~/usr/bin/solc~. Wherever that is not the case you would have to manually
33+
** Interface with linters
34+
*** Provide path to solc binary
35+
The ~solc~ binary is assumed to be part of the PATH. Wherever that is not the case you would have to manually
3536
set the location of the binary like below:
3637
#+BEGIN_SRC emacs-lisp
3738
(setq solidity-solc-path "/home/lefteris/ew/cpp-ethereum/build/solc/solc")
3839
#+END_SRC
3940

4041
Note: This better be set before requiring solidity mode.
4142

43+
*** Provide path to solium binary
44+
The ~solium~ binary is assumed to be part of the user's ~PATH~. If this is not the case
45+
then set its location like below:
46+
#+BEGIN_SRC emacs-lisp
47+
(setq solidity-solium-path "/home/lefteris/.npm-global/bin/solium")
48+
#+END_SRC
49+
50+
4251
** [Optional] Flycheck interface
4352
Solidity mode can also interface with [[https://github.com/flycheck/flycheck][flycheck]] if you have it. Make sure to
4453
download and install the flycheck package. Then configure it to either work on
4554
all modes or enable it only for solidity mode.
4655

56+
Flycheck can interface either with solc or with [[http://solium.readthedocs.io/en/latest/][solium]]. Choose the appropriate
57+
linter to use by providing the following in your emacs init:
58+
59+
#+BEGIN_SRC emacs-lisp
60+
(setq solidity-flycheck-active-checker "solium")
61+
#+END_SRC
62+
63+
If you want to use ~"solc"~ replace ~"solium"~ with it.
64+
65+
Keep in mind that you need to provide the path to either solc or solium unless
66+
emacs can already find it in your environment's ~PATH~. Even if you can call it
67+
from the command line it does not mean that EMACS can see it as emacs may be started
68+
by systemd at which point ~PATH~ is not fully populated.
69+
70+
*** Configuring solc checker
71+
72+
You can configure flycheck's solc invocation with the following arguments
73+
74+
**** std contracts
75+
By default this is false. If you want to include the standard contracts just add the following to your emacs init file
76+
77+
#+BEGIN_SRC emacs-lisp
78+
(setq flycheck-solidity-solc-addstd-contracts t)
79+
#+END_SRC
80+
81+
*** Configuring solium checker
82+
You can configure flycheck's solium incocation with the following arguments
83+
84+
**** solium RC file
85+
By default solium looks at the current directory of the file you are editing in order to find ~.soliumrc.json~. Having this
86+
file is required. But you can point to an external configuration file by putting the following anywhere in your emacs init file.
87+
88+
#+BEGIN_SRC emacs-lisp
89+
(setq flycheck-solidity-solium-soliumrcfile "/home/path/to/common/.soliumrc.json")
90+
#+END_SRC
91+
4792
* Features
4893
+ Syntax highlighting
4994
+ Indentation

solidity-mode.el

Lines changed: 66 additions & 9 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.3
7+
;; Version: 0.1.4
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
@@ -43,12 +43,24 @@
4343
"Callback hook to execute whenever a solidity file is loaded."
4444
:group 'solidity)
4545

46-
(defcustom solidity-solc-path "/usr/bin/solc"
46+
(defcustom solidity-solc-path "solc"
4747
"Path to the solc binary."
4848
:group 'solidity
4949
:type 'string
5050
:package-version '(solidity . "0.1.1"))
5151

52+
(defcustom solidity-solium-path "solium"
53+
"Path to the solium binary."
54+
:group 'solidity
55+
:type 'string
56+
:package-version '(solidity . "0.1.4"))
57+
58+
(defcustom solidity-flycheck-active-checker "solc"
59+
"Choice of active checker. Either solc or solium."
60+
:group 'solidity
61+
:type 'string
62+
:package-version '(solidity . "0.1.4"))
63+
5264
(defvar solidity-mode-map
5365
(let ((map (make-keymap)))
5466
(define-key map "\C-j" 'newline-and-indent)
@@ -446,8 +458,9 @@ Highlight the 1st result."
446458
(when (eval-when-compile (require 'flycheck nil 'noerror))
447459
;; Avoid reference to free variable warnings
448460
(defvar flycheck-solidity-checker-executable)
461+
(defvar flycheck-solium-checker-executable)
449462

450-
(flycheck-def-option-var flycheck-solidity-addstd-contracts nil solidity-checker
463+
(flycheck-def-option-var flycheck-solidity-solc-addstd-contracts nil solidity-checker
451464
"Whether to add standard solidity contracts.
452465
453466
When non-nil, enable add also standard solidity contracts via
@@ -456,26 +469,70 @@ When non-nil, enable add also standard solidity contracts via
456469
:safe #'booleanp
457470
:package-version '(solidity-mode . "0.1.3"))
458471

472+
(flycheck-def-option-var flycheck-solidity-solium-soliumrcfile nil solium-check
473+
"The path to use for soliumrc.json
474+
475+
The value of this variable is either a string denoting a path to the soliumrc.json
476+
or nil, to use the current directory. When non-nil,
477+
we pass the directory to solium via the `--config' option."
478+
:type '(choice (const :tag "No custom soliumrc" nil)
479+
(string :tag "Custom soliumrc file location"))
480+
:safe #'stringp
481+
:package-version '(solidity-mode . "0.1.4"))
482+
459483
;; add dummy source-inplace definition to avoid errors
460484
(defvar source-inplace t)
461485
;; add a solidity mode callback to set the executable of solc for flycheck
462486
;; define solidity's flycheck syntax checker
463487
(flycheck-define-checker solidity-checker
464488
"A Solidity syntax checker using the solc compiler"
465489
:command ("solc"
466-
(option-flag "--add-std" flycheck-solidity-addstd-contracts)
490+
(option-flag "--add-std" flycheck-solidity-solc-addstd-contracts)
467491
source-inplace)
468492
:error-patterns
469493
((error line-start (file-name) ":" line ":" column ":" " Error: " (message))
470494
(error line-start "Error: " (message))
471495
(warning line-start (file-name) ":" line ":" column ":" " Warning: " (message)))
472496
:modes solidity-mode
473497
:predicate (lambda () (eq major-mode 'solidity-mode)))
474-
(add-to-list 'flycheck-checkers 'solidity-checker)
475-
(add-hook 'solidity-mode-hook
476-
(lambda ()
477-
(let ((solidity-command (concat solidity-solc-path)))
478-
(setq flycheck-solidity-checker-executable solidity-command)))))
498+
499+
;; define solium flycheck syntax checker
500+
(flycheck-define-checker solium-checker
501+
"A Solidity linter using solium"
502+
:command ("solium"
503+
(option "--config=" flycheck-solidity-solium-soliumrcfile concat)
504+
"-f"
505+
source-inplace)
506+
:error-patterns
507+
((error line-start (zero-or-more " ") line ":" column (zero-or-more " ") "error" (message))
508+
(error line-start (zero-or-more not-newline) "[Fatal error]" (message))
509+
(warning line-start (zero-or-more " ") line ":" column (zero-or-more " ") "warning" (message)))
510+
:error-filter
511+
;; Add fake line numbers if they are missing in the lint output
512+
(lambda (errors)
513+
(dolist (err errors)
514+
(unless (flycheck-error-line err)
515+
(setf (flycheck-error-line err) 1)))
516+
errors)
517+
:modes solidity-mode
518+
:predicate (lambda () (eq major-mode 'solidity-mode)))
519+
520+
(when (string= solidity-flycheck-active-checker "solc")
521+
(if (file-executable-p solidity-solc-path)
522+
(progn
523+
(add-to-list 'flycheck-checkers 'solidity-checker)
524+
(add-hook 'solidity-mode-hook
525+
(lambda ()
526+
(let ((solidity-command (concat solidity-solc-path)))
527+
(setq flycheck-solidity-checker-executable solidity-command)))))
528+
(error (format "Solidity Mode Configuration error. Requested solc flycheck integration but can't find solc at: %s" solidity-solc-path))))
529+
530+
(when (string= solidity-flycheck-active-checker "solium")
531+
(if (file-executable-p solidity-solium-path)
532+
(progn
533+
(add-to-list 'flycheck-checkers 'solium-checker)
534+
(setq flycheck-solium-checker-executable solidity-solium-path))
535+
(error (format "Solidity Mode Configuration error. Requested solium flycheck integration but can't find solium at: %s" solidity-solium-path)))))
479536

480537
(provide 'solidity-mode)
481538
;;; solidity-mode.el ends here

0 commit comments

Comments
 (0)