Skip to content

Commit 73257de

Browse files
committed
Merge pull request #36 from ubolonton/syntax-highlighting
2 parents cf6e0f4 + 671694b commit 73257de

29 files changed

+1381
-216
lines changed

.azure-pipelines/main.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ resources:
5050
steps:
5151
- template: .azure-pipelines/steps/setup-rust.yml@emacs-module-rs
5252
- template: .azure-pipelines/steps/setup-llvm.yml@emacs-module-rs
53-
- template: steps/-build.yml
5453
- template: .azure-pipelines/steps/setup-emacs.yml@emacs-module-rs
54+
- template: .azure-pipelines/steps/setup-cask.yml@emacs-module-rs
55+
- template: steps/-build.yml
56+
parameters:
57+
target: release
5558
- template: steps/setup-tree-sitter-cli.yml
5659
- template: steps/-test.yml

.azure-pipelines/release.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ jobs:
3333
steps:
3434
- template: .azure-pipelines/steps/setup-rust.yml@emacs-module-rs
3535
- template: .azure-pipelines/steps/setup-llvm.yml@emacs-module-rs
36+
- template: .azure-pipelines/steps/setup-emacs.yml@emacs-module-rs
37+
- template: .azure-pipelines/steps/setup-cask.yml@emacs-module-rs
3638
- template: steps/-build.yml
3739
parameters:
3840
target: release
39-
- template: .azure-pipelines/steps/setup-emacs.yml@emacs-module-rs
4041
- template: steps/setup-tree-sitter-cli.yml
4142
- template: steps/-test.yml
4243
- template: steps/-save-binaries.yml

.azure-pipelines/steps/-build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ parameters:
33

44
steps:
55
- powershell: |
6+
cask install
67
.\bin\build.ps1 ${{ parameters.target }}
78
displayName: Build all packages (Windows)
89
condition: and(succeeded(), eq(variables['Agent.OS'], 'Windows_NT'))
910

1011
- bash: |
12+
cask install
1113
./bin/build ${{ parameters.target }}
1214
displayName: Build all packages
1315
condition: and(succeeded(), ne(variables['Agent.OS'], 'Windows_NT'))

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/target
22
**/*.rs.bk
3-
**/*.elc
3+
**/*.elc*
44
**/.cask
55
grammars/
66
lisp/tree-sitter-dyn.*

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ before_install:
6262
- git clone -b master https://github.com/ubolonton/evm.git $HOME/.evm
6363
- evm config path /tmp
6464
- evm install $EVM_EMACS --use --skip
65+
- curl -fsSL https://raw.githubusercontent.com/cask/cask/master/go | python
66+
- export PATH="$HOME/.cask/bin:$PATH"
6567

6668
install:
67-
- make build
69+
- cask install
70+
- ./bin/build
6871

6972
before_script:
7073
- nvm install 10

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ name = "tree_sitter_dyn"
1111
crate-type = ["cdylib"]
1212

1313
[dependencies]
14-
emacs = "0.13.0"
14+
emacs = "0.14.0"
1515
libloading = "0.5.1"
1616
tree-sitter = "0.6.3"
1717

Cask

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@
88
"Cargo.toml"
99
"Cargo.lock"
1010
"src")
11+
12+
(source melpa)
13+
14+
(development
15+
(depends-on "rust-mode"))

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ If you want to hack on `emacs-tree-sitter` itself, see the section [Setup for De
3838
- Show the debug view of a buffer's parse tree
3939
```emacs-lisp
4040
(require 'tree-sitter-debug)
41-
(tree-sitter-debug-enable)
41+
(tree-sitter-debug-mode)
4242
```
4343
- Get names of all functions in a Rust file:
4444
```emacs-lisp
4545
(with-current-buffer "types.rs"
4646
(seq-map (lambda (capture)
4747
(pcase-let ((`(_ . ,node) capture))
4848
(ts-node-text node)))
49-
(tree-sitter-query [(function_item (identifier) @name)])))
49+
(tree-sitter-debug-query [(function_item (identifier) @name)])))
5050
```
5151
- Write a simple extension to `expand-region`:
5252
```emacs-lisp
@@ -125,12 +125,13 @@ For consistency with Emacs's conventions, this binding has some differences comp
125125
+ `ts-make-query`: create a new query.
126126
+ `ts-make-query-cursor`: create a new query cursor.
127127
+ `ts-query-matches`, `ts-query-captures`: execute a query, returning matches/captures.
128-
+ `ts-set-byte-range`, `ts-set-point-range`: limit query execution to a range.
129128
130129
## Setup for Development
131130
132131
Clone this repo and add its `lisp` and `langs` directories to `load-path`.
133132
133+
Install [cask](https://cask.readthedocs.io) and run `cask install` to install dev dependencies.
134+
134135
If you want to hack on the high-level features (in Lisp) only:
135136
- Evaluate this (once) to download the necessary binaries:
136137
```emacs-lisp

bin/build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ source "$here/env.bash"
2020

2121
MODULE_DIR="$PROJECT_ROOT/target/$TARGET"
2222
cp -f "$MODULE_DIR/$MODULE_ORIGINAL" "./lisp/$MODULE_RENAMED"
23+
24+
cask build
2325
)

bin/build.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Push-Location $project_root
1717

1818
cargo build --all $extra
1919

20-
Pop-Location
21-
2220
Copy-Item $module_dir\$module_name.dll $project_root\lisp\$module_renamed.dll
21+
22+
cask build
23+
24+
Pop-Location

bin/test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if [[ $* == "watch" ]]; then
1212
cargo watch -s bin/build -s bin/test
1313
)
1414
else
15-
$EMACS --batch \
15+
cask emacs --batch \
1616
--directory "$PROJECT_ROOT/lisp" \
1717
--directory "$PROJECT_ROOT/langs" \
1818
-l ert \

bin/test.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if ($args[0] -eq "watch") {
1919
# https://github.com/PowerShell/PowerShell/issues/4002
2020
# https://stackoverflow.com/questions/2095088/error-when-calling-3rd-party-executable-from-powershell-when-using-an-ide
2121
$ErrorActionPreference = 'Continue'
22-
emacs --batch `
22+
cask emacs --batch `
2323
--directory "$project_root\lisp" `
2424
--directory "$project_root\langs" `
2525
-l ert `

langs/tree-sitter-langs-build.el

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ If VERSION and OS are not spcified, use the defaults of
8888
(php "v0.16.1")
8989
(python "v0.16.0")
9090
(ruby "v0.16.1")
91-
(rust "v0.16.0")
91+
(rust "3e5ec5a")
9292
(scala "v0.13.0")
9393
(swift "a22fa5e")
9494
(typescript "v0.16.1" ("typescript" "tsx")))
@@ -181,9 +181,10 @@ This function requires git and tree-sitter CLI."
181181
(if (file-directory-p dir)
182182
(let ((default-directory dir))
183183
(tree-sitter-langs--call "git" "remote" "-v" "update"))
184-
(tree-sitter-langs--call "git" "clone" "-v" repo dir))
184+
(tree-sitter-langs--call "git" "clone" "-q" repo dir))
185185
(let ((default-directory dir))
186186
(tree-sitter-langs--call "git" "reset" "--hard" version)
187+
(tree-sitter-langs--call "npm" "set" "progress=false")
187188
;; TODO: Figure out why we need to skip `npm install' for some repos.
188189
(ignore-errors
189190
(tree-sitter-langs--call "npm" "install"))

langs/tree-sitter-langs.el

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,37 @@
1111

1212
;;; Commentary:
1313

14-
;; This is a convenient bundle of language grammars for `tree-sitter'. It serves
15-
;; as an interim distribution mechanism, until `tree-sitter' is widespread
16-
;; enough for language major modes to include the definitions on their own.
14+
;; This is a convenient bundle of language grammars and queries for
15+
;; `tree-sitter'. It serves as an interim distribution mechanism, until
16+
;; `tree-sitter' is widespread enough for language major modes to include these
17+
;; definitions on their own.
18+
;;
19+
;; Basically it's a multi-step process:
20+
;;
21+
;; 1. `tree-sitter-langs' populates global registries of grammars and queries.
22+
;; These global registries are defined by `tree-sitter-mode' and other
23+
;; `tree-sitter'-based language-agnostic minor modes, to extend existing
24+
;; major modes.
25+
;;
26+
;; 2. New `tree-sitter'-based language-specific minor modes use these global
27+
;; registries to extend existing major modes.
28+
;;
29+
;; 3. Major modes adopt new `tree-sitter'-based features, and distribute the
30+
;; grammars and queries on their own. They can either put these definitions
31+
;; in the global registries, or keep using them only internally.
1732

1833
;;; Code:
1934

35+
(require 'cl-lib)
36+
2037
(require 'tree-sitter)
2138
(require 'tree-sitter-load)
39+
(require 'tree-sitter-hl)
2240

2341
(require 'tree-sitter-langs-build)
2442

2543
(eval-when-compile
26-
(require 'pcase)
27-
(require 'cl-lib))
44+
(require 'pcase))
2845

2946
(defun tree-sitter-langs-ensure (lang-symbol)
3047
"Return the language object identified by LANG-SYMBOL.
@@ -71,5 +88,26 @@ See `tree-sitter-langs-repos'."
7188
(setf (map-elt tree-sitter-major-mode-language-alist major-mode)
7289
lang-symbol))
7390

91+
(defun tree-sitter-langs--hl-default-patterns (lang-symbol)
92+
"Return default syntax highlighting patterns for LANG-SYMBOL."
93+
(let ((query-path (cl-reduce
94+
(lambda (dir name) (expand-file-name name dir))
95+
`("repos" ,(format "tree-sitter-%s" lang-symbol)
96+
"queries" "highlights.scm")
97+
:initial-value tree-sitter-langs--dir)))
98+
(with-temp-buffer
99+
(insert-file-contents query-path)
100+
(buffer-string))))
101+
102+
(defun tree-sitter-langs--set-hl-default-patterns (&rest _args)
103+
"Use syntax highlighting patterns provided by `tree-sitter-langs'."
104+
(unless tree-sitter-hl-default-patterns
105+
(let ((lang-symbol (alist-get major-mode tree-sitter-major-mode-language-alist)))
106+
(setq tree-sitter-hl-default-patterns
107+
(tree-sitter-langs--hl-default-patterns lang-symbol)))))
108+
109+
(advice-add 'tree-sitter-hl--setup :before
110+
#'tree-sitter-langs--set-hl-default-patterns)
111+
74112
(provide 'tree-sitter-langs)
75113
;;; tree-sitter-langs.el ends here

lisp/test-files/change-case-region.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Change case of "this text" repeatedly.
2+
unsafe {
3+
input_function.call_unprotected((bytepos, point.line_number(), point.byte_column()))
4+
.and_then(|v| v.into_rust())
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Delete the 8 chars next line.
2+
// ấấấấấấấấ
3+
unsafe {
4+
input_function.call_unprotected((bytepos, point.line_number(), point.byte_column()))
5+
}

lisp/test-files/extend-region.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
macro_rules! impl_pred {}
2+
3+
// In evil's normal-mode, with point after `i`, or `;`, or at the end of the file, eval
4+
// (font-lock-flush). A correct implementation would highlight `i` and `!`.
5+
abc
6+
impl_pred!(foo, bar);

0 commit comments

Comments
 (0)