Files
SwiftLint/Source/SwiftLintFramework/Models/Location.swift
T
JP Simard 3a0c2b0c05 Migrate LinterCache to use Codable models (#2799)
* Migrate LinterCache to use Codable models

improving performance and type safety

* Fix Linux

* Avoid creating a Decoder if it won't be used

For example if the file doesn't exist or can't be read.

* Use corelibs plist coder if available

It's available in the Swift 5.1 branch: https://github.com/apple/swift-corelibs-foundation/pull/1849

* Remove unused error case
2019-07-07 00:35:10 -07:00

73 lines
2.2 KiB
Swift

import Foundation
import SourceKittenFramework
public struct Location: CustomStringConvertible, Comparable, Codable {
public let file: String?
public let line: Int?
public let character: Int?
public var description: String {
// Xcode likes warnings and errors in the following format:
// {full_path_to_file}{:line}{:character}: {error,warning}: {content}
let fileString: String = file ?? "<nopath>"
let lineString: String = ":\(line ?? 1)"
let charString: String = ":\(character ?? 1)"
return [fileString, lineString, charString].joined()
}
public var relativeFile: String? {
return file?.replacingOccurrences(of: FileManager.default.currentDirectoryPath + "/", with: "")
}
public init(file: String?, line: Int? = nil, character: Int? = nil) {
self.file = file
self.line = line
self.character = character
}
public init(file: File, byteOffset offset: Int) {
self.file = file.path
if let lineAndCharacter = file.contents.bridge().lineAndCharacter(forByteOffset: offset) {
line = lineAndCharacter.line
character = lineAndCharacter.character
} else {
line = nil
character = nil
}
}
public init(file: File, characterOffset offset: Int) {
self.file = file.path
if let lineAndCharacter = file.contents.bridge().lineAndCharacter(forCharacterOffset: offset) {
line = lineAndCharacter.line
character = lineAndCharacter.character
} else {
line = nil
character = nil
}
}
// MARK: Comparable
public static func < (lhs: Location, rhs: Location) -> Bool {
if lhs.file != rhs.file {
return lhs.file < rhs.file
}
if lhs.line != rhs.line {
return lhs.line < rhs.line
}
return lhs.character < rhs.character
}
}
private extension Optional where Wrapped: Comparable {
static func < (lhs: Optional, rhs: Optional) -> Bool {
switch (lhs, rhs) {
case let (lhs?, rhs?):
return lhs < rhs
case (nil, _?):
return true
default:
return false
}
}
}