-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
597 additions
and
94 deletions.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
62 changes: 62 additions & 0 deletions
62
Core/Sources/CodeCompletionService/StreamLineLimiter.swift
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,62 @@ | ||
import Foundation | ||
|
||
final class StreamLineLimiter { | ||
public private(set) var result = "" | ||
private var currentLine = "" | ||
private var existedLines = [String]() | ||
private let lineLimit: Int | ||
private let strategy: any StreamStopStrategy | ||
|
||
enum PushResult: Equatable { | ||
case `continue` | ||
case finish(String) | ||
} | ||
|
||
init( | ||
lineLimit: Int = UserDefaults.shared.value(for: \.maxNumberOfLinesOfSuggestion), | ||
strategy: any StreamStopStrategy | ||
) { | ||
self.lineLimit = lineLimit | ||
self.strategy = strategy | ||
} | ||
|
||
func push(_ token: String) -> PushResult { | ||
currentLine.append(token) | ||
if let newLine = currentLine.last(where: { $0.isNewline }) { | ||
let lines = currentLine | ||
.breakLines(proposedLineEnding: String(newLine), appendLineBreakToLastLine: false) | ||
let (newLines, lastLine) = lines.headAndTail | ||
existedLines.append(contentsOf: newLines) | ||
currentLine = lastLine ?? "" | ||
} | ||
|
||
let stopResult = if lineLimit <= 0 { | ||
StreamStopStrategyResult.continue | ||
} else { | ||
strategy.shouldStop( | ||
existedLines: existedLines, | ||
currentLine: currentLine, | ||
proposedLineLimit: lineLimit | ||
) | ||
} | ||
|
||
switch stopResult { | ||
case .continue: | ||
result.append(token) | ||
return .continue | ||
case let .stop(appendingNewContent): | ||
if appendingNewContent { | ||
result.append(token) | ||
} | ||
return .finish(result) | ||
} | ||
} | ||
} | ||
|
||
extension Array { | ||
var headAndTail: ([Element], Element?) { | ||
guard let tail = last else { return ([], nil) } | ||
return (Array(dropLast()), tail) | ||
} | ||
} | ||
|
15 changes: 15 additions & 0 deletions
15
Core/Sources/CodeCompletionService/StreamStopStrategy/DefaultStreamStopStrategy.swift
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,15 @@ | ||
public struct DefaultStreamStopStrategy: StreamStopStrategy { | ||
public init() {} | ||
|
||
public func shouldStop( | ||
existedLines: [String], | ||
currentLine: String, | ||
proposedLineLimit: Int | ||
) -> StreamStopStrategyResult { | ||
if existedLines.count >= proposedLineLimit { | ||
return .stop(appendingNewContent: true) | ||
} | ||
return .continue | ||
} | ||
} | ||
|
12 changes: 12 additions & 0 deletions
12
Core/Sources/CodeCompletionService/StreamStopStrategy/NeverStreamStopStrategy.swift
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,12 @@ | ||
public struct NeverStreamStopStrategy: StreamStopStrategy { | ||
public init() {} | ||
|
||
public func shouldStop( | ||
existedLines: [String], | ||
currentLine: String, | ||
proposedLineLimit: Int | ||
) -> StreamStopStrategyResult { | ||
.continue | ||
} | ||
} | ||
|
31 changes: 31 additions & 0 deletions
31
.../Sources/CodeCompletionService/StreamStopStrategy/OpeningTagBasedStreamStopStrategy.swift
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,31 @@ | ||
import Foundation | ||
|
||
public struct OpeningTagBasedStreamStopStrategy: StreamStopStrategy { | ||
public let openingTag: String | ||
public let toleranceIfNoOpeningTagFound: Int | ||
|
||
public init(openingTag: String, toleranceIfNoOpeningTagFound: Int) { | ||
self.openingTag = openingTag | ||
self.toleranceIfNoOpeningTagFound = toleranceIfNoOpeningTagFound | ||
} | ||
|
||
public func shouldStop( | ||
existedLines: [String], | ||
currentLine: String, | ||
proposedLineLimit: Int | ||
) -> StreamStopStrategyResult { | ||
if let index = existedLines.firstIndex(where: { $0.contains(openingTag) }) { | ||
if existedLines.count - index - 1 >= proposedLineLimit { | ||
return .stop(appendingNewContent: true) | ||
} | ||
return .continue | ||
} else { | ||
if existedLines.count >= proposedLineLimit + toleranceIfNoOpeningTagFound { | ||
return .stop(appendingNewContent: true) | ||
} else { | ||
return .continue | ||
} | ||
} | ||
} | ||
} | ||
|
12 changes: 12 additions & 0 deletions
12
Core/Sources/CodeCompletionService/StreamStopStrategy/StreamStopStrategy.swift
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,12 @@ | ||
import Foundation | ||
|
||
public enum StreamStopStrategyResult { | ||
case `continue` | ||
case stop(appendingNewContent: Bool) | ||
} | ||
|
||
public protocol StreamStopStrategy { | ||
func shouldStop(existedLines: [String], currentLine: String, proposedLineLimit: Int) | ||
-> StreamStopStrategyResult | ||
} | ||
|
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
Oops, something went wrong.