-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathFilespace+SuggestionService.swift
More file actions
130 lines (109 loc) · 4.41 KB
/
Filespace+SuggestionService.swift
File metadata and controls
130 lines (109 loc) · 4.41 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import Foundation
import SuggestionBasic
import Workspace
import XPCShared
public struct FilespaceSuggestionSnapshot: Equatable {
public let linesHash: Int
public let prefixLinesHash: Int
public let suffixLinesHash: Int
public let cursorPosition: CursorPosition
public let currentLine: String
public init(lines: [String], cursorPosition: CursorPosition) {
func safeIndex(_ index: Int) -> Int {
return max(min(index, lines.endIndex), lines.startIndex)
}
self.linesHash = lines.hashValue
self.cursorPosition = cursorPosition
self.prefixLinesHash = lines[0..<safeIndex(cursorPosition.line)].hashValue
self.suffixLinesHash = lines[safeIndex(cursorPosition.line + 1)..<lines.endIndex].hashValue
self.currentLine = cursorPosition.line >= lines.startIndex && cursorPosition.line < lines.endIndex ? lines[safeIndex(cursorPosition.line)] : ""
}
public init(content: EditorContent) {
self.init(lines: content.lines, cursorPosition: content.cursorPosition)
}
public func equalOrOnlyCurrentLineDiffers(comparedTo: FilespaceSuggestionSnapshot) -> Bool {
return prefixLinesHash == comparedTo.prefixLinesHash &&
suffixLinesHash == comparedTo.suffixLinesHash &&
cursorPosition.line == comparedTo.cursorPosition.line
}
}
public struct FilespaceSuggestionSnapshotKey: FilespacePropertyKey {
public static func createDefaultValue()
-> FilespaceSuggestionSnapshot { .init(lines: [], cursorPosition: .outOfScope) }
}
public extension FilespacePropertyValues {
@WorkspaceActor
var suggestionSourceSnapshot: FilespaceSuggestionSnapshot {
get { self[FilespaceSuggestionSnapshotKey.self] }
set { self[FilespaceSuggestionSnapshotKey.self] = newValue }
}
}
public extension Filespace {
@WorkspaceActor
func resetSnapshot() {
// swiftformat:disable redundantSelf
self.suggestionSourceSnapshot = FilespaceSuggestionSnapshotKey.createDefaultValue()
// swiftformat:enable all
}
/// Validate the suggestion is still valid.
/// - Parameters:
/// - lines: lines of the file
/// - cursorPosition: cursor position
/// - Returns: `true` if the suggestion is still valid
@WorkspaceActor
func validateSuggestions(lines: [String], cursorPosition: CursorPosition) -> Bool {
guard let presentingSuggestion else { return false }
let updatedSnapshot = FilespaceSuggestionSnapshot(lines: lines, cursorPosition: cursorPosition)
// document state is unchanged
if updatedSnapshot == self.suggestionSourceSnapshot {
return true
}
// other parts of the document have changed
if !self.suggestionSourceSnapshot.equalOrOnlyCurrentLineDiffers(comparedTo: updatedSnapshot) {
reset()
resetSnapshot()
return false
}
// the suggestion does not start on the current line
if presentingSuggestion.range.start.line != cursorPosition.line ||
presentingSuggestion.range.start.character != 0 {
reset()
resetSnapshot()
return false
}
// the cursor position is invalid
if cursorPosition.line >= lines.count {
reset()
resetSnapshot()
return false
}
let edit = LineEdit(
snapshot: self.suggestionSourceSnapshot,
suggestion: presentingSuggestion,
lines: lines,
cursor: cursorPosition
)
let suggestionLines = presentingSuggestion.text.split(whereSeparator: \.isNewline)
let suggestionFirstLine = suggestionLines.first ?? ""
// there is user-entered text to the right of the cursor
if edit.userEntered.count > cursorPosition.character {
reset()
resetSnapshot()
return false
}
// the replacement range can't be adjusted
if presentingSuggestion.range.end.line != cursorPosition.line {
reset()
resetSnapshot()
return false
}
// typing into the completion
if edit.line.count < suggestionFirstLine.count && suggestionFirstLine.hasPrefix(edit.userEntered) {
updateSuggestionsWithSameSelection(edit.updateSuggestions(suggestions))
return true
}
reset()
resetSnapshot()
return false
}
}