Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

csharp: context shows attributes instead of class/method names, obscuring context detail #160

Closed
cptchuckles opened this issue Sep 4, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@cptchuckles
Copy link

cptchuckles commented Sep 4, 2022

In an example C# source file, i have:

/*    using statements     */

[ApiController]
[Route("api/")]
public class AuthController : ControllerBase
{
    /*    constructor etc.    */

    [AllowAnonymous]
    [HttpPost("Register")]
    public async Task<ActionResult<string>> Register([FromBody] UserRegisterDto request)
    {
        if (await _context.Users.AnyAsync(u => u.Email.ToLower() == request.Email.ToLower()))
            return BadRequest($"User with email {request.Email} already exists");
        if (await _context.Users.AnyAsync(u => u.Username.ToLower() == request.Username.ToLower()))
            return BadRequest($"Username {request.Username} already exists");

        var user = new User(request.Username, request.Email, request.Password);

But observe how the context looks like:
treesitter context shows attributes

I would prefer if it showed like:

public class AuthController : ControllerBase
    public async Task<ActionResult<string>> Register([FromBody] UserREgisterDto request)
------------------------------------------------------------------------------------------------------------

I assume I should be able to configure it to ignore attributes in cs filetype buffers, but the readme doesn't go into enough detail for me to work out how to exclude things like that.

@cptchuckles
Copy link
Author

I looked in the code and found a reference to userOptions.exclude_patterns, so i tried the following to no avail:

require('treesitter-context').setup({
    exclude_patterns = {
        cs = {
            'attribute_list',
            'attribute',
        },
    },
})

@cptchuckles cptchuckles changed the title csharp: context shows attributes instead of class/method declarations, obscuring context detail csharp: context shows attributes instead of class/method names, obscuring context detail Sep 4, 2022
@cptchuckles
Copy link
Author

It's probably happening because treesitter nests the attributes under the class_declaration, and the class_declaration starts on the line with the first attribute. The playground shows that the class_declaration starts on line 11, but the name: identifier for the actual class is on line 13.

  class_declaration [11, 0] - [108, 1]
    attribute_list [11, 0] - [11, 15]
      attribute [11, 1] - [11, 14]
        name: identifier [11, 1] - [11, 14]
    attribute_list [12, 0] - [12, 15]
      attribute [12, 1] - [12, 14]
        name: identifier [12, 1] - [12, 6]
        attribute_argument_list [12, 6] - [12, 14]
          attribute_argument [12, 7] - [12, 13]
            string_literal [12, 7] - [12, 13]
    modifier [13, 0] - [13, 6]
    name: identifier [13, 13] - [13, 27]
    bases: base_list [13, 28] - [13, 44]
      identifier [13, 30] - [13, 44]
    body: declaration_list [14, 0] - [108, 1]

so I'm doubtful that this is going to be trivial now, unless there's some way I can exclude class_declaration yet include class_declaration.name, or something. I've tried a few ways, but if i exclude class_declaration then I get nothing.

@lewis6991 lewis6991 added the bug Something isn't working label Nov 22, 2022
@mg979 mg979 mentioned this issue Jan 10, 2023
@saecki
Copy link
Contributor

saecki commented Jan 12, 2023

There already is the skip_leading_types table, which does exactly what is needed here. In my fork I just restructured the way things are declared. Adding another entry should to the trick.

@lewis6991
Copy link
Member

#198 C sharp is no longer supported and the information in this thread is no longer useful. Will happily accept a PR that adds a query for C sharp.

@l1n3n01z
Copy link

l1n3n01z commented Dec 2, 2023

#198 C sharp is no longer supported and the information in this thread is no longer useful. Will happily accept a PR that adds a query for C sharp.
For anyone reading this, a csharp query file was added a month later in 68eaeb8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants