-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathXcodeKitExt.swift
More file actions
48 lines (43 loc) · 1.65 KB
/
XcodeKitExt.swift
File metadata and controls
48 lines (43 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// XcodeKitExt.swift
// Linex
//
// Created by Kaunteya Suryawanshi on 11/09/17.
// Copyright © 2017 Kaunteya Suryawanshi. All rights reserved.
//
import Foundation
import XcodeKit
enum SelectionType {
case none(line: Int, col: Int)
case words(line: Int, colStart: Int, colEnd: Int)
case lines(start:XCSourceTextPosition, end: XCSourceTextPosition)//Complete line selection is counted multiline
case multiLocation([XCSourceTextRange])
}
/// Returns nil if nothing is selected
func selectionRanges(of buffer: XCSourceTextBuffer) -> SelectionType {
let selections = buffer.selections as! [XCSourceTextRange]
if selections.count == 1 {
let range = selections.first!
if range.start.line == range.end.line {
if range.start.column == range.end.column {
return .none(line: range.start.line, col: range.start.column)
}
return .words(line: range.start.line, colStart: range.start.column, colEnd: range.end.column)
}
return .lines(start: range.start, end: range.end)
}
let textRangeList = buffer.selections.map { $0 as! XCSourceTextRange }
return .multiLocation(textRangeList)
}
/// Indexes of all the lines
///
/// - Returns: Returns nil if no selection
func selectedLinesIndexSet(for selectedRanges: SelectionType) -> IndexSet {
switch selectedRanges {
case .none(let line, _): return IndexSet(integer: line)
case .words(let line, _, _): return IndexSet(integer: line)
case .lines(let start, let end):
return IndexSet(integersIn: start.line...(end.column == 0 ? end.line - 1 : end.line))
case .multiLocation(_): fatalError()
}
}