Skip to content

Commit 07714e3

Browse files
authored
Merge pull request #18 from ethereum/chain_flycheck_checkers
Chain flycheck checkers
2 parents a1bc9ca + bb8ed49 commit 07714e3

File tree

2 files changed

+114
-34
lines changed

2 files changed

+114
-34
lines changed

README.org

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Regardless of where you installed solidity mode from, you need to require the pa
3232

3333
** Interface with linters
3434
*** 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
35+
The =solc= binary is assumed to be part of the PATH. Wherever that is not the case you would have to manually
3636
set the location of the binary like below:
3737
#+BEGIN_SRC emacs-lisp
3838
(setq solidity-solc-path "/home/lefteris/ew/cpp-ethereum/build/solc/solc")
@@ -41,7 +41,7 @@ set the location of the binary like below:
4141
Note: This better be set before requiring solidity mode.
4242

4343
*** 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
44+
The =solium= binary is assumed to be part of the user's =PATH=. If this is not the case
4545
then set its location like below:
4646
#+BEGIN_SRC emacs-lisp
4747
(setq solidity-solium-path "/home/lefteris/.npm-global/bin/solium")
@@ -53,19 +53,24 @@ Solidity mode can also interface with [[https://github.com/flycheck/flycheck][fl
5353
download and install the flycheck package. Then configure it to either work on
5454
all modes or enable it only for solidity mode.
5555

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:
56+
Flycheck can interface with solc and/or with [[http://solium.readthedocs.io/en/latest/][solium]]. You have to specifically set the path
57+
to both executables and activate the checker integration by setting the following:
5858

59+
To activate =solc= checker
5960
#+BEGIN_SRC emacs-lisp
60-
(setq solidity-flycheck-active-checker "solium")
61+
(setq solidity-flycheck-solc-checker-active t)
62+
#+END_SRC
63+
64+
To activate =solium= checker
65+
#+BEGIN_SRC emacs-lisp
66+
(setq solidity-flycheck-solium-checker-active t)
6167
#+END_SRC
6268

63-
If you want to use ~"solc"~ replace ~"solium"~ with it.
6469

6570
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
71+
emacs can already find it in your environment's =PATH=. Even if you can call it
6772
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.
73+
by systemd at which point =PATH= is not fully populated.
6974

7075
*** Configuring solc checker
7176

@@ -82,13 +87,19 @@ By default this is false. If you want to include the standard contracts just add
8287
You can configure flycheck's solium incocation with the following arguments
8388

8489
**** 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
90+
By default solium looks at the current directory of the file you are editing in order to find =.soliumrc.json=. Having this
8691
file is required. But you can point to an external configuration file by putting the following anywhere in your emacs init file.
8792

8893
#+BEGIN_SRC emacs-lisp
8994
(setq flycheck-solidity-solium-soliumrcfile "/home/path/to/common/.soliumrc.json")
9095
#+END_SRC
9196

97+
*** Chaining both checkers
98+
If you enable both checkers then their results are chained. The variable =solidity-flycheck-chaining-error-level= controls
99+
how they are chained. Its value can be either =t=, =error=, =warning= or =info= and that controls the maximum error level
100+
of the solc checker after which solium will not run. If =t= is given solium will always run. The default is =warning=, so
101+
if anything over than a warning is found in solc solium will not run.
102+
92103
* Features
93104
+ Syntax highlighting
94105
+ Indentation

solidity-mode.el

Lines changed: 94 additions & 25 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.4
7+
;; Version: 0.1.5
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
@@ -55,11 +55,47 @@
5555
:type 'string
5656
:package-version '(solidity . "0.1.4"))
5757

58-
(defcustom solidity-flycheck-active-checker "solc"
59-
"Choice of active checker. Either solc or solium."
58+
(defcustom solidity-flycheck-solc-checker-active nil
59+
"A boolean flag denoting if solc flycheck checker should be active."
6060
:group 'solidity
61-
:type 'string
62-
:package-version '(solidity . "0.1.4"))
61+
:type 'boolean
62+
:safe #'booleanp
63+
:package-version '(solidity . "0.1.5"))
64+
65+
(defcustom solidity-flycheck-solium-checker-active nil
66+
"A boolean flag denoting if solium flycheck checker should be active."
67+
:group 'solidity
68+
:type 'boolean
69+
:safe #'booleanp
70+
:package-version '(solidity . "0.1.5"))
71+
72+
(defcustom solidity-flycheck-chaining-error-level 'warning
73+
"The maximum error level at which chaining of checkers will happen.
74+
75+
This means that this is the error level for which solc checker will allow
76+
next checkers to run. By default this is the warning level.
77+
Possible values are:
78+
79+
`info'
80+
If any errors higher than info level are found in solc, then solium
81+
will not run.
82+
83+
`warning'
84+
If any errors higher than warning level are found in solc, then solium
85+
will not run.
86+
87+
`error'
88+
If any errors higher than error level are found in solc, then solium
89+
will not run.
90+
t
91+
Solium will always run."
92+
:group 'solidity
93+
:type '(choice (const :tag "Chain after info-error level" info)
94+
(const :tag "Chain after warning-error level" warning)
95+
(const :tag "Chain after error-error level" error)
96+
(const :tag "Always chain" t))
97+
:package-version '(solidity . "0.1.5")
98+
:safe #'symbolp)
6399

64100
(defvar solidity-mode-map
65101
(let ((map (make-keymap)))
@@ -482,19 +518,50 @@ we pass the directory to solium via the `--config' option."
482518

483519
;; add dummy source-inplace definition to avoid errors
484520
(defvar source-inplace t)
521+
485522
;; add a solidity mode callback to set the executable of solc for flycheck
486523
;; define solidity's flycheck syntax checker
487-
(flycheck-define-checker solidity-checker
488-
"A Solidity syntax checker using the solc compiler"
489-
:command ("solc"
490-
(option-flag "--add-std" flycheck-solidity-solc-addstd-contracts)
491-
source-inplace)
492-
:error-patterns
493-
((error line-start (file-name) ":" line ":" column ":" " Error: " (message))
494-
(error line-start "Error: " (message))
495-
(warning line-start (file-name) ":" line ":" column ":" " Warning: " (message)))
496-
:modes solidity-mode
497-
:predicate (lambda () (eq major-mode 'solidity-mode)))
524+
;; (let ((next-checkers-val `((,solidity-flycheck-chaining-error-level . solium-checker))))
525+
;; (flycheck-define-checker solidity-checker
526+
;; "A Solidity syntax checker using the solc compiler"
527+
;; :command ("solc"
528+
;; (option-flag "--add-std" flycheck-solidity-solc-addstd-contracts)
529+
;; source-inplace)
530+
;; :error-patterns
531+
;; ((error line-start (file-name) ":" line ":" column ":" " Error: " (message))
532+
;; (error line-start "Error: " (message))
533+
;; (warning line-start (file-name) ":" line ":" column ":" " Warning: " (message)))
534+
;; :next-checkers next-checkers-val
535+
;; ;; :next-checkers `((,solidity-flycheck-chaining-error-level . solium-checker))
536+
;; :modes solidity-mode
537+
;; :predicate (lambda () (eq major-mode 'solidity-mode))))
538+
539+
;; expanded the flycheck-define-checker macro as per advice given in gitter
540+
;; https://gitter.im/flycheck/flycheck?at=5a43b3a8232e79134d98872b in order to avoid the
541+
;; next-checkers `'` introduced by the flycheck-define-checker macro
542+
(progn
543+
(flycheck-def-executable-var solidity-checker "solc")
544+
(flycheck-define-command-checker 'solidity-checker "A Solidity syntax checker using the solc compiler" :command
545+
'("solc"
546+
(option-flag "--add-std" flycheck-solidity-solc-addstd-contracts)
547+
source-inplace)
548+
:error-patterns
549+
'((error line-start
550+
(file-name)
551+
":" line ":" column ":" " Error: "
552+
(message))
553+
(error line-start "Error: "
554+
(message))
555+
(warning line-start
556+
(file-name)
557+
":" line ":" column ":" " Warning: "
558+
(message)))
559+
:modes 'solidity-mode :predicate
560+
#'(lambda nil
561+
(eq major-mode 'solidity-mode))
562+
:next-checkers
563+
`((,solidity-flycheck-chaining-error-level . solium-checker))
564+
:standard-input 'nil :working-directory 'nil))
498565

499566
;; define solium flycheck syntax checker
500567
(flycheck-define-checker solium-checker
@@ -517,22 +584,24 @@ we pass the directory to solium via the `--config' option."
517584
:modes solidity-mode
518585
:predicate (lambda () (eq major-mode 'solidity-mode)))
519586

520-
(when (string= solidity-flycheck-active-checker "solc")
587+
;; first try to add solium to the checker's list since if we got solc
588+
;; it must come after it in the list due to it being chained after solc
589+
(when solidity-flycheck-solium-checker-active
590+
(if (file-executable-p solidity-solium-path)
591+
(progn
592+
(add-to-list 'flycheck-checkers 'solium-checker)
593+
(setq flycheck-solium-checker-executable solidity-solium-path))
594+
(error (format "Solidity Mode Configuration error. Requested solium flycheck integration but can't find solium at: %s" solidity-solium-path))))
595+
596+
(when solidity-flycheck-solc-checker-active
521597
(if (file-executable-p solidity-solc-path)
522598
(progn
523599
(add-to-list 'flycheck-checkers 'solidity-checker)
524600
(add-hook 'solidity-mode-hook
525601
(lambda ()
526602
(let ((solidity-command (concat solidity-solc-path)))
527603
(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)))))
604+
(error (format "Solidity Mode Configuration error. Requested solc flycheck integration but can't find solc at: %s" solidity-solc-path)))))
536605

537606
(provide 'solidity-mode)
538607
;;; solidity-mode.el ends here

0 commit comments

Comments
 (0)