├── .gitignore ├── assets ├── demo.gif └── diagram.png ├── Makefile ├── tests ├── test-elisp.el ├── test-evil.el ├── test-c.el ├── setup.el ├── test-python.el └── test-java.el ├── Cask ├── .github └── workflows │ └── test.yml ├── tree-edit-elisp.el ├── doc ├── using-tree-edit.org ├── parser-examples.org ├── implementation.org └── evil-tree-edit.org ├── tree-edit-java.el ├── README.org ├── tree-edit-build.el ├── tree-edit-python.el ├── tree-edit-c.el ├── evil-tree-edit.el └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | /.cask/ 2 | /.test-grammars/ 3 | -------------------------------------------------------------------------------- /assets/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ethan-leba/tree-edit/HEAD/assets/demo.gif -------------------------------------------------------------------------------- /assets/diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ethan-leba/tree-edit/HEAD/assets/diagram.png -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: test clean 2 | 3 | test: 4 | cask 5 | TESTING=1 cask exec buttercup -L . -l "tests/setup.el" --traceback pretty 6 | 7 | clean: 8 | rm -rf .cask 9 | rm -rf .test-grammars 10 | -------------------------------------------------------------------------------- /tests/test-elisp.el: -------------------------------------------------------------------------------- 1 | (require 'evil-tree-edit) 2 | (ignore-errors (load-file "setup.el")) 3 | 4 | (describe "elisp regressions" 5 | (it "properly spaces the cons ." 6 | ;; Fixed in commit 0393dc6 7 | (expect (with-tree-test-buffer #'emacs-lisp-mode "'([foo] . bar)" 8 | (evil-tree-edit-exchange 'symbol)) 9 | :to-have-buffer-contents"'([TREE] . bar)"))) 10 | -------------------------------------------------------------------------------- /Cask: -------------------------------------------------------------------------------- 1 | (source gnu) 2 | (source melpa) 3 | 4 | ;; read metadata from this file's package headers 5 | (package-file "tree-edit.el") 6 | 7 | (development 8 | (depends-on "buttercup") 9 | ;; FIXME: can't reference evil-tree-edit's package-file directly, since it 10 | ;; relies on tree-edit (which causes a dependency error). manually declaring 11 | ;; the deps. instead. 12 | (depends-on "evil") 13 | (depends-on "avy")) 14 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | pull_request: 5 | push: 6 | 7 | jobs: 8 | build: 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | emacs_version: 13 | # Only using snapshot for now since we're using cutting edge TS features 14 | - snapshot 15 | steps: 16 | - uses: purcell/setup-emacs@master 17 | with: 18 | version: ${{ matrix.emacs_version }} 19 | 20 | - name: Checkout 21 | uses: actions/checkout@v3 22 | 23 | - name: Install Cask 24 | run: git clone https://github.com/cask/cask ~/.cask 25 | 26 | - name: Add to path 27 | run: echo "${HOME}/.cask/bin" >> $GITHUB_PATH 28 | 29 | - name: Run tests 30 | run: 31 | # XXX: This is really dumb but building grammars with C++ fails to load unless I do this 32 | mkdir wtf/; 33 | cp /lib/x86_64-linux-gnu/libstdc++.so.6 wtf/; 34 | LD_LIBRARY_PATH=wtf/ make test 35 | -------------------------------------------------------------------------------- /tests/test-evil.el: -------------------------------------------------------------------------------- 1 | (require 'evil-tree-edit) 2 | (ignore-errors (load-file "setup.el")) 3 | 4 | (describe "movement cmds work" 5 | (it "out" 6 | (expect (with-tree-test-buffer #'python-mode " 7 | if foo: 8 | bar([foo()])" 9 | (evil-tree-edit-out 'if_statement)) 10 | :to-have-buffer-contents " 11 | [if foo: 12 | bar(foo())]")) 13 | (it "sig out" 14 | (expect (with-tree-test-buffer #'python-mode " 15 | def foo(): 16 | 17 | def bar(): 18 | x = x + [5]" 19 | (evil-tree-edit-goto-sig-parent)) 20 | :to-have-buffer-contents " 21 | def foo(): 22 | 23 | [def bar(): 24 | x = x + 5]") 25 | (expect (with-tree-test-buffer #'python-mode " 26 | def foo(): 27 | 28 | def bar(): 29 | x = x + [5]" 30 | (evil-tree-edit-goto-sig-parent)) 31 | :to-have-buffer-contents " 32 | def foo(): 33 | 34 | [def bar(): 35 | x = x + 5]")) 36 | (it "avy jumps" 37 | (expect (with-tree-test-buffer-avy #'python-mode " 38 | [def foo(): 39 | 40 | def bar(): 41 | x = x + 5]" 2 42 | (evil-tree-edit-avy-jump 'identifier)) 43 | :to-have-buffer-contents " 44 | def foo(): 45 | 46 | def bar(): 47 | [x] = x + 5"))) 48 | -------------------------------------------------------------------------------- /tree-edit-elisp.el: -------------------------------------------------------------------------------- 1 | ;;; tree-edit-elisp.el --- Description -*- lexical-binding: t; -*- 2 | ;; 3 | ;; Copyright (C) 2021 Ethan Leba 4 | ;; Author: Ethan Leba 5 | ;; SPDX-License-Identifier: GPL-3.0-or-later 6 | ;; Version: 0.1.0 7 | ;; Package-Requires: ((emacs "27.0")) 8 | ;; Homepage: https://github.com/ethan-leba/tree-edit 9 | ;; 10 | ;; This file is not part of GNU Emacs. 11 | ;; 12 | ;;; Commentary: 13 | ;; 14 | ;; This file contains key bindings and other configuration for `tree-edit' to 15 | ;; work with Elisp. 16 | ;; 17 | ;; 18 | ;; 19 | ;; This file is not part of GNU Emacs. 20 | ;; 21 | ;;; Commentary: 22 | ;; 23 | ;;; Code: 24 | (require 'tree-edit) 25 | 26 | (tree-edit--set-parser-local-vars 27 | 'elisp 28 | 29 | tree-edit-placeholder-node-type 30 | 'symbol 31 | 32 | tree-edit-syntax-snippets 33 | ((list . ("(" symbol ")")) 34 | (vector . ("[" "]")) 35 | (quote . ("'" symbol)) 36 | (unquote . ("," symbol)) 37 | (symbol . ("TREE"))) 38 | 39 | tree-edit-whitespace-rules 40 | ((nil (comment nil (:newline)))) 41 | 42 | tree-edit-node-deletion-override 43 | ((source_file . tree-edit-simple-delete-override)) 44 | 45 | tree-edit-node-replacement-override 46 | ((source_file . tree-edit-simple-insertion-replacement-override)) 47 | 48 | tree-edit-node-insertion-override 49 | ((source_file . tree-edit-simple-insertion-replacement-override)) 50 | 51 | tree-edit-nodes 52 | ((:type list 53 | :key "l" 54 | :wrap-override '((list . ("(" symbol ")")))) 55 | (:type symbol 56 | :key "s") 57 | (:type vector 58 | :key "v") 59 | (:type hash_table 60 | :key "h") 61 | (:type quote 62 | :key "q") 63 | (:type unquote 64 | :key "u") 65 | (:type unquote_splice 66 | :key "U"))) 67 | 68 | 69 | (provide 'tree-edit-elisp) 70 | ;;; tree-edit-elisp.el ends here 71 | -------------------------------------------------------------------------------- /tests/test-c.el: -------------------------------------------------------------------------------- 1 | (require 'evil-tree-edit) 2 | (require 'buttercup) 3 | (ignore-errors (load-file "setup.el")) 4 | (describe "C specific regressions" 5 | (xit "handles alias-related issues" 6 | (expect (with-tree-test-buffer #'c-mode "{struct Test {int [foo]};}" 7 | (evil-tree-edit-wrap-node 'pointer_declarator)) 8 | :to-have-buffer-contents "{struct Test {int [*foo]};}") 9 | ;; pointer decl uses non-aliased name 10 | (expect (with-tree-test-buffer #'c-mode " 11 | struct foo { 12 | char **[data]; 13 | };" 14 | (evil-tree-edit-raise)) 15 | :to-have-buffer-contents " 16 | struct foo { 17 | char *[data]; 18 | };") 19 | (expect (with-tree-test-buffer #'c-mode "char*[* foo](){};" 20 | (evil-tree-edit-raise)) 21 | :to-have-buffer-contents "char[* foo()]{};")) 22 | (it "can handle non-named aliases" 23 | (expect (with-tree-test-buffer #'c-mode " 24 | #if FOO 25 | [hi();] 26 | #endif" 27 | (evil-tree-edit-delete)) 28 | :to-have-buffer-contents " 29 | #if [FOO] 30 | #endif")) 31 | ;; puts the same thing back, reduced number of nodes not true? 32 | (it "can delete node by replacing named node with syntax" 33 | (expect (with-tree-test-buffer #'c-mode "for([void TREE;] TREE;){}" 34 | (evil-tree-edit-delete)) 35 | :to-have-buffer-contents "for(;[TREE];){}")) 36 | ;; delete surrounding syntax? 37 | (xit "can reorganize syntax" 38 | (expect (with-tree-test-buffer #'c-mode "for (; [TREE];) {}" 39 | (evil-tree-edit-insert-sibling 'identifier)) 40 | :to-have-buffer-contents "for (; TREE; [TREE]) {}")) 41 | ;; needs to be wrapped in 'type specifier' 42 | (xit "has dwim nodes where needed" 43 | (expect (with-tree-test-buffer #'c-mode "{sizeof([foo]);}" 44 | (evil-tree-edit-exchange "struct Foo")) 45 | :to-have-buffer-contents "{sizeof([struct Foo]);}")) 46 | (it "?" (expect (with-tree-test-buffer #'c-mode " 47 | {if (foo) [bar;]}" 48 | (evil-tree-edit-exchange "{bar;}")) 49 | :to-have-buffer-contents 50 | " 51 | {if (foo)[{ 52 | bar; 53 | }]}"))) 54 | -------------------------------------------------------------------------------- /doc/using-tree-edit.org: -------------------------------------------------------------------------------- 1 | Documentation on using the tree-edit library. More to come! 2 | 3 | * Rolling your own tree-edit commands 4 | 5 | ** Evil style 6 | Something like a [[https://github.com/noctuid/lispyville][lispyville]] may be of interest for those who prefer to be in 7 | normal mode. Here's some simple examples of how you could implement that: 8 | 9 | #+begin_src elisp 10 | (evil-define-operator my/tree-edit-delete (beg end) 11 | "Delete node between BEG and END, if possible." 12 | (let ((node (tsc-get-named-descendant-for-position-range 13 | (tsc-root-node tree-sitter-tree) beg end))) 14 | (tree-edit-delete node))) 15 | 16 | (evil-define-operator my/tree-edit-raise (beg end) 17 | "Raise node between BEG and END, if possible." 18 | (let ((node (tsc-get-named-descendant-for-position-range 19 | (tsc-root-node tree-sitter-tree) beg end))) 20 | (tree-edit-raise node))) 21 | 22 | (evil-define-key '(normal visual) global-map "gk" #'my/tree-edit-delete) 23 | (evil-define-key '(normal visual) global-map "g/" #'my/tree-edit-raise) 24 | #+end_src 25 | 26 | #+RESULTS: 27 | 28 | One thing to be careful of is whitespace: for example if you want to raise an 29 | identifier, typing =g/w= on a word would include the whitespace and would select 30 | the surrounding node instead (So =g/e= should be used). Similarly with text 31 | objects selecting the entire line. 32 | 33 | ** Emacs style 34 | 35 | #+begin_src elisp 36 | (defun my/tree-edit-raise-word () 37 | (interactive) 38 | (pcase-let* ((`(,beg . ,end) (bounds-of-thing-at-point 'word)) 39 | (node (tsc-get-named-descendant-for-position-range 40 | (tsc-root-node tree-sitter-tree) beg end))) 41 | (tree-edit-raise node))) 42 | 43 | (define-key (current-global-map) (kbd "M-r") #'my/tree-edit-raise-word) 44 | #+end_src 45 | 46 | #+RESULTS: 47 | : my/tree-edit-raise-word 48 | 49 | * Adding new languages to tree-edit 50 | 51 | 1. [[https://github.com/cask/cask][Install Cask]] 52 | 2. Install grammar using =tree-edit-install-grammar= 53 | 3. Copy one of the language files (=tree-edit-python.el= or similar) and adapt it to the new language 54 | 4. Add language to =tree-edit-language-alist= 55 | 5. See what breaks! 56 | 57 | ** Customizing languages 58 | 59 | Check out the docstrings of the variables used in =tree-sitter-python.el= and the pre-existing language files to see how to customize languages. 60 | -------------------------------------------------------------------------------- /doc/parser-examples.org: -------------------------------------------------------------------------------- 1 | #+TITLE: Implementation 2 | #+begin_quote 3 | Open this document in Emacs and type @@html:@@C-c C-c@@html:@@ to execute the code examples! 4 | #+end_quote 5 | 6 | 7 | #+begin_src elisp :results none 8 | (require 'reazon) 9 | (require 'tree-edit) 10 | (require 'tree-edit-java-grammar) 11 | 12 | (setq-local java-grammar 13 | (with-mode-local java-mode tree-edit-grammar)) 14 | #+end_src 15 | 16 | * A simple example 17 | 18 | #+begin_src elisp :results value list 19 | 20 | (reazon-run 10 (q) 21 | (reazon-set-equalo q '(1 2 3 4))) 22 | #+end_src 23 | 24 | * Relational parser (try statement) 25 | #+begin_src elisp :results value list 26 | (reazon-run 5 (tokens) 27 | ;; TOKENS is a list of tokens that parses as a 'try_statement'. (Ignore the '()) 28 | (tree-edit-parseo (alist-get 'try_statement java-grammar) tokens '())) 29 | #+end_src 30 | 31 | 32 | * Relational parser (function arguments) 33 | #+begin_src elisp :results value list 34 | (reazon-run 5 (tokens) 35 | ;; TOKENS is a list of tokens that parses as a 'argument_list'. 36 | (tree-edit-parseo (alist-get 'argument_list java-grammar) tokens '())) 37 | #+end_src 38 | 39 | 40 | * Inserting new tokens into a node (1) 41 | Insert an expression after =x= in =func(x)= 42 | 43 | #+begin_src elisp :results value list 44 | (reazon-run 1 (new-tokens) 45 | (reazon-fresh (tokens) 46 | ;; A token of type 'expression' exists in the new tokens 47 | (reazon-membero 'expression new-tokens) 48 | ;; NEW-TOKENS is preceeded by "(" and is followed by ")", where TOKENS is the entire list. 49 | (tree-edit--prefixpostfixo '("(" expression) new-tokens '(")") tokens) 50 | ;; TOKENS is a list of tokens that parses as a 'argument_list'. 51 | (tree-edit-parseo (alist-get 'argument_list java-grammar) tokens '()))) 52 | #+end_src 53 | 54 | 55 | * Inserting new tokens into a node (2) 56 | Insert an expression before =x= in =func(x)= 57 | 58 | #+begin_src elisp :results value list 59 | (reazon-run 1 (new-tokens) 60 | (reazon-fresh (tokens) 61 | ;; A token of type 'expression' exists in the new tokens 62 | (reazon-membero 'expression new-tokens) 63 | ;; NEW-TOKENS is preceeded by "(" and is followed by ")", where TOKENS is the entire list. 64 | (tree-edit--prefixpostfixo '("(") new-tokens '(expression ")") tokens) 65 | ;; TOKENS is a list of tokens that parses as a 'argument_list'. 66 | (tree-edit-parseo (alist-get 'argument_list java-grammar) tokens '()))) 67 | #+end_src 68 | 69 | * Inserting new tokens into a node (3) 70 | Insert another expression after =if (...) {...}= 71 | 72 | #+begin_src elisp :results value list 73 | (reazon-run 1 (new-tokens) 74 | (reazon-fresh (tokens) 75 | ;; TOKENS is a list of tokens that parses as a 'argument_list'. 76 | (tree-edit-parseo (alist-get 'if_statement java-grammar) tokens '()) 77 | ;; NEW-TOKENS is preceeded by "(" and is followed by ")", where TOKENS is the entire list. 78 | (tree-edit--prefixpostfixo '("if" parenthesized_expression statement) new-tokens '() tokens) 79 | ;; A token of type 'expression' exists in the new tokens 80 | (reazon-membero 'statement new-tokens))) 81 | #+end_src 82 | 83 | -------------------------------------------------------------------------------- /doc/implementation.org: -------------------------------------------------------------------------------- 1 | #+TITLE: Notes on the implementation 2 | 3 | Tree-edit's syntax generation has a fairly novel implementation (as far as I 4 | know), and is also unfortunately complicated by some issues with the tree-sitter 5 | API and some concessions made for performance. So this document serves to both 6 | explain the high level architecture of =tree-edit= and some of the complications 7 | it faces, which hopefully can result in upstream fixes! 8 | 9 | * High level overview 10 | 11 | Tree-edit's syntax generation consists of three main phases: 12 | 13 | - Converting text into a syntax tree 14 | - Modifying tokens in a syntactically correct manner 15 | - Converting the changes back into text 16 | 17 | ** Converting the buffer text into a syntax tree 18 | 19 | To convert the text of the buffer into a syntax tree, [[https://github.com/emacs-tree-sitter/elisp-tree-sitter][elisp-tree-sitter]] is used. 20 | Tree-sitter only offers a way one way conversion from text to syntax tree, so 21 | the rest is handled by tree-edit. 22 | 23 | ** Performing modifications on a node 24 | 25 | To perform the syntax generation in tree-edit, [[https://github.com/nickdrozd/reazon][reazon]], an elisp implementation 26 | of the logic programming DSL [[http://minikanren.org/][miniKanren]] is used. 27 | 28 | TL;DR on miniKanren: you can build functions and then 'reverse' them. So 29 | tree-edit defines a parser which can be used in reverse to generate all possible 30 | correct set of tokens for a node type. Tree-sitter generates a JSON file that 31 | describes the grammar of the language under parse, which tree-edit uses. Using 32 | this reverse parser along with some additional constraints can enable us to 33 | create structural editing functions: 34 | 35 | - Insertion :: Insert a new node of /node-type/ before or after /node/. This 36 | operation asserts that there are a number of new tokens to the left or right 37 | of /node/, which contain /node-type/. 38 | - Deletion :: Delete /node/. This operation removes /node/, and all surrounding 39 | syntax -- but syntax is allowed to repopulate if needed. 40 | - Replacement :: Replace /node/ with a new node of /node-type/. This operation 41 | simply checks if swapping /node/'s token for /node-type/ still parses. 42 | 43 | Syntax is never explicitly dealt with, it is simply added by the parser to meet 44 | the constraints. For example, the only way to add another expression in an 45 | argument list is to add a comma first, so if we assert that a new expression 46 | should exist in an argument list it will naturally follow. 47 | 48 | These operations compose very well to construct more complex operations. For 49 | example, raise travels up the parents until it can find an ancestor that can be 50 | replaced with /node/, and then replaces it, Slurp/barf are composed of 51 | delete/insertion operations, etc. 52 | 53 | See [[file:parser-examples.org][here]] for some interactive examples. 54 | 55 | ** Converting modified tokens back into text 56 | 57 | For newly inserted node, there's no singular way to represent it as text. An 58 | argument list could have 20 elements, or 0 -- either way it's an argument list! 59 | So =tree-edit-syntax-snippets= in the =tree-edit-.el= files define how 60 | new nodes are represented. 61 | 62 | ** Language files & mode-local variables 63 | 64 | Tree-edit interacts with different languages by using mode-local variables for 65 | each language, defined in the =tree-edit-= files. 66 | 67 | ** Generating grammar files 68 | 69 | While tree-sitter emits JSON files, we don't use them directly -- instead we 70 | preprocess the JSON and output it to a =tree-edit--grammar.el= file. 71 | This is handled by [[https://github.com/ethan-leba/tree-edit/blob/main/dev/tree-edit-generate-grammars.el][dev/tree-edit-generate-grammars.el]]. It performs some 72 | transformations on the grammar for better use with the relational parser, and 73 | performs some precalculations, like what node types show up for a given node, 74 | supertypes and subtypes, etc. 75 | 76 | * WIP Complications 77 | 78 | ** Hidden nodes 79 | Tree-sitter allows nodes to be hidden by prefixing their names with =_=. This 80 | can be disastrous for tree-edit in that it provides a false view of the tokens 81 | for a given node, causing a seemingly correct node to fail to parse. Tree-edit 82 | simply ignores the existence of these nodes in the parser. 83 | 84 | For example, =_newline=, =_indent=, and =_dedent= are [[https://github.com/tree-sitter/tree-sitter-python/issues/136][hidden in the Python 85 | grammar]]. Tree-edit hacks around this by using the whitespace rules, which works 86 | fine for the most part. 87 | -------------------------------------------------------------------------------- /tree-edit-java.el: -------------------------------------------------------------------------------- 1 | ;;; tree-edit-java.el --- Java configuration for tree-edit -*- lexical-binding: t; -*- 2 | ;; 3 | ;; Copyright (C) 2021 Ethan Leba 4 | ;; Author: Ethan Leba 5 | ;; SPDX-License-Identifier: GPL-3.0-or-later 6 | ;; Version: 0.1.0 7 | ;; Package-Requires: ((emacs "27.0")) 8 | ;; Homepage: https://github.com/ethan-leba/tree-edit 9 | ;; 10 | ;; This file is not part of GNU Emacs. 11 | ;; 12 | ;;; Commentary: 13 | ;; 14 | ;; This file contains key bindings and other configuration for `tree-edit' to 15 | ;; work with Java. 16 | ;; 17 | ;; This file is not part of GNU Emacs. 18 | ;; 19 | ;;; Commentary: 20 | ;; 21 | ;;; Code: 22 | (require 'tree-edit) 23 | 24 | (tree-edit--set-parser-local-vars 25 | 'java 26 | 27 | tree-edit-syntax-snippets 28 | ((class_declaration . ("class" identifier "{" "}")) 29 | (method_declaration . ("void" identifier "(" ")" "{" "}")) 30 | (method_declaration . ("void" identifier "(" ")" "{" "}")) 31 | (object_creation_expression . ("new" method_invocation)) 32 | (if_statement . ("if" parenthesized_expression block)) 33 | (while_statement . ("while" parenthesized_expression block)) 34 | (method_invocation . (identifier argument_list)) 35 | (try_statement . ("try" block catch_clause)) 36 | (catch_clause . ("catch" "(" catch_formal_parameter ")" block)) 37 | (finally_clause . ("finally" block)) 38 | (string_literal . ("\"\"")) 39 | (catch_formal_parameter . (catch_type "e")) 40 | (catch_type . ("Exception")) 41 | (local_variable_declaration . ("void" identifier "=" identifier ";")) 42 | (argument_list . ("(" ")")) 43 | (continue_statement . ("continue" ";")) 44 | (break_statement . ("break" ";")) 45 | (return_statement . ("return" ";")) 46 | (null_literal . ("null")) 47 | ;; I would use an identifier here, but java LSP won't format when 'TREE;' is present... 48 | (expression_statement . (method_invocation ";")) 49 | (block . ("{" "}")) 50 | (parenthesized_expression . ("(" identifier ")")) 51 | (expression . (identifier)) 52 | (identifier . ("TREE")) 53 | (formal_parameter . ("int" "TREE")) 54 | (import_declaration . ("import" "TREE" ";"))) 55 | 56 | tree-edit-whitespace-rules 57 | ((nil (comment nil (:newline)))) 58 | 59 | tree-edit-significant-node-types 60 | (block class_body) 61 | 62 | tree-edit-nodes 63 | ((:type if_statement 64 | :key "i" 65 | :wrap-override '((block . ("{" expression_statement "}")))) 66 | (:type return_statement 67 | :key "r") 68 | (:type while_statement 69 | :key "w" 70 | :wrap-override '((block . ("{" expression_statement "}")))) 71 | (:type expression_statement 72 | :key "e") 73 | (:type class_declaration 74 | :key "C") 75 | (:type identifier 76 | :key "a") 77 | (:type break_statement 78 | :key "B") 79 | (:type block 80 | :key "{") 81 | (:type string_literal 82 | :key "\"") 83 | (:type continue_statement 84 | :key "c") 85 | (:type local_variable_declaration 86 | :key "v") 87 | (:type method_invocation 88 | :name "static call" 89 | :key "c" 90 | :wrap-override '((argument_list . ("(" expression ")")))) 91 | (:type method_declaration 92 | :key "m") 93 | (:type object_creation_expression 94 | :key "n" 95 | :name "new object") 96 | (:type binary_expression 97 | :key "o+" 98 | :name "+ operator" 99 | :node-override '((binary_expression . (expression "-" expression)))) 100 | (:type binary_expression 101 | :key "o-" 102 | :name "- operator" 103 | :node-override '((binary_expression . (expression "-" expression)))) 104 | (:type binary_expression 105 | :key "o&" 106 | :name "and operator" 107 | :node-override '((binary_expression . (expression "&&" expression)))) 108 | (:type binary_expression 109 | :key "o|" 110 | :name "or operator" 111 | :node-override '((binary_expression . (expression "||" expression)))) 112 | (:type binary_expression 113 | :key "o=" 114 | :name "== operator" 115 | :node-override '((binary_expression . (expression "==" expression)))) 116 | (:type update_expression 117 | :key "+" 118 | :name "value++" 119 | :node-override '((update_expression . (expression "++")))) 120 | (:type modifiers 121 | :key "tp" 122 | :name "private" 123 | :node-override '((modifiers . ("private")))) 124 | (:type formal_parameter 125 | :key "P") 126 | (:type import_declaration 127 | :key "I") 128 | (:type try_statement 129 | :key "T") 130 | (:type catch_clause 131 | :key "C") 132 | (:type finally_clause 133 | :key "f") 134 | (:type null_literal 135 | :key "n"))) 136 | 137 | (provide 'tree-edit-java) 138 | ;;; tree-edit-java.el ends here 139 | -------------------------------------------------------------------------------- /tests/setup.el: -------------------------------------------------------------------------------- 1 | ;; -*- lexical-binding: t; -*- 2 | (require 'dash) 3 | (require 'buttercup) 4 | (require 'url-parse) 5 | 6 | (require 'tree-edit) 7 | (require 'tree-edit-build) 8 | (require 'evil-tree-edit) 9 | 10 | (defvar tree-edit--test-grammar-location (expand-file-name ".test-grammars/")) 11 | (defvar tree-edit--test-major-mode-mapping 12 | '((python-mode . python) 13 | (java-mode . java) 14 | (c-mode . c))) 15 | 16 | (when (getenv "TESTING") 17 | (make-directory tree-edit--test-grammar-location 'parents) 18 | (setq user-emacs-directory tree-edit--test-grammar-location) 19 | (setq tree-edit-storage-dir tree-edit--test-grammar-location) 20 | 21 | (setq treesit-extra-load-path `(,(expand-file-name "tree-sitter" user-emacs-directory))) 22 | (tree-edit-install-grammars-wizard nil :accept-all)) 23 | 24 | (defun buffer-status-as-string () 25 | (if (equal evil-state 'tree) 26 | (progn 27 | (let* ((current-node (or (evil-tree-edit-current-node) 28 | (buttercup-fail "Buffer in invalid state, no current node"))) 29 | (start (treesit-node-start current-node)) 30 | (end (treesit-node-end current-node))) 31 | (evil-tree-state -1) 32 | (goto-char end) 33 | (insert "]") 34 | (goto-char start) 35 | (insert "["))) 36 | (let ((p (point)) 37 | (m (when mark-active (mark)))) 38 | (goto-char p) 39 | (insert "|") 40 | 41 | ;; show mark as well (other side of selection, if any) 42 | (when m 43 | (goto-char m) 44 | (insert "~")))) 45 | 46 | (buffer-substring-no-properties (point-min) 47 | (point-max))) 48 | 49 | (defun select-node () 50 | (when (search-forward "[") 51 | (delete-char -1) 52 | (let ((start (point))) 53 | (when (search-forward "]") 54 | (delete-char -1) 55 | (let ((temp-node (treesit-node-descendant-for-range 56 | (treesit-buffer-root-node) start (point) :named))) 57 | (evil-tree-state) 58 | (evil-tree-edit-set-current-node temp-node) 59 | (evil-tree-edit--update-overlay)))))) 60 | 61 | (defun tree-edit--test-setup (mode) 62 | (funcall mode) 63 | 64 | (evil-mode) 65 | (evil-tree-edit-mode)) 66 | 67 | (defmacro with-base-test-buffer (mode contents &rest test-forms) 68 | "This awesome macro is adapted (borrowed) from 69 | https://github.com/abo-abo/lispy/blob/master/lispy-test.el#L15" 70 | (declare (indent 2)) 71 | `(progn 72 | (-when-let (b (get-buffer "tree-edit-test-buffer")) 73 | (kill-buffer b)) 74 | (let ((temp-buffer (get-buffer-create "tree-edit-test-buffer")) 75 | evil-tree-edit-after-change-hook 76 | evil-tree-edit-movement-hook 77 | python-indent-guess-indent-offset-verbose) 78 | (save-window-excursion 79 | (switch-to-buffer temp-buffer) 80 | 81 | (tree-edit--test-setup ,mode) 82 | (insert ,contents) 83 | (goto-char 0) 84 | 85 | ,@test-forms 86 | 87 | temp-buffer)))) 88 | 89 | (defmacro with-test-buffer (mode contents &rest test-forms) 90 | "This awesome macro is adapted (borrowed) from 91 | https://github.com/abo-abo/lispy/blob/master/lispy-test.el#L15" 92 | (declare (indent 2)) 93 | `(with-base-test-buffer ,mode ,contents 94 | (when (search-forward "|") 95 | (backward-delete-char 1)) 96 | ,@test-forms)) 97 | 98 | (defmacro with-type-cache (k v &rest body) 99 | (declare (indent 2)) 100 | (let ((hashname (gensym))) 101 | `(let ((,hashname (make-hash-table :test #'equal))) 102 | (puthash ,k ,v ,hashname) 103 | (let ((tree-edit--type-cache ,hashname)) 104 | ,@body)))) 105 | 106 | (defmacro with-tree-test-buffer (mode contents &rest test-forms) 107 | "This awesome macro is adapted (borrowed) from 108 | https://github.com/abo-abo/lispy/blob/master/lispy-test.el#L15" 109 | (declare (indent 2)) 110 | `(with-base-test-buffer ,mode ,contents 111 | (evil-tree-state -1) 112 | (select-node) 113 | ,@test-forms)) 114 | 115 | (buttercup-define-matcher :to-have-buffer-contents (test-buffer 116 | expected-contents) 117 | (setq test-buffer (funcall test-buffer)) 118 | (setq expected-contents (funcall expected-contents)) 119 | (with-current-buffer test-buffer 120 | (let ((contents (buffer-status-as-string))) 121 | (if (equal contents expected-contents) 122 | t 123 | `(nil . ,(format "Expected '%s' to equal '%s'." 124 | contents 125 | expected-contents)))))) 126 | 127 | (defmacro with-tree-test-buffer-avy (mode contents avy-index &rest test-forms) 128 | "If AVY-INDEX is out of bounds a 'user-ptr' error will appear." 129 | (declare (indent 3)) 130 | `(with-tree-test-buffer ,mode ,contents 131 | (cl-letf (((symbol-function 'avy-process) 132 | (lambda (positions) (funcall avy-action (nth ,avy-index positions))))) 133 | ,@test-forms))) 134 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | #+HTML:

This project is currently inactive - now that I'm a full time SWE I don't have the energy for side projects :(

2 | 3 | #+HTML:
MELPA
4 | #+HTML:

Tree-edit is very much a work-in-progress. Expect to run into bugs and breaking changes!

5 | #+HTML:

6 | 7 | Every programming language has a formally defined structure, but most text 8 | editors are completely ignorant to it. As a result, editing can oftentimes 9 | devolve into a tedious exercise in character manipulation. 10 | 11 | *Tree-edit provides [[#supported-languages][language-agnostic]] editing operations that map directly to 12 | the structure of the language*, abstracting away the process of manually 13 | entering syntax. Leveraging the [[https://github.com/tree-sitter/tree-sitter][tree-sitter]] parser, tree-edit always has access 14 | to the precise state of the syntax tree -- and directly wields the grammars of 15 | the languages under edit to power it's editing capabilities. 16 | 17 | * Overview 18 | The repository contains two co-existing packages (that will eventually be 19 | split). 20 | 21 | - [[file:doc/using-tree-edit.org][tree-edit]] :: The core library for structural editing. This library is 22 | intended to be used by other elispers who would like to implement their own 23 | structural editing or refactoring commands. 24 | - [[file:doc/evil-tree-edit.org][evil-tree-edit]] :: An evil state for structural editing with preconfigured 25 | bindings and visualization, as seen in the GIF. 26 | 27 | To get an overview of tree-edit's capabilities, check out the [[https://emacsconf.org/2021/talks/structural/][EmacsConf talk]]! 28 | 29 | * How does it work? 30 | 31 | #+HTML:

32 | 33 | Tree-edit relies heavily on the tree-sitter parser, leveraging the JSON intermediate representation that tree-sitter outputs to have a full 34 | understanding of what is valid for a given language with no language specific 35 | efforts on tree-edit's part. 36 | 37 | To learn more about how tree-edit works under the hood, see [[file:doc/implementation.org][this high-level 38 | overview]] or check out this [[file:doc/parser-examples.org][org doc with executable code examples]] demonstrating 39 | how the syntax generation works. 40 | 41 | * Supported languages 42 | 43 | | Status | Language | 44 | |--------+----------------| 45 | | ✅ | [[https://github.com/tree-edit/tree-sitter-python][Python]] ([[https://github.com/ethan-leba/tree-edit/issues/33][issue]]) | 46 | | 🔨 | [[https://github.com/tree-edit/tree-sitter-c][C]] ([[https://github.com/ethan-leba/tree-edit/issues/54][issue]]) | 47 | | 🔨 | [[https://github.com/tree-edit/tree-sitter-java][Java]] ([[https://github.com/ethan-leba/tree-edit/issues/34][issue]]) | 48 | 49 | See links for grammar repository and issue tracker respectively. 50 | 51 | | ✅ | Supported | 52 | | 🔨 | Under development | 53 | 54 | Tree-edit is designed to be as language-agnostic as possible. Currently the list of supported languages is not very impressive, but /in theory/ it should be as simple as running a script to preprocess a grammar and adding a configuration file for the language. In practice the grammars usually also need modifications in order to make the grammar ergonomic for structural modification. 55 | 56 | See [[https://github.com/ethan-leba/tree-edit/blob/main/doc/using-tree-edit.org#adding-new-languages-to-tree-edit][here]] to learn the process for adding a new language. 57 | 58 | * Custom grammars 59 | 60 | Tree-edit uses forked version of tree-sitter grammars to power it's editing. They are intended to work as a drop-in replacement for the standard grammar, but with tweaks to better work with tree-edit. See below for how install the forked grammars. 61 | 62 | The tree-sitter API and grammars were not designed with the structural editing 63 | usecase in mind, so most grammars are structured in a way that makes navigation and editing in tree-edit awkward or impossible without complex and fragile hackarounds. For more context, see this GH issue: 64 | https://github.com/tree-sitter/tree-sitter/issues/1558 65 | 66 | I hope that in the future more thought will be given to this usecase in terms of the tree-sitter API and grammar design so that the forks will eventually become unnecessary, but for now they're needed. 67 | 68 | ** Installing custom grammars 69 | The function =tree-edit-install-grammars-wizard= can be used interactively to install grammars. 70 | 71 | * Contributing 72 | 73 | Contributions are very much welcome! In particular, adding language files would be a great place to help. Otherwise, the issues are a good place to propose features or find ones to implement. 74 | 75 | In addition, reporting bugs and providing feedback on the overall design and UX of the package is much appreciated! Providing a good UX for structural editing is crucial and will become increasingly important to this package as more of the fundamental shortcomings get ironed out. 76 | 77 | The project is fairly complex and the documentation is still in progress, so 78 | feel free to open a discussion if you're interested in helping out but you're 79 | not sure where to start! 80 | 81 | ** Running tests 82 | 83 | The tests can be run with =make test=, while cached grammars can be cleaned out with =make clean=. 84 | 85 | * Related projects 86 | - [[https://github.com/drym-org/symex.el][symex]] :: Structural navigation and editing with backends for lisp and tree-sitter 87 | - [[https://github.com/mickeynp/combobulate][combobulate]] :: Structural navigation and limited structural editing 88 | - [[https://github.com/manateelazycat/grammatical-edit][grammatical-edit]] :: Smartparens-like using tree-sitter (?) 89 | - [[https://github.com/meain/evil-textobj-tree-sitter][evil-textobj-tree-sitter]] :: Evil mode text objects using tree-sitter queries. 90 | - [[https://github.com/abo-abo/lispy][lispy]] :: Lisp structural editing package -- big inspiration for tree-edit! 91 | - [[https://github.com/Fuco1/smartparens][smartparens]] :: Multilingual package with structural editing limited to matching delimiters. 92 | -------------------------------------------------------------------------------- /tests/test-python.el: -------------------------------------------------------------------------------- 1 | (require 'evil-tree-edit) 2 | (ignore-errors (load-file "setup.el")) 3 | 4 | (describe "slurp" 5 | (it "correctly indents" 6 | (expect (with-tree-test-buffer #'python-mode " 7 | if foo: 8 | [foo()] 9 | bar() 10 | baz()" 11 | (evil-tree-edit-goto-parent) 12 | (evil-tree-edit-slurp)) 13 | :to-have-buffer-contents " 14 | if foo: 15 | [foo() 16 | bar()] 17 | baz()") 18 | (expect (with-tree-test-buffer #'python-mode " 19 | if foo: 20 | [foo()] 21 | if nested: 22 | indentation(x) 23 | with difficulty(): 24 | sadness() 25 | qwert() 26 | baz()" 27 | (evil-tree-edit-goto-parent) 28 | (evil-tree-edit-slurp)) 29 | :to-have-buffer-contents " 30 | if foo: 31 | [foo() 32 | if nested: 33 | indentation(x) 34 | with difficulty(): 35 | sadness() 36 | qwert()] 37 | baz()")) 38 | (it "can slurp multilevel" 39 | (expect (with-tree-test-buffer #'python-mode " 40 | if TREE: 41 | FOO 42 | else: 43 | [BAR] 44 | TREE" 45 | (evil-tree-edit-goto-parent) 46 | (evil-tree-edit-slurp)) 47 | :to-have-buffer-contents " 48 | if TREE: 49 | FOO 50 | else: 51 | [BAR 52 | TREE]"))) 53 | 54 | (describe "barf" 55 | (it "correctly indents" 56 | (expect (with-tree-test-buffer #'python-mode " 57 | if foo: 58 | [foo() 59 | bar()] 60 | baz()" 61 | (evil-tree-edit-barf)) 62 | :to-have-buffer-contents " 63 | if foo: 64 | [foo()] 65 | bar() 66 | baz()")) 67 | (it "can barf multilevel" 68 | (expect (with-tree-test-buffer #'python-mode " 69 | if TREE: 70 | FOO 71 | else: 72 | [BAR 73 | TREE]" 74 | (evil-tree-edit-barf)) 75 | :to-have-buffer-contents " 76 | if TREE: 77 | FOO 78 | else: 79 | [BAR] 80 | TREE") 81 | (expect (with-tree-test-buffer #'python-mode " 82 | if TREE: 83 | FOO 84 | else: 85 | [BAR 86 | TREE]" 87 | (evil-tree-edit-barf)) 88 | :to-have-buffer-contents " 89 | if TREE: 90 | FOO 91 | else: 92 | [BAR] 93 | TREE"))) 94 | 95 | (describe "raise" 96 | (it "correctly indents" 97 | (expect (with-tree-test-buffer #'python-mode " 98 | if foo: 99 | [with gumbo: 100 | eat()]" 101 | (evil-tree-edit-raise)) 102 | :to-have-buffer-contents " 103 | [with gumbo: 104 | eat()]"))) 105 | 106 | (describe "copy/paste node" 107 | (xit "can copy blocks" 108 | (expect (with-tree-test-buffer #'python-mode 109 | " 110 | if foo: 111 | [qwert() 112 | bert()]" 113 | (evil-tree-edit-copy) 114 | (evil-tree-edit-exchange (car kill-ring))) 115 | :to-have-buffer-contents " 116 | if foo: 117 | [qwert() 118 | bert()]")) 119 | (it "can handle context-specific types" 120 | (expect (with-tree-test-buffer #'python-mode "foo([*bar])" 121 | (evil-tree-edit-copy) 122 | (evil-tree-edit-insert-sibling (car kill-ring))) 123 | :to-have-buffer-contents "foo(*bar,[*bar])") 124 | (expect (with-tree-test-buffer #'python-mode "{[1:2]}" 125 | (evil-tree-edit-copy) 126 | (evil-tree-edit-insert-sibling (car kill-ring))) 127 | :to-have-buffer-contents "{1:2,[1:2]}") 128 | (expect (with-tree-test-buffer #'python-mode "{[1:2],3:4}" 129 | (evil-tree-edit-copy) 130 | (evil-tree-edit-goto-next-sibling) 131 | (evil-tree-edit-exchange (car kill-ring))) 132 | :to-have-buffer-contents "{1:2,[1:2]}"))) 133 | 134 | (describe "insert sibling" 135 | (it "works" 136 | (expect (with-tree-test-buffer #'python-mode " 137 | if foo: 138 | [foo() 139 | bar()] 140 | baz()" 141 | (evil-tree-edit-insert-sibling 'elif_clause)) 142 | :to-have-buffer-contents " 143 | if foo: 144 | foo() 145 | bar() 146 | [elif TREE: 147 | TREE] 148 | baz()")) 149 | (it "handles DWIM nodes" 150 | (expect (with-tree-test-buffer #'python-mode " 151 | [foo()] 152 | bar()" 153 | (evil-tree-edit-insert-sibling 'call)) 154 | :to-have-buffer-contents " 155 | foo() 156 | [TREE()] 157 | bar()")) 158 | (it "can handle ambiguous fragments" 159 | ;; Could be identifier, or expression statement 160 | (expect (with-tree-test-buffer #'python-mode "foo([x])" 161 | (evil-tree-edit-insert-sibling "y")) 162 | :to-have-buffer-contents "foo(x,[y])") 163 | (expect (with-tree-test-buffer #'python-mode " 164 | if TREE: 165 | [x]" 166 | (evil-tree-edit-insert-sibling "y")) 167 | :to-have-buffer-contents " 168 | if TREE: 169 | x 170 | [y]"))) 171 | 172 | (describe "exchange" 173 | (it "works" 174 | (expect (with-tree-test-buffer #'python-mode " 175 | for [foo] in TREE: 176 | TREE 177 | " 178 | (evil-tree-edit-exchange "bar")) 179 | :to-have-buffer-contents " 180 | for [bar] in TREE: 181 | TREE 182 | "))) 183 | 184 | (describe "wrap" 185 | (it "works" 186 | (expect (with-tree-test-buffer #'python-mode " 187 | [for FOO in BAR: 188 | hi]" 189 | (evil-tree-edit-wrap-node 'for_statement)) 190 | :to-have-buffer-contents " 191 | [for TREE in TREE: 192 | for FOO in BAR: 193 | hi]")) 194 | ;; fixed by multitype? 195 | ;; expression_statement[identifier] -> return[identifier] 196 | (xit "can unwrap type" 197 | (expect (with-tree-test-buffer #'python-mode " 198 | for FOO in BAR: 199 | [hi]" 200 | (let ((tree-edit-syntax-snippets 201 | `((return_statement . ("return" expression)) . ,tree-edit-syntax-snippets))) 202 | (evil-tree-edit-wrap-node 'return_statement))) 203 | :to-have-buffer-contents " 204 | for FOO in BAR: 205 | [return hi]"))) 206 | 207 | (describe "goto next placeholder" 208 | (it "works" 209 | (expect (with-tree-test-buffer #'python-mode " 210 | [for TREE in BAR: 211 | TREE]" 212 | (evil-tree-edit-goto-next-placeholder)) 213 | :to-have-buffer-contents " 214 | for [TREE] in BAR: 215 | TREE") 216 | (expect (with-tree-test-buffer #'python-mode " 217 | [for qwer in BAR: 218 | TREE]" 219 | (evil-tree-edit-goto-next-placeholder)) 220 | :to-have-buffer-contents " 221 | for qwer in BAR: 222 | [TREE]"))) 223 | 224 | (describe "delete node" 225 | (it "doesn't allow empty blocks (manual grammar edit)" 226 | ;; Should select bounds of new named node 227 | (expect (with-tree-test-buffer #'python-mode " 228 | if foo: 229 | [bar()]" 230 | (evil-tree-edit-delete)) 231 | :to-throw 'tree-edit-transformation-error)) 232 | ;; previously would not put 'pass' on a newline 233 | (it "maintains spacing after deleted text" 234 | ;; Should select bounds of new named node 235 | (expect (with-tree-test-buffer #'python-mode " 236 | def foo() -> [bar]: 237 | pass" 238 | (evil-tree-edit-delete)) 239 | :to-have-buffer-contents " 240 | def foo(): 241 | [pass]"))) 242 | 243 | (describe "change node" 244 | ;; Simulating LSP autoimport 245 | (it "will default to node at point if stored node position is invalid" 246 | (expect (with-tree-test-buffer #'python-mode " 247 | foo([x])" 248 | (evil-tree-edit-change) 249 | (insert "baz") 250 | (save-excursion 251 | (goto-char (point-min)) 252 | (newline) 253 | (insert "import baz") 254 | (newline)) 255 | (evil-tree-edit-normal-or-tree-state)) 256 | :to-have-buffer-contents " 257 | import baz 258 | 259 | foo([baz])"))) 260 | -------------------------------------------------------------------------------- /doc/evil-tree-edit.org: -------------------------------------------------------------------------------- 1 | * Table of content 2 | :PROPERTIES: 3 | :TOC: :include all 4 | :END: 5 | :CONTENTS: 6 | - [[#table-of-content][Table of content]] 7 | - [[#getting-started][Getting started]] 8 | - [[#usage][Usage]] 9 | - [[#navigation][Navigation]] 10 | - [[#editing-operations][Editing operations]] 11 | - [[#pasting][Pasting]] 12 | - [[#a-note-on-raise-vs-delete-wrap-vs-insert][A note on raise vs. delete, wrap vs. insert]] 13 | - [[#customization][Customization]] 14 | :END: 15 | 16 | * Getting started 17 | =evil-tree-edit= can be installed on MELPA. 18 | 19 | Many, if not all languages will require custom grammars to be installed. See 20 | main README for instructions. 21 | 22 | 23 | After installation, add hooks for any language you'd like evil-tree-edit to 24 | automatically enable in. 25 | 26 | #+begin_src elisp 27 | (add-hook 'java-mode-hook #'evil-tree-edit-mode) 28 | #+end_src 29 | 30 | It's also recommended to use tree-edit alongside an autoformatter in it's 31 | current state, as tree-edit does not always produce text consistent in 32 | formatting with the surrounding nodes. 33 | 34 | * Usage 35 | The concept of the *cursor*, a position in the 2D plane of text, is replaced by 36 | the *current node*, which is a position in the syntax tree in tree-edit. All 37 | operations unless otherwise specified are performed on the current node. To help 38 | visualize the syntax tree, tree-edit provides @@html:@@M-x 39 | tree-edit-view-mode@@html:@@ as seen in the demo GIF. 40 | 41 | Tree-edit adopts a vim-style approach to editing, where certain operators also 42 | require a noun. In vim's case, the nouns are text objects; In tree-edit's case, 43 | the nouns are node types. For example, 44 | @@html:@@i@@html:@@@@html:@@v@@html:@@ would insert a 45 | variable declaration. Due to the fact that most languages contain a large number 46 | of node types, and vary across languages, *using [[https://github.com/justbur/emacs-which-key][which-key]] with tree-edit is 47 | highly recommended.* 48 | 49 | To activate tree-edit from normal state, press @@html:@@Q@@html:@@, 50 | and to return to normal state press @@html:@@ESC@@html:@@. 51 | 52 | ** Navigation 53 | The navigation primitives follow the tree structure of the language. 54 | 55 | | Operation | Keybind | Description | 56 | |-----------------------------+--------------------------------+-------------------------------------------------------------------------------------| 57 | | Next | @@html:@@j@@html:@@ | Move cursor to the next sibling. | 58 | | Previous | @@html:@@k@@html:@@ | Move cursor to the previous sibling. | 59 | | Inwards | @@html:@@f@@html:@@ | Move cursor to the first child. | 60 | | Outwards | @@html:@@h@@html:@@ | Move cursor to the parent. | 61 | | Jump to | @@html:@@s@@html:@@ | Avy jump to a node of /node-type/ for a node inside the current. | 62 | | Outwards Significant | @@html:@@A@@html:@@ | Move outwards until a significant node (e.g. function or class declaration) is hit. | 63 | | Goto Placeholder | @@html:@@n@@html:@@ | Jump to the first placeholder node within the current. | 64 | 65 | The definition of a placeholder node is configurable, but generally it's the 66 | =TREE= identifiers as seen in the GIF demo. 67 | 68 | ** Editing operations 69 | The most important feature of tree-edit: editing the syntax tree. 70 | 71 | For any editing operation, the syntax will be added or deleted based on the 72 | needs of the operation. For example, when adding an additional argument to a 73 | function, tree-edit can infer that a comma is needed based on the grammar of the 74 | language. 75 | 76 | =tree-edit-syntax-snippets= defines how node types will actually be represented 77 | upon insertion: see example [[https://github.com/ethan-leba/tree-edit/blob/main/tree-edit-java.el#L29][here]]. 78 | 79 | Any transformations will be rejected if a syntactically valid result cannot be 80 | generated. 81 | 82 | | Operation | Keybind | Description | 83 | |-------------------------------+--------------------------------+------------------------------------------------------------------------------------------------------------------------| 84 | | Raise | @@html:@@r@@html:@@ | Replace the current node's parent with the current node. | 85 | | Delete | @@html:@@d@@html:@@ | Delete the current node. | 86 | | Move | @@html:@@m@@html:@@ | Copy then delete the current node. | 87 | | Change | @@html:@@c@@html:@@ | Delete the current node and drop into insert state. Tree state will be re-entered on @@html:@@ESC@@html:@@. | 88 | | Wrap | @@html:@@w@@html:@@ | Create a new node of /node-type/ and insert the current one in it. | 89 | | Exchange | @@html:@@e@@html:@@ | Exchange the current node with a new node of /node-type/. | 90 | | Insert | @@html:@@i@@html:@@ | Insert a new node of /node-type/ to the right of the current. | 91 | | Append | @@html:@@a@@html:@@ | Insert a new node of /node-type/ to the left of the current. | 92 | | Insert Child | @@html:@@I@@html:@@ | Insert a new node of /node-type/ as a child of the current. Useful for nodes with no named children, i.e. ={}= | 93 | | Goto Placeholder and Change | @@html:@@N@@html:@@ | Jump to the first placeholder node within the current and edit it. | 94 | | Append Placeholder and Change | @@html:@@x@@html:@@ | Add a placeholder node and then immediately edit it. | 95 | | Slurp | @@html:@@>@@html:@@ | Grow the current node to contain the nearest right-most element. | 96 | | Barf | @@html:@@<@@html:@@ | Shrink the current node to place it's left-most element into the parent node. | 97 | | Copy | @@html:@@y@@html:@@ | Copy the text of the current node. | 98 | | Undo | @@html:@@u@@html:@@ | Undo the last operation. | 99 | | Preview | @@html:@@?@@html:@@ | Preview the possible variations of the current node. | 100 | | Tree view | @@html:@@V@@html:@@ | Enable =tree-edit-view= or display if already enabled. | 101 | 102 | ** Pasting 103 | Along with the standard node-types of the given language, tree-edit has a 104 | special node-type @@html:@@p@@html:@@ that will attempt to parse the 105 | type of the most recently copied text. If a type can be identified and the 106 | operation is valid, the copied text will be used. 107 | 108 | ** A note on raise vs. delete, wrap vs. insert 109 | 110 | Both of the following definition for argument list produce the same result on a 111 | textual level: 112 | 113 | #+begin_src 114 | argument_list = expression | seq[expression "," argument_list] 115 | argument_list = seq[expression, repeat["," expression]] 116 | #+end_src 117 | 118 | However, at the tree level, these two constructions result in different ways to 119 | modify the node. 120 | 121 | For the first construction, you'd need to use raise/wrap to add and remove expressions: 122 | #+begin_src 123 | (foo, [bar]) ==raise==> (foo) 124 | ([foo]) ===wrap==> (foo, bar) 125 | #+end_src 126 | 127 | While for the second, you can use insert/delete. 128 | #+begin_src 129 | (foo, [bar]) ==delete=> (foo) 130 | ([foo]) ==insert=> (foo, bar) 131 | #+end_src 132 | This is something you may need to be aware of if you're running trying to 133 | perform an operation that you think should work, but doesn't! In doubt, check 134 | the =grammar.js= of the language. 135 | 136 | * Customization 137 | 138 | Currently adding customization ontop of the preset language files requires a 139 | fair bit of boilerplate, but here's some code to get started. 140 | 141 | #+begin_src elisp 142 | (with-eval-after-load 'tree-edit-java 143 | (with-mode-local java-mode 144 | (setq-mode-local 145 | java-mode 146 | 147 | tree-edit-syntax-snippets 148 | (append 149 | ;; Put your snippets here 150 | '((identifier . ("FOOBAR"))) 151 | tree-edit-syntax-snippets) 152 | 153 | tree-edit-nodes 154 | (append 155 | ;; Put your nodes here 156 | '((:type if_statement 157 | :key "z" 158 | :name "if-else statement" 159 | :node-override '((if_statement . ("if" parenthesized_expression block "else" block))))) 160 | tree-edit-nodes))) 161 | 162 | (evil-tree-edit-set-state-bindings 'java-mode)) 163 | #+end_src 164 | 165 | See [[file:tree-edit-java.el]] and the docstrings of the accompanying variables 166 | for more information. 167 | -------------------------------------------------------------------------------- /tree-edit-build.el: -------------------------------------------------------------------------------- 1 | ;;; tree-edit-build.el --- Description -*- lexical-binding: t; -*- 2 | ;; 3 | ;; Copyright (C) 2021 Ethan Leba 4 | ;; 5 | ;; Author: Ethan Leba 6 | ;; SPDX-License-Identifier: GPL-3.0-or-later 7 | ;; 8 | ;; This file is not part of GNU Emacs. 9 | ;; 10 | ;;; Commentary: 11 | ;; 12 | ;; Utilities for compiling tree-sitter grammars and preprocessing them for use 13 | ;; with `tree-edit'. 14 | ;; 15 | ;;; Code: 16 | (require 'dash) 17 | (require 'json) 18 | (require 'cl-extra) 19 | (require 'tree-edit) 20 | 21 | (defun tree-edit--invert-supertypes (supertypes) 22 | "Invert SUPERTYPES alist into subtypes." 23 | (let ((subtypes '())) 24 | (progn 25 | (mapc (-lambda ((sub . supers)) 26 | (mapc 27 | (lambda (super) 28 | (if-let ((record (assoc super subtypes))) 29 | (setcdr record (cons sub (cdr record))) 30 | (setq subtypes `((,super . (,sub)) . ,subtypes)))) 31 | supers)) 32 | supertypes) 33 | subtypes))) 34 | 35 | (defun tree-edit--generate-supertype (type grammar possible-supertypes) 36 | "Return TYPE's supertypes (and itself) in GRAMMAR." 37 | (->> (-map #'car grammar) 38 | (--filter (and (or (s-starts-with-p "_" (symbol-name it)) 39 | (member it possible-supertypes)) 40 | (reazon-run 1 q (tree-edit-parseo (alist-get it grammar) `(,type) '())))) 41 | (--mapcat `(,it . ,(tree-edit--generate-supertype it grammar possible-supertypes))) 42 | (-uniq) 43 | (cons type))) 44 | 45 | (defun tree-edit--generate-supertypes (grammar possible-supertypes) 46 | "Return an alist of a type to it's supertypes (and itself) in GRAMMAR." 47 | (--map 48 | (let ((type (car it))) 49 | `(,type . ,(tree-edit--generate-supertype type grammar possible-supertypes))) 50 | grammar)) 51 | 52 | (defun tree-edit--generate-containing-types (grammar) 53 | "Return an alist of a type to all the types it contained in it's GRAMMAR." 54 | (--map `(,(car it) . ,(tree-edit--extract-types (cdr it))) grammar)) 55 | 56 | (defun tree-edit--extract-types (grammar) 57 | "Return a list of all the symbol types in GRAMMAR." 58 | (pcase grammar 59 | ((and `((type . ,type) 60 | (value . ,_) 61 | (content . ,content)) 62 | (guard (string-prefix-p "PREC" type))) 63 | ;; Silence the foolish linter. 64 | (ignore type) 65 | (tree-edit--extract-types content)) 66 | (`((type . "SEQ") 67 | (members . ,members)) 68 | (-mapcat #'tree-edit--extract-types members)) 69 | (`((type . "ALIAS") 70 | (content . ,_) 71 | (named . ,_) 72 | (value . ,alias-name)) 73 | `(,alias-name)) 74 | (`((type . "IMMEDIATE_TOKEN") 75 | (content . ,content)) 76 | (tree-edit--extract-types content)) 77 | (`((type . "REPEAT") 78 | (content . ,content)) 79 | (tree-edit--extract-types content)) 80 | (`((type . "REPEAT1") 81 | (content . ,content)) 82 | (tree-edit--extract-types content)) 83 | (`((type . "FIELD") 84 | (name . ,_) 85 | (content . ,content)) 86 | (tree-edit--extract-types content)) 87 | (`((type . "SYMBOL") 88 | (name . ,name)) 89 | `(,name)) 90 | (`((type . "CHOICE") 91 | (members . ,members)) 92 | (-mapcat #'tree-edit--extract-types members)) 93 | (_ '()))) 94 | 95 | (defun tree-edit--generate-alias-names (grammar) 96 | "Return an alist of a type to all the types it contained in it's GRAMMAR. 97 | 98 | Assumes that each node has only one alias. This may not be true!" 99 | (--map `(,(car it) . ,(tree-edit--extract-aliases (cdr it))) grammar)) 100 | 101 | (defun tree-edit--extract-aliases (grammar) 102 | "Return a list of all the symbol types in GRAMMAR." 103 | (pcase grammar 104 | ((and `((type . ,type) 105 | (value . ,_) 106 | (content . ,content)) 107 | (guard (string-prefix-p "PREC" type))) 108 | ;; Silence the foolish linter. 109 | (ignore type) 110 | (tree-edit--extract-aliases content)) 111 | (`((type . "SEQ") 112 | (members . ,members)) 113 | (-mapcat #'tree-edit--extract-aliases members)) 114 | (`((type . "ALIAS") 115 | (content . ((type . "SYMBOL") 116 | (name . ,name))) 117 | (named . ,_) 118 | (value . ,alias-name)) 119 | `((,name . ,alias-name))) 120 | (`((type . "ALIAS") 121 | (content . ,_) 122 | (named . ,_) 123 | (value . ,_)) 124 | '()) 125 | (`((type . "REPEAT") 126 | (content . ,content)) 127 | (tree-edit--extract-aliases content)) 128 | (`((type . "REPEAT1") 129 | (content . ,content)) 130 | (tree-edit--extract-aliases content)) 131 | (`((type . "FIELD") 132 | (name . ,_) 133 | (content . ,content)) 134 | (tree-edit--extract-aliases content)) 135 | (`((type . "SYMBOL") 136 | (name . ,name)) 137 | '()) 138 | (`((type . "CHOICE") 139 | (members . ,members)) 140 | (-mapcat #'tree-edit--extract-aliases members)) 141 | (_ '()))) 142 | 143 | (defun tree-edit--inline-type (node grammar) 144 | "Inlines anonymous nodes for NODE in GRAMMAR. 145 | 146 | https://tree-sitter.github.io/tree-sitter/using-parsers#named-vs-anonymous-nodes" 147 | (pcase node 148 | ((and `((type . ,type) 149 | (value . ,value) 150 | (content . ,content)) 151 | (guard (string-prefix-p "PREC" type))) 152 | `((type . ,type) 153 | (value . ,value) 154 | (content . ,(tree-edit--inline-type content grammar)))) 155 | (`((type . "SEQ") 156 | (members . ,members)) 157 | `((type . "SEQ") 158 | (members . ,(--map (tree-edit--inline-type it grammar) members)))) 159 | (`((type . "ALIAS") 160 | (content . ,content) 161 | (named . ,named) 162 | (value . ,value)) 163 | `((type . "ALIAS") 164 | (content . ,(tree-edit--inline-type content grammar)) 165 | (named . ,named) 166 | (value . ,value))) 167 | (`((type . "IMMEDIATE_TOKEN") 168 | (content . ,content)) 169 | `((type . "IMMEDIATE_TOKEN") 170 | (content . ,(tree-edit--inline-type content grammar)))) 171 | (`((type . "REPEAT") 172 | (content . ,content)) 173 | `((type . "REPEAT") 174 | (content . ,(tree-edit--inline-type content grammar)))) 175 | (`((type . "REPEAT1") 176 | (content . ,content)) 177 | `((type . "REPEAT1") 178 | (content . ,(tree-edit--inline-type content grammar)))) 179 | (`((type . "FIELD") 180 | (name . ,name) 181 | (content . ,content)) 182 | `((type . "FIELD") 183 | (name . ,name) 184 | (content . ,(tree-edit--inline-type content grammar)))) 185 | ((and `((type . "SYMBOL") 186 | (name . ,name)) 187 | (guard (and (string-prefix-p "_" (if (symbolp name) (symbol-name name) name)) 188 | (alist-get (if (symbolp name) name (intern name)) grammar)))) 189 | (tree-edit--inline-type (alist-get (if (symbolp name) name (intern name)) grammar) grammar)) 190 | (`((type . "CHOICE") 191 | (members . ,members)) 192 | `((type . "CHOICE") 193 | (members . ,(--map (tree-edit--inline-type it grammar) members)))) 194 | (other other))) 195 | 196 | (defun tree-edit--process-grammar (node) 197 | "Convert node types to symbols in NODE." 198 | (pcase node 199 | ((and `((type . ,type) 200 | (value . ,value) 201 | (content . ,content)) 202 | (guard (string-prefix-p "PREC" type))) 203 | `((type . ,type) 204 | (value . ,value) 205 | (content . ,(tree-edit--process-grammar content)))) 206 | (`((type . "SEQ") 207 | (members . ,members)) 208 | `((type . "SEQ") 209 | (members . ,(-map #'tree-edit--process-grammar members)))) 210 | (`((type . "ALIAS") 211 | (content . ,content) 212 | (named . ,named) 213 | (value . ,value)) 214 | `((type . "ALIAS") 215 | (content . ,(tree-edit--process-grammar content)) 216 | (named . ,named) 217 | (value . ,(intern value)))) 218 | (`((type . "REPEAT") 219 | (content . ,content)) 220 | `((type . "REPEAT") 221 | (content . ,(tree-edit--process-grammar content)))) 222 | (`((type . "REPEAT1") 223 | (content . ,content)) 224 | `((type . "REPEAT1") 225 | (content . ,(tree-edit--process-grammar content)))) 226 | (`((type . "FIELD") 227 | (name . ,name) 228 | (content . ,content)) 229 | `((type . "FIELD") 230 | (name . ,name) 231 | (content . ,(tree-edit--process-grammar content)))) 232 | (`((type . "SYMBOL") 233 | (name . ,name)) 234 | `((type . "SYMBOL") 235 | (name . ,(intern name)))) 236 | (`((type . "CHOICE") 237 | (members . ,members)) 238 | `((type . "CHOICE") 239 | (members . ,(-map #'tree-edit--process-grammar members)))) 240 | (other other))) 241 | 242 | (defun tree-edit--extract-word-regex (grammar) 243 | "Extract regex for words in GRAMMAR." 244 | (let ((identifier-node (alist-get (intern (alist-get 'word grammar)) (alist-get 'rules grammar)))) 245 | (pcase identifier-node 246 | (`((type . "PATTERN") 247 | (value . ,regex)) regex) 248 | (_ (error "Expected regex node, found %s" identifier-node))))) 249 | 250 | (defun tree-edit--extract-grammar (grammar) 251 | "Retrieve and format grammar rules for GRAMMAR." 252 | (map-apply (lambda (node node-grammar) (cons node (tree-edit--inline-type node-grammar grammar))) grammar)) 253 | 254 | (defun tree-edit--generate-grammar-file (path) 255 | "Generate file contents based on the grammar at PATH." 256 | (let* ((raw-grammar 257 | (let ((json-array-type 'list)) 258 | (json-read-file path))) 259 | (grammar 260 | (map-apply (lambda (type node) (cons type (tree-edit--process-grammar node))) 261 | (alist-get 'rules raw-grammar))) 262 | (grammar (tree-edit--extract-grammar grammar)) 263 | ;; FIXME, I broke the reazon queries? 264 | (supertypes (tree-edit--generate-supertypes grammar (-map #'intern (alist-get 'supertypes raw-grammar))))) 265 | (prin1-to-string 266 | `((tree-edit-grammar . ,grammar) 267 | (tree-edit--supertypes . ,supertypes) 268 | (tree-edit--subtypes . ,(tree-edit--invert-supertypes supertypes)) 269 | (tree-edit--alias-map . ,(tree-edit--generate-alias-names grammar)) 270 | (tree-edit--containing-types . ,(tree-edit--generate-containing-types grammar)))))) 271 | 272 | ;;;###autoload 273 | (cl-defun tree-edit-install-language-grammar (lang) 274 | "Compile grammar at PATH, and place the resulting shared library in DESTINATION." 275 | (treesit-install-language-grammar lang) 276 | 277 | (unless (treesit-language-available-p lang) 278 | (princ (string-join (directory-files (expand-file-name ".test-grammars/tree-sitter")) "\n")) 279 | (error "%S grammar did not compile properly!" lang)) 280 | 281 | ;; XXX: We're cloning the repo again since `treesit-install-language-grammar' 282 | ;; deletes the repo after using it, sadly. 283 | 284 | (-let* ((default-directory (make-temp-file "tree-edit-workdir" t)) 285 | (workdir (expand-file-name "repo")) 286 | ((url revision source-dir) (alist-get lang treesit-language-source-alist)) 287 | (grammar 288 | (expand-file-name "grammar.json" (expand-file-name (or source-dir "src") workdir)))) 289 | (if revision 290 | (treesit--call-process-signal 291 | "git" nil t nil "clone" url "--depth" "1" "--quiet" 292 | "-b" revision workdir) 293 | (treesit--call-process-signal 294 | "git" nil t nil "clone" url "--depth" "1" "--quiet" 295 | workdir)) 296 | (make-directory tree-edit-storage-dir 'parents) 297 | (with-temp-file (expand-file-name (format "tree-edit-%s-grammar.el" 298 | (thread-last grammar 299 | (json-read-file) 300 | (alist-get 'name))) 301 | tree-edit-storage-dir) 302 | (message "Processing grammar file... this may take a minute or two") 303 | ;; TODO: This is quite slow... we should speed this up or cache it 304 | (-> grammar (tree-edit--generate-grammar-file) (insert))) 305 | 306 | (message "Completed compilation"))) 307 | 308 | ;;;###autoload 309 | (defun tree-edit-install-grammars-wizard (force-rebuild &optional accept-all) 310 | "Install all available tree-sitter grammars compatible with tree-edit. 311 | 312 | FORCE-REBUILD, if non-nil or C-u prefix is used, forces a rebuild of the grammars even if cached. 313 | ACCEPT-ALL, if non-nil, installs all grammars without confirmation." 314 | (interactive "P") 315 | (pcase-dolist 316 | (`(,lang . ,url) 317 | '((python "https://github.com/tree-edit/tree-sitter-python") 318 | (c "https://github.com/tree-edit/tree-sitter-c") 319 | (elisp "https://github.com/tree-edit/tree-sitter-elisp") 320 | (java "https://github.com/tree-edit/tree-sitter-java"))) 321 | (when 322 | ;; TODO: Some way to detect if the grammar is out of date 323 | (and (or (not (treesit-language-available-p lang)) force-rebuild) 324 | (or accept-all 325 | (y-or-n-p (format "Would you like to install the %S grammar? " lang)))) 326 | 327 | (add-to-list 'treesit-language-source-alist `(,lang . ,url)) 328 | (message "Building %s grammar" lang) 329 | (tree-edit-install-language-grammar lang))) 330 | (message "Installations complete.")) 331 | 332 | (provide 'tree-edit-build) 333 | ;;; tree-edit-build.el ends here 334 | -------------------------------------------------------------------------------- /tree-edit-python.el: -------------------------------------------------------------------------------- 1 | ;;; tree-edit-python.el --- Description -*- lexical-binding: t; -*- 2 | ;; 3 | ;; Copyright (C) 2021 Ethan Leba 4 | ;; Author: Ethan Leba 5 | ;; SPDX-License-Identifier: GPL-3.0-or-later 6 | ;; Version: 0.1.0 7 | ;; Package-Requires: ((emacs "27.0")) 8 | ;; Homepage: https://github.com/ethan-leba/tree-edit 9 | ;; 10 | ;; This file is not part of GNU Emacs. 11 | ;; 12 | ;;; Commentary: 13 | ;; 14 | ;; This file contains key bindings and other configuration for `tree-edit' to 15 | ;; work with Python. 16 | ;; 17 | ;; 18 | ;; 19 | ;; This file is not part of GNU Emacs. 20 | ;; 21 | ;;; Commentary: 22 | ;; 23 | ;;; Code: 24 | (require 'mode-local) 25 | (require 'tree-edit) 26 | 27 | (defun tree-edit-python-block-deletion-override (_ parent start-index end-index) 28 | "Allow deletion of NODE in block, unless it's the only node." 29 | (when (> (treesit-node-child-count parent :named) 1) 30 | (make-tree-edit-result :start-index start-index :end-index end-index :tokens nil))) 31 | 32 | (tree-edit--set-parser-local-vars 33 | 'python 34 | 35 | tree-edit-syntax-snippets 36 | (;; Statements 37 | (if_statement . ("if" expression ":" block)) 38 | (with_statement . ("with" expression ":" block)) 39 | (else_clause . ("else" ":" block)) 40 | (elif_clause . ("elif" expression ":" block)) 41 | (for_statement . ("for" identifier "in" expression ":" block)) 42 | (try_statement . ("try" ":" block except_clause)) 43 | (except_clause . ("except" identifier ":" block)) 44 | (finally_clause . ("finally" ":" block)) 45 | (return_statement . ("return")) 46 | (raise_statement . ("raise" expression)) 47 | (break_statement . ("break")) 48 | (continue_statement . ("continue")) 49 | (pass_statement . ("pass")) 50 | (global_statement . ("global" identifier)) 51 | (nonlocal_statement . ("nonlocal" identifier)) 52 | (import_statement . ("import" identifier)) 53 | (import_from_statement . ("from" identifier "import" identifier)) 54 | (delete_statement . ("del" expression)) 55 | (assert_statement . ("assert" expression)) 56 | (function_definition . ("def" identifier parameters ":" block)) 57 | (class_definition . ("class" identifier ":" block)) 58 | (parameters . ("(" ")")) 59 | (typed_parameter . (identifier ":" identifier)) 60 | (decorated_definition . (decorator function_definition)) 61 | (assignment . (identifier "=" expression)) 62 | (block . (expression)) 63 | (decorator . ("@" primary_expression)) 64 | 65 | ;; Expressions & values 66 | (list . ("[" "]")) 67 | (list_splat . ("*" expression)) 68 | (list_splat_pattern . ("*" expression)) 69 | (list_comprehension . ("[" expression for_in_clause "]")) 70 | (dictionary . ("{" "}")) 71 | (dictionary_comprehension . ("{" pair for_in_clause "}")) 72 | (tuple . ("(" expression "," ")")) 73 | (dictionary_splat . ("**" expression)) 74 | (dictionary_splat_pattern . ("**" expression)) 75 | (not_operator . ("not" expression)) 76 | (pair . (identifier ":" identifier)) 77 | (keyword_argument . (identifier "=" identifier)) 78 | (pattern_list . (identifier "," identifier)) 79 | ;; Set can't be empty, otherwise it's a dict 80 | (set . ("{" identifier "}")) 81 | (set_comprehension . ("{" expression for_in_clause "}")) 82 | (conditional_expression . (expression "if" expression "else" expression)) 83 | (for_in_clause . ("for" identifier "in" identifier)) 84 | (if_clause . ("if" identifier)) 85 | (named_expression . (identifier ":=" expression)) 86 | (lambda . ("lambda" lambda_parameters ":" expression)) 87 | (lambda_parameters . (identifier)) 88 | (call . (primary_expression argument_list)) 89 | (argument_list . ("(" ")")) 90 | (true . ("True")) 91 | (false . ("False")) 92 | (none . ("None")) 93 | (attribute . (primary_expression "." identifier)) 94 | (primary_expression . (identifier)) 95 | (subscript . (identifier "[" expression "]")) 96 | (slice . (expression ":" expression)) 97 | (parenthesized_expression . ("(" expression ")")) 98 | (expression . (identifier)) 99 | (identifier . ("TREE"))) 100 | 101 | ;; WARNING: Python is whitespace dependent, so messing with these parameters 102 | ;; may produce unparseable text 103 | tree-edit-whitespace-rules 104 | ((nil (block (:newline :indent) (:dedent :newline)) 105 | (comment nil (:newline)) 106 | (decorator nil (:newline)) 107 | (except_clause nil (:newline)) 108 | (finally_clause nil (:newline)) 109 | (elif_clause nil (:newline)) 110 | (else_clause nil (:newline)) 111 | (_compound_statement nil (:newline)) 112 | ;; XXX: expression is still a simp statemet 113 | (expression nil nil) 114 | (_simple_statement nil (:newline))) 115 | (block (expression nil (:newline))) 116 | (module (expression nil (:newline)))) 117 | 118 | tree-edit-significant-node-types 119 | (decorated_definition function_definition class_definition) 120 | 121 | tree-edit-placeholder-node-type 122 | identifier 123 | 124 | tree-edit-indentation-level 125 | 4 126 | 127 | tree-edit-node-deletion-override 128 | ((block . tree-edit-python-block-deletion-override) 129 | (module . tree-edit-simple-delete-override)) 130 | 131 | tree-edit-node-replacement-override 132 | ((block . tree-edit-simple-insertion-replacement-override) 133 | (module . tree-edit-simple-insertion-replacement-override)) 134 | 135 | tree-edit-node-insertion-override 136 | ((block . tree-edit-simple-insertion-replacement-override) 137 | (module . tree-edit-simple-insertion-replacement-override)) 138 | 139 | ;; TODO: this should be auto-generated in the grammar file 140 | tree-edit--hidden-node-types 141 | (_newline _indent _dedent) 142 | 143 | tree-edit-nodes 144 | (;; Statements 145 | (:type if_statement 146 | :key "i") 147 | (:type with_statement 148 | :key "W") 149 | (:type return_statement 150 | :key "r" 151 | :wrap-override '((return_statement . ("return" expression)))) 152 | (:type delete_statement 153 | :key "x") 154 | (:type raise_statement 155 | :key "R") 156 | (:type break_statement 157 | :key "b") 158 | (:type try_statement 159 | :key "T") 160 | (:type for_statement 161 | :key "f") 162 | (:type function_definition 163 | :key "F") 164 | (:type typed_parameter 165 | :key ";") 166 | (:type class_definition 167 | :key "C") 168 | (:type assert_statement 169 | :key "A") 170 | (:type assignment 171 | :key "v" 172 | :name "variable declaration") 173 | ;; *shrug* 174 | (:type import_statement 175 | :key "q") 176 | (:type import_from_statement 177 | :key "Q") 178 | 179 | ;; Expressions 180 | (:type attribute 181 | :key ".") 182 | (:type parenthesized_expression 183 | :key "(") 184 | (:type named_expression 185 | :key "w" 186 | :name "walrus operator") 187 | (:type list 188 | :key "l") 189 | (:type list_comprehension 190 | :key "L") 191 | (:type set 192 | :key "s") 193 | (:type set_comprehension 194 | :key "S") 195 | (:type dictionary 196 | :key "d") 197 | (:type dictionary_comprehension 198 | :key "D") 199 | (:type tuple 200 | :key "t") 201 | (:type identifier 202 | :key "a") 203 | (:type not_operator 204 | :key "n") 205 | (:type call 206 | :key "c" 207 | :wrap-override '((argument_list . ("(" expression ")")))) 208 | (:type conditional_expression 209 | :key "I" 210 | :name "ternary conditional") 211 | (:type pair 212 | :key ":") 213 | (:type keyword_argument 214 | :key "K") 215 | (:type unary_operator 216 | :key "-" 217 | :name "negation" 218 | :node-override '((unary_operator . ("-" expression)))) 219 | (:type (pattern_list 220 | expression_list) 221 | :key ",") 222 | (:type decorator 223 | :key "@") 224 | ;; Uncommon nodes and nodes that are only valid in specific contexts 225 | (:type (list_splat 226 | list_splat_pattern) 227 | :key "m*") 228 | (:type (dictionary_splat 229 | dictionary_splat_pattern) 230 | :key "m8") 231 | (:type continue_statement 232 | :key "mc") 233 | (:type elif_clause 234 | :key "ml") 235 | (:type else_clause 236 | :key "me") 237 | (:type except_clause 238 | :key "mx") 239 | (:type finally_clause 240 | :key "mF") 241 | (:type for_in_clause 242 | :key "mf") 243 | (:type if_clause 244 | :key "mi") 245 | (:type (argument_list 246 | parameters) 247 | :key "ma") 248 | (:type decorated_definition 249 | :key "md") 250 | (:type subscript 251 | :key "ms") 252 | (:type subscript 253 | :key "mS" 254 | :name "subscript slice" 255 | :node-override '((subscript . (identifier "[" slice "]")))) 256 | (:type lambda 257 | :key "mL") 258 | (:type true 259 | :key "mt") 260 | (:type false 261 | :key "mn") 262 | (:type none 263 | :key "mN") 264 | (:type call 265 | :key "mm" 266 | :name "method call" 267 | :node-override '((call . (attribute argument_list)))) 268 | 269 | ;; Augmented assignment 270 | (:type augmented_assignment 271 | :key "=-" 272 | :name "-" 273 | :node-override '((augmented_assignment . (expression "-=" expression)))) 274 | (:type augmented_assignment 275 | :key "=+" 276 | :name "+" 277 | :node-override '((augmented_assignment . (expression "+=" expression)))) 278 | (:type augmented_assignment 279 | :key "=*" 280 | :name "*" 281 | :node-override '((augmented_assignment . (expression "*=" expression)))) 282 | (:type augmented_assignment 283 | :key "=@" 284 | :name "@" 285 | :node-override '((augmented_assignment . (expression "@=" expression)))) 286 | (:type augmented_assignment 287 | :key "=/" 288 | :name "/" 289 | :node-override '((augmented_assignment . (expression "/=" expression)))) 290 | (:type augmented_assignment 291 | :key "=\\" 292 | :name "//" 293 | :node-override '((augmented_assignment . (expression "//=" expression)))) 294 | (:type augmented_assignment 295 | :key "=%" 296 | :name "%" 297 | :node-override '((augmented_assignment . (expression "%=" expression)))) 298 | (:type augmented_assignment 299 | :key "=e" 300 | :name "**" 301 | :node-override '((augmented_assignment . (expression "**=" expression)))) 302 | (:type augmented_assignment 303 | :key "=|" 304 | :name "|" 305 | :node-override '((augmented_assignment . (expression "|=" expression)))) 306 | (:type augmented_assignment 307 | :key "=&" 308 | :name "&" 309 | :node-override '((augmented_assignment . (expression "&=" expression)))) 310 | ;; Operators 311 | (:type binary_operator 312 | :key "o-" 313 | :name "-" 314 | :node-override '((binary_operator . (expression "-" expression)))) 315 | (:type binary_operator 316 | :key "o+" 317 | :name "+" 318 | :node-override '((binary_operator . (expression "+" expression)))) 319 | (:type binary_operator 320 | :key "o*" 321 | :name "*" 322 | :node-override '((binary_operator . (expression "*" expression)))) 323 | (:type binary_operator 324 | :key "o@" 325 | :name "@" 326 | :node-override '((binary_operator . (expression "@" expression)))) 327 | (:type binary_operator 328 | :key "o/" 329 | :name "/" 330 | :node-override '((binary_operator . (expression "/" expression)))) 331 | (:type binary_operator 332 | :key "o\\" 333 | :name "//" 334 | :node-override '((binary_operator . (expression "//" expression)))) 335 | (:type binary_operator 336 | :key "o%" 337 | :name "%" 338 | :node-override '((binary_operator . (expression "%" expression)))) 339 | (:type binary_operator 340 | :key "oe" 341 | :name "**" 342 | :node-override '((binary_operator . (expression "**" expression)))) 343 | (:type binary_operator 344 | :key "o|" 345 | :name "|" 346 | :node-override '((binary_operator . (expression "|" expression)))) 347 | (:type binary_operator 348 | :key "o&" 349 | :name "&" 350 | :node-override '((binary_operator . (expression "&" expression)))) 351 | (:type binary_operator 352 | :key "o^" 353 | :name "^" 354 | :node-override '((binary_operator . (expression "^" expression)))) 355 | (:type binary_operator 356 | :key "ol" 357 | :name "<<" 358 | :node-override '((binary_operator . (expression "<<" expression)))) 359 | (:type binary_operator 360 | :key "or" 361 | :name ">>" 362 | :node-override '((binary_operator . (expression ">>" expression)))) 363 | (:type comparison_operator 364 | :key "o=" 365 | :name "==" 366 | :node-override '((comparison_operator . (expression "==" expression)))) 367 | (:type comparison_operator 368 | :key "o!" 369 | :name "!=" 370 | :node-override '((comparison_operator . (expression "==" expression)))) 371 | (:type comparison_operator 372 | :key "on" 373 | :name "in" 374 | :node-override '((comparison_operator . (expression "in" expression)))) 375 | (:type comparison_operator 376 | :key "oN" 377 | :name "not in" 378 | :node-override '((comparison_operator . (expression "not" "in" expression)))) 379 | (:type comparison_operator 380 | :key "oi" 381 | :name "is" 382 | :node-override '((comparison_operator . (expression "is" expression)))) 383 | (:type comparison_operator 384 | :key "oI" 385 | :name "is not" 386 | :node-override '((comparison_operator . (expression "is" "not" expression)))) 387 | (:type comparison_operator 388 | :key "o>" 389 | :name ">" 390 | :node-override '((comparison_operator . (expression ">" expression)))) 391 | (:type comparison_operator 392 | :key "o<" 393 | :name "<" 394 | :node-override '((comparison_operator . (expression "<" expression)))) 395 | (:type comparison_operator 396 | :key "o," 397 | :name "<=" 398 | :node-override '((comparison_operator . (expression "<=" expression)))) 399 | (:type comparison_operator 400 | :key "o." 401 | :name ">=" 402 | :node-override '((comparison_operator . (expression ">=" expression)))) 403 | (:type boolean_operator 404 | :key "oa" 405 | :name "and" 406 | :node-override '((boolean_operator . (expression "and" expression)))) 407 | (:type boolean_operator 408 | :key "oo" 409 | :name "or" 410 | :node-override '((boolean_operator . (expression "or" expression))))) 411 | 412 | tree-edit-query-nodes 413 | ((:type (identifier integer float string true false none) 414 | :name "atoms" 415 | :key "a") 416 | (:type (set list tuple dictionary) 417 | :name "containers" 418 | :key "j") 419 | (:type (decorated_definition 420 | class_definition 421 | function_definition 422 | with_statement 423 | try_statement 424 | while_statement 425 | for_statement 426 | if_statement 427 | future_import_statement 428 | import_statement 429 | import_from_statement 430 | print_statement 431 | assert_statement 432 | return_statement 433 | delete_statement 434 | raise_statement 435 | pass_statement 436 | break_statement 437 | continue_statement 438 | global_statement 439 | nonlocal_statement 440 | exec_statement) 441 | :name "statement" 442 | :key "k") 443 | (:type block 444 | :key "B"))) 445 | 446 | 447 | (provide 'tree-edit-python) 448 | ;;; tree-edit-python.el ends here 449 | -------------------------------------------------------------------------------- /tree-edit-c.el: -------------------------------------------------------------------------------- 1 | ;;; tree-edit-c.el --- Description -*- lexical-binding: t; -*- 2 | ;; 3 | ;; Copyright (C) 2021 Ethan Leba 4 | ;; Author: Ethan Leba 5 | ;; SPDX-License-Identifier: GPL-3.0-or-later 6 | ;; Version: 0.1.0 7 | ;; Package-Requires: ((emacs "27.0")) 8 | ;; Homepage: https://github.com/ethan-leba/tree-edit 9 | ;; 10 | ;; This file is not part of GNU Emacs. 11 | ;; 12 | ;;; Commentary: 13 | ;; 14 | ;; This file contains key bindings and other configuration for `tree-edit' to 15 | ;; work with c. 16 | ;; 17 | ;; 18 | ;; This file is not part of GNU Emacs. 19 | ;; 20 | ;;; Commentary: 21 | ;; 22 | ;;; Code: 23 | (require 'tree-edit) 24 | 25 | (tree-edit--set-parser-local-vars 26 | 'c 27 | 28 | tree-edit-whitespace-rules 29 | ((nil ("{" nil (:indent :newline)) 30 | ("}" (:dedent :newline) nil) 31 | (comment nil (:newline)) 32 | ;; TODO: should dig inwards for ; rules 33 | (type_definition nil (:newline)) 34 | (field_declaration nil (:newline)) 35 | (_statement nil (:newline)) 36 | (preproc_def nil (:newline)) 37 | (declaration nil (:newline)) 38 | (_non_case_statement nil (:newline)))) 39 | 40 | tree-edit-significant-node-types 41 | () 42 | 43 | tree-edit-placeholder-node-type 44 | identifier 45 | 46 | tree-edit-indentation-level 47 | 2 48 | 49 | tree-edit-node-deletion-override 50 | nil 51 | 52 | tree-edit-node-replacement-override 53 | nil 54 | 55 | tree-edit-node-insertion-override 56 | nil 57 | 58 | tree-edit-nodes 59 | (;; Statements 60 | (:type if_statement 61 | :key "i") 62 | (:type union_specifier 63 | :key "u") 64 | 65 | (:type while_statement 66 | :key "w") 67 | (:type return_statement 68 | :key "r" 69 | :wrap-override '((return_statement . ("return" identifier)))) 70 | (:type delete_statement 71 | :key "x") 72 | (:type raise_statement 73 | :key "R") 74 | (:type break_statement 75 | :key "b") 76 | (:type compound_statement 77 | :key "B") 78 | (:type expression_statement 79 | :key "e") 80 | (:type enum_specifier 81 | :key "E") 82 | (:type for_statement 83 | :key "f") 84 | (:type function_definition 85 | :key "F") 86 | (:type cast_expression 87 | :key "C") 88 | (:type assert_statement 89 | :key "A") 90 | (:type assignment_expression 91 | :key "v") 92 | (:type declaration 93 | :key "d" 94 | :name "variable init" 95 | :node-override '((declaration primitive_type init_declarator ";"))) 96 | (:type declaration 97 | :key "D" 98 | :name "variable declaration") 99 | ;; *shrug* 100 | (:type preproc_include 101 | :key "q") 102 | 103 | ;; Types prefix 104 | (:type struct_specifier 105 | :key "ts" 106 | :node-override '((struct_specifier "struct" identifier))) 107 | 108 | 109 | ;; Expressions 110 | (:type field_expression 111 | :key ".") 112 | (:type field_expression 113 | :key ">" 114 | :node-override '((field_expression identifier "->" identifier))) 115 | (:type parenthesized_expression 116 | :key "(") 117 | (:type pointer_expression 118 | :key "*") 119 | (:type pointer_expression 120 | :key "&" 121 | :node-override 122 | '((pointer_expression "&" identifier))) 123 | (:type pointer_declarator 124 | :key "8") 125 | (:type parameter_declaration 126 | :key "A" 127 | :name "function argument") 128 | (:type list 129 | :key "l") 130 | (:type list_comprehension 131 | :key "L") 132 | (:type struct_specifier 133 | :key "s") 134 | (:type switch_statement 135 | :key "S") 136 | (:type identifier 137 | :key "a") 138 | (:type not_operator 139 | :key "n") 140 | (:type call_expression 141 | :key "c" 142 | :wrap-override '((argument_list . ("(" identifier ")")))) 143 | (:type conditional_expression 144 | :key "?" 145 | :name "ternary conditional") 146 | (:type pair 147 | :key ":") 148 | (:type keyword_argument 149 | :key "K") 150 | (:type unary_expression 151 | :key "-" 152 | :name "-TREE" 153 | :node-override '((unary_expression . ("-" identifier)))) 154 | (:type unary_expression 155 | :key "!" 156 | :name "!TREE" 157 | :node-override '((unary_expression . ("!" identifier)))) 158 | (:type pattern_list 159 | :key ",") 160 | ;; Uncommon nodes and nodes that are only valid in specific contexts 161 | (:type preproc_def 162 | :key "mv") 163 | (:type case_statement 164 | :key "mc") 165 | (:type case_statement 166 | :key "md" 167 | :node-override '((case_statement "default" ":" expression_statement)) 168 | :name "default case") 169 | (:type field_declaration 170 | :key "mf") 171 | (:type update_expression 172 | :key "m+" 173 | :node-override '((update_expression identifier "++")) 174 | :name "TREE++") 175 | (:type update_expression 176 | :key "m=" 177 | :node-override '((update_expression "++" identifier)) 178 | :name "++TREE") 179 | (:type update_expression 180 | :key "m-" 181 | :node-override '((update_expression identifier "--")) 182 | :name "TREE--") 183 | (:type update_expression 184 | :key "m_" 185 | :node-override '((update_expression "--" identifier)) 186 | :name "--TREE") 187 | (:type elif_clause 188 | :key "ml") 189 | (:type else_clause 190 | :key "me") 191 | (:type except_clause 192 | :key "mx") 193 | 194 | 195 | (:type if_clause 196 | :key "mi") 197 | (:type argument_list 198 | :key "ma") 199 | (:type decorated_definition 200 | :key "md") 201 | (:type subscript_expression 202 | :key "ms") 203 | (:type lambda 204 | :key "mL") 205 | (:type true 206 | :key "mt") 207 | (:type false 208 | :key "mn") 209 | (:type null 210 | :key "mN") 211 | (:type call 212 | :key "mm" 213 | :name "method call" 214 | :node-override '((call . (attribute argument_list)))) 215 | 216 | ;; Augmented assignment 217 | (:type augmented_assignment 218 | :key "=-" 219 | :name "-" 220 | :node-override '((augmented_assignment . (identifier "-=" identifier)))) 221 | (:type augmented_assignment 222 | :key "=+" 223 | :name "+" 224 | :node-override '((augmented_assignment . (identifier "+=" identifier)))) 225 | (:type augmented_assignment 226 | :key "=*" 227 | :name "*" 228 | :node-override '((augmented_assignment . (identifier "*=" identifier)))) 229 | (:type augmented_assignment 230 | :key "=@" 231 | :name "@" 232 | :node-override '((augmented_assignment . (identifier "@=" identifier)))) 233 | (:type augmented_assignment 234 | :key "=/" 235 | :name "/" 236 | :node-override '((augmented_assignment . (identifier "/=" identifier)))) 237 | (:type augmented_assignment 238 | :key "=\\" 239 | :name "//" 240 | :node-override '((augmented_assignment . (identifier "//=" identifier)))) 241 | (:type augmented_assignment 242 | :key "=%" 243 | :name "%" 244 | :node-override '((augmented_assignment . (identifier "%=" identifier)))) 245 | (:type augmented_assignment 246 | :key "=e" 247 | :name "**" 248 | :node-override '((augmented_assignment . (identifier "**=" identifier)))) 249 | (:type augmented_assignment 250 | :key "=|" 251 | :name "|" 252 | :node-override '((augmented_assignment . (identifier "|=" identifier)))) 253 | (:type augmented_assignment 254 | :key "=&" 255 | :name "&" 256 | :node-override '((augmented_assignment . (identifier "&=" identifier)))) 257 | ;; Operators 258 | (:type binary_expression 259 | :key "o-" 260 | :name "-" 261 | :node-override '((binary_expression . (identifier "-" identifier)))) 262 | (:type binary_expression 263 | :key "o+" 264 | :name "+" 265 | :node-override '((binary_expression . (identifier "+" identifier)))) 266 | (:type binary_expression 267 | :key "o*" 268 | :name "*" 269 | :node-override '((binary_expression . (identifier "*" identifier)))) 270 | (:type binary_expression 271 | :key "o@" 272 | :name "@" 273 | :node-override '((binary_expression . (identifier "@" identifier)))) 274 | (:type binary_expression 275 | :key "o/" 276 | :name "/" 277 | :node-override '((binary_expression . (identifier "/" identifier)))) 278 | (:type binary_expression 279 | :key "o\\" 280 | :name "//" 281 | :node-override '((binary_expression . (identifier "//" identifier)))) 282 | (:type binary_expression 283 | :key "o%" 284 | :name "%" 285 | :node-override '((binary_expression . (identifier "%" identifier)))) 286 | (:type binary_expression 287 | :key "oe" 288 | :name "**" 289 | :node-override '((binary_expression . (identifier "**" identifier)))) 290 | (:type binary_expression 291 | :key "o|" 292 | :name "|" 293 | :node-override '((binary_expression . (identifier "|" identifier)))) 294 | (:type binary_expression 295 | :key "o&" 296 | :name "&" 297 | :node-override '((binary_expression . (identifier "&" identifier)))) 298 | (:type binary_expression 299 | :key "o^" 300 | :name "^" 301 | :node-override '((binary_expression . (identifier "^" identifier)))) 302 | (:type binary_expression 303 | :key "ol" 304 | :name "<<" 305 | :node-override '((binary_expression . (identifier "<<" identifier)))) 306 | (:type binary_expression 307 | :key "or" 308 | :name ">>" 309 | :node-override '((binary_expression . (identifier ">>" identifier)))) 310 | (:type binary_expression 311 | :key "o=" 312 | :name "==" 313 | :node-override '((binary_expression . (identifier "==" identifier)))) 314 | (:type binary_expression 315 | :key "o!" 316 | :name "!=" 317 | :node-override '((binary_expression . (identifier "!=" identifier)))) 318 | (:type binary_expression 319 | :key "o>" 320 | :name ">" 321 | :node-override '((binary_expression . (identifier ">" identifier)))) 322 | (:type binary_expression 323 | :key "o<" 324 | :name "<" 325 | :node-override '((binary_expression . (identifier "<" identifier)))) 326 | (:type binary_expression 327 | :key "o," 328 | :name "<=" 329 | :node-override '((binary_expression . (identifier "<=" identifier)))) 330 | (:type binary_expression 331 | :key "o." 332 | :name ">=" 333 | :node-override '((binary_expression . (identifier ">=" identifier)))) 334 | (:type binary_expression 335 | :key "oo" 336 | :name "or" 337 | :node-override '((binary_expression . (identifier "||" identifier)))) 338 | (:type binary_expression 339 | :key "oa" 340 | :name "and" 341 | :node-override '((binary_expression . (identifier "&&" identifier))))) 342 | 343 | tree-edit-query-nodes 344 | ((:type (struct_specifier 345 | union_specifier 346 | enum_specifier 347 | macro_type_specifier 348 | sized_type_specifier 349 | primitive_type 350 | type_identifier) 351 | :name "types" 352 | :key "t") 353 | (:type (identifier 354 | field_identifier 355 | type_identifier) 356 | :name "identifiers" 357 | :key "a") 358 | (:type (number_literal 359 | string_literal 360 | true 361 | false 362 | null 363 | concatenated_string 364 | char_literal) 365 | :name "values" 366 | :key "V")) 367 | 368 | ;; TODO: Check me! 369 | tree-edit-syntax-snippets 370 | ((translation_unit) 371 | (_top_level_item function_definition) 372 | (preproc_include "#include" string_literal) 373 | (preproc_def "#define" identifier identifier) 374 | (preproc_function_def \#define identifier preproc_params " 375 | ") 376 | (preproc_params "(" ")") 377 | (preproc_call preproc_directive " 378 | ") 379 | (preproc_if \#if identifier " 380 | " \#endif) 381 | (preproc_ifdef \#ifdef identifier \#endif) 382 | (preproc_else \#else) 383 | (preproc_elif \#elif identifier " 384 | ") 385 | (preproc_if_in_field_declaration_list \#if identifier " 386 | " \#endif) 387 | (preproc_ifdef_in_field_declaration_list \#ifdef identifier \#endif) 388 | (preproc_else_in_field_declaration_list \#else) 389 | (preproc_elif_in_field_declaration_list \#elif identifier " 390 | ") 391 | (preproc_directive :regex) 392 | (preproc_arg :regex) 393 | (_preproc_expression identifier) 394 | (preproc_parenthesized_expression "(" identifier ")") 395 | (preproc_defined "defined" identifier) 396 | (preproc_unary_expression "!" identifier) 397 | (preproc_call_expression identifier argument_list) 398 | (preproc_argument_list "(" ")") 399 | (preproc_binary_expression identifier "+" identifier) 400 | (function_definition primitive_type identifier parameter_list compound_statement) 401 | (declaration primitive_type identifier ";") 402 | (type_definition "typedef" struct_specifier pointer_declarator ";") 403 | (_declaration_specifiers struct_specifier) 404 | (linkage_specification "extern" string_literal function_definition) 405 | (attribute_specifier "__attribute__" "(" argument_list ")") 406 | (ms_declspec_modifier "__declspec" "(" identifier ")") 407 | (ms_based_modifier "__based" argument_list) 408 | (ms_call_modifier "__cdecl") 409 | (ms_restrict_modifier "__restrict") 410 | (ms_unsigned_ptr_modifier "__uptr") 411 | (ms_signed_ptr_modifier "__sptr") 412 | (ms_unaligned_ptr_modifier "_unaligned") 413 | (ms_pointer_modifier ms_unaligned_ptr_modifier) 414 | (declaration_list "{" "}") 415 | (_declarator pointer_declarator) 416 | (_field_declarator pointer_declarator) 417 | (_type_declarator pointer_declarator) 418 | (_abstract_declarator abstract_pointer_declarator) 419 | (parenthesized_declarator "(" pointer_declarator ")") 420 | (parenthesized_field_declarator "(" pointer_declarator ")") 421 | (parenthesized_type_declarator "(" pointer_declarator ")") 422 | (abstract_parenthesized_declarator "(" abstract_pointer_declarator ")") 423 | (pointer_declarator "*" identifier) 424 | (pointer_field_declarator "*" pointer_declarator) 425 | (pointer_type_declarator "*" pointer_declarator) 426 | (abstract_pointer_declarator "*") 427 | (function_declarator pointer_declarator parameter_list) 428 | (function_field_declarator pointer_declarator parameter_list) 429 | (function_type_declarator pointer_declarator parameter_list) 430 | (abstract_function_declarator parameter_list) 431 | (array_declarator pointer_declarator "[" "]") 432 | (array_field_declarator pointer_declarator "[" "]") 433 | (array_type_declarator pointer_declarator "[" "]") 434 | (abstract_array_declarator "[" "]") 435 | (init_declarator identifier "=" identifier) 436 | (compound_statement "{" "}") 437 | (storage_class_specifier "extern") 438 | (type_qualifier "const") 439 | (_type_specifier struct_specifier) 440 | (sized_type_specifier "signed") 441 | (primitive_type "void") 442 | (enum_specifier "enum" identifier enumerator_list) 443 | (enumerator_list "{" "}") 444 | (struct_specifier "struct" identifier field_declaration_list) 445 | (union_specifier "union" field_declaration_list) 446 | (field_declaration_list "{" "}") 447 | (_field_declaration_list_item field_declaration) 448 | (field_declaration primitive_type identifier ";") 449 | (bitfield_clause ":" identifier) 450 | (enumerator identifier) 451 | (parameter_list "(" ")") 452 | (parameter_declaration primitive_type identifier) 453 | (_statement compound_statement) 454 | (_non_case_statement labeled_statement) 455 | (labeled_statement statement_identifier ":" compound_statement) 456 | (expression_statement identifier ";") 457 | (if_statement "if" parenthesized_expression compound_statement) 458 | (switch_statement "switch" parenthesized_expression compound_statement) 459 | (case_statement "case" identifier ":" expression_statement) 460 | (while_statement "while" parenthesized_expression compound_statement) 461 | (do_statement "do" compound_statement "while" parenthesized_expression ";") 462 | (for_statement "for" "(" declaration ";" ")" compound_statement) 463 | (return_statement "return" ";") 464 | (break_statement "break" ";") 465 | (continue_statement "continue" ";") 466 | (goto_statement "goto" statement_identifier ";") 467 | (_expression identifier) 468 | (comma_expression identifier "," identifier) 469 | (conditional_expression identifier 470 | "?" 471 | identifier 472 | ":" 473 | identifier) 474 | (_assignment_left_expression identifier) 475 | (assignment_expression identifier "=" identifier) 476 | (pointer_expression "*" identifier) 477 | (unary_expression "!" identifier) 478 | (binary_expression identifier "+" identifier) 479 | (update_expression "--" identifier) 480 | (cast_expression "(" type_descriptor ")" identifier) 481 | (type_descriptor identifier) 482 | (sizeof_expression "sizeof" identifier) 483 | (subscript_expression identifier "[" identifier "]") 484 | (call_expression identifier argument_list) 485 | (argument_list "(" ")") 486 | (field_expression identifier "." identifier) 487 | (compound_literal_expression "(" type_descriptor ")" initializer_list) 488 | (parenthesized_expression "(" identifier ")") 489 | (initializer_list "{" "}") 490 | (initializer_pair subscript_designator "=" initializer_list) 491 | (subscript_designator "[" identifier "]") 492 | (field_designator "." field_identifier) 493 | (number_literal "." :regex) 494 | (char_literal "L'" escape_sequence "'") 495 | (concatenated_string string_literal string_literal) 496 | (string_literal "\"TREE\"") 497 | (escape_sequence "\\" :regex) 498 | (system_lib_string "<" ">") 499 | (true "TRUE") 500 | (false "FALSE") 501 | (null "NULL") 502 | (identifier "TREE") 503 | (_type_identifier type_identifier) 504 | (_field_identifier field_identifier) 505 | (_statement_identifier statement_identifier) 506 | (_empty_declaration struct_specifier ";") 507 | (macro_type_specifier identifier "(" type_descriptor ")") 508 | (comment "//" :regex))) 509 | 510 | (provide 'tree-edit-c) 511 | ;;; tree-edit-c.el ends here 512 | -------------------------------------------------------------------------------- /tests/test-java.el: -------------------------------------------------------------------------------- 1 | (require 'evil-tree-edit) 2 | (ignore-errors (load-file "setup.el")) 3 | 4 | (describe "test set up" 5 | (it "inserts buffer contents and returns them" 6 | (expect (with-test-buffer #'java-mode "hello world|") 7 | :to-have-buffer-contents "hello world|"))) 8 | 9 | (describe "mode local settings" 10 | (it "sets keybindings" 11 | (expect (with-tree-test-buffer #'java-mode 12 | "if ([foo] == 3) {}" 13 | (execute-kbd-macro "j")) 14 | :to-have-buffer-contents "if (foo == [3]) {}"))) 15 | 16 | (describe "defining tree-edit verbs" 17 | :var (dummy-verb) 18 | 19 | (before-each 20 | (setf (symbol-function 'dummy-verb) 21 | (lambda (noun))) 22 | (spy-on 'dummy-verb)) 23 | 24 | ;; TODO: Test which-key 25 | (it "sets keybindings" 26 | (with-base-test-buffer #'java-mode "" 27 | (let ((evil-tree-state-map (make-sparse-keymap)) 28 | (tree-edit-nodes 29 | '((java (:type if_statement 30 | :key "i"))))) 31 | (define-evil-tree-edit-verb evil-tree-state-map "t" #'dummy-verb) 32 | (evil-tree-state) 33 | (expect (key-binding "ti")) 34 | (expect (not (key-binding "tq"))) 35 | (execute-kbd-macro "ti") 36 | (expect 'dummy-verb :to-have-been-called-with 'if_statement) 37 | (prog-mode) 38 | (expect (not (key-binding "ti")))))) 39 | 40 | ;; TODO 41 | (xit "uses node overrides")) 42 | 43 | (describe "load grammar based on major mode" 44 | (it "loads grammar" 45 | (with-base-test-buffer #'java-mode "" (expect tree-edit-grammar))) 46 | (it "errors and disables tree-edit-mode on unknown mode" 47 | (expect (with-base-test-buffer #'fundamental-mode "") :to-throw 'error)) 48 | 49 | ;; TODO 50 | (xit "uses node overrides")) 51 | 52 | (describe "basic navigation" 53 | (it "can move between sibling nodes" 54 | (expect (with-tree-test-buffer #'java-mode "if ([foo] == 3) {}" 55 | (evil-tree-edit-goto-next-sibling)) 56 | :to-have-buffer-contents "if (foo == [3]) {}") 57 | (expect (with-tree-test-buffer #'java-mode "if (foo == [3]) {}" 58 | (evil-tree-edit-goto-prev-sibling)) 59 | :to-have-buffer-contents "if ([foo] == 3) {}")) 60 | (it "can move between parent and children nodes" 61 | (expect (with-tree-test-buffer #'java-mode "if (foo == [3]) {}" 62 | (evil-tree-edit-goto-parent)) 63 | :to-have-buffer-contents "if ([foo == 3]) {}") 64 | (expect (with-tree-test-buffer #'java-mode "if (foo == [3]) {}" 65 | (evil-tree-edit-goto-parent) 66 | (evil-tree-edit-goto-parent)) 67 | :to-have-buffer-contents "if [(foo == 3)] {}") 68 | (expect (with-tree-test-buffer #'java-mode "if (foo == [3]) {}" 69 | (evil-tree-edit-goto-parent) 70 | (evil-tree-edit-goto-parent) 71 | (evil-tree-edit-goto-parent)) 72 | :to-have-buffer-contents "[if (foo == 3) {}]") 73 | (expect (with-tree-test-buffer #'java-mode "[if (foo == 3) {}]" 74 | (evil-tree-edit-goto-child)) 75 | :to-have-buffer-contents "if [(foo == 3)] {}") 76 | (expect (with-tree-test-buffer #'java-mode "if ([foo == 3]) {}" 77 | (evil-tree-edit-goto-child)) 78 | :to-have-buffer-contents "if ([foo] == 3) {}")) 79 | (it "can move to sig node" 80 | (expect (with-tree-test-buffer #'java-mode "{if (foo == [3]) {}}" 81 | (let ((tree-edit-significant-node-types '(block))) 82 | (evil-tree-edit-goto-sig-parent))) 83 | :to-have-buffer-contents "[{if (foo == 3) {}}]") 84 | (expect (with-tree-test-buffer #'java-mode "{[{if (foo == 3) {}}]}" 85 | (let ((tree-edit-significant-node-types '(block))) 86 | (evil-tree-edit-goto-sig-parent))) 87 | :to-have-buffer-contents "[{{if (foo == 3) {}}}]")) 88 | (it "will go to top most (excluding root) if no sig node found" 89 | (expect (with-tree-test-buffer #'java-mode "{{if (foo == [3]) {}}}" 90 | (let ((tree-edit-significant-node-types '())) 91 | (evil-tree-edit-goto-sig-parent))) 92 | :to-have-buffer-contents "[{{if (foo == 3) {}}}]"))) 93 | 94 | (describe "entering tree state" 95 | (it "will select the smallest node at point" 96 | (expect (with-test-buffer #'java-mode "if (|foo == 3) {}" 97 | (evil-tree-state)) 98 | :to-have-buffer-contents "if ([foo] == 3) {}") 99 | (expect (with-test-buffer #'java-mode "if| (foo == 3) {}" 100 | (evil-tree-state)) 101 | :to-have-buffer-contents "[if (foo == 3) {}]") 102 | (expect (with-test-buffer #'java-mode "i|f (foo == 3) {}" 103 | (evil-tree-state)) 104 | :to-have-buffer-contents "[if (foo == 3) {}]") 105 | (expect (with-test-buffer #'java-mode "if (foo =|= 3) {}" 106 | (evil-tree-state)) 107 | :to-have-buffer-contents "if ([foo == 3]) {}")) 108 | (expect (with-test-buffer #'java-mode " 109 | if (foo == 3) { 110 | int x |= 3; 111 | }" 112 | (evil-tree-state)) 113 | :to-have-buffer-contents " 114 | if (foo == 3) { 115 | int [x = 3]; 116 | }")) 117 | 118 | (defun tree-edit-test-make-node (&rest args) 119 | (with-temp-buffer 120 | (with-mode-local java-mode 121 | (tree-edit--render-node nil (apply #'tree-edit--generate-node args) nil 0) 122 | (buffer-substring-no-properties (point-min) (point-max))))) 123 | 124 | ;;* Raise node 125 | (describe "raise node" 126 | (it "replaces the parent node with selected child" 127 | (expect (with-tree-test-buffer #'java-mode "{[foo] == 3;}" 128 | (evil-tree-edit-raise)) 129 | :to-have-buffer-contents "{[foo];}") 130 | (expect (with-tree-test-buffer #'java-mode "{foo == [3];}" 131 | (evil-tree-edit-raise)) 132 | :to-have-buffer-contents "{[3];}") 133 | ;; XXX: how to multi-select? 134 | (expect (with-tree-test-buffer #'java-mode "{if (foo) {[foo;]bar;baz;}}" 135 | (evil-tree-edit-raise)) 136 | :to-have-buffer-contents "{if (foo)[foo;]}") 137 | (expect (with-tree-test-buffer #'java-mode "{if (foo) {[foo;]bar;baz;}}" 138 | (evil-tree-edit-raise) 139 | (evil-tree-edit-raise)) 140 | :to-have-buffer-contents "{[foo;]}")) 141 | ;; XXX: there should be a limit to this... 142 | (it "travels up the syntax tree until a valid construction is found" 143 | (expect (with-tree-test-buffer #'java-mode "{foo([bar()]);}" 144 | (evil-tree-edit-raise)) 145 | :to-have-buffer-contents "{[bar()];}") 146 | (expect (with-tree-test-buffer #'java-mode "{foo(bar([baz]));}" 147 | (evil-tree-edit-raise)) 148 | :to-have-buffer-contents "{foo([baz]);}") 149 | (expect (with-tree-test-buffer #'java-mode "{foo(bar([baz], bomb));}" 150 | (evil-tree-edit-raise)) 151 | :to-have-buffer-contents "{foo([baz]);}")) 152 | (it "errors if a node cannot be raised" 153 | (expect (with-tree-test-buffer #'java-mode "{[foo];}" 154 | (evil-tree-edit-raise)) 155 | :to-throw 'tree-edit-transformation-error) 156 | (expect (with-tree-test-buffer #'java-mode "[{foo;}]" 157 | (evil-tree-edit-raise)) 158 | :to-throw 'tree-edit-transformation-error))) 159 | 160 | (describe "change node" 161 | (it "enters insert mode after deleting the current node" 162 | (expect (with-tree-test-buffer #'java-mode "{foo([x]);}" 163 | (evil-tree-edit-change)) 164 | :to-have-buffer-contents "{foo(|);}")) 165 | (it "re-enters tree mode on escape and reselects the node" 166 | (expect (with-tree-test-buffer #'java-mode "{foo([x]);}" 167 | (evil-tree-edit-change) 168 | (insert "foo") 169 | (evil-tree-edit-normal-or-tree-state)) 170 | :to-have-buffer-contents "{foo([foo]);}") 171 | (expect (with-tree-test-buffer #'java-mode "{foo(x + [y]);}" 172 | (evil-tree-edit-change) 173 | (insert "z") 174 | (evil-tree-edit-normal-or-tree-state)) 175 | :to-have-buffer-contents "{foo(x + [z]);}")) 176 | ;; TODO 177 | (xit "only allows string nodes to be changed" 178 | (expect (with-tree-test-buffer #'java-mode "[{foo;}]" 179 | (evil-tree-edit-change)) 180 | :to-throw 'tree-edit-transformation-error) 181 | (expect (with-tree-test-buffer #'java-mode "{[3 + 5];}" 182 | (evil-tree-edit-change)) 183 | :to-throw 'tree-edit-transformation-error))) 184 | 185 | ;; TODO: add proper tests 186 | ;; (describe "exchange node" 187 | ;; (it "correctly replaces valid transformations" 188 | ;; (expect (with-tree-test-buffer #'java-mode "{[foo.bar] x;}" 189 | ;; (evil-tree-edit-exchange 'identifier) 190 | ;; :to-have-buffer-contents "{[TREE] x;}"))) 191 | ;; (xit "does not allow invalid transformations")) 192 | 193 | (describe "copy/paste node" 194 | (it "correctly replaces valid transformations" 195 | (expect (with-tree-test-buffer #'java-mode "{foo([x]);}" 196 | (evil-tree-edit-copy) 197 | (evil-tree-edit-insert-sibling (car kill-ring))) 198 | :to-have-buffer-contents "{foo(x,[x]);}") 199 | ;; Regression: "foo.readl()" would parse as an expression_statement with a missing ";" 200 | (expect (with-tree-test-buffer #'java-mode "{foo([x]);}" 201 | (evil-tree-edit-copy) 202 | (evil-tree-edit-insert-sibling "foo.readl()")) 203 | :to-have-buffer-contents "{foo(x,[foo.readl()]);}") 204 | (expect (with-tree-test-buffer #'java-mode "{[foo;]bar;}" 205 | (evil-tree-edit-copy) 206 | (evil-tree-edit-goto-next-sibling) 207 | (evil-tree-edit-exchange (car kill-ring))) 208 | :to-have-buffer-contents "{foo;[foo;]}") 209 | (expect (with-tree-test-buffer #'java-mode "{foo([x]);}" 210 | (evil-tree-edit-copy) 211 | (evil-tree-edit-goto-parent) 212 | (evil-tree-edit-goto-prev-sibling) 213 | (evil-tree-edit-exchange (car kill-ring))) 214 | :to-have-buffer-contents "{[x](x);}"))) 215 | 216 | (describe "insert sibling" 217 | (it "correctly inserts sibling nodes" 218 | (expect (with-tree-test-buffer #'java-mode "{foo([x]);}" 219 | (evil-tree-edit-insert-sibling 'identifier)) 220 | :to-have-buffer-contents "{foo(x,[TREE]);}") 221 | (expect (with-tree-test-buffer #'java-mode "{foo([x]);}" 222 | (evil-tree-edit-insert-sibling 'method_invocation)) 223 | :to-have-buffer-contents "{foo(x,[TREE()]);}") 224 | (expect (with-tree-test-buffer #'java-mode "{[foo(x);]}" 225 | (evil-tree-edit-insert-sibling 'break_statement)) 226 | :to-have-buffer-contents "{foo(x);[break;]}") 227 | (expect (with-tree-test-buffer #'java-mode "{if(TREE)[{}]}" 228 | (evil-tree-edit-insert-sibling 'if_statement)) 229 | :to-have-buffer-contents "{if(TREE){}else [if(TREE){}]}") 230 | (expect (with-tree-test-buffer #'java-mode "{if(TREE)[{}]}" 231 | (evil-tree-edit-insert-sibling 'block)) 232 | :to-have-buffer-contents "{if(TREE){}else[{}]}") 233 | (expect (with-tree-test-buffer #'java-mode "{try{}[catch(Exception e) {}]}" 234 | (evil-tree-edit-insert-sibling 'catch_clause)) 235 | :to-have-buffer-contents "{try{}catch(Exception e) {}[catch(Exception e){}]}") 236 | (expect (with-tree-test-buffer #'java-mode "{try{}[catch(Exception e) {}]}" 237 | (evil-tree-edit-insert-sibling 'finally_clause)) 238 | :to-have-buffer-contents "{try{}catch(Exception e) {}[finally{}]}")) 239 | ;; FIXME: Grammar has been changed so comment nodes are line_comment or block_comment 240 | (xit "can handle comments" 241 | (expect (with-tree-test-buffer #'java-mode " 242 | {[foo();]// i'm a comment! 243 | }" 244 | (evil-tree-edit-insert-sibling 'break_statement)) 245 | :to-have-buffer-contents " 246 | {foo();[break;]// i'm a comment! 247 | }")) 248 | ;; XXX: DWIM is out, grammar mods are in 249 | (xit "can perform DWIM insertions" 250 | (expect (with-tree-test-buffer #'java-mode "{[foo(x);]}" 251 | (evil-tree-edit-insert-sibling 'null_literal)) 252 | :to-have-buffer-contents "{foo(x);[null;]}") 253 | (expect (with-tree-test-buffer #'java-mode "{[foo(x);]}" 254 | (evil-tree-edit-insert-sibling "bar()")) 255 | :to-have-buffer-contents "{foo(x);[bar();]}") 256 | (expect (with-tree-test-buffer #'java-mode "{[foo(x);]}" 257 | (with-type-cache "bar()" '(method_invocation method_invocation "bar()") 258 | (evil-tree-edit-insert-sibling "bar()"))) 259 | :to-have-buffer-contents "{foo(x);[bar();]}")) 260 | (it "works with list of types" 261 | (expect (with-tree-test-buffer #'java-mode "{foo([x]);}" 262 | (evil-tree-edit-insert-sibling '(break_statement identifier))) 263 | :to-have-buffer-contents "{foo(x,[TREE]);}") 264 | (expect (with-tree-test-buffer #'java-mode "{foo([x]);}" 265 | (evil-tree-edit-insert-sibling '(method_invocation identifier))) 266 | :to-have-buffer-contents "{foo(x,[TREE()]);}")) 267 | (xit "can perform multi-node insertions" 268 | ;; Should select bounds of new named node 269 | (expect (with-tree-test-buffer #'java-mode "{if(foo) [bar;]}" 270 | (evil-tree-edit-insert-sibling '("else" 'block))) 271 | :to-have-buffer-contents "{if(foo) bar;else{TREE;}}")) 272 | (it "does not allow invalid transformations" 273 | (expect (with-tree-test-buffer #'java-mode "[]" 274 | (evil-tree-edit-insert-sibling 'break_statement)) 275 | :to-throw 'tree-edit-transformation-error) 276 | (expect (with-tree-test-buffer #'java-mode "{foo([x]);}" 277 | (evil-tree-edit-insert-sibling 'break_statement)) 278 | :to-throw 'tree-edit-transformation-error) 279 | ;; Only one else block 280 | (expect (with-tree-test-buffer #'java-mode "{if(TREE){}else[{}]}" 281 | (evil-tree-edit-insert-sibling 'block)) 282 | :to-throw 'tree-edit-transformation-error) 283 | ;; Only one finally clause 284 | (expect (with-tree-test-buffer #'java-mode "{try{}catch(Exception e) {}[finally{}]}" 285 | (evil-tree-edit-insert-sibling 'finally_clause)) 286 | :to-throw 'tree-edit-transformation-error) 287 | ;; Catch cannot go after finally 288 | (expect (with-tree-test-buffer #'java-mode "{try{}catch(Exception e) {}[finally{}]}" 289 | (evil-tree-edit-insert-sibling 'catch_clause)) 290 | :to-throw 'tree-edit-transformation-error)) 291 | (it "gracefully rejects bad data" 292 | (expect (with-tree-test-buffer #'java-mode "{foo([x]);}" 293 | (evil-tree-edit-insert-sibling 345)) 294 | :to-throw 'user-error))) 295 | 296 | (describe "insert child" 297 | (it "correctly inserts child nodes" 298 | ;; Should select bounds of new named node 299 | (expect (with-tree-test-buffer #'java-mode "[]" 300 | (evil-tree-edit-insert-child 'break_statement)) 301 | :to-have-buffer-contents "[break;]") 302 | (expect (with-tree-test-buffer #'java-mode "{if (TREE) [{}]}" 303 | (evil-tree-edit-insert-child 'break_statement)) 304 | :to-have-buffer-contents "{if (TREE) {[break;]}}") 305 | (expect (with-tree-test-buffer #'java-mode "{foo[()];}" 306 | (evil-tree-edit-insert-child 'identifier)) 307 | :to-have-buffer-contents "{foo([TREE]);}") 308 | (expect (with-tree-test-buffer #'java-mode "{foo[(TREE)];}" 309 | (evil-tree-edit-insert-child 'method_invocation)) 310 | :to-have-buffer-contents "{foo([TREE()],TREE);}")) 311 | (it "works with list of types" 312 | ;; Should select bounds of new named node 313 | (expect (with-tree-test-buffer #'java-mode "[]" 314 | (evil-tree-edit-insert-child '(identifier break_statement))) 315 | :to-have-buffer-contents "[break;]") 316 | (expect (with-tree-test-buffer #'java-mode "[]" 317 | (evil-tree-edit-insert-child '(continue_statement break_statement))) 318 | :to-have-buffer-contents "[continue;]")) 319 | (it "can insert text fragments" 320 | ;; Should select bounds of new named node 321 | (expect (with-tree-test-buffer #'java-mode "{if (TREE) [{}]}" 322 | (evil-tree-edit-insert-child "break;")) 323 | :to-have-buffer-contents "{if (TREE) {[break;]}}") 324 | (expect (with-tree-test-buffer #'java-mode "{foo[()];}" 325 | (evil-tree-edit-insert-child "3 + 4")) 326 | :to-have-buffer-contents "{foo([3 + 4]);}")) 327 | (it "does not allow invalid transformations" 328 | (expect (with-tree-test-buffer #'java-mode "{foo[()];}" 329 | (evil-tree-edit-insert-child 'break_statement)) 330 | :to-throw 'tree-edit-transformation-error) 331 | (expect (with-tree-test-buffer #'java-mode "{foo[()];}" 332 | (evil-tree-edit-insert-child "break;")) 333 | :to-throw 'tree-edit-transformation-error))) 334 | 335 | (describe "slurp" 336 | (it "correctly slurps nodes" 337 | (expect (with-tree-test-buffer #'java-mode "{if(foo)[{}]break;}" 338 | (evil-tree-edit-slurp)) 339 | :to-have-buffer-contents "{if(foo)[{break;}]}") 340 | (expect (with-tree-test-buffer #'java-mode "{if(foo)[{break;}]break;}" 341 | (evil-tree-edit-slurp)) 342 | :to-have-buffer-contents "{if(foo)[{break;break;}]}") 343 | (expect (with-tree-test-buffer #'java-mode "{if(foo)[{break;}] if(foobar){} else if (qwerty){}}" 344 | (evil-tree-edit-slurp)) 345 | :to-have-buffer-contents "{if(foo)[{break;if(foobar){} else if (qwerty){}}]}") 346 | (expect (with-tree-test-buffer #'java-mode "{foo(bar[()], x, y, z)}" 347 | (evil-tree-edit-slurp)) 348 | :to-have-buffer-contents "{foo(bar[(x)],y, z)}") 349 | (expect (with-tree-test-buffer #'java-mode "{foo(bar[(x)], y, z)}" 350 | (evil-tree-edit-slurp)) 351 | :to-have-buffer-contents "{foo(bar[(x,y)],z)}")) 352 | (it "gracefully fails if slurp is impossible" 353 | (expect (with-tree-test-buffer #'java-mode "{foo(bar[()])}" 354 | (ignore-errors (evil-tree-edit-slurp))) 355 | :to-have-buffer-contents "{foo(bar[()])}") 356 | (expect (with-tree-test-buffer #'java-mode "{foo(bar[(x)])}" 357 | (ignore-errors (evil-tree-edit-slurp))) 358 | :to-have-buffer-contents "{foo(bar[(x)])}") 359 | (expect (with-tree-test-buffer #'java-mode "{if(foo)[{}]}" 360 | (ignore-errors (evil-tree-edit-slurp))) 361 | :to-have-buffer-contents "{if(foo)[{}]}") 362 | (expect (with-tree-test-buffer #'java-mode "{if(foo)[{break;}]}" 363 | (ignore-errors (evil-tree-edit-slurp))) 364 | :to-have-buffer-contents "{if(foo)[{break;}]}"))) 365 | 366 | (describe "barf" 367 | (it "correctly barfs nodes" 368 | (expect (with-tree-test-buffer #'java-mode "{if(foo)[{break;}]}" 369 | (evil-tree-edit-barf)) 370 | :to-have-buffer-contents "{if(foo)[{}]break;}") 371 | (expect (with-tree-test-buffer #'java-mode "{if(foo)[{break;break;}]}" 372 | (evil-tree-edit-barf)) 373 | :to-have-buffer-contents "{if(foo)[{break;}]break;}") 374 | (expect (with-tree-test-buffer #'java-mode "{if(foo)[{break;if(foobar){} else if (qwerty){}}]}" 375 | (evil-tree-edit-barf)) 376 | :to-have-buffer-contents "{if(foo)[{break;}]if(foobar){} else if (qwerty){}}") 377 | (expect (with-tree-test-buffer #'java-mode "{foo(bar[(x)], y, z)}" 378 | (evil-tree-edit-barf)) 379 | :to-have-buffer-contents "{foo(bar[()],x, y, z)}") 380 | (expect (with-tree-test-buffer #'java-mode "{foo(bar[(x,y)], z)}" 381 | (evil-tree-edit-barf)) 382 | :to-have-buffer-contents "{foo(bar[(x)],y, z)}")) 383 | (it "gracefully fails if barf is impossible" 384 | (expect (with-tree-test-buffer #'java-mode "{foo(bar[()])}" 385 | (ignore-errors (evil-tree-edit-barf))) 386 | :to-have-buffer-contents "{foo(bar[()])}") 387 | (expect (with-tree-test-buffer #'java-mode "{if(foo)[{}]}" 388 | (ignore-errors (evil-tree-edit-barf))) 389 | :to-have-buffer-contents "{if(foo)[{}]}"))) 390 | 391 | (describe "wrap node" 392 | ;; FIXME: Monkeypatching the syntax snippets doesn't work anymore due to the 393 | ;; way grammars are loaded 394 | (xit "correctly wraps nodes" 395 | (expect (with-tree-test-buffer-avy #'java-mode "{[break;]}" 0 396 | (let ((tree-edit-syntax-snippets `((block . ("{" expression_statement "}")) . ,tree-edit-syntax-snippets))) 397 | (evil-tree-edit-wrap-node 'if_statement))) 398 | :to-have-buffer-contents "{[if(TREE)break;]}") 399 | (expect (with-tree-test-buffer-avy #'java-mode "{[break;]}" 1 400 | (let ((tree-edit-syntax-snippets `((block . ("{" expression_statement "}")) . ,tree-edit-syntax-snippets))) 401 | (evil-tree-edit-wrap-node 'if_statement))) 402 | :to-have-buffer-contents "{[if(TREE){break;}]}") 403 | (expect (with-tree-test-buffer #'java-mode "{[3 + 3];}" 404 | (let ((tree-edit-syntax-snippets `((argument_list . ("(" expression ")")) . ,tree-edit-syntax-snippets))) 405 | (evil-tree-edit-wrap-node 'method_invocation))) 406 | :to-have-buffer-contents "{[TREE(3 + 3)];}")) 407 | (it "gracefully fails if node is unwrappable" 408 | (expect (with-tree-test-buffer #'java-mode "{[break;]}" 409 | (ignore-errors (evil-tree-edit-wrap-node 'method_invocation))) 410 | :to-have-buffer-contents "{[break;]}") 411 | (expect (with-tree-test-buffer #'java-mode "{[3 + 3];}" 412 | (ignore-errors (evil-tree-edit-wrap-node 'method_invocation))) 413 | :to-have-buffer-contents "{[3 + 3];}"))) 414 | 415 | ;; (describe "modify node" 416 | ;; (xit "correctly replaces valid transformations") 417 | ;; (xit "does not allow invalid transformations") 418 | ;; (xit "provides an accurate list of possible replacements")) 419 | 420 | (describe "delete node" 421 | (it "correctly replaces valid transformations" 422 | ;; Should select bounds of new named node 423 | (expect (with-tree-test-buffer #'java-mode "{foo([x]);}" 424 | (evil-tree-edit-delete)) 425 | :to-have-buffer-contents "{foo[()];}") 426 | ;; TODO: Check tree-equals, I don't care about formatting 427 | (expect (with-tree-test-buffer #'java-mode " 428 | class Main { 429 | [void foo() {}] 430 | void bar() {} 431 | }" 432 | (evil-tree-edit-delete)) 433 | :to-have-buffer-contents " 434 | class Main {[void bar() {}] 435 | }") 436 | (expect (with-tree-test-buffer #'java-mode " 437 | { 438 | break; 439 | [break;] 440 | break; 441 | }" 442 | (evil-tree-edit-delete)) 443 | :to-have-buffer-contents " 444 | { 445 | break;[break;] 446 | }") 447 | ;; (expect (with-tree-test-buffer #'java-mode " 448 | ;; class Main {[public] void main() {} 449 | ;; }" 450 | ;; ;; FIXME: Test selects anonymous keyword 'public', instead of 'modifiers 451 | ;; (evil-tree-edit-goto-parent) 452 | ;; (evil-tree-edit-delete)) 453 | ;; :to-have-buffer-contents " 454 | ;; class Main {[void] main() {} 455 | ;; }") 456 | (expect (with-tree-test-buffer #'java-mode "{foo([x],y);}" 457 | (evil-tree-edit-delete)) 458 | :to-have-buffer-contents "{foo([y]);}") 459 | (expect (with-tree-test-buffer #'java-mode "{foo(x,[y]);}" 460 | (evil-tree-edit-delete)) 461 | :to-have-buffer-contents "{foo([x]);}") 462 | 463 | (expect (with-tree-test-buffer #'java-mode "{foo(x);[break;]}" 464 | (evil-tree-edit-delete)) 465 | :to-have-buffer-contents "{[foo(x);]}")) 466 | (xit "can handle comments" 467 | (expect (with-tree-test-buffer #'java-mode " 468 | class Main { 469 | // should we delete this? 470 | [void foo() {}] 471 | void bar() {}}" 472 | (evil-tree-edit-delete)) 473 | :to-have-buffer-contents " 474 | class Main { 475 | // should we delete this? 476 | [void bar() {}]}") 477 | (expect (with-tree-test-buffer #'java-mode " 478 | class Main { 479 | // should we delete this? 480 | // i'm a second comment in a row 481 | [void foo() {}] 482 | void bar() {} 483 | // here too 484 | }" 485 | (evil-tree-edit-delete)) 486 | :to-have-buffer-contents " 487 | class Main { 488 | // should we delete this? 489 | // i'm a second comment in a row 490 | [void bar() {}] 491 | // here too 492 | }")) 493 | (it "can delete nodes with aliased types" 494 | ;; Should select bounds of new named node 495 | ;; (expect (with-tree-test-buffer #'java-mode "class Foo {[public] int bar;}" 496 | ;; ;; FIXME: selects unnamed node 'public', not modifiers node 497 | ;; (evil-tree-edit-goto-parent) 498 | ;; (evil-tree-edit-delete)) 499 | ;; :to-have-buffer-contents "class Foo {[int] bar;}") 500 | ) 501 | (it "does not allow invalid transformations" 502 | (expect (with-tree-test-buffer #'java-mode "[]" 503 | (evil-tree-edit-delete)) 504 | :to-throw 'tree-edit-transformation-error) 505 | (expect (with-tree-test-buffer #'java-mode "{[foo](x);}" 506 | (evil-tree-edit-delete)) 507 | :to-throw 'tree-edit-transformation-error) 508 | (expect (with-tree-test-buffer #'java-mode " 509 | class Main { 510 | [void] main() {} 511 | }" 512 | (evil-tree-edit-delete)) 513 | :to-throw 'tree-edit-transformation-error)) 514 | (xit "can deal with comments in between relevant syntax" 515 | (expect (with-tree-test-buffer #'java-mode "{ 516 | foo(x, // comment 517 | [y]); 518 | }" 519 | (evil-tree-edit-delete)) 520 | :to-have-buffer-contents "{ 521 | foo(x // comment 522 | ); 523 | }"))) 524 | -------------------------------------------------------------------------------- /evil-tree-edit.el: -------------------------------------------------------------------------------- 1 | ;;; evil-tree-edit.el --- Evil structural editing for any language! -*- lexical-binding: t; -*- 2 | ;; 3 | ;; Copyright (C) Ethan Leba 4 | ;; 5 | ;; Author: Ethan Leba 6 | ;; Version: 0.1.0 7 | ;; Homepage: https://github.com/ethan-leba/tree-edit 8 | ;; 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")) 9 | ;; SPDX-License-Identifier: GPL-3.0-or-later 10 | ;; 11 | ;; This file is not part of GNU Emacs. 12 | ;; 13 | ;;; Commentary: 14 | ;; 15 | ;; Provides a 'tree' evil state that allows structural navigation and 16 | ;; modification of the buffer. 17 | ;; 18 | ;; See https://github.com/ethan-leba/tree-edit for usage. 19 | ;; 20 | ;;; Code: 21 | (require 'avy) 22 | (require 'evil) 23 | (require 'mode-local) 24 | (require 'tree-edit) 25 | (require 's) 26 | 27 | ;;* Variables 28 | (defvar evil-tree-edit-mode-map (make-sparse-keymap)) 29 | (defvar-local evil-tree-edit--current-node nil 30 | "The current node to apply editing commands to.") 31 | (defvar-local evil-tree-edit--node-overlay nil 32 | "The display overlay to show the current node.") 33 | (defvar-local evil-tree-edit--return-position nil 34 | "The location that the cursor should be returned to after exiting insert mode, if set.") 35 | 36 | (defvar evil-tree-edit-pos-ring (make-ring 100) 37 | "Ring for node position history.") 38 | (defgroup evil-tree-edit nil 39 | "Evil structural editing for tree-sitter languages." 40 | :group 'bindings 41 | :prefix "evil-tree-edit-") 42 | (defcustom evil-tree-edit-movement-hook nil 43 | "Functions to call after a tree-edit movement command has been issued." 44 | :type 'hook 45 | :group 'evil-tree-edit) 46 | (defcustom evil-tree-edit-after-change-hook nil 47 | "Functions to call after a tree-edit command modifies the buffer." 48 | :type 'hook 49 | :group 'evil-tree-edit) 50 | (defcustom evil-tree-edit-disable-nontree-bindings nil 51 | "If non-nil, don't set the default normal and insert state bindings." 52 | :type 'boolean 53 | :group 'evil-tree-edit) 54 | 55 | ;;* Misc 56 | (defun evil-tree-edit-current-node () 57 | (when (and evil-tree-edit--current-node 58 | (treesit-node-check evil-tree-edit--current-node 'outdated)) 59 | (ignore-errors 60 | (evil-tree-edit-set-current-node 61 | (tree-edit--node-from-steps evil-tree-edit--return-position)) 62 | (run-hooks 'evil-tree-edit-after-change-hook) 63 | (evil-tree-edit--update-overlay))) 64 | evil-tree-edit--current-node) 65 | 66 | (defun evil-tree-edit-set-current-node (val) 67 | (setq evil-tree-edit--current-node val)) 68 | 69 | ;;* Navigation 70 | (defmacro evil-tree-edit--preserve-location (&rest body) 71 | "Preserves the location of NODE during the execution of the BODY. 72 | 73 | Optionally applies a MOVEMENT to the node after restoration, 74 | moving the sibling index by the provided value." 75 | (declare (debug t)) 76 | (let ((location-sym (gensym "location"))) 77 | `(let ((,location-sym (tree-edit--node-steps (evil-tree-edit-current-node)))) 78 | ,@body 79 | (run-hooks 'evil-tree-edit-after-change-hook) 80 | (evil-tree-edit-set-current-node 81 | (tree-edit--node-from-steps ,location-sym)) 82 | (evil-tree-edit--update-overlay)))) 83 | 84 | (defun evil-tree-edit--preserve-current-node-before (_ __) 85 | "Save the location of the current node before the buffer is re-parsed." 86 | (when (evil-tree-state-p) 87 | (setq evil-tree-edit--return-position (tree-edit--node-steps (evil-tree-edit-current-node))))) 88 | 89 | (defun evil-tree-edit--preserve-current-node-after () 90 | "Restore the location of the current node after the buffer is re-parsed. 91 | 92 | `tree-sitter-after-change-functions' provides an old-tree arg, 93 | but it seems to not work reliably with `tree-edit--node-from-steps'." 94 | (when (evil-tree-state-p) 95 | (evil-tree-edit-current-node))) 96 | 97 | (defun evil-tree-edit--apply-movement (fun) 98 | "Apply movement FUN, and then update the node position and display." 99 | (evil-tree-edit-ensure-current-node) 100 | (when-let ((new-pos (tree-edit--apply-until-interesting fun (evil-tree-edit-current-node)))) 101 | (evil-tree-edit--goto-node new-pos))) 102 | 103 | (defun evil-tree-edit--get-sig-parent (node) 104 | "Move NODE to the next (interesting) named sibling." 105 | (let ((parent (treesit-node-parent node))) 106 | (cond 107 | ((or (not parent) 108 | (not (treesit-node-parent parent))) node) 109 | ((treesit-node-eq parent (treesit-buffer-root-node)) node) 110 | ((--any (member (tree-edit--node-type parent) 111 | (cons it (alist-get it tree-edit--subtypes '()))) 112 | (tree-edit-significant-node-types)) 113 | parent) 114 | (t (evil-tree-edit--get-sig-parent parent))))) 115 | 116 | (defun evil-tree-edit--remember () 117 | "Store the current point and mark in history." 118 | (let* ((emptyp (zerop (ring-length evil-tree-edit-pos-ring))) 119 | (top (unless emptyp (ring-ref evil-tree-edit-pos-ring 0))) 120 | (pos (tree-edit--node-steps (evil-tree-edit-current-node)))) 121 | (when (or emptyp (not (equal top pos))) 122 | (ring-insert evil-tree-edit-pos-ring pos)))) 123 | 124 | (defun evil-tree-edit--read-from-kill-ring (prompt) 125 | "Prompt for kill ring string with PROMPT in backwards compatible manner." 126 | (if (>= emacs-major-version 28) 127 | (read-from-kill-ring prompt) 128 | (completing-read prompt kill-ring))) 129 | 130 | (defun evil-tree-edit-ensure-current-node () 131 | "Error if `evil-tree-edit-current-node' is nil." 132 | (unless (evil-tree-edit-current-node) 133 | (user-error "`evil-tree-edit-current-node' is nil, are you in `evil-tree-state'?"))) 134 | 135 | ;;* Globals: navigation 136 | (defun evil-tree-edit-goto-next-sibling () 137 | "Move to the next (interesting) named sibling." 138 | (interactive) 139 | (evil-tree-edit-ensure-current-node) 140 | (evil-tree-edit--apply-movement (lambda (node) (treesit-node-next-sibling node :named)))) 141 | 142 | (defun evil-tree-edit-goto-prev-sibling () 143 | "Move to the previous (interesting) named sibling." 144 | (interactive) 145 | (evil-tree-edit-ensure-current-node) 146 | (evil-tree-edit--apply-movement (lambda (node) (treesit-node-prev-sibling node :named)))) 147 | 148 | (defun evil-tree-edit-goto-parent () 149 | "Move up to the next interesting parent." 150 | (interactive) 151 | (evil-tree-edit-ensure-current-node) 152 | (evil-tree-edit--apply-movement #'treesit-node-parent)) 153 | 154 | (defun evil-tree-edit-goto-child () 155 | "Move to the first child, unless it's an only child." 156 | (interactive) 157 | (evil-tree-edit-ensure-current-node) 158 | (evil-tree-edit--apply-movement (lambda (node) (treesit-node-child node 0 :named)))) 159 | 160 | (defun evil-tree-edit-goto-sig-parent () 161 | "Move to the next (interesting) named sibling." 162 | (interactive) 163 | (evil-tree-edit-ensure-current-node) 164 | (evil-tree-edit--remember) 165 | (evil-tree-edit--apply-movement #'evil-tree-edit--get-sig-parent)) 166 | 167 | (defun evil-tree-edit-back () 168 | "Set current node to the last remembered position." 169 | (interactive) 170 | (unless (zerop (ring-length evil-tree-edit-pos-ring)) 171 | (-> (ring-remove evil-tree-edit-pos-ring 0) 172 | (tree-edit--node-from-steps) 173 | (evil-tree-edit--goto-node)))) 174 | 175 | ;;* Evil tree-edit functions 176 | (defun evil-tree-edit-change (&optional return-location) 177 | "Change the current node, and return to RETURN-LOCATION on exit. 178 | 179 | If RETURN-NODE is unset, `evil-tree-edit-current-node' is used." 180 | (interactive) 181 | (evil-tree-edit-ensure-current-node) 182 | (setq evil-tree-edit--return-position 183 | (or return-location (tree-edit--node-steps (evil-tree-edit-current-node)))) 184 | (evil-change-state 'insert) 185 | (delete-region (treesit-node-start (evil-tree-edit-current-node)) 186 | (treesit-node-end (evil-tree-edit-current-node)))) 187 | 188 | (defun evil-tree-edit-copy () 189 | "Copy the current node." 190 | (interactive) 191 | (evil-tree-edit-ensure-current-node) 192 | (tree-edit-copy (evil-tree-edit-current-node))) 193 | 194 | (defun evil-tree-edit-undo (count) 195 | "Undo COUNT actions while saving the cursor position." 196 | (interactive "*p") 197 | (evil-tree-edit-ensure-current-node) 198 | (evil-undo count)) 199 | 200 | (defun evil-tree-edit-append-sibling-placeholder-and-change () 201 | "Add a placeholder node and then change it." 202 | (interactive) 203 | (evil-tree-edit-insert-sibling (tree-edit-placeholder-node-type)) 204 | (evil-tree-edit-change)) 205 | 206 | (defun evil-tree-edit-insert-child-placeholder-and-change () 207 | "Add a placeholder node and then change it." 208 | (interactive) 209 | (evil-tree-edit-insert-child (tree-edit-placeholder-node-type)) 210 | (evil-tree-edit-change)) 211 | 212 | (defun evil-tree-edit-sig-avy-jump (node-type) 213 | "Avy jump to a node with the NODE-TYPE within scope of the nearest sig node. 214 | 215 | NODE-TYPE can be a symbol or a list of symbol." 216 | (interactive) 217 | (evil-tree-edit-ensure-current-node) 218 | (evil-tree-edit--remember) 219 | (let ((query-node 220 | (if (member (tree-edit--node-type (evil-tree-edit-current-node)) 221 | (tree-edit-significant-node-types)) 222 | (evil-tree-edit-current-node) 223 | (evil-tree-edit--get-sig-parent (evil-tree-edit-current-node))))) 224 | (-> node-type 225 | (tree-edit--format-query-string) 226 | (tree-edit-query query-node) 227 | (evil-tree-edit--avy-jump)))) 228 | 229 | (defun evil-tree-edit-avy-jump (node-type) 230 | "Avy jump to a node with NODE-TYPE under the current node. 231 | 232 | NODE-TYPE can be a symbol or a list of symbol." 233 | (interactive) 234 | (evil-tree-edit-ensure-current-node) 235 | (evil-tree-edit--remember) 236 | (-> node-type 237 | (tree-edit--format-query-string) 238 | (tree-edit-query (evil-tree-edit-current-node)) 239 | (evil-tree-edit--avy-jump))) 240 | 241 | (defun evil-tree-edit--goto-node (node) 242 | "Set current node to NODE and run hooks." 243 | (evil-tree-edit-set-current-node node) 244 | (run-hooks 'evil-tree-edit-movement-hook)) 245 | 246 | (defun evil-tree-edit-out (node-type/s) 247 | "Move outwards until a node of NODE-TYPE/S has been hit." 248 | (interactive) 249 | (evil-tree-edit-ensure-current-node) 250 | (evil-tree-edit--remember) 251 | (let* ((node-types (if (listp node-type/s) node-type/s `(,node-type/s)))) 252 | (if-let (result (treesit-parent-until 253 | (evil-tree-edit-current-node) 254 | (lambda (node) (member (tree-edit--node-type node) node-types)))) 255 | (evil-tree-edit--goto-node result) 256 | (user-error "Current node has no parent of type %s" node-type/s)))) 257 | 258 | (defun evil-tree-edit--avy-jump (nodes) 259 | "Avy jump to one of NODES." 260 | (interactive) 261 | (evil-tree-edit-ensure-current-node) 262 | (let* ((window-top-boundary (window-start)) 263 | (window-bottom-boundary (window-end)) 264 | (position->node 265 | (--keep 266 | (let ((position (treesit-node-start it))) 267 | (if (>= window-bottom-boundary position window-top-boundary) 268 | (cons position it))) 269 | nodes)) 270 | ;; avy-action declares what should be done with the result of avy-process 271 | (avy-action (lambda (pos) (evil-tree-edit--goto-node (alist-get pos position->node))))) 272 | (cond ((not position->node) (user-error "Nothing to jump to!")) 273 | ((equal (length position->node) 1) (funcall avy-action (caar position->node))) 274 | (t (avy-process (-map #'car position->node)))))) 275 | 276 | (defun evil-tree-edit-wrap-node (type) 277 | "Wrap the current node in a node of selected TYPE." 278 | (evil-tree-edit-ensure-current-node) 279 | (evil-tree-edit--preserve-location 280 | (let ((node-text (treesit-node-text (evil-tree-edit-current-node))) 281 | (node-type (tree-edit--node-type (evil-tree-edit-current-node)))) 282 | (tree-edit-cache-node (evil-tree-edit-current-node)) 283 | (atomic-change-group 284 | (evil-tree-edit-exchange type) 285 | (evil-tree-edit--avy-jump 286 | (--filter (tree-edit--valid-replacement-p node-type it) 287 | (tree-edit--all-named-descendants (evil-tree-edit-current-node)))) 288 | (evil-tree-edit-exchange node-text))))) 289 | 290 | (defun evil-tree-edit-exchange (type-or-text) 291 | "Exchange current node for TYPE-OR-TEXT. 292 | 293 | See `tree-edit-exchange'." 294 | 295 | (interactive) 296 | (evil-tree-edit-ensure-current-node) 297 | (tree-edit-exchange type-or-text (evil-tree-edit-current-node))) 298 | 299 | (defun evil-tree-edit-delete () 300 | "Delete the current node. 301 | 302 | See `tree-edit-delete'." 303 | (interactive) 304 | (evil-tree-edit-ensure-current-node) 305 | (tree-edit-delete (evil-tree-edit-current-node))) 306 | 307 | (defun evil-tree-edit-move () 308 | "Copy then delete the current node. 309 | 310 | See `tree-edit-delete'." 311 | (interactive) 312 | (evil-tree-edit-ensure-current-node) 313 | (tree-edit-copy (evil-tree-edit-current-node)) 314 | (tree-edit-delete (evil-tree-edit-current-node))) 315 | 316 | (defun evil-tree-edit-raise () 317 | "Move the current node up the syntax tree until a valid replacement is found. 318 | 319 | See `tree-edit-raise'." 320 | (interactive) 321 | (evil-tree-edit-ensure-current-node) 322 | (let ((raised-node (tree-edit-raise (evil-tree-edit-current-node)))) 323 | (evil-tree-edit-set-current-node raised-node)) 324 | (evil-tree-edit--update-overlay)) 325 | 326 | (defun evil-tree-edit-insert-sibling (type-or-text &optional before) 327 | "Insert a node of the given TYPE-OR-TEXT next to the current node. 328 | 329 | if BEFORE is t, the sibling node will be inserted before the 330 | current, otherwise after. 331 | 332 | See `tree-edit-insert-sibling'." 333 | (interactive) 334 | (evil-tree-edit-ensure-current-node) 335 | (tree-edit-insert-sibling type-or-text (evil-tree-edit-current-node) before) 336 | (unless before 337 | (evil-tree-edit-goto-next-sibling))) 338 | 339 | (defun evil-tree-edit-insert-sibling-before (type) 340 | "Insert a node of the given TYPE before the current." 341 | (evil-tree-edit-insert-sibling type :before)) 342 | 343 | (defun evil-tree-edit-insert-child (type-or-text) 344 | "Insert a node of the given TYPE-OR-TEXT inside of the current node at the beginning." 345 | (interactive) 346 | (evil-tree-edit-ensure-current-node) 347 | (tree-edit-insert-child type-or-text (evil-tree-edit-current-node)) 348 | (evil-tree-edit-goto-child)) 349 | 350 | (defun evil-tree-edit-insert-child-last (type-or-text) 351 | "Insert a node of the given TYPE-OR-TEXT inside of the current node at the end." 352 | (interactive) 353 | (evil-tree-edit-ensure-current-node) 354 | (tree-edit-insert-child type-or-text (evil-tree-edit-current-node) -1) 355 | (evil-tree-edit--goto-node (treesit-node-child (evil-tree-edit-current-node) -1 :named))) 356 | 357 | (defun evil-tree-edit-slurp () 358 | "Transform current node's next sibling into it's leftmost child, if possible." 359 | (interactive) 360 | (evil-tree-edit-ensure-current-node) 361 | (tree-edit-slurp (evil-tree-edit-current-node))) 362 | 363 | (defun evil-tree-edit-barf () 364 | "Transform current node's leftmost child into it's next sibling, if possible." 365 | (interactive) 366 | (evil-tree-edit-ensure-current-node) 367 | (tree-edit-barf (evil-tree-edit-current-node))) 368 | 369 | (defun evil-tree-edit-goto-next-placeholder () 370 | "Move cursor to the next placeholder node. 371 | 372 | Placeholder is defined by `tree-edit-placeholder-node-type'." 373 | (interactive) 374 | (evil-tree-edit-ensure-current-node) 375 | (unless (tree-edit-placeholder-node-type) 376 | (user-error "`tree-edit-placeholder-node-type' not set!")) 377 | (pcase (tree-edit-query 378 | (format "((%s) (#equal @node %s))" 379 | (tree-edit--format-query-string 380 | (cons (tree-edit-placeholder-node-type) 381 | (tree-edit-all-aliases-for-type (tree-edit-placeholder-node-type)))) 382 | ;; XXX: Assuming the placeholder type is a singleton list containing a string 383 | (car (alist-get (tree-edit-placeholder-node-type) (tree-edit-syntax-snippets)))) 384 | (evil-tree-edit-current-node) 385 | :want-text t) 386 | (`(,first . ,_) (evil-tree-edit--goto-node first)) 387 | (_ (user-error "No placeholders contained in the current!")))) 388 | 389 | (defun evil-tree-edit-change-next-placeholder () 390 | "Move cursor to the next placeholder node and change it." 391 | (interactive) 392 | (evil-tree-edit-ensure-current-node) 393 | (let ((current-location (tree-edit--node-steps (evil-tree-edit-current-node)))) 394 | (evil-tree-edit-goto-next-placeholder) 395 | (evil-tree-edit-change current-location))) 396 | 397 | (defun evil-tree-edit-change-next-placeholder-from-insert () 398 | "Complete edit of current node and change the next placeholder node." 399 | (interactive) 400 | (evil-tree-edit-normal-or-tree-state) 401 | (evil-tree-edit-change-next-placeholder)) 402 | 403 | (defun evil-tree-edit-node-info () 404 | "Preview the different variations of the current node." 405 | (interactive) 406 | (cond 407 | (current-prefix-arg (evil-tree-edit-preview-node)) 408 | (t (evil-tree-edit-ensure-current-node) 409 | (message "Current node type is '%s', bound to key '%s'." 410 | (tree-edit--node-type (evil-tree-edit-current-node)) 411 | (let ((node-type (thread-last 412 | (evil-tree-edit-current-node) 413 | (tree-edit--node-type)))) 414 | (--any 415 | (when (member node-type 416 | (if (listp (plist-get it :type)) 417 | (plist-get it :type) 418 | `(,(plist-get it :type)))) 419 | (plist-get it :key)) 420 | (tree-edit-nodes))))))) 421 | 422 | (defun evil-tree-edit-preview-node () 423 | "Preview the different variations of the current node." 424 | (interactive) 425 | (evil-tree-edit-ensure-current-node) 426 | (let ((reazon-occurs-check nil) 427 | (reazon-timeout 0.1) 428 | (tree-edit-parse-comments nil)) 429 | (--> (evil-tree-edit-current-node) 430 | (tree-edit--node-type it) 431 | (alist-get it (tree-edit-grammar)) 432 | ;; TODO: Parametrize 433 | (reazon-run 10 q (tree-edit-parseo it q '())) 434 | ;; Sometime parser returns repeats, not sure if that's expected 435 | (-uniq it) 436 | ;; TODO: Prettier print 437 | (message (string-join (-map #'prin1-to-string it) "\n"))))) 438 | 439 | (defun evil-tree-edit-yank () 440 | "Exchange the current node with the top of the kill ring." 441 | (interactive) 442 | (evil-tree-edit-exchange (car kill-ring))) 443 | 444 | (defun evil-tree-edit-yank-pop () 445 | "Exchange the current node with a selection from the kill-ring." 446 | (interactive) 447 | (evil-tree-edit-exchange (evil-tree-edit--read-from-kill-ring "Exchange node: "))) 448 | 449 | (defun evil-tree-edit-clone () 450 | "Insert a copy of the current node." 451 | (interactive) 452 | (let ((node-text (treesit-node-text (evil-tree-edit-current-node)))) 453 | (tree-edit-cache-node (evil-tree-edit-current-node)) 454 | (evil-tree-edit-insert-sibling node-text))) 455 | 456 | (defun evil-tree-edit--treesit--explorer-refresh-advice (func &rest args) 457 | "If in `evil-tree-state', set the region to the range of the current node. 458 | 459 | This is so that the current node will be properly highlighted in explorer mode." 460 | (if (not (eq evil-state 'tree)) (apply func args) 461 | (save-mark-and-excursion 462 | (goto-char (treesit-node-start (evil-tree-edit-current-node))) 463 | (set-mark (treesit-node-end (evil-tree-edit-current-node))) 464 | (apply func args)))) 465 | 466 | (advice-add 'treesit--explorer-refresh :around 467 | #'evil-tree-edit--treesit--explorer-refresh-advice) 468 | 469 | (defun evil-tree-edit-toggle-tree-view () 470 | "Toggle `evil-tree-edit-view-mode'." 471 | (interactive) 472 | (evil-tree-edit-ensure-current-node) 473 | (treesit-explore-mode)) 474 | 475 | (defun evil-tree-edit-select-in-visual-state () 476 | "Move to visual state with the current node as the selection." 477 | (interactive) 478 | (evil-tree-edit-ensure-current-node) 479 | (evil-visual-select (treesit-node-start (evil-tree-edit-current-node)) 480 | (1- (treesit-node-end (evil-tree-edit-current-node))))) 481 | 482 | 483 | (defun evil-tree-edit--ambiguous-node-range-p (node-a node-b) 484 | "Do NODE-A and NODE-B share the same range?" 485 | (and node-a node-b 486 | (equal (treesit-node-start node-a) 487 | (treesit-node-start node-b)) 488 | (equal (treesit-node-end node-a) 489 | (treesit-node-end node-b)))) 490 | 491 | ;;* Mode and evil state definitions 492 | (defun evil-tree-edit--update-overlay () 493 | "Update the display of the current selected node, and move the cursor." 494 | (move-overlay evil-tree-edit--node-overlay 495 | (or (treesit-node-start (evil-tree-edit-current-node)) (point-min)) 496 | (or (treesit-node-end (evil-tree-edit-current-node)) (point-max))) 497 | (goto-char (treesit-node-start (evil-tree-edit-current-node))) 498 | (if (or (evil-tree-edit--ambiguous-node-range-p 499 | (treesit-node-parent (evil-tree-edit-current-node)) 500 | (evil-tree-edit-current-node)) 501 | (evil-tree-edit--ambiguous-node-range-p 502 | (treesit-node-child (evil-tree-edit-current-node) 0) 503 | (evil-tree-edit-current-node))) 504 | (overlay-put evil-tree-edit--node-overlay 'after-string 505 | (propertize 506 | (let ((type (tree-edit--node-type (evil-tree-edit-current-node)))) 507 | (s-concat " " (if (stringp type) type (symbol-name type)))) 508 | ;; TODO: Abstract into var 509 | 'face '(italic :foreground "dark gray"))) 510 | (overlay-put evil-tree-edit--node-overlay 'after-string ""))) 511 | 512 | (defun evil-tree-edit--enter-tree-state () 513 | "Activate tree-edit state." 514 | (unless evil-tree-edit--node-overlay 515 | (setq evil-tree-edit--node-overlay (make-overlay 0 0))) 516 | (let ((node (treesit-node-descendant-for-range 517 | (treesit-buffer-root-node) (point) (point)))) 518 | (evil-tree-edit-set-current-node 519 | (if (tree-edit--boring-nodep node) 520 | (tree-edit--apply-until-interesting #'treesit-node-parent node) 521 | node))) 522 | (overlay-put evil-tree-edit--node-overlay 'face 'region) 523 | (evil-tree-edit--update-overlay)) 524 | 525 | (defun evil-tree-edit--exit-tree-state () 526 | "De-activate tree-edit state." 527 | (unless (eq evil-next-state 'insert) 528 | (setq evil-tree-edit--return-position nil)) 529 | (when evil-tree-edit--node-overlay 530 | (overlay-put evil-tree-edit--node-overlay 'after-string "") 531 | (overlay-put evil-tree-edit--node-overlay 'face '()))) 532 | 533 | (defun evil-tree-edit-normal-or-tree-state () 534 | "Enter normal or tree state contextually." 535 | (interactive) 536 | (if evil-tree-edit--return-position 537 | (progn 538 | (evil-tree-state) 539 | (evil-tree-edit--goto-node 540 | (or (tree-edit--node-from-steps-strict evil-tree-edit--return-position) 541 | (progn 542 | (message "Could not restore node position, selecting at point.") 543 | (treesit-node-descendant-for-range 544 | ;; Entering normal state will put the point before the selected 545 | ;; text, so we increment it by one. 546 | (treesit-buffer-root-node) (1+ (point)) (1+ (point)))))) 547 | (setq evil-tree-edit--return-position nil)) 548 | (evil-normal-state))) 549 | 550 | (defun evil-tree-edit--teardown () 551 | "De-activate tree-edit state." 552 | (when evil-tree-edit--node-overlay 553 | (delete-overlay evil-tree-edit--node-overlay))) 554 | 555 | (evil-define-state tree 556 | "evil-tree-edit state" 557 | :tag " " 558 | :cursor 'hollow 559 | :entry-hook (evil-tree-edit--enter-tree-state) 560 | :exit-hook (evil-tree-edit--exit-tree-state) 561 | :suppress-keymap t) 562 | 563 | ;;;###autoload 564 | (define-minor-mode evil-tree-edit-mode 565 | "Structural editing for any* language." 566 | :init-value nil 567 | :group 'tree-edit 568 | :keymap evil-tree-edit-mode-map 569 | :lighter " TE" 570 | (cond 571 | (evil-tree-edit-mode 572 | (condition-case err 573 | (tree-edit-load-grammar-for-major-mode) 574 | (error 575 | (evil-tree-edit-mode -1) 576 | (signal (car err) (cdr err)))) 577 | (evil-tree-edit-set-state-bindings major-mode) 578 | ;; HACK: Above mode binding won't come into effect until the state is changed. 579 | (evil-normal-state) 580 | (add-hook 'before-revert-hook #'evil-tree-edit--teardown nil 'local) 581 | ;; TODO: can we just run these on load? 582 | (add-hook 'post-command-hook #'evil-tree-edit--preserve-current-node-after nil 'local) 583 | (add-hook 'before-change-functions #'evil-tree-edit--preserve-current-node-before nil 'local) 584 | (add-hook 'evil-tree-edit-movement-hook #'evil-tree-edit--update-overlay nil 'local)) 585 | (t 586 | (remove-hook 'post-command-hook #'evil-tree-edit--preserve-current-node-after 'local) 587 | (remove-hook 'before-revert-hook #'evil-tree-edit--teardown 'local) 588 | (remove-hook 'before-change-functions #'evil-tree-edit--preserve-current-node-before 'local)))) 589 | 590 | (defun define-evil-tree-edit-avy-jump (keymap key func) 591 | "Define a key command in KEYMAP prefixed by KEY calling FUNC. 592 | 593 | FUNC must take one argument, a symbol of the node type." 594 | (dolist (node (append (tree-edit-nodes) (tree-edit-query-nodes))) 595 | (define-key 596 | keymap 597 | (string-join (list key (plist-get node :key))) 598 | (cons 599 | ;; emacs-which-key integration 600 | (cond ((plist-get node :name) (plist-get node :name)) 601 | ((listp (plist-get node :type)) (s-join "/" (--map (s-replace "_" " " (symbol-name it)) (plist-get node :type)))) 602 | (t (s-replace "_" " " (symbol-name (plist-get node :type))))) 603 | `(lambda () 604 | (interactive) 605 | (,func ',(plist-get node :type))))))) 606 | 607 | (defun define-evil-tree-edit-verb (keymap key func &optional wrap) 608 | "Define a key command in KEYMAP prefixed by KEY calling FUNC. 609 | 610 | FUNC must take one argument, a symbol of the node type. 611 | If WRAP is t, include :wrap-override." 612 | (dolist (node (tree-edit-nodes)) 613 | (define-key 614 | keymap 615 | (string-join (list key (plist-get node :key))) 616 | (cons 617 | ;; emacs-which-key integration 618 | (cond ((plist-get node :name) (plist-get node :name)) 619 | ((listp (plist-get node :type)) (s-join "/" (--map (s-replace "_" " " (symbol-name it)) (plist-get node :type)))) 620 | (t (s-replace "_" " " (symbol-name (plist-get node :type))))) 621 | `(lambda () 622 | (interactive) 623 | (let ((tree-edit-syntax-snippets 624 | (append ,(plist-get node :node-override) 625 | ,(if wrap (plist-get node :wrap-override)) 626 | tree-edit-syntax-snippets))) 627 | (,func ',(plist-get node :type))))))) 628 | ;; Can this be integrated into the loop? 629 | (define-key 630 | keymap 631 | (string-join (list key "p")) 632 | (cons 633 | "yank" 634 | `(lambda () 635 | (interactive) 636 | (,func (car kill-ring))))) 637 | (define-key 638 | keymap 639 | (string-join (list key "P")) 640 | (cons 641 | "yank-pop" 642 | `(lambda () 643 | (interactive) 644 | (,func (evil-tree-edit--read-from-kill-ring "Kill-ring: ")))))) 645 | 646 | (defun evil-tree-edit-set-state-bindings (parser) 647 | "Set keybindings for MODE in `evil-tree-state'. 648 | 649 | Should only be used in the context of mode-local bindings, as 650 | each language will have it's own set of nouns." 651 | (let ((mode-local-keymap (make-composed-keymap (make-sparse-keymap) evil-suppress-map))) 652 | (define-evil-tree-edit-verb mode-local-keymap "i" #'evil-tree-edit-insert-sibling-before) 653 | (define-evil-tree-edit-verb mode-local-keymap "a" #'evil-tree-edit-insert-sibling) 654 | (define-evil-tree-edit-verb mode-local-keymap "I" #'evil-tree-edit-insert-child) 655 | (define-evil-tree-edit-verb mode-local-keymap "A" #'evil-tree-edit-insert-child-last) 656 | (define-evil-tree-edit-verb mode-local-keymap "e" #'evil-tree-edit-exchange) 657 | (define-evil-tree-edit-verb mode-local-keymap "w" #'evil-tree-edit-wrap-node t) 658 | (define-evil-tree-edit-avy-jump mode-local-keymap "s" #'evil-tree-edit-avy-jump) 659 | (define-evil-tree-edit-avy-jump mode-local-keymap "q" #'evil-tree-edit-sig-avy-jump) 660 | (define-evil-tree-edit-avy-jump mode-local-keymap "o" #'evil-tree-edit-out) 661 | (define-key mode-local-keymap [escape] 'evil-normal-state) 662 | (define-key mode-local-keymap ">" #'evil-tree-edit-slurp) 663 | (define-key mode-local-keymap "<" #'evil-tree-edit-barf) 664 | (define-key mode-local-keymap "j" #'evil-tree-edit-goto-next-sibling) 665 | (define-key mode-local-keymap "k" #'evil-tree-edit-goto-prev-sibling) 666 | (define-key mode-local-keymap "h" #'evil-tree-edit-goto-parent) 667 | (define-key mode-local-keymap "f" #'evil-tree-edit-goto-child) 668 | (define-key mode-local-keymap "b" #'evil-tree-edit-back) 669 | (define-key mode-local-keymap "x" #'evil-tree-edit-append-sibling-placeholder-and-change) 670 | (define-key mode-local-keymap "X" #'evil-tree-edit-insert-child-placeholder-and-change) 671 | (define-key mode-local-keymap "n" #'evil-tree-edit-goto-next-placeholder) 672 | (define-key mode-local-keymap "N" #'evil-tree-edit-change-next-placeholder) 673 | (define-key mode-local-keymap "c" #'evil-tree-edit-change) 674 | (define-key mode-local-keymap "d" #'evil-tree-edit-delete) 675 | (define-key mode-local-keymap "m" #'evil-tree-edit-move) 676 | (define-key mode-local-keymap "r" #'evil-tree-edit-raise) 677 | (define-key mode-local-keymap "y" #'evil-tree-edit-copy) 678 | (define-key mode-local-keymap "p" #'evil-tree-edit-yank) 679 | (define-key mode-local-keymap "P" #'evil-tree-edit-yank-pop) 680 | (define-key mode-local-keymap "C" #'evil-tree-edit-clone) 681 | (define-key mode-local-keymap "u" #'evil-tree-edit-undo) 682 | (define-key mode-local-keymap "A" #'evil-tree-edit-goto-sig-parent) 683 | (define-key mode-local-keymap "?" #'evil-tree-edit-node-info) 684 | (define-key mode-local-keymap "v" #'evil-tree-edit-select-in-visual-state) 685 | (define-key mode-local-keymap "V" #'evil-tree-edit-toggle-tree-view) 686 | (define-key mode-local-keymap "zz" #'evil-scroll-line-to-center) 687 | ;; TODO: This could probably be better 688 | (setq-local evil-tree-state-map mode-local-keymap))) 689 | 690 | (unless evil-tree-edit-disable-nontree-bindings 691 | (evil-define-key 'normal evil-tree-edit-mode-map "Q" #'evil-tree-state) 692 | (evil-define-key 'insert evil-tree-edit-mode-map (kbd "") #'evil-tree-edit-normal-or-tree-state) 693 | (evil-define-key 'insert evil-tree-edit-mode-map (kbd "C-") #'evil-tree-edit-change-next-placeholder-from-insert)) 694 | 695 | (provide 'evil-tree-edit) 696 | ;;; evil-tree-edit.el ends here 697 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------