Skip to content

Commit

Permalink
feat: add tree-sitter
Browse files Browse the repository at this point in the history
  • Loading branch information
xhcoding committed Dec 11, 2023
1 parent 8433ea5 commit 9a424ec
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 16 deletions.
16 changes: 0 additions & 16 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
- [[#expand-region-递增选区][expand-region 递增选区]]
- [[#openwith-外部工具打开文件][openwith 外部工具打开文件]]
- [[#启用-treesit][启用 treesit]]
- [[#ts-fold-基于-treesit-的代码折叠][ts-fold 基于 treesit 的代码折叠]]
- [[#elisp-配置][elisp 配置]]
- [[#c-开发配置][C++ 开发配置]]
- [[#citre][citre]]
Expand Down Expand Up @@ -1797,21 +1796,6 @@ ref:https://github.com/manateelazycat/toggle-one-window
(zig . ("https://github.com/GrayJack/tree-sitter-zig"))))
#+end_src

** ts-fold 基于 treesit 的代码折叠

#+begin_src emacs-lisp
(use-package ts-fold
:if full?
:straight (ts-fold :type git :host github :repo "AndrewSwerlick/ts-fold" :branch "andrew-sw/treesit-el-support")
:bind ("M-z" . ts-fold-toggle)
:config
;; elisp support
(add-to-list 'ts-fold-range-alist '(emacs-lisp-mode
(function_definition . ts-fold-range-seq)
(list . ts-fold-range-seq)
(special_form . ts-fold-range-seq))))
#+end_src

** elisp 配置

#+begin_src emacs-lisp
Expand Down
57 changes: 57 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -e

readonly EMACS_DIR="$(dirname "$(readlink -f "$0")")"
readonly LOAD_PATH=$EMACS_DIR/etc
readonly BUILD_PATH=$EMACS_DIR/build

PACK_NAME=emacs-x
PACK_SYSTEM=x86_64-linux
Expand Down Expand Up @@ -31,6 +32,62 @@ echo "Start install emacs-git"

guix install -L $LOAD_PATH emacs-git

echo "Start install tree sitter grammers"

if [[ ! -d $BUILD_PATH ]] ; then
mkdir $BUILD_PATH
fi

pushd $BUILD_PATH

guix pack -RR -r tree-sitter-grammar.tar.gz -S /opt/tree-sitter/lib=lib \
tree-sitter-bash \
tree-sitter-bibtex \
tree-sitter-c \
tree-sitter-c-sharp \
tree-sitter-clojure \
tree-sitter-cmake \
tree-sitter-cpp \
tree-sitter-css \
tree-sitter-dockerfile \
tree-sitter-elisp \
tree-sitter-elixir \
tree-sitter-elm \
tree-sitter-go \
tree-sitter-gomod \
tree-sitter-haskell \
tree-sitter-heex \
tree-sitter-html \
tree-sitter-java \
tree-sitter-javascript \
tree-sitter-json \
tree-sitter-julia \
tree-sitter-lua \
tree-sitter-markdown \
tree-sitter-markdown-gfm \
tree-sitter-meson \
tree-sitter-ocaml \
tree-sitter-org \
tree-sitter-php \
tree-sitter-plantuml \
tree-sitter-python \
tree-sitter-r \
tree-sitter-racket \
tree-sitter-ruby \
tree-sitter-rust \
tree-sitter-scala \
tree-sitter-scheme \
tree-sitter-typescript

tar xf tree-sitter-grammar.tar.gz

if [[ ! -d $EMACS_DIR/tree-sitter ]] ; then
mkdir $EMACS_DIR/tree-sitter
fi

cp -f opt/tree-sitter/lib/tree-sitter/* $EMACS_DIR/tree-sitter/
popd

echo "Start init emacs-config"

GUIX_PROFILE="${HOME}/.guix-profile"
Expand Down
70 changes: 70 additions & 0 deletions etc/emacs-x.scm
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#:use-module (guix git-download)
#:use-module (guix build-system)
#:use-module (guix build-system copy)
#:use-module (guix build-system tree-sitter)
#:use-module (guix utils)
#:use-module (gnu packages)
#:use-module (gnu packages base)
#:use-module (gnu packages emacs)
#:use-module (gnu packages version-control)
#:use-module (gnu packages tls)
#:use-module (gnu packages tree-sitter)
)

(define-public emacs-git
Expand Down Expand Up @@ -41,6 +43,74 @@
((#:configure-flags flags #~'())
#~(cons* "--with-native-compilation=no" (delete "--with-native-compilation=aot" #$flags))))))))

(define (tree-sitter-delete-generated-files grammar-directories)
#~(begin
(use-modules (guix build utils))
(delete-file "binding.gyp")
(delete-file-recursively "bindings")
(for-each
(lambda (lang)
(with-directory-excursion lang
(delete-file "src/grammar.json")
(delete-file "src/node-types.json")
(delete-file "src/parser.c")
(delete-file-recursively "src/tree_sitter")))
'#$grammar-directories)))

(define* (tree-sitter-grammar
name text hash version
#:key
(commit (string-append "v" version))
(repository-url
(format #f "https://github.com/tree-sitter/tree-sitter-~a" name))
(grammar-directories '("."))
(article "a")
(inputs '())
(get-cleanup-snippet tree-sitter-delete-generated-files)
(license license:expat))
"Returns a package for Tree-sitter grammar. NAME will be used with
tree-sitter- prefix to generate package name and also for generating
REPOSITORY-URL value if it's not specified explicitly, TEXT is a string which
will be used in description and synopsis. GET-CLEANUP-SNIPPET is a function,
it recieves GRAMMAR-DIRECTORIES as an argument and should return a G-exp,
which will be used as a snippet in origin."
(let* ((multiple? (> (length grammar-directories) 1))
(grammar-names (string-append text " grammar" (if multiple? "s" "")))
(synopsis (string-append "Tree-sitter " grammar-names))
(description
(string-append "This package provides "
(if multiple? "" article) (if multiple? "" " ")
grammar-names " for the Tree-sitter library."))
(name (string-append "tree-sitter-" name)))
(package
(name name)
(version version)
(home-page repository-url)
(source (origin
(method git-fetch)
(uri (git-reference
(url repository-url)
(commit commit)))
(file-name (git-file-name name version))
(sha256 (base32 hash))
(snippet
(get-cleanup-snippet grammar-directories))))
(build-system tree-sitter-build-system)
(arguments (list #:grammar-directories grammar-directories))
(inputs inputs)
(synopsis synopsis)
(description description)
(license license))))

(define-public tree-sitter-elisp
(let ((commit "e5524fdccf8c22fc726474a910e4ade976dfc7bb")
(revision "0"))
(tree-sitter-grammar
"elisp" "emacs-lisp"
"1wyzfb27zgpvm4110jgv0sl598mxv5dkrg2cwjw3p9g2bq9mav5d"
(git-version "1.3.0" revision commit)
#:repository-url "https://github.com/Wilfred/tree-sitter-elisp"
#:commit commit)))

(define (parent-directory file)
"Return FILE's parent directory.
Expand Down

0 comments on commit 9a424ec

Please sign in to comment.