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

Get Buffer(ID) by file name #815

Open
suzizecat opened this issue Aug 24, 2023 · 1 comment
Open

Get Buffer(ID) by file name #815

suzizecat opened this issue Aug 24, 2023 · 1 comment

Comments

@suzizecat
Copy link
Contributor

Is your feature request related to a problem? Please describe.
As of today (except if I missed an API function), there is no way to easily get the BufferID of an already read file.

An application example is in the case of a LSP server. If I want to lookup a symbol by position, I will the LSP will provide a file path and a position in this file. In order to fetch information in the AST, I will need to look for the SyntaxNode related to the target position.

In order to do so, It will be relevant to use a slang::SourceLocation to hold the target location, but I then need the BufferID linked to my file path.

Describe the solution you'd like
I would like a function BufferID getBufferByPath(std::filesystem::path fp) (or equivalent) in the source manager class.

Describe alternatives you've considered
I came up with a solution that iterates through all buffers of the source manager and search for the right filename.

const slang::SourceManager* sm = compilation->getSourceManager();
BufferID target_id;
for (BufferID id : sm->getAllBuffers())
{
    if (sm->getFullPath(id).filename() == "my_file.sv")
    {
        target_id = id;
        break;
    }
}

int target_position = 1234;
slang::SourceLocation sl(target_id, target_position);
@MikePopoloski
Copy link
Owner

This seems reasonable. I can add this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants