Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
7fa80a3
Merge tag '0.25.0' into develop
intitni Oct 11, 2023
4dd4f2f
Add SuggestionServiceMiddleware
intitni Oct 15, 2023
e1a6722
Update
intitni Oct 15, 2023
5116c1d
Fix calling middleware creates infinite loop
intitni Oct 16, 2023
daab571
Merge branch 'feature/suggestion-cheatsheet-2' into develop
intitni Oct 16, 2023
b8ae34e
Support chat tab self-closing
intitni Oct 16, 2023
08b0d76
Add TerminalChatTab
intitni Oct 16, 2023
6cfab90
Update TerminalChatTab to support restoration
intitni Oct 16, 2023
c674551
Add icons to chat tab
intitni Oct 16, 2023
9c1a592
Update
intitni Oct 17, 2023
0e8cad3
Merge branch 'feature/terminal-tab' into develop
intitni Oct 17, 2023
f531278
Support ChatGPTService in debug window
intitni Oct 17, 2023
2ad4d33
Adjust system prompt
intitni Oct 18, 2023
6e1e387
Revert SwiftFocusCodeFinder behavior
intitni Oct 19, 2023
ec796af
Update
intitni Oct 19, 2023
4281a6a
Merge branch 'feature/adjust-symbol-detection-from-conversation' into…
intitni Oct 19, 2023
eec4881
Update
intitni Oct 20, 2023
8099410
Update
intitni Oct 20, 2023
0a28d58
Merge branch 'feature/retrieve-from-active-document-pre' into develop
intitni Oct 20, 2023
6f970de
Update getEditorContent to actively get the focused editor content wh…
intitni Oct 20, 2023
2d864ea
Update custom command template to support {{clipboard}}
intitni Oct 20, 2023
7701377
Update custom prompt to code to support templates
intitni Oct 20, 2023
f71eb47
Merge branch 'feature/custom-command-template-clipboard' into develop
intitni Oct 20, 2023
8dbb5eb
Add global hotkey to show/hide widget
intitni Oct 21, 2023
e77fd16
Allow making hotkey non-global
intitni Oct 21, 2023
9982ee3
Fix the timing of KeyboardShortcuts initialization
intitni Oct 21, 2023
88fc06b
Update README.md
intitni Oct 21, 2023
4983750
Fix that command+M is not hiding chat panel
intitni Oct 21, 2023
c2837ee
Update
intitni Oct 22, 2023
0b93f92
Move MessageScopeParser to ChatContextCollector
intitni Oct 22, 2023
27c9cdc
Fix that the host app is observing the global shortcut
intitni Oct 22, 2023
dd398b5
Fix archiving
intitni Oct 22, 2023
b72dcca
Update README.md
intitni Oct 22, 2023
2cbd4b6
Update
intitni Oct 22, 2023
48f92d5
Update
intitni Oct 22, 2023
d64c817
Bump version to 0.26.0
intitni Oct 22, 2023
f9d2241
Update appcast.xml
intitni Oct 22, 2023
cd41739
Merge branch 'release/0.26.0'
intitni Oct 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add SuggestionServiceMiddleware
  • Loading branch information
intitni committed Oct 15, 2023
commit 4dd4f2f36fc43832cc680f31262be748bb5643ea
2 changes: 1 addition & 1 deletion Pro
Submodule Pro updated from 72fd94 to 064b3e
32 changes: 13 additions & 19 deletions Tool/Sources/SuggestionService/CodeiumSuggestionProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,17 @@ actor CodeiumSuggestionProvider: SuggestionServiceProvider {
}

extension CodeiumSuggestionProvider {
func getSuggestions(
fileURL: URL,
content: String,
cursorPosition: SuggestionModel.CursorPosition,
tabSize: Int,
indentSize: Int,
usesTabsForIndentation: Bool,
ignoreSpaceOnlySuggestions: Bool
) async throws -> [SuggestionModel.CodeSuggestion] {
try await (try createCodeiumServiceIfNeeded()).getCompletions(
fileURL: fileURL,
content: content,
cursorPosition: cursorPosition,
tabSize: tabSize,
indentSize: indentSize,
usesTabsForIndentation: usesTabsForIndentation,
ignoreSpaceOnlySuggestions: ignoreSpaceOnlySuggestions
func getSuggestions(_ request: SuggestionRequest) async throws
-> [SuggestionModel.CodeSuggestion]
{
try await (createCodeiumServiceIfNeeded()).getCompletions(
fileURL: request.fileURL,
content: request.content,
cursorPosition: request.cursorPosition,
tabSize: request.tabSize,
indentSize: request.indentSize,
usesTabsForIndentation: request.usesTabsForIndentation,
ignoreSpaceOnlySuggestions: request.ignoreSpaceOnlySuggestions
)
}

Expand All @@ -70,12 +64,12 @@ extension CodeiumSuggestionProvider {
}

func notifySaveTextDocument(fileURL: URL) async throws {}

func cancelRequest() async {
await (try? createCodeiumServiceIfNeeded())?
.cancelRequest()
}

func terminate() async {
(try? createCodeiumServiceIfNeeded())?.terminate()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,17 @@ actor GitHubCopilotSuggestionProvider: SuggestionServiceProvider {
}

extension GitHubCopilotSuggestionProvider {
func getSuggestions(
fileURL: URL,
content: String,
cursorPosition: SuggestionModel.CursorPosition,
tabSize: Int,
indentSize: Int,
usesTabsForIndentation: Bool,
ignoreSpaceOnlySuggestions: Bool
) async throws -> [SuggestionModel.CodeSuggestion] {
try await (try createGitHubCopilotServiceIfNeeded()).getCompletions(
fileURL: fileURL,
content: content,
cursorPosition: cursorPosition,
tabSize: tabSize,
indentSize: indentSize,
usesTabsForIndentation: usesTabsForIndentation,
ignoreSpaceOnlySuggestions: ignoreSpaceOnlySuggestions,
func getSuggestions(_ request: SuggestionRequest) async throws
-> [SuggestionModel.CodeSuggestion]
{
try await (createGitHubCopilotServiceIfNeeded()).getCompletions(
fileURL: request.fileURL,
content: request.content,
cursorPosition: request.cursorPosition,
tabSize: request.tabSize,
indentSize: request.indentSize,
usesTabsForIndentation: request.usesTabsForIndentation,
ignoreSpaceOnlySuggestions: request.ignoreSpaceOnlySuggestions,
ignoreTrailingNewLinesAndSpaces: UserDefaults.shared
.value(for: \.gitHubCopilotIgnoreTrailingNewLines)
)
Expand Down
93 changes: 67 additions & 26 deletions Tool/Sources/SuggestionService/SuggestionService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,36 @@ import Preferences
import SuggestionModel
import UserDefaultsObserver

public protocol SuggestionServiceType {
func getSuggestions(
public struct SuggestionRequest {
public var fileURL: URL
public var content: String
public var cursorPosition: CursorPosition
public var tabSize: Int
public var indentSize: Int
public var usesTabsForIndentation: Bool
public var ignoreSpaceOnlySuggestions: Bool

public init(
fileURL: URL,
content: String,
cursorPosition: CursorPosition,
tabSize: Int,
indentSize: Int,
usesTabsForIndentation: Bool,
ignoreSpaceOnlySuggestions: Bool
) async throws -> [CodeSuggestion]
) {
self.fileURL = fileURL
self.content = content
self.cursorPosition = cursorPosition
self.tabSize = tabSize
self.indentSize = indentSize
self.usesTabsForIndentation = usesTabsForIndentation
self.ignoreSpaceOnlySuggestions = ignoreSpaceOnlySuggestions
}
}

public protocol SuggestionServiceType {
func getSuggestions(_ request: SuggestionRequest) async throws -> [CodeSuggestion]

func notifyAccepted(_ suggestion: CodeSuggestion) async
func notifyRejected(_ suggestions: [CodeSuggestion]) async
Expand All @@ -25,9 +45,45 @@ public protocol SuggestionServiceType {
func terminate() async
}

public extension SuggestionServiceType {
func getSuggestions(
fileURL: URL,
content: String,
cursorPosition: CursorPosition,
tabSize: Int,
indentSize: Int,
usesTabsForIndentation: Bool,
ignoreSpaceOnlySuggestions: Bool
) async throws -> [CodeSuggestion] {
return try await getSuggestions(.init(
fileURL: fileURL,
content: content,
cursorPosition: cursorPosition,
tabSize: tabSize,
indentSize: indentSize,
usesTabsForIndentation: usesTabsForIndentation,
ignoreSpaceOnlySuggestions: ignoreSpaceOnlySuggestions
))
}
}

protocol SuggestionServiceProvider: SuggestionServiceType {}

public actor SuggestionService: SuggestionServiceType {
static var builtInMiddlewares: [SuggestionServiceMiddleware] = [
DisabledLanguageSuggestionServiceMiddleware()
]

static var customMiddlewares: [SuggestionServiceMiddleware] = []

static var middlewares: [SuggestionServiceMiddleware] {
builtInMiddlewares + customMiddlewares
}

public static func addMiddleware(_ middleware: SuggestionServiceMiddleware) {
customMiddlewares.append(middleware)
}

let projectRootURL: URL
let onServiceLaunched: (SuggestionServiceType) -> Void
let providerChangeObserver = UserDefaultsObserver(
Expand Down Expand Up @@ -75,31 +131,16 @@ public actor SuggestionService: SuggestionServiceType {
}

public extension SuggestionService {
func getSuggestions(
fileURL: URL,
content: String,
cursorPosition: SuggestionModel.CursorPosition,
tabSize: Int,
indentSize: Int,
usesTabsForIndentation: Bool,
ignoreSpaceOnlySuggestions: Bool
) async throws -> [SuggestionModel.CodeSuggestion] {
let language = languageIdentifierFromFileURL(fileURL)
if UserDefaults.shared.value(for: \.suggestionFeatureDisabledLanguageList)
.contains(where: { $0 == language.rawValue })
{
return []
func getSuggestions(_ request: SuggestionRequest) async throws -> [SuggestionModel.CodeSuggestion] {
var getSuggestion = suggestionProvider.getSuggestions

for middleware in Self.middlewares.reversed() {
getSuggestion = { request in
try await middleware.getSuggestion(request, next: getSuggestion)
}
}

return try await suggestionProvider.getSuggestions(
fileURL: fileURL,
content: content,
cursorPosition: cursorPosition,
tabSize: tabSize,
indentSize: indentSize,
usesTabsForIndentation: usesTabsForIndentation,
ignoreSpaceOnlySuggestions: ignoreSpaceOnlySuggestions
)
return try await getSuggestion(request)
}

func notifyAccepted(_ suggestion: SuggestionModel.CodeSuggestion) async {
Expand Down
41 changes: 41 additions & 0 deletions Tool/Sources/SuggestionService/SuggestionServiceMiddleware.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Foundation
import SuggestionModel
import Logger

public protocol SuggestionServiceMiddleware {
typealias Next = (SuggestionRequest) async throws -> [CodeSuggestion]

func getSuggestion(_ request: SuggestionRequest, next: Next) async throws -> [CodeSuggestion]
}

struct DisabledLanguageSuggestionServiceMiddleware: SuggestionServiceMiddleware {
func getSuggestion(_ request: SuggestionRequest, next: Next) async throws -> [CodeSuggestion] {
let language = languageIdentifierFromFileURL(request.fileURL)
if UserDefaults.shared.value(for: \.suggestionFeatureDisabledLanguageList)
.contains(where: { $0 == language.rawValue })
{
#if DEBUG
Logger.service.info("Suggestion service is disabled for \(language).")
#endif
return []
}

return try await next(request)
}
}

public struct DebugSuggestionServiceMiddleware: SuggestionServiceMiddleware {
public init() {}

public func getSuggestion(_ request: SuggestionRequest, next: Next) async throws -> [CodeSuggestion] {
Logger.service.debug("""
Get suggestion for \(request.fileURL) at \(request.cursorPosition)
""")
let suggestions = try await next(request)
Logger.service.debug("""
Receive \(suggestions.count) suggestions for \(request.fileURL) at \(request.cursorPosition)
""")

return suggestions
}
}