Skip to content

Commit 8cbea46

Browse files
committed
Initial version.
Extracted from swift-mode.
0 parents  commit 8cbea46

10 files changed

+1063
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*~
2+
/.cask/
3+
*.elc
4+
/dist/

COPYING

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

Cask

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(source gnu)
2+
(source melpa)
3+
4+
(package-file "flycheck-swift.el")
5+
6+
(development
7+
(depends-on "f")
8+
(depends-on "ecukes")
9+
(depends-on "ert-runner")
10+
(depends-on "el-mock"))

Makefile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
CASK ?= cask
2+
EMACS ?= emacs
3+
VERSION := $(shell EMACS=$(EMACS) $(CASK) version)
4+
5+
SRC = $(wildcard *.el)
6+
PACKAGE = dist/flycheck-swift-$(VERSION).el
7+
8+
.PHONY: help all cask-install package install test clean
9+
10+
help:
11+
## Shows this message.
12+
# Process this Makefile with following filters
13+
#
14+
# - Remove empty line.
15+
# - Remove lien containing ## no-doc.
16+
# - Remove after colon if the line is not a comment line.
17+
# - Replace /^## / to " ".
18+
# - Remove other comment lines.
19+
# - Insert newline before rules.
20+
@sed -e '/^\s*$$/d; /^[ .A-Z]/d; /## no-doc/d; s/^\([^#][^:]*\):.*/\1/; s/^## / /; /^#/d; s/^[^ ]/\n&/' Makefile
21+
22+
all: package
23+
## Builds the package.
24+
25+
cask-install:
26+
## Installs the dependencies.
27+
$(CASK) install
28+
29+
$(PACKAGE): $(SRC) ## no-doc
30+
$(MAKE) cask-install
31+
rm -rf dist
32+
$(CASK) package
33+
34+
package: $(PACKAGE)
35+
## Builds the package.
36+
37+
install: package
38+
## Installs the package.
39+
$(CASK) exec $(EMACS) --batch \
40+
-l package \
41+
-f package-initialize \
42+
--eval '(package-install-file "$(PACKAGE)")'
43+
44+
clean:
45+
## Cleans the dist directory.
46+
rm -rf dist
47+
48+
test: package
49+
## Tests the package.
50+
$(CASK) exec $(EMACS) --batch \
51+
-l $(PACKAGE) \
52+
-l test/flycheck-swift-test.el \
53+
-f ert-run-tests-batch-and-exit

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
[![License GPL 3][badge-license]][copying]
2+
3+
# flycheck-swift
4+
5+
Flycheck extension for Apple's Swift programming language.
6+
7+
## Installation
8+
9+
### Using MELPA
10+
11+
Coming soon.
12+
13+
### Manual installation
14+
15+
Install [Flycheck](http://www.flycheck.org/en/latest/user/installation.html).
16+
17+
```
18+
git clone --depth 1 https://github.com/swift-emacs/flycheck-swift.git
19+
cd flycheck-swift
20+
emacs --batch -l package -f package-initialize --eval '(package-install-file "flycheck-swift.el")'
21+
```
22+
23+
Add the following line to the `~/.emacs.d/init.el` or `~/.emacs`.
24+
25+
```
26+
(eval-after-load 'flycheck '(flycheck-swift-setup))
27+
```
28+
29+
### Using cask (for developer)
30+
31+
Install [cask](https://github.com/cask/cask).
32+
33+
```
34+
git clone https://github.com/swift-emacs/flycheck-swift.git
35+
cd flycheck-swift
36+
make install
37+
```
38+
39+
Add the following line to the `~/.emacs.d/init.el` or `~/.emacs`.
40+
41+
```
42+
(eval-after-load 'flycheck '(flycheck-swift-setup))
43+
```
44+
45+
## Customization
46+
47+
### flycheck-swift-executable
48+
49+
The name of the executable, i.e. `swiftc`.
50+
51+
### flycheck-swift:extra-flags
52+
53+
Extra flags prepended to arguments of swiftc.
54+
55+
### flycheck-swift:sdk-path
56+
57+
A path to the targeted SDK.
58+
59+
### flycheck-swift:linked-sources
60+
61+
A list of source file paths to link against. Each path can be glob, i.e. `*.swift`.
62+
63+
### flycheck-swift:framework-search-paths
64+
65+
A list of framework search paths.
66+
67+
### flycheck-swift:cc-include-search-paths
68+
69+
A list of include file search paths to pass to the Objective-C compiler.
70+
71+
### flycheck-swift:target
72+
73+
Target used by swift compiler.
74+
75+
### flycheck-swift:import-objc-header
76+
77+
Objective C header file to import.
78+
79+
## License
80+
81+
GPLv3. See [COPYING][] for details. Copyright (C) 2014-2016 taku0, Chris Barrett, Bozhidar Batsov, Arthur Evstifeev.
82+
83+
[badge-license]: https://img.shields.io/badge/license-GPL_3-green.svg
84+
[COPYING]: ./COPYING

flycheck-swift.el

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
;;; flycheck-swift.el --- Flycheck extension for Apple's Swift. -*- lexical-binding: t -*-
2+
3+
;; Copyright (C) 2014-2016 taku0, Chris Barrett, Bozhidar Batsov, Arthur Evstifeev
4+
5+
;; Authors: taku0 (http://github.com/taku0)
6+
;; Chris Barrett <[email protected]>
7+
;; Bozhidar Batsov <[email protected]>
8+
;; Arthur Evstifeev <[email protected]>
9+
;; Version: 1.0
10+
;; Package-Requires: ((emacs "24.4") (flycheck "0.25"))
11+
;; Keywords: languages swift
12+
13+
;; This file is not part of GNU Emacs.
14+
15+
;; This program is free software: you can redistribute it and/or modify
16+
;; it under the terms of the GNU General Public License as published by
17+
;; the Free Software Foundation, either version 3 of the License, or
18+
;; (at your option) any later version.
19+
20+
;; This program is distributed in the hope that it will be useful,
21+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23+
;; GNU General Public License for more details.
24+
25+
;; You should have received a copy of the GNU General Public License
26+
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
27+
28+
;;; Commentary:
29+
30+
;; Flycheck extension for Apple's Swift.
31+
32+
;;; Code:
33+
34+
(require 'flycheck)
35+
(require 'cl-lib)
36+
37+
(defgroup flycheck-swift nil
38+
"Configuration for swift-mode."
39+
:group 'flycheck
40+
:prefix "swift-")
41+
42+
;;; Flycheck
43+
44+
(flycheck-def-executable-var swift "swiftc")
45+
46+
(flycheck-def-option-var flycheck-swift:extra-flags nil swift
47+
"Extra flags prepended to arguments of swiftc"
48+
:type '(repeat (string :tag "Flags"))
49+
:safe #'flycheck-string-list-p)
50+
51+
(flycheck-def-option-var flycheck-swift:sdk-path nil swift
52+
"A path to the targeted SDK"
53+
:type '(choice (const :tag "Don't link against sdk" nil)
54+
(string :tag "Targeted SDK path"))
55+
:safe #'stringp)
56+
57+
(flycheck-def-option-var flycheck-swift:linked-sources '("*.swift") swift
58+
"Source files path to link against. Can be glob, i.e. *.swift"
59+
:type '(repeat (string :tag "Linked Sources"))
60+
:safe #'flycheck-string-list-p)
61+
62+
(flycheck-def-option-var flycheck-swift:framework-search-paths nil swift
63+
"A list of framework search paths"
64+
:type '(repeat (directory :tag "Include directory"))
65+
:safe #'flycheck-string-list-p)
66+
67+
(flycheck-def-option-var flycheck-swift:cc-include-search-paths nil swift
68+
"A list of include file search paths to pass to the Objective-C compiler"
69+
:type '(repeat (directory :tag "Include directory"))
70+
:safe #'flycheck-string-list-p)
71+
72+
(flycheck-def-option-var flycheck-swift:target nil swift
73+
"Target used by swift compiler"
74+
:type '(choice (const :tag "Don't specify target" nil)
75+
(string :tag "Build target"))
76+
:safe #'stringp)
77+
78+
(flycheck-def-option-var flycheck-swift:import-objc-header nil swift
79+
"Objective C header file to import, if any"
80+
:type '(choice (const :tag "Don't specify objective C bridging header" nil)
81+
(string :tag "Objective C bridging header path"))
82+
:safe #'stringp)
83+
84+
(flycheck-define-checker swift
85+
"Flycheck plugin for for Apple's Swift programming language."
86+
:command ("swiftc"
87+
(eval flycheck-swift:extra-flags)
88+
"-parse"
89+
(option "-sdk" flycheck-swift:sdk-path)
90+
(option-list "-F" flycheck-swift:framework-search-paths)
91+
;; Swift compiler will complain about redeclaration
92+
;; if we will include original file along with
93+
;; temporary source file created by flycheck.
94+
;; We also don't want a hidden emacs interlock files.
95+
(eval
96+
(let ((source-original (expand-file-name
97+
(car
98+
(flycheck-substitute-argument
99+
'source-original
100+
'swift))))
101+
(default-directory-old default-directory))
102+
(unwind-protect
103+
(progn
104+
(setq default-directory
105+
(file-name-directory source-original))
106+
(cl-remove-if
107+
#'(lambda (path)
108+
(or
109+
(string-match "^\\.#" (file-name-nondirectory path))
110+
(equal source-original path)))
111+
(cl-mapcan
112+
#'(lambda (path) (file-expand-wildcards path t))
113+
flycheck-swift:linked-sources)))
114+
(setq default-directory default-directory-old))))
115+
(option "-target" flycheck-swift:target)
116+
(option "-import-objc-header" flycheck-swift:import-objc-header)
117+
(option-list "-Xcc" flycheck-swift:cc-include-search-paths
118+
flycheck-swift:prepend-for-cc-include-search-path)
119+
source)
120+
:error-patterns ((error line-start (file-name) ":" line ":" column ": "
121+
"error: " (message) line-end)
122+
(warning line-start (file-name) ":" line ":" column ": "
123+
"warning: " (message) line-end))
124+
:modes (swift-mode swift3-mode))
125+
126+
(defun flycheck-swift:prepend-for-cc-include-search-path (opt path)
127+
"Return list for -Xcc -I option.
128+
129+
OPT is assumed to be -Xcc.
130+
PATH is a include search path."
131+
(list opt (concat "-I" path)))
132+
133+
;;;###autoload
134+
(defun flycheck-swift:setup ()
135+
"Setup Flycheck for Swift."
136+
(interactive)
137+
;; Add checkers in reverse order, because `add-to-list' adds to front.
138+
(add-to-list 'flycheck-checkers 'swift))
139+
140+
;;;###autoload
141+
(defalias 'flycheck-swift-setup 'flycheck-swift:setup)
142+
143+
144+
(provide 'flycheck-swift)
145+
146+
;;; flycheck-swift.el ends here
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// this file is used to test that flycheck-swift-test excludes file names starting with ".#".

test/flycheck-swift-test.el

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
;;; flycheck-swift-test.el --- Unit test for flycheck-swift. -*- lexical-binding: t -*-
2+
3+
;; Copyright (C) 2016 taku0
4+
5+
;; Authors: taku0 (http://github.com/taku0)
6+
7+
;; This file is not part of GNU Emacs.
8+
9+
;; This program is free software: you can redistribute it and/or modify
10+
;; it under the terms of the GNU General Public License as published by
11+
;; the Free Software Foundation, either version 3 of the License, or
12+
;; (at your option) any later version.
13+
14+
;; This program is distributed in the hope that it will be useful,
15+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
;; GNU General Public License for more details.
18+
19+
;; You should have received a copy of the GNU General Public License
20+
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+
;;; Commentary:
23+
24+
;; Flycheck extension for Apple's Swift.
25+
26+
;;; Code:
27+
28+
(require 'flycheck-swift)
29+
30+
(require 'ert)
31+
(require 'cl-lib)
32+
33+
(flycheck-swift:setup)
34+
35+
(defvar flycheck-swift:test:basedir (file-name-directory (or load-file-name buffer-file-name)))
36+
37+
(ert-deftest flycheck-swift:passing-correct-arguments ()
38+
"Test (flycheck-checker-substituted-arguments 'swift)."
39+
(with-temp-buffer
40+
(let
41+
((filename (concat
42+
(file-name-as-directory flycheck-swift:test:basedir)
43+
"test.swift"))
44+
(expected-butlast
45+
(list
46+
"-extra-flag1"
47+
"-extra-flag2"
48+
"-parse"
49+
"-sdk" "test-sdk-path"
50+
"-F" "search-path-1"
51+
"-F" "search-path-2"
52+
(concat (file-name-as-directory flycheck-swift:test:basedir)
53+
"linked-source.swift")
54+
"-target" "test-target"
55+
"-import-objc-header" "test-objc-header.h"
56+
"-Xcc" "-Iinclude-search-path-1"
57+
"-Xcc" "-Iinclude-search-path-2"))
58+
args)
59+
(insert-file-contents-literally filename)
60+
(setq buffer-file-name filename)
61+
(set-buffer-modified-p nil)
62+
63+
(setq-local flycheck-swift:extra-flags '("-extra-flag1" "-extra-flag2"))
64+
(setq-local flycheck-swift:sdk-path "test-sdk-path")
65+
(setq-local flycheck-swift:linked-sources '("*.swift"))
66+
(setq-local flycheck-swift:framework-search-paths
67+
'("search-path-1" "search-path-2"))
68+
(setq-local flycheck-swift:cc-include-search-paths
69+
'("include-search-path-1" "include-search-path-2"))
70+
(setq-local flycheck-swift:target "test-target")
71+
(setq-local flycheck-swift:import-objc-header "test-objc-header.h")
72+
73+
(setq args (flycheck-checker-substituted-arguments 'swift))
74+
75+
(should
76+
(equal (file-name-nondirectory (car (last args))) "test.swift"))
77+
78+
(cl-mapcar
79+
(lambda (actual expected)
80+
(should (equal actual expected)))
81+
(butlast args)
82+
expected-butlast)
83+
84+
;; to show better error message, assert on the nthcdr instead of length.
85+
(should (null (nthcdr (length expected-butlast) (butlast args)) )))))
86+
87+
(provide 'flycheck-swift-test)
88+
89+
;;; flycheck-swift-test.el ends here

test/linked-source.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// this file is used as a linked-source file.

test/test.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// this file is used as the primary file for the test

0 commit comments

Comments
 (0)