ProvidesSourceCodeLocation.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. /// Protocol that all the Descriptors conform to for original .proto file
  13. /// location lookup.
  14. public protocol ProvidesSourceCodeLocation {
  15. /// Returns the Location of a given object (Descriptor).
  16. var sourceCodeInfoLocation: Google_Protobuf_SourceCodeInfo.Location? { get }
  17. }
  18. /// Default implementation for things that support ProvidesLocationPath.
  19. extension ProvidesSourceCodeLocation where Self: ProvidesLocationPath {
  20. public var sourceCodeInfoLocation: Google_Protobuf_SourceCodeInfo.Location? {
  21. var path = IndexPath()
  22. getLocationPath(path: &path)
  23. return file.sourceCodeInfoLocation(path: path)
  24. }
  25. }
  26. extension ProvidesSourceCodeLocation {
  27. /// Helper to get a source comments as a string.
  28. public func protoSourceComments(
  29. commentPrefix: String = "///",
  30. leadingDetachedPrefix: String? = nil
  31. ) -> String {
  32. guard let loc = sourceCodeInfoLocation else { return String() }
  33. return loc.asSourceComment(
  34. commentPrefix: commentPrefix,
  35. leadingDetachedPrefix: leadingDetachedPrefix
  36. )
  37. }
  38. }