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

Add symbolName property for some syntax nodes #2583

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Matejkob
Copy link
Contributor

@Matejkob Matejkob commented Apr 2, 2024

FunctionDecl, InitializerDecl, SubscriptDecl, EnumCaseElement, and MacroDecl gained a new computed property: symbolName

The symbolName property provides a string representation of the declaration's name along with its parameter labels. For example, a function func greet(name: String) will have the symbol name greet(name:).

Resolves #2488

Copy link
Contributor Author

@Matejkob Matejkob left a comment

Choose a reason for hiding this comment

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

Which other nodes should have a symbolName property?

  1. enum, protocol, struct, class, actor?
  2. typealias?
  3. property - this one for sure.
  4. deinit ?
  5. macro ?
  6. operator ?

@ahoppen
Copy link
Member

ahoppen commented Apr 3, 2024

I think it only makes sense to offer it for nodes that have a FunctionParameterClauseSyntax or EnumCaseParameterClauseSyntax and thus need handling of argument labels. That is

  • FunctionDeclSyntax
  • InitializerDeclSyntax
  • MacroDeclSyntax
  • SubscriptDeclSyntax
  • EnumCaseDeclSyntax

@Matejkob Matejkob changed the title [WIP] Symbol name Add symbolName property for some syntax nodes Apr 8, 2024
@Matejkob Matejkob marked this pull request as ready for review April 8, 2024 19:43
@Matejkob Matejkob requested a review from bnbarham as a code owner April 8, 2024 19:43
@Matejkob
Copy link
Contributor Author

Matejkob commented Apr 8, 2024

Depending on: #2593

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thank you. Looks great, I’m just being nitpicky again.

Sources/SwiftSyntax/SymbolName.swift Outdated Show resolved Hide resolved
Sources/SwiftSyntax/SymbolName.swift Outdated Show resolved Hide resolved
Sources/SwiftSyntax/SymbolName.swift Outdated Show resolved Hide resolved
Sources/SwiftSyntax/SymbolName.swift Outdated Show resolved Hide resolved
Sources/SwiftSyntax/SymbolName.swift Outdated Show resolved Hide resolved
Sources/SwiftSyntax/SymbolName.swift Outdated Show resolved Hide resolved
Tests/SwiftSyntaxTest/SymbolNameTests.swift Outdated Show resolved Hide resolved
Tests/SwiftSyntaxTest/SymbolNameTests.swift Outdated Show resolved Hide resolved
Tests/SwiftSyntaxTest/SymbolNameTests.swift Outdated Show resolved Hide resolved
Tests/SwiftSyntaxTest/SymbolNameTests.swift Outdated Show resolved Hide resolved
@Matejkob
Copy link
Contributor Author

Matejkob commented Apr 9, 2024

Thanks for the review Alex 🫶

`FunctionDecl`, `InitializerDecl`, `SubscriptDecl`, `EnumCaseElement`, and `MacroDecl` gained a new computed property: `symbolName`

The `symbolName` property provides a string representation of the declaration's name along with its parameter labels. For example, a function `func greet(name: String)` will have the symbol name `greet(name:)`.
@ktoso
Copy link
Contributor

ktoso commented Apr 16, 2024

@swift-ci please smoke test

Copy link
Contributor

@hamishknight hamishknight left a comment

Choose a reason for hiding this comment

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

A small nit over the naming in the documentation, but otherwise this LGTM

@@ -2,6 +2,11 @@

## New APIs

- `FunctionDecl`, `InitializerDecl`, `SubscriptDecl`, `EnumCaseElement`, and `MacroDecl` gained a new computed property: `symbolName`
- Description: The `symbolName` property provides a string representation of the declaration's name along with its parameter labels. For example, a function `func greet(name: String)` will have the symbol name `greet(name:)`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: These are called argument labels in TSPL, and we have e.g SyntaxClassification.argumentLabel, I think we probably ought to be consistent.

Suggested change
- Description: The `symbolName` property provides a string representation of the declaration's name along with its parameter labels. For example, a function `func greet(name: String)` will have the symbol name `greet(name:)`.
- Description: The `symbolName` property provides a string representation of the declaration's name along with its argument labels. For example, a function `func greet(name: String)` will have the symbol name `greet(name:)`.

extension FunctionDeclSyntax {
/// The symbol name of the function declaration.
///
/// The symbol name is a string representation of the function name along with its parameter labels.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// The symbol name is a string representation of the function name along with its parameter labels.
/// The symbol name is a string representation of the function name along with its argument labels.

extension InitializerDeclSyntax {
/// The symbol name of the initializer declaration.
///
/// The symbol name is a string representation of the initializer along with its parameter labels.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// The symbol name is a string representation of the initializer along with its parameter labels.
/// The symbol name is a string representation of the initializer along with its argument labels.

extension SubscriptDeclSyntax {
/// The symbol name of the subscript declaration.
///
/// The symbol name is a string representation of the subscript along with its parameter labels.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// The symbol name is a string representation of the subscript along with its parameter labels.
/// The symbol name is a string representation of the subscript along with its argument labels.

extension MacroDeclSyntax {
/// The symbol name of the macro declaration.
///
/// The symbol name is a string representation of the macro name along with its parameter labels.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// The symbol name is a string representation of the macro name along with its parameter labels.
/// The symbol name is a string representation of the macro name along with its argument labels.

}
}

/// Generates the symbol name by combining the base name and parameter labels.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Generates the symbol name by combining the base name and parameter labels.
/// Generates the symbol name by combining the base name and argument labels.

@rintaro
Copy link
Member

rintaro commented Apr 16, 2024

This resembles DeclName in C++ compiler.
I think we should also make it a struct, like:

struct SymbolBaseName {
  enum Kind { case normal, subscript, initializer, deinitializer }
  var asString: String {get}
  var kind: Kind {get}
  var isOperator: Bool {get}
}
struct SymbolName {  
  var asString: String {get}
  var baseName: SymbolBaseName {get}
  var argumentLabels: some Collection<String> {get}
}

WDYT?

@ahoppen
Copy link
Member

ahoppen commented Apr 17, 2024

That is a great idea, @rintaro.

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

Successfully merging this pull request may close these issues.

FunctionDecl and similar should have method to get name as "hello(name:surname:)"
6 participants