-
Notifications
You must be signed in to change notification settings - Fork 42
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
IB connection rule #9
Closed
Closed
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3622bd8
parse outlet connections
kateinoigakukun b86b885
add OutletConnectionRule abstruct
kateinoigakukun e47b6fb
install SourceKitten
kateinoigakukun 635ec55
add view find logic
kateinoigakukun 0898d3b
wip
kateinoigakukun ee87f2d
wip
kateinoigakukun 7e41f3a
implement connection validator
kateinoigakukun 47446b0
wip
kateinoigakukun ef1ed62
refactoring
kateinoigakukun 6403a43
refactoring
kateinoigakukun f433c15
wip
kateinoigakukun d9642e0
add barButtonItem
kateinoigakukun e165540
add unknown view
kateinoigakukun 2779db4
fix BarButtonItem connection
kateinoigakukun bb34d55
add gesture recognizer
kateinoigakukun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// Identifiable.swift | ||
// IBLinterCore | ||
// | ||
// Created by SaitoYuta on 2018/01/06. | ||
// | ||
|
||
public protocol Identifiable { | ||
var id: String { get } | ||
} | ||
|
||
extension Identifiable where Self: ViewProtocol { | ||
|
||
// TODO: use cache | ||
public func find(by id: String) -> InterfaceBuilderNode.View? { | ||
if let matched = subviews?.first(where: { $0.id == id }) { | ||
return matched | ||
} else { | ||
return subviews?.lazy.flatMap { $0.find(by: id) }.first | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// SwiftFile.swift | ||
// IBLinterCore | ||
// | ||
// Created by SaitoYuta on 2018/01/12. | ||
// | ||
|
||
import Foundation | ||
|
||
public class SwiftFile: FileProtocol { | ||
public var pathString: String | ||
public var fileName: String { | ||
return pathString.components(separatedBy: "/").last! | ||
} | ||
|
||
public init(path: String) { | ||
self.pathString = path | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// | ||
// SwiftIBParser.swift | ||
// IBLinterCore | ||
// | ||
// Created by SaitoYuta on 2018/01/06. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public class SwiftIBParser { | ||
|
||
public struct Class { | ||
public let file: SwiftFile | ||
public let name: String | ||
public let connections: [Connection] | ||
public let inheritedClassNames: [String] | ||
public let declaration: Declaration | ||
|
||
public init(file: SwiftFile, name: String, connections: [Connection], | ||
inheritedClassNames: [String], declaration: Declaration) { | ||
self.file = file | ||
self.name = name | ||
self.connections = connections | ||
self.declaration = declaration | ||
self.inheritedClassNames = inheritedClassNames | ||
} | ||
} | ||
|
||
public enum Connection { | ||
case action(selector: String, declaration: Declaration) | ||
case outlet(property: String, isOptional: Bool, declaration: Declaration) | ||
|
||
var swiftFile: SwiftFile? { | ||
switch self { | ||
case .action(_, let declaration), | ||
.outlet(_, _, let declaration): | ||
return declaration.path.map { SwiftFile.init(path: $0) } | ||
} | ||
} | ||
} | ||
|
||
public struct Declaration { | ||
|
||
public let line: Int | ||
public let column: Int | ||
public let path: String? | ||
|
||
public init(line: Int, column: Int, path: String?) { | ||
self.line = line | ||
self.column = column | ||
self.path = path | ||
} | ||
|
||
public init(file: File, offset: Int64) { | ||
let fileOffset = type(of: self).getLineColumnNumber(of: file, offset: Int(offset)) | ||
|
||
self.line = fileOffset.line | ||
self.column = fileOffset.column | ||
// SourceKitten use no file scheme prefix path | ||
self.path = file.path.map { URL.init(fileURLWithPath: $0).absoluteString } | ||
} | ||
|
||
private static func getLineColumnNumber(of file: File, offset: Int) -> (line: Int, column: Int) { | ||
let range = file.contents.startIndex..<file.contents.index(file.contents.startIndex, offsetBy: offset) | ||
let subString = file.contents[range] | ||
let lines = subString.components(separatedBy: "\n") | ||
|
||
if let column = lines.last?.characters.count { | ||
return (line: lines.count, column: column) | ||
} | ||
return (line: lines.count, column: 0) | ||
} | ||
} | ||
|
||
public private(set) var classNameToStructure: [String: Class] = [:] | ||
|
||
public init(swiftFilePaths: [String]) { | ||
|
||
swiftFilePaths.forEach(mappingFile) | ||
} | ||
|
||
private func mappingFile(at path: String) { | ||
guard let file = File(path: path) else { return } | ||
let fileStructure = Structure(file: file) | ||
|
||
fileStructure.dictionary.substructure.forEach { [weak self] structure in | ||
var connections: [Connection] = [] | ||
|
||
guard let kind = structure["key.kind"] as? String, let name = structure["key.name"] as? String, | ||
let nameOffset64 = structure["key.nameoffset"] as? Int64, | ||
let inheritedTypes = structure["key.inheritedtypes"] as? [[String: String]], | ||
kind == "source.lang.swift.decl.class" || kind == "source.lang.swift.decl.extension" else { return } | ||
|
||
structure.substructure.forEach { insideStructure in | ||
guard let attributes = insideStructure["key.attributes"] as? [[String: String]], | ||
let propertyName = insideStructure["key.name"] as? String else { return } | ||
|
||
let isOutlet = attributes.contains { $0.values.contains("source.decl.attribute.iboutlet") } | ||
if isOutlet, let nameOffset64 = insideStructure["key.nameoffset"] as? Int64 { | ||
connections.append(.outlet(property: propertyName, isOptional: insideStructure.isOptional, | ||
declaration: .init(file: file, offset: nameOffset64))) | ||
} | ||
|
||
let isIBAction = attributes.contains { $0.values.contains("source.decl.attribute.ibaction") } | ||
|
||
if isIBAction, let selectorName = insideStructure["key.selector_name"] as? String, | ||
let nameOffset64 = insideStructure["key.nameoffset"] as? Int64 { | ||
connections.append(.action(selector: selectorName, | ||
declaration: .init(file: file, offset: nameOffset64))) | ||
} | ||
} | ||
|
||
self?.classNameToStructure[name] = Class(file: SwiftFile(path: path), | ||
name: name, connections: connections, | ||
inheritedClassNames: inheritedTypes.flatMap { $0["key.name"] }, | ||
declaration: .init(file: file, offset: nameOffset64)) | ||
} | ||
} | ||
} | ||
|
||
private extension Dictionary where Key: ExpressibleByStringLiteral { | ||
var substructure: [[String: SourceKitRepresentable]] { | ||
let substructure = self["key.substructure"] as? [SourceKitRepresentable] ?? [] | ||
return substructure.flatMap { $0 as? [String: SourceKitRepresentable] } | ||
} | ||
|
||
var isOptional: Bool { | ||
if let typename = self["key.typename"] as? String, | ||
let optionalString = typename.characters.last { | ||
return optionalString == "?" | ||
} | ||
return false | ||
} | ||
} | ||
|
||
extension SwiftIBParser.Class: Equatable { | ||
public static func ==(lhs: SwiftIBParser.Class, rhs: SwiftIBParser.Class) -> Bool { | ||
return lhs.name == rhs.name && lhs.connections == rhs.connections | ||
} | ||
} | ||
|
||
extension SwiftIBParser.Connection: Equatable { | ||
public static func ==(lhs: SwiftIBParser.Connection, rhs: SwiftIBParser.Connection) -> Bool { | ||
switch (lhs, rhs) { | ||
case (.action(let selector1, let declaration1), | ||
.action(let selector2, let declaration2)): | ||
return selector1 == selector2 && declaration1 == declaration2 | ||
case (.outlet(let property1, let isOptional1, let declaration1), | ||
.outlet(let property2, let isOptional2, let declaration2)): | ||
return property1 == property2 && isOptional1 == isOptional2 && declaration1 == declaration2 | ||
default: return false | ||
} | ||
} | ||
} | ||
|
||
extension SwiftIBParser.Declaration: Equatable { | ||
public static func ==(lhs: SwiftIBParser.Declaration, rhs: SwiftIBParser.Declaration) -> Bool { | ||
return lhs.column == rhs.column && | ||
lhs.line == rhs.line && | ||
lhs.path == rhs.path | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to analyze not only
xib
orstoryboard
but alsoswift