Comments (6)
Thanks for the snippet. I changed it a little bit, make it even more smarter (i guess).
Code:
;;;; Smart f-strings
;; https://github.com/ubolonton/emacs-tree-sitter/issues/52
(defun fk/python-f-string-ify (&rest _)
;; Does nothing if major-mode is not python or point is not on a string.
(when-let* ((python-mode-p (eq major-mode 'python-mode))
(str (tree-sitter-node-at-point 'string))
(text (ts-node-text str)))
(let ((is-f-string (string-match-p "^[bru]*f+[bru]*\\(\"\\|'\\)" text))
(should-f-string (and (s-contains-p "{" text)
(s-contains-p "}" text))))
(if should-f-string
(unless is-f-string
(save-excursion
(goto-char (ts-node-start-position str))
(insert "f")))
(when is-f-string
(save-excursion
(goto-char (ts-node-start-position str))
(when (char-equal (char-after) ?f)
(delete-char 1))))))))
;; When not using wrap-region or anything that changes "{" keybinding
;; (define-key python-mode-map (kbd "{") (lambda ()
;; (interactive)
;; (call-interactively 'self-insert-command)
;; (fk/python-f-string-ify)))
(defadvice wrap-region-trigger (after smart-f-string activate)
(fk/python-f-string-ify))
(defadvice delete-char (after smart-f-string activate)
(fk/python-f-string-ify))
(defadvice delete-active-region (after smart-f-string activate)
(fk/python-f-string-ify))
from elisp-tree-sitter.
Forgot to add ‘f’ to your f-string? PyCharm now auto-enables f-strings when the user adds curly braces within a string statement
We need a function that makes a string into an f-string, if it's not one already:
(defun ublt/python-f-string-ify (&rest _)
;; Does nothing if point is not on a string.
(when-let* ((str (tree-sitter-node-at-point 'string))
(text (ts-node-text str)))
;; This shouldn't be necessary when/if the Python grammar
;; gets a separate node type for "prefixed strings".
(let ((is-f-string (string-match-p "^[bru]*f+[bru]*\\(\"\\|'\\)" text)))
(unless is-f-string
(save-excursion
(goto-char (ts-node-start-position str))
(insert "f"))))))
If you already use some "smart-bracket" package, this could be added as an advice on the function that handles {
. For example:
(define-advice paredit-open-curly (:after (&rest _))
(when (and (eq 'python major-mode)
tree-sitter-mode)
(ublt/python-f-string-ify)))
Otherwise, you can write a custom "smart-curly" command and bind it to {
in python-mode-map
.
(defun ublt/python-smart-insert ()
(interactive)
(call-interactively #'self-insert-command)
(ublt/python-f-string-ify))
text-function seems extraneous, is there a better way to just check if the node i'm
on is a string, returning t or nil?
In 0.10.0, tree-sitter-node-at-point
takes an optional node type. There is now some documentation on the core APIs, including node inspection functions.
(eq 'string (ts-node-type node))
is there a tree-sitter way of replacing a node?
Tree-sitter only deals with parsing. It doesn't do source code transformation/generation. You should use Emacs's normal text editing functions.
from elisp-tree-sitter.
Love this idea! I always forget the f
and this seems like an awesome use-case for tree-sitter :-)
from elisp-tree-sitter.
I believe this is a use case where you relay on this package, but at least in my opinion it is out of scope of the project to include such functionality
from elisp-tree-sitter.
I believe this is a use case where you relay on this package, but at least in my opinion it is out of scope of the project to include such functionality
Definitely. I probably wasn't clear, I'm not proposing adding this to this repository, I was asking for advice on the best way to do this using tree-sitter.
from elisp-tree-sitter.
OH, okay
maybe @ubolonton can point you into the right direction as ;; todo: is there a tree-sitter way of replacing a node?
is hard for me to answer but since the tree is made out of the code, the only way I know is to actually change the text in the buffer.
from elisp-tree-sitter.
Related Issues (20)
- Doesn't highlight Rust documentation code examples
- Swift support, revisited. HOT 1
- wrong ELF class: ELFCLASS64 on RPI4 HOT 1
- `Unsupported Operating System: OpenBSD` HOT 7
- bin/build not work on linux because builtin cd will output the folder name. HOT 1
- tree-sitter languages download of windows binaries failed with error
- tsc-lang-abi-too-new when installing new language HOT 11
- Crash when large file contains an ERROR node
- Invalid version syntax: ‘[email protected] HOT 1
- Colors look wrong in haskell mode. HOT 3
- `tree-sitter-debug-mode` is broken
- Crash when loading one particular Markdown file HOT 7
- Reporting Vulnerability
- (ts-fold-indicators-mode) not working when running emacs from terminal
- How do I add a new grammer to this mode? HOT 2
- why the submodule langs pointed to an old commit?
- tree-sitter-hl-mode doesn't honor the :extend font face in single line comments HOT 1
- request to rebuild tsc-dyn.so with older Linux OS
- Provide `aarch64` build of `tsc-dyn.so` for AArch64 Linux
- Repetitions and Predicate Problems
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elisp-tree-sitter.