Skip to content

trimming trivia nodes corrupts source ranges #2687

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

Closed
tayloraswift opened this issue Jun 17, 2024 · 1 comment
Closed

trimming trivia nodes corrupts source ranges #2687

tayloraswift opened this issue Jun 17, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@tayloraswift
Copy link
Member

Description

using the trimmed family of SyntaxProtocol methods corrupts the ByteSourceRanges when obtaining syntax classifications.

i was able to reproduce this on 510.0.2, and on 600.0.0-prerelease-2024-06-12.

Steps to Reproduce

import SwiftIDEUtils
import SwiftParser
import SwiftSyntax

let text:String = """
@_documentation(metadata: blah)
public mutating
func f() {}
"""
let utf8:[UInt8] = .init(text.utf8)

var parser:Parser = utf8.withUnsafeBufferPointer { .init($0) }
let decl:DeclSyntax = .parse(from: &parser)

let function:FunctionDeclSyntax = decl.as(FunctionDeclSyntax.self)!

for span:SyntaxClassifiedRange in function.modifiers.trimmed.classifications
{
    let range:Range<Int> = span.range.offset ..< span.range.offset + span.range.length
    print(String.init(decoding: utf8[range], as: Unicode.UTF8.self))
}

publi
c
 mutatin
@ahoppen
Copy link
Member

ahoppen commented Jun 17, 2024

This behaves as intended. Running trimmed modifies the syntax tree to remove the leading and trailing trivia of the first and last token, respectively. The trimmed node should probably be detached from the original tree, which would make the behavior more obvious.

I would suggest that you do something like the following

let trimmed = function.modifiers.detached.trimmed
let trimmedBytes = trimmed.syntaxTextBytes
for span in trimmed.classifications {
    let range = span.range.offset ..< span.range.offset + span.range.length
    print(String.init(decoding: syntaxTextBytes[range], as: Unicode.UTF8.self))
}

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

2 participants