Skip to content

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

Description

@wojnilowicz

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() {}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions