ProvidesSourceCodeLocation.swift 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Sources/SwiftProtobufPluginLibrary/ProvidesSourceCodeLocation.swift - SourceCodeInfo.Location provider
  2. //
  3. // Copyright (c) 2014 - 2017 Apple Inc. and the project authors
  4. // Licensed under Apache License v2.0 with Runtime Library Exception
  5. //
  6. // See LICENSE.txt for license information:
  7. // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
  8. //
  9. // -----------------------------------------------------------------------------
  10. import Foundation
  11. import SwiftProtobuf
  12. public protocol ProvidesSourceCodeLocation {
  13. var sourceCodeInfoLocation: Google_Protobuf_SourceCodeInfo.Location? { get }
  14. }
  15. // Default implementation for things that support ProvidesLocationPath.
  16. extension ProvidesSourceCodeLocation where Self: ProvidesLocationPath {
  17. public var sourceCodeInfoLocation: Google_Protobuf_SourceCodeInfo.Location? {
  18. var path = IndexPath()
  19. getLocationPath(path: &path)
  20. return file.sourceCodeInfoLocation(path: path)
  21. }
  22. }
  23. // Helper to get source comments out of ProvidesSourceCodeLocation
  24. extension ProvidesSourceCodeLocation {
  25. public func protoSourceComments(commentPrefix: String = "///",
  26. leadingDetachedPrefix: String? = nil) -> String {
  27. if let loc = sourceCodeInfoLocation {
  28. return loc.asSourceComment(commentPrefix: commentPrefix,
  29. leadingDetachedPrefix: leadingDetachedPrefix)
  30. }
  31. return String()
  32. }
  33. }