|
| 1 | +import CopilotModel |
| 2 | +import Foundation |
| 3 | +import SuggestionInjector |
| 4 | +import XPCShared |
| 5 | + |
| 6 | +@ServiceActor |
| 7 | +struct WindowBaseCommandHandler: SuggestionCommandHanlder { |
| 8 | + nonisolated init() {} |
| 9 | + |
| 10 | + func presentSuggestions(editor: EditorContent) async throws -> UpdatedContent? { |
| 11 | + Task { |
| 12 | + try await _presentSuggestions(editor: editor) |
| 13 | + } |
| 14 | + return nil |
| 15 | + } |
| 16 | + |
| 17 | + private func _presentSuggestions(editor: EditorContent) async throws { |
| 18 | + let fileURL = try await Environment.fetchCurrentFileURL() |
| 19 | + let (workspace, filespace) = try await Workspace |
| 20 | + .fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL) |
| 21 | + |
| 22 | + try Task.checkCancellation() |
| 23 | + |
| 24 | + let snapshot = Filespace.Snapshot( |
| 25 | + linesHash: editor.lines.hashValue, |
| 26 | + cursorPosition: editor.cursorPosition |
| 27 | + ) |
| 28 | + |
| 29 | + // There is no need to regenerate suggestions for the same editor content. |
| 30 | + guard filespace.suggestionSourceSnapshot != snapshot else { return } |
| 31 | + |
| 32 | + try await workspace.generateSuggestions( |
| 33 | + forFileAt: fileURL, |
| 34 | + content: editor.content, |
| 35 | + lines: editor.lines, |
| 36 | + cursorPosition: editor.cursorPosition, |
| 37 | + tabSize: editor.tabSize, |
| 38 | + indentSize: editor.indentSize, |
| 39 | + usesTabsForIndentation: editor.usesTabsForIndentation |
| 40 | + ) |
| 41 | + |
| 42 | + if let suggestion = filespace.presentingSuggestion { |
| 43 | + presentSuggestion(suggestion, lines: editor.lines) |
| 44 | + } else { |
| 45 | + Task { @MainActor in |
| 46 | + GraphicalUserInterfaceController.shared.suggestionPanelController.viewModel |
| 47 | + .suggestion = [] |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + func presentNextSuggestion(editor: EditorContent) async throws -> UpdatedContent? { |
| 53 | + Task { |
| 54 | + try await _presentNextSuggestion(editor: editor) |
| 55 | + } |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + private func _presentNextSuggestion(editor: EditorContent) async throws { |
| 60 | + let fileURL = try await Environment.fetchCurrentFileURL() |
| 61 | + let (workspace, filespace) = try await Workspace |
| 62 | + .fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL) |
| 63 | + workspace.selectNextSuggestion( |
| 64 | + forFileAt: fileURL, |
| 65 | + content: editor.content, |
| 66 | + lines: editor.lines |
| 67 | + ) |
| 68 | + |
| 69 | + if let suggestion = filespace.presentingSuggestion { |
| 70 | + presentSuggestion(suggestion, lines: editor.lines) |
| 71 | + } else { |
| 72 | + Task { @MainActor in |
| 73 | + GraphicalUserInterfaceController.shared.suggestionPanelController.viewModel |
| 74 | + .suggestion = [] |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + func presentPreviousSuggestion(editor: EditorContent) async throws -> UpdatedContent? { |
| 80 | + Task { |
| 81 | + try await _presentPreviousSuggestion(editor: editor) |
| 82 | + } |
| 83 | + return nil |
| 84 | + } |
| 85 | + |
| 86 | + private func _presentPreviousSuggestion(editor: EditorContent) async throws { |
| 87 | + let fileURL = try await Environment.fetchCurrentFileURL() |
| 88 | + let (workspace, filespace) = try await Workspace |
| 89 | + .fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL) |
| 90 | + workspace.selectPreviousSuggestion( |
| 91 | + forFileAt: fileURL, |
| 92 | + content: editor.content, |
| 93 | + lines: editor.lines |
| 94 | + ) |
| 95 | + |
| 96 | + if let suggestion = filespace.presentingSuggestion { |
| 97 | + presentSuggestion(suggestion, lines: editor.lines) |
| 98 | + } else { |
| 99 | + Task { @MainActor in |
| 100 | + GraphicalUserInterfaceController.shared.suggestionPanelController.viewModel |
| 101 | + .suggestion = [] |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + func rejectSuggestion(editor: EditorContent) async throws -> UpdatedContent? { |
| 107 | + Task { |
| 108 | + try await _rejectSuggestion(editor: editor) |
| 109 | + } |
| 110 | + return nil |
| 111 | + } |
| 112 | + |
| 113 | + private func _rejectSuggestion(editor: EditorContent) async throws { |
| 114 | + let fileURL = try await Environment.fetchCurrentFileURL() |
| 115 | + let (workspace, _) = try await Workspace.fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL) |
| 116 | + workspace.rejectSuggestion(forFileAt: fileURL) |
| 117 | + |
| 118 | + // hide it |
| 119 | + |
| 120 | + Task { @MainActor in |
| 121 | + GraphicalUserInterfaceController.shared.suggestionPanelController.viewModel |
| 122 | + .suggestion = [] |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + func acceptSuggestion(editor: EditorContent) async throws -> UpdatedContent? { |
| 127 | + Task { @MainActor in |
| 128 | + GraphicalUserInterfaceController.shared.suggestionPanelController.viewModel |
| 129 | + .suggestion = [] |
| 130 | + } |
| 131 | + return try await CommentBaseCommandHandler().acceptSuggestion(editor: editor) |
| 132 | + } |
| 133 | + |
| 134 | + func presentRealtimeSuggestions(editor: EditorContent) async throws -> UpdatedContent? { |
| 135 | + // not needed. |
| 136 | + return nil |
| 137 | + } |
| 138 | + |
| 139 | + func generateRealtimeSuggestions(editor: EditorContent) async throws -> UpdatedContent? { |
| 140 | + try await presentSuggestions(editor: editor) |
| 141 | + } |
| 142 | + |
| 143 | + func presentSuggestion(_ suggestion: CopilotCompletion, lines: [String]) { |
| 144 | + Task { @MainActor in |
| 145 | + let viewModel = GraphicalUserInterfaceController.shared.suggestionPanelController |
| 146 | + .viewModel |
| 147 | + viewModel.suggestCode(suggestion.text, startLineIndex: suggestion.position.line) |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments