tree-sitter / tree-sitter/tree-sitter-cpp
bug: Emac's narrow-to-defun narrows to the next function when "#if defined" present
Nobody has claimed this yet.
- 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:
- it's not there
- it's replaced with "#ifdef(A)"
- I try to narrow on the line above "#if defined(A)"
- I use c++-mode instead of c++-ts-mode
- I use c-ts-mode instead of c++-ts-mode
then everything works as expected.
Additional information:
- tree-sitter-cpp 0.23.4
- Fedora 44
Steps To Reproduce/Bad Parse Tree
Steps to reproduce the issue:
- Open Emacs
- 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() {}
- Press M-x and then choose c++-ts-mode
- Place the cursor on the line with "int narrowingOnThisLineNarrowsTheNextFunction"
- 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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