GithubHelp home page GithubHelp logo

Comments (4)

ubolonton avatar ubolonton commented on April 26, 2024 3

There's a macro in 0.5.0 for this: tree-sitter-save-excursion. It keeps the cursor on the same node, and on the same vertical position.

The following works for me in Python (provided that tree-sitter-mode is turned on in the buffer), with Black formatter:

(require 'tree-sitter-extras)

(define-advice format-all-buffer (:around (f &rest args) tree-sitter-save-excursion)
  (if tree-sitter-mode
      (tree-sitter-save-excursion
        (apply f args))
    (apply f args)))

I think it's better to combine this with the diff-based approach mentioned in the issue you linked, though.

from elisp-tree-sitter.

ubolonton avatar ubolonton commented on April 26, 2024

This is a nice idea! I think it can be done as followed:

  • Before formatting, store the "path" leading from the root node to the current node.
  • After formatting, use that path to search for the corresponding node in the new syntax tree.

from elisp-tree-sitter.

leungbk avatar leungbk commented on April 26, 2024

@raxod502, who wrote apheleia.el, is knowledgeable about this. If we use Needleman-Wunsch as with apheleia, it's not clear to me how we'd combine this with a tree-sitter-based approach.

from elisp-tree-sitter.

raxod502 avatar raxod502 commented on April 26, 2024

Yes, there are basically two good options for handling this issue:

  • The smart approach, which is to have an understanding of the AST and the cursor's position within a specific node
  • The dumb approach, which is to use a string alignment or regexp-based algorithm

If you've got the resources available to do the first approach, it'll probably work better. The reason I think the second approach is good is because the first approach requires a lot of work for each language / formatter, whereas the second approach works for all languages and formatters.

The disadvantages of the second approach are:

  • it needs some heuristics to optimize for large diff regions, because the dynamic programming algorithm scales quadratically (radian-software/apheleia#4)
  • it needs some heuristics to nudge the cursor to the most appropriate position sometimes, because the dynamic programming algorithm can select one of several different string alignments which score equally well but which do not seem equally appropriate to a user (radian-software/apheleia#2)

from elisp-tree-sitter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.