Skip to content

Add semantic highlighting for function parameter labels #983

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

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/SourceKitLSP/Swift/SemanticTokens.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ extension SyntaxClassification {
return (.comment, [])
case .docLineComment, .docBlockComment:
return (.comment, .documentation)
case .argumentLabel:
return (.function, [])
}
}
}
45 changes: 42 additions & 3 deletions Tests/SourceKitLSPTests/SemanticTokensTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ final class SemanticTokensTests: XCTestCase {
[
Token(line: 0, utf16index: 0, length: 4, kind: .keyword),
Token(line: 0, utf16index: 5, length: 1, kind: .identifier),
Token(line: 0, utf16index: 7, length: 1, kind: .identifier),
Token(line: 0, utf16index: 7, length: 1, kind: .function),
Token(line: 0, utf16index: 10, length: 3, kind: .struct, modifiers: .defaultLibrary),
Token(line: 0, utf16index: 17, length: 1, kind: .identifier),
Token(line: 0, utf16index: 20, length: 6, kind: .struct, modifiers: .defaultLibrary),
Expand Down Expand Up @@ -833,13 +833,52 @@ final class SemanticTokensTests: XCTestCase {
Token(line: 2, utf16index: 7, length: 8, kind: .identifier),
Token(line: 4, utf16index: 0, length: 4, kind: .keyword),
Token(line: 4, utf16index: 5, length: 1, kind: .identifier),
Token(line: 5, utf16index: 4, length: 1, kind: .identifier),
Token(line: 5, utf16index: 4, length: 1, kind: .function),
Token(line: 5, utf16index: 7, length: 7, kind: .actor),
Token(line: 6, utf16index: 4, length: 1, kind: .identifier),
Token(line: 6, utf16index: 4, length: 1, kind: .function),
Token(line: 6, utf16index: 7, length: 8, kind: .struct),
]
)
}

func testArgumentLabels() async throws {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a first + second name test at all? Is the second name a parameter or an identifier?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I'd expect one here too since the sourcekitd response could add more information if it wanted to (eg. all params + references could be "parameters").

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test case

let text = """
func foo(arg: Int) {}
foo(arg: 1)
"""

let tokens = try await openAndPerformSemanticTokensRequest(text: text)
XCTAssertEqual(
tokens,
[
Token(line: 0, utf16index: 0, length: 4, kind: .keyword),
Token(line: 0, utf16index: 5, length: 3, kind: .identifier),
Token(line: 0, utf16index: 9, length: 3, kind: .function),
Token(line: 0, utf16index: 14, length: 3, kind: .struct, modifiers: .defaultLibrary),
Token(line: 1, utf16index: 0, length: 3, kind: .function),
Token(line: 1, utf16index: 4, length: 3, kind: .function),
Token(line: 1, utf16index: 9, length: 1, kind: .number),
]
)
}

func testFunctionDeclarationWithFirstAndSecondName() async throws {
let text = """
func foo(arg internalName: Int) {}
"""

let tokens = try await openAndPerformSemanticTokensRequest(text: text)
XCTAssertEqual(
tokens,
[
Token(line: 0, utf16index: 0, length: 4, kind: .keyword),
Token(line: 0, utf16index: 5, length: 3, kind: .identifier),
Token(line: 0, utf16index: 9, length: 3, kind: .function),
Token(line: 0, utf16index: 13, length: 12, kind: .identifier),
Token(line: 0, utf16index: 27, length: 3, kind: .struct, modifiers: .defaultLibrary),
]
)
}
}

extension Token {
Expand Down