Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc changes #70

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions evil-tree-edit.el
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
;; Author: Ethan Leba <[email protected]>
;; Version: 0.1.0
;; Homepage: https://github.com/ethan-leba/tree-edit
;; Package-Requires: ((emacs "27.1") (tree-edit "0.1.0") (tree-sitter "0.15.0") (evil "1.0.0") (avy "0.5.0") (s "0.0.0"))
;; Package-Requires: ((emacs "29.1") (tree-edit "0.1.0") (tree-sitter "0.15.0") (evil "1.0.0") (avy "0.5.0") (s "0.0.0"))
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
;; This file is not part of GNU Emacs.
Expand Down Expand Up @@ -107,7 +107,7 @@ but it seems to not work reliably with `tree-edit--node-from-steps'."
((or (not parent)
(not (treesit-node-parent parent))) node)
((treesit-node-eq parent (treesit-buffer-root-node)) node)
((--any (member (treesit-node-type parent)
((--any (member (tree-edit--node-type parent)
(cons it (alist-get it tree-edit--subtypes '())))
(tree-edit-significant-node-types))
parent)
Expand Down Expand Up @@ -160,8 +160,8 @@ but it seems to not work reliably with `tree-edit--node-from-steps'."
(defun evil-tree-edit-goto-sig-parent ()
"Move to the next (interesting) named sibling."
(interactive)
(evil-tree-edit--remember)
(evil-tree-edit-ensure-current-node)
(evil-tree-edit--remember)
(evil-tree-edit--apply-movement #'evil-tree-edit--get-sig-parent))

(defun evil-tree-edit-back ()
Expand Down Expand Up @@ -217,7 +217,7 @@ NODE-TYPE can be a symbol or a list of symbol."
(evil-tree-edit-ensure-current-node)
(evil-tree-edit--remember)
(let ((query-node
(if (member (treesit-node-type (evil-tree-edit-current-node))
(if (member (tree-edit--node-type (evil-tree-edit-current-node))
(tree-edit-significant-node-types))
(evil-tree-edit-current-node)
(evil-tree-edit--get-sig-parent (evil-tree-edit-current-node)))))
Expand Down Expand Up @@ -248,14 +248,12 @@ NODE-TYPE can be a symbol or a list of symbol."
(interactive)
(evil-tree-edit-ensure-current-node)
(evil-tree-edit--remember)
(let ((new-node (evil-tree-edit-current-node))
(node-types (if (listp node-type/s) node-type/s `(,node-type/s))))
(while (and (treesit-node-parent new-node)
(not (member (treesit-node-type new-node) node-types)))
(setq new-node (treesit-node-parent new-node)))
(if (not (treesit-node-parent new-node))
(user-error "Current node has no parent of type %s" node-type/s)
(evil-tree-edit--goto-node new-node))))
(let* ((node-types (if (listp node-type/s) node-type/s `(,node-type/s))))
(if-let (result (treesit-parent-until
(evil-tree-edit-current-node)
(lambda (node) (member (tree-edit--node-type node) node-types))))
(evil-tree-edit--goto-node result)
(user-error "Current node has no parent of type %s" node-type/s))))

(defun evil-tree-edit--avy-jump (nodes)
"Avy jump to one of NODES."
Expand Down Expand Up @@ -409,14 +407,17 @@ Placeholder is defined by `tree-edit-placeholder-node-type'."
(current-prefix-arg (evil-tree-edit-preview-node))
(t (evil-tree-edit-ensure-current-node)
(message "Current node type is '%s', bound to key '%s'."
(treesit-node-type (evil-tree-edit-current-node))
(--any
(when (member (treesit-node-type (evil-tree-edit-current-node))
(if (listp (plist-get it :type))
(plist-get it :type)
`(,(plist-get it :type))))
(plist-get it :key))
(tree-edit-nodes))))))
(tree-edit--node-type (evil-tree-edit-current-node))
(let ((node-type (thread-last
(evil-tree-edit-current-node)
(tree-edit--node-type))))
(--any
(when (member node-type
(if (listp (plist-get it :type))
(plist-get it :type)
`(,(plist-get it :type))))
(plist-get it :key))
(tree-edit-nodes)))))))

(defun evil-tree-edit-preview-node ()
"Preview the different variations of the current node."
Expand All @@ -426,8 +427,8 @@ Placeholder is defined by `tree-edit-placeholder-node-type'."
(reazon-timeout 0.1)
(tree-edit-parse-comments nil))
(--> (evil-tree-edit-current-node)
(treesit-node-type it)
(alist-get it tree-edit-grammar)
(tree-edit--node-type it)
(alist-get it (tree-edit-grammar))
;; TODO: Parametrize
(reazon-run 10 q (tree-edit-parseo it q '()))
;; Sometime parser returns repeats, not sure if that's expected
Expand Down Expand Up @@ -502,7 +503,7 @@ This is so that the current node will be properly highlighted in explorer mode."
(evil-tree-edit-current-node)))
(overlay-put evil-tree-edit--node-overlay 'after-string
(propertize
(let ((type (treesit-node-type (evil-tree-edit-current-node))))
(let ((type (tree-edit--node-type (evil-tree-edit-current-node))))
(s-concat " " (if (stringp type) type (symbol-name type))))
;; TODO: Abstract into var
'face '(italic :foreground "dark gray")))
Expand Down
59 changes: 3 additions & 56 deletions tests/setup.el
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
;; -*- lexical-binding: t; -*-
(require 'dash)
(require 'mode-local)
(require 'buttercup)
(require 'url-parse)

(require 'tree-edit)
(require 'evil-tree-edit)
(require 'tree-edit-build)
(require 'url-parse)
(require 'evil-tree-edit)

(defvar tree-edit--test-grammar-location (expand-file-name ".test-grammars/"))
(defvar tree-edit--test-major-mode-mapping
Expand All @@ -14,52 +14,6 @@
(c-mode . c)))

(when (getenv "TESTING")

;; HACK!! bug#62704 introduces a call to `read-string` that breaks CI
(defun treesit-install-language-grammar (lang)
"Build and install the tree-sitter language grammar library for LANG.

Interactively, if `treesit-language-source-alist' doesn't already
have data for building the grammar for LANG, prompt for its
repository URL and the C/C++ compiler to use.

This command requires Git, a C compiler and (sometimes) a C++ compiler,
and the linker to be installed and on PATH. It also requires that the
recipe for LANG exists in `treesit-language-source-alist'.

See `exec-path' for the current path where Emacs looks for
executable programs, such as the C/C++ compiler and linker."
(interactive (list (intern
(completing-read
"Language: "
(mapcar #'car treesit-language-source-alist)))))
(when-let ((recipe
(or (assoc lang treesit-language-source-alist)
(treesit--install-language-grammar-build-recipe
lang))))
(condition-case err
(apply #'treesit--install-language-grammar-1
;; The nil is OUT-DIR.
(cons nil recipe))
(error
(display-warning
'treesit
(format "Error encountered when installing language grammar: %s"
err)))))

;; Check that the installed language grammar is loadable.
(pcase-let ((`(,available . ,err)
(treesit-language-available-p lang t)))
(when (not available)
(display-warning
'treesit
(format "The installed language grammar for %s cannot be located or has problems (%s): %s"
lang (nth 0 err)
(string-join
(mapcar (lambda (x) (format "%s" x))
(cdr err))
" "))))))

(make-directory tree-edit--test-grammar-location 'parents)
(setq user-emacs-directory tree-edit--test-grammar-location)
(setq tree-edit-storage-dir tree-edit--test-grammar-location)
Expand Down Expand Up @@ -105,14 +59,8 @@ executable programs, such as the C/C++ compiler and linker."
(evil-tree-edit--update-overlay))))))

(defun tree-edit--test-setup (mode)
;; FIXME, disable mode grammar activaiton?
(funcall mode)

(treesit-parser-create
(or (alist-get mode tree-edit-language-alist)
(user-error "[test harness] no grammar specified for %s" major-mode))
temp-buffer)

(evil-mode)
(evil-tree-edit-mode))

Expand All @@ -131,7 +79,6 @@ executable programs, such as the C/C++ compiler and linker."
(switch-to-buffer temp-buffer)

(tree-edit--test-setup ,mode)
(mode-local--activate-bindings)
(insert ,contents)
(goto-char 0)

Expand Down
47 changes: 47 additions & 0 deletions tests/test-evil.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(require 'evil-tree-edit)
(ignore-errors (load-file "setup.el"))

(describe "movement cmds work"
(it "out"
(expect (with-tree-test-buffer #'python-mode "
if foo:
bar([foo()])"
(evil-tree-edit-out 'if_statement))
:to-have-buffer-contents "
[if foo:
bar(foo())]"))
(it "sig out"
(expect (with-tree-test-buffer #'python-mode "
def foo():

def bar():
x = x + [5]"
(evil-tree-edit-goto-sig-parent))
:to-have-buffer-contents "
def foo():

[def bar():
x = x + 5]")
(expect (with-tree-test-buffer #'python-mode "
def foo():

def bar():
x = x + [5]"
(evil-tree-edit-goto-sig-parent))
:to-have-buffer-contents "
def foo():

[def bar():
x = x + 5]"))
(it "avy jumps"
(expect (with-tree-test-buffer-avy #'python-mode "
[def foo():

def bar():
x = x + 5]" 2
(evil-tree-edit-avy-jump 'identifier))
:to-have-buffer-contents "
def foo():

def bar():
[x] = x + 5")))