Skip to content

Implement editor placeholder parsing logic in SwiftSyntax #2878

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
Oct 11, 2024
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
136 changes: 30 additions & 106 deletions Sources/SwiftRefactor/ExpandEditorPlaceholder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
#if swift(>=6)
import SwiftBasicFormat
import SwiftParser
public import SwiftSyntax
@_spi(RawSyntax) public import SwiftSyntax
import SwiftSyntaxBuilder
#else
import SwiftBasicFormat
import SwiftParser
import SwiftSyntax
@_spi(RawSyntax) import SwiftSyntax
import SwiftSyntaxBuilder
#endif

Expand Down Expand Up @@ -88,31 +88,26 @@ struct ExpandSingleEditorPlaceholder: EditRefactoringProvider {
}

static func textRefactor(syntax token: TokenSyntax, in context: Context = Context()) -> [SourceEdit] {
guard let placeholder = EditorPlaceholderData(token: token) else {
guard let placeholder = EditorPlaceholderExpansionData(token: token) else {
return []
}

let expanded: String
switch placeholder {
case let .basic(text):
expanded = String(text)
case let .typed(text, type):
if let functionType = type.as(FunctionTypeSyntax.self) {
let basicFormat = BasicFormat(
indentationWidth: context.indentationWidth,
initialIndentation: context.initialIndentation
)
var formattedExpansion = functionType.closureExpansion.formatted(using: basicFormat).description
// Strip the initial indentation from the placeholder itself. We only introduced the initial indentation to
// format consecutive lines. We don't want it at the front of the initial line because it replaces an expression
// that might be in the middle of a line.
if formattedExpansion.hasPrefix(context.initialIndentation.description) {
formattedExpansion = String(formattedExpansion.dropFirst(context.initialIndentation.description.count))
}
expanded = formattedExpansion
} else {
expanded = String(text)
if let functionType = placeholder.typeForExpansion?.as(FunctionTypeSyntax.self) {
let basicFormat = BasicFormat(
indentationWidth: context.indentationWidth,
initialIndentation: context.initialIndentation
)
var formattedExpansion = functionType.closureExpansion.formatted(using: basicFormat).description
// Strip the initial indentation from the placeholder itself. We only introduced the initial indentation to
// format consecutive lines. We don't want it at the front of the initial line because it replaces an expression
// that might be in the middle of a line.
if formattedExpansion.hasPrefix(context.initialIndentation.description) {
formattedExpansion = String(formattedExpansion.dropFirst(context.initialIndentation.description.count))
}
expanded = formattedExpansion
} else {
expanded = placeholder.displayText
}

return [
Expand Down Expand Up @@ -325,8 +320,8 @@ extension FunctionCallExprSyntax {
for arg in arguments.reversed() {
guard let expr = arg.expression.as(DeclReferenceExprSyntax.self),
expr.baseName.isEditorPlaceholder,
let data = EditorPlaceholderData(token: expr.baseName),
case let .typed(_, type) = data,
let data = EditorPlaceholderExpansionData(token: expr.baseName),
let type = data.typeForExpansion,
type.is(FunctionTypeSyntax.self)
else {
break
Expand Down Expand Up @@ -371,87 +366,27 @@ extension FunctionCallExprSyntax {
}
}

/// Placeholder text must start with '<#' and end with
/// '#>'. Placeholders can be one of the following formats:
/// ```
/// 'T##' display-string '##' type-string ('##' type-for-expansion-string)?
/// 'T##' display-and-type-string
/// display-string
/// ```
///
/// NOTE: It is required that '##' is not a valid substring of display-string
/// or type-string. If this ends up not the case for some reason, we can consider
/// adding escaping for '##'.
@_spi(SourceKitLSP)
public enum EditorPlaceholderData {
case basic(text: Substring)
case typed(text: Substring, type: TypeSyntax)
private struct EditorPlaceholderExpansionData {
let displayText: String
let typeForExpansion: TypeSyntax?

init?(token: TokenSyntax) {
self.init(text: token.text)
}

@_spi(SourceKitLSP)
public init?(text: String) {
guard isPlaceholder(text) else {
guard let rawData = token.rawEditorPlaceHolderData else {
return nil
}
var text = text.dropFirst(2).dropLast(2)

if !text.hasPrefix("T##") {
// No type information
self = .basic(text: text)
return
}

// Drop 'T##'
text = text.dropFirst(3)

var typeText: Substring
(text, typeText) = split(text, separatedBy: "##")
if typeText.isEmpty {
// Only type information present
self.init(typeText: text)
return
}

// Have type text, see if we also have expansion text

let expansionText: Substring
(typeText, expansionText) = split(typeText, separatedBy: "##")
if expansionText.isEmpty {
if typeText.isEmpty {
// No type information
self = .basic(text: text)
} else {
// Only have type text, use it for the placeholder expansion
self.init(typeText: typeText)
}

return
}

// Have expansion type text, use it for the placeholder expansion
self.init(typeText: expansionText)
}

init(typeText: Substring) {
var parser = Parser(String(typeText))

let type: TypeSyntax = TypeSyntax.parse(from: &parser)
if type.hasError {
self = .basic(text: typeText)
if let typeText = rawData.typeForExpansionText, !typeText.isEmpty {
self.displayText = String(syntaxText: typeText)
var parser = Parser(UnsafeBufferPointer(start: typeText.baseAddress, count: typeText.count))
let type: TypeSyntax = TypeSyntax.parse(from: &parser)
self.typeForExpansion = type.hasError ? nil : type
} else {
self = .typed(text: typeText, type: type)
self.displayText = String(syntaxText: rawData.displayText)
self.typeForExpansion = nil
}
}
}

@_spi(Testing)
public func isPlaceholder(_ str: String) -> Bool {
return str.hasPrefix(placeholderStart) && str.hasSuffix(placeholderEnd)
}

@_spi(Testing)
public func wrapInPlaceholder(_ str: String) -> String {
return placeholderStart + str + placeholderEnd
Expand All @@ -462,16 +397,5 @@ public func wrapInTypePlaceholder(_ str: String, type: String) -> String {
return wrapInPlaceholder("T##" + str + "##" + type)
}

/// Split the given string into two components on the first instance of
/// `separatedBy`. The second element is empty if `separatedBy` is missing
/// from the initial string.
fileprivate func split(_ text: Substring, separatedBy separator: String) -> (Substring, Substring) {
var rest = text
while !rest.isEmpty && !rest.hasPrefix(separator) {
rest = rest.dropFirst()
}
return (text.dropLast(rest.count), rest.dropFirst(2))
}

fileprivate let placeholderStart: String = "<#"
fileprivate let placeholderEnd: String = "#>"
1 change: 1 addition & 0 deletions Sources/SwiftSyntax/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_swift_syntax_library(SwiftSyntax
CommonAncestor.swift
Convenience.swift
CustomTraits.swift
EditorPlaceholder.swift
Identifier.swift
MemoryLayout.swift
MissingNodeInitializers.swift
Expand Down
109 changes: 109 additions & 0 deletions Sources/SwiftSyntax/EditorPlaceholder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

extension TokenSyntax {
/// Whether the token text is an editor placeholder or not.
public var isEditorPlaceholder: Bool {
rawTokenKind == .identifier && isPlaceholder(syntaxText: rawText)
}

@_spi(RawSyntax)
public var rawEditorPlaceHolderData: RawEditorPlaceholderData? {
RawEditorPlaceholderData(syntaxText: rawText)
}
}

/// Placeholder text must start with '<#' and end with
/// '#>'. Placeholders can be one of the following formats:
///
/// Typed:
/// ```
/// 'T##' display-string '##' type-string ('##' type-for-expansion-string)?
/// 'T##' display-and-type-string
/// ```
///
/// Basic:
/// ```
/// display-string
/// ```
///
/// NOTE: It is required that '##' is not a valid substring of display-string
/// or type-string. If this ends up not the case for some reason, we can consider
/// adding escaping for '##'.
@_spi(RawSyntax)
public struct RawEditorPlaceholderData {
/// The part that is displayed in the editor.
public var displayText: SyntaxText

/// The type text for the placeholder.
/// It can be same as `displayText`. `nil` if the placeholder is not "Typed".
public var typeText: SyntaxText?

/// The type text to be considered for placeholder expansion.
/// It can be same as `typeText`. `nil` if the placeholder is not "Typed".
public var typeForExpansionText: SyntaxText?

public init(displayText: SyntaxText, typeText: SyntaxText? = nil, typeForExpansionText: SyntaxText? = nil) {
self.displayText = displayText
self.typeText = typeText
self.typeForExpansionText = typeForExpansionText
}

public init?(syntaxText: SyntaxText) {
guard isPlaceholder(syntaxText: syntaxText) else {
return nil
}
self = parseEditorPlaceholder(syntaxText: syntaxText)
}
}

private let placeholderStart: SyntaxText = "<#"
private let placeholderEnd: SyntaxText = "#>"

private func isPlaceholder(syntaxText: SyntaxText) -> Bool {
syntaxText.hasPrefix(placeholderStart) && syntaxText.hasSuffix(placeholderEnd)
}

private func parseEditorPlaceholder(syntaxText: SyntaxText) -> RawEditorPlaceholderData {
assert(isPlaceholder(syntaxText: syntaxText))
var text = SyntaxText(rebasing: syntaxText.dropFirst(2).dropLast(2))

if !text.hasPrefix("T##") {
// Basic, no type texts.
return RawEditorPlaceholderData(displayText: text)
}

// Typed, drop 'T##'
text = SyntaxText(rebasing: text.dropFirst(3))

guard let sep = text.firstRange(of: "##") else {
return RawEditorPlaceholderData(displayText: text, typeText: text, typeForExpansionText: text)
}
let displayText = SyntaxText(rebasing: text[..<sep.lowerBound])
text = SyntaxText(rebasing: text[sep.upperBound...])

guard !text.isEmpty else {
return RawEditorPlaceholderData(displayText: displayText, typeText: displayText, typeForExpansionText: displayText)
}

guard let sep = text.firstRange(of: "##") else {
return RawEditorPlaceholderData(displayText: displayText, typeText: text, typeForExpansionText: text)
}
let typeText = SyntaxText(rebasing: text[..<sep.lowerBound])
text = SyntaxText(rebasing: text[sep.upperBound...])

guard !text.isEmpty else {
return RawEditorPlaceholderData(displayText: displayText, typeText: typeText, typeForExpansionText: typeText)
}

return RawEditorPlaceholderData(displayText: displayText, typeText: typeText, typeForExpansionText: text)
}
10 changes: 0 additions & 10 deletions Sources/SwiftSyntax/TokenSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,6 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
return raw.totalLength
}

/// Whether the token text is an editor placeholder or not.
public var isEditorPlaceholder: Bool {
switch self.tokenKind {
case .identifier(let text):
return text.hasPrefix("<#") && text.hasSuffix("#>")
default:
return false
}
}

/// An identifier created from `self`.
public var identifier: Identifier? {
switch self.tokenKind {
Expand Down