Function: claude-code-ide-mcp-treesit-info

claude-code-ide-mcp-treesit-info is a natively compiled function defined in claude-code-ide-emacs-tools.el.

Signature

(claude-code-ide-mcp-treesit-info FILE-PATH &optional LINE COLUMN WHOLE_FILE INCLUDE_ANCESTORS INCLUDE_CHILDREN)

Documentation

Get tree-sitter parse tree information for FILE-PATH.

Optional LINE and COLUMN specify the position (1-based line, 0-based column). If WHOLE_FILE is non-nil, show the entire file's syntax tree. If neither position is specified, defaults to current cursor position (point). If INCLUDE_ANCESTORS is non-nil, include parent node hierarchy. If INCLUDE_CHILDREN is non-nil, include child nodes.

Source Code

;; Defined in /nix/store/yjvbhwl7p6zrbw9xlnni7y4hra8sa3pl-emacs-packages-deps/share/emacs/site-lisp/claude-code-ide-emacs-tools.el
(defun claude-code-ide-mcp-treesit-info (file-path &optional line column whole_file include_ancestors include_children)
  "Get tree-sitter parse tree information for FILE-PATH.
Optional LINE and COLUMN specify the position (1-based line, 0-based column).
If WHOLE_FILE is non-nil, show the entire file's syntax tree.
If neither position is specified, defaults to current cursor position (point).
If INCLUDE_ANCESTORS is non-nil, include parent node hierarchy.
If INCLUDE_CHILDREN is non-nil, include child nodes."
  (if (not file-path)
      (error "file_path parameter is required")
    (claude-code-ide-mcp-server-with-session-context nil
      (condition-case err
          (if (not (treesit-available-p))
              "Tree-sitter is not available in this Emacs build"
            (let ((target-buffer (or (find-buffer-visiting file-path)
                                     (find-file-noselect file-path))))
              (with-current-buffer target-buffer
                (let* ((parsers (treesit-parser-list))
                       (parser (car parsers)))
                  (if (not parser)
                      (format "No tree-sitter parser available for %s" file-path)
                    (let* ((root-node (treesit-parser-root-node parser))
                           ;; Determine position from line/column or use current point
                           (pos (cond (whole_file nil)
                                      (line (claude-code-ide-mcp--line-column-to-point
                                             line (or column 0)))
                                      ;; Use current point in the target buffer
                                      (t (point))))
                           (node (if whole_file
                                     root-node
                                   (treesit-node-at pos parser)))
                           (results '()))
                      (if (not node)
                          "No tree-sitter node found"
                        ;; For full tree, use a different display function
                        (if whole_file
                            (claude-code-ide-mcp-treesit--format-tree root-node 0 20)
                          ;; Basic node information for specific position
                          (push (format "Node Type: %s" (treesit-node-type node)) results)
                          (push (format "Range: %d-%d"
                                        (treesit-node-start node)
                                        (treesit-node-end node)) results)
                          (push (format "Text: %s"
                                        (truncate-string-to-width
                                         (treesit-node-text node t)
                                         80 nil nil "...")) results)

                          ;; Check if node is named
                          (when (treesit-node-check node 'named)
                            (push "Named: yes" results))

                          ;; Field name if available
                          (let ((field-name (treesit-node-field-name node)))
                            (when field-name
                              (push (format "Field: %s" field-name) results)))

                          ;; Include ancestors if requested
                          (when include_ancestors
                            (push "\nAncestors:" results)
                            (let ((parent (treesit-node-parent node))
                                  (level 1))
                              (while (and parent (< level 10))
                                (push (format "  %s[%d] %s (%d-%d)"
                                              (make-string level ?-)
                                              level
                                              (treesit-node-type parent)
                                              (treesit-node-start parent)
                                              (treesit-node-end parent))
                                      results)
                                (setq parent (treesit-node-parent parent))
                                (cl-incf level))))

                          ;; Include children if requested
                          (when include_children
                            (push "\nChildren:" results)
                            (let ((child-count (treesit-node-child-count node))
                                  (i 0))
                              (if (= child-count 0)
                                  (push "  (no children)" results)
                                (while (< i (min child-count 20))
                                  (let ((child (treesit-node-child node i)))
                                    (when child
                                      (push (format "  [%d] %s%s (%d-%d)"
                                                    i
                                                    (treesit-node-type child)
                                                    (if (treesit-node-check child 'named)
                                                        " (named)" "")
                                                    (treesit-node-start child)
                                                    (treesit-node-end child))
                                            results)))
                                  (cl-incf i))
                                (when (> child-count 20)
                                  (push (format "  ... and %d more children"
                                                (- child-count 20))
                                        results)))))

                          ;; Return formatted results
                          (string-join (nreverse results) "\n")))))))))
        (error
         (format "Error getting tree-sitter info for %s: %s"
                 file-path (error-message-string err)))))))