tree-sitter / tree-sitter/tree-sitter-cpp

bug: Emac's narrow-to-defun narrows to the next function when "#if defined" present

Open
#359 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
453
Forks
185
PR merge metrics
No merged PRs in 30d

Description

Did you check existing issues?
  • I have read all the tree-sitter docs if it relates to using the parser
  • I have searched the existing issues of tree-sitter-cpp
Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

No response

Describe the bug

I'm using Emacs 30.2 and its narrow-to-defun function, which acts faulty i.e. it narrows the next function instead of the function that I'm currently in.

I tracked this issue down to the presence of "#if defined(A)" as shown in the example code below. If:

  1. it's not there
  2. it's replaced with "#ifdef(A)"
  3. I try to narrow on the line above "#if defined(A)"
  4. I use c++-mode instead of c++-ts-mode
  5. I use c-ts-mode instead of c++-ts-mode
    then everything works as expected.

Additional information:

  1. tree-sitter-cpp 0.23.4
  2. Fedora 44
Steps To Reproduce/Bad Parse Tree

Steps to reproduce the issue:

  1. Open Emacs
  2. Load the file below
void functionThatShouldBeNarrowed() {
  int narrowingOnThisLineNarrowsThisFunction; // Press "C-x n d" on this line.
#if defined(A) // Changing this line to "#ifdef(A)" makes the bug disappear.
#endif
  int narrowingOnThisLineNarrowsTheNextFunction; // Press "C-x n d" on this line.
}

void functionThatGetsNarrowed() {}
  1. Press M-x and then choose c++-ts-mode
  2. Place the cursor on the line with "int narrowingOnThisLineNarrowsTheNextFunction"
  3. Press "C-x n d" to launch narrow-to-defun

Behavior:
The function "void functionThatGetsNarrowed()" gets narrowed.

Expected Behavior/Parse Tree

Expected behavior:
The function "void functionThatShouldBeNarrowed()" should be narrowed.

Repro
// Example code that causes the issue
++
void functionThatShouldBeNarrowed() {
  int narrowingOnThisLineNarrowsThisFunction; // Press "C-x n d" on this line.
#if defined(A) // Changing this line to "#ifdef(A)" makes the bug disappear.
#endif
  int narrowingOnThisLineNarrowsTheNextFunction; // Press "C-x n d" on this line.
}

void functionThatGetsNarrowed() {}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal C++ reproduction in the issue and inspect the parse tree produced for the #if defined(A) block in c++-ts-mode. Compare narrow-to-defun behavior with the listed working variants; done when the cursor inside the first function narrows that function rather than the next one.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.