diff --git a/Core/Sources/HostApp/FeatureSettings/PromptToCodeSettingsView.swift b/Core/Sources/HostApp/FeatureSettings/PromptToCodeSettingsView.swift index 12808a59..6f33d599 100644 --- a/Core/Sources/HostApp/FeatureSettings/PromptToCodeSettingsView.swift +++ b/Core/Sources/HostApp/FeatureSettings/PromptToCodeSettingsView.swift @@ -8,10 +8,10 @@ import ProHostApp struct PromptToCodeSettingsView: View { final class Settings: ObservableObject { - @AppStorage(\.hideCommonPrecedingSpacesInSuggestion) - var hideCommonPrecedingSpacesInSuggestion - @AppStorage(\.suggestionCodeFontSize) - var suggestionCodeFontSize + @AppStorage(\.hideCommonPrecedingSpacesInPromptToCode) + var hideCommonPrecedingSpaces + @AppStorage(\.promptToCodeCodeFontSize) + var fontSize @AppStorage(\.promptToCodeGenerateDescription) var promptToCodeGenerateDescription @AppStorage(\.promptToCodeGenerateDescriptionInUserPreferredLanguage) @@ -84,25 +84,25 @@ struct PromptToCodeSettingsView: View { } } - SettingsDivider("Mirroring Settings of Suggestion Feature") + SettingsDivider("UI") Form { - Toggle(isOn: $settings.hideCommonPrecedingSpacesInSuggestion) { + Toggle(isOn: $settings.hideCommonPrecedingSpaces) { Text("Hide Common Preceding Spaces") - }.disabled(true) + } HStack { TextField(text: .init(get: { - "\(Int(settings.suggestionCodeFontSize))" + "\(Int(settings.fontSize))" }, set: { - settings.suggestionCodeFontSize = Double(Int($0) ?? 0) + settings.fontSize = Double(Int($0) ?? 0) })) { Text("Font size of suggestion code") } .textFieldStyle(.roundedBorder) Text("pt") - }.disabled(true) + } } ScopeForm() diff --git a/Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlockSuggestionPanel.swift b/Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlockSuggestionPanel.swift index b5f60d67..8ba5d57a 100644 --- a/Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlockSuggestionPanel.swift +++ b/Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlockSuggestionPanel.swift @@ -7,6 +7,7 @@ struct CodeBlockSuggestionPanel: View { @AppStorage(\.suggestionCodeFontSize) var fontSize @AppStorage(\.suggestionDisplayCompactMode) var suggestionDisplayCompactMode @AppStorage(\.suggestionPresentationMode) var suggestionPresentationMode + @AppStorage(\.hideCommonPrecedingSpacesInSuggestion) var hideCommonPrecedingSpaces struct ToolBar: View { @ObservedObject var suggestion: CodeSuggestionProvider @@ -101,7 +102,8 @@ struct CodeBlockSuggestionPanel: View { language: suggestion.language, startLineIndex: suggestion.startLineIndex, colorScheme: colorScheme, - fontSize: fontSize + fontSize: fontSize, + droppingLeadingSpaces: hideCommonPrecedingSpaces ) .frame(maxWidth: .infinity) } diff --git a/Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift b/Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift index 98b5707f..2414803e 100644 --- a/Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift +++ b/Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift @@ -202,7 +202,8 @@ extension PromptToCodePanel { struct Content: View { let store: StoreOf @Environment(\.colorScheme) var colorScheme - @AppStorage(\.suggestionCodeFontSize) var fontSize + @AppStorage(\.promptToCodeCodeFontSize) var fontSize + @AppStorage(\.hideCommonPrecedingSpacesInPromptToCode) var hideCommonPrecedingSpaces struct CodeContent: Equatable { var code: String @@ -278,7 +279,8 @@ extension PromptToCodePanel { colorScheme: colorScheme, firstLinePrecedingSpaceCount: viewStore.state .firstLinePrecedingSpaceCount, - fontSize: fontSize + fontSize: fontSize, + droppingLeadingSpaces: hideCommonPrecedingSpaces ) .frame(maxWidth: .infinity) .scaleEffect(x: 1, y: -1, anchor: .center) diff --git a/Core/Sources/SuggestionWidget/WidgetView.swift b/Core/Sources/SuggestionWidget/WidgetView.swift index 04c11c86..2b4b3b8e 100644 --- a/Core/Sources/SuggestionWidget/WidgetView.swift +++ b/Core/Sources/SuggestionWidget/WidgetView.swift @@ -140,7 +140,6 @@ struct WidgetAnimatedCircle: View { struct WidgetContextMenu: View { @AppStorage(\.useGlobalChat) var useGlobalChat @AppStorage(\.realtimeSuggestionToggle) var realtimeSuggestionToggle - @AppStorage(\.hideCommonPrecedingSpacesInSuggestion) var hideCommonPrecedingSpacesInSuggestion @AppStorage(\.disableSuggestionFeatureGlobally) var disableSuggestionFeatureGlobally @AppStorage(\.suggestionFeatureEnabledProjectList) var suggestionFeatureEnabledProjectList @AppStorage(\.suggestionFeatureDisabledLanguageList) var suggestionFeatureDisabledLanguageList @@ -198,15 +197,6 @@ struct WidgetContextMenu: View { Image(systemName: "checkmark") } } - - Button(action: { - hideCommonPrecedingSpacesInSuggestion.toggle() - }, label: { - Text("Hide Common Preceding Spaces in Suggestion") - if hideCommonPrecedingSpacesInSuggestion { - Image(systemName: "checkmark") - } - }) } Divider() diff --git a/Tool/Sources/Preferences/Keys.swift b/Tool/Sources/Preferences/Keys.swift index d020accf..a7588692 100644 --- a/Tool/Sources/Preferences/Keys.swift +++ b/Tool/Sources/Preferences/Keys.swift @@ -294,6 +294,14 @@ public extension UserDefaultPreferenceKeys { var enableSenseScopeByDefaultInPromptToCode: PreferenceKey { .init(defaultValue: false, key: "EnableSenseScopeByDefaultInPromptToCode") } + + var promptToCodeCodeFontSize: PreferenceKey { + .init(defaultValue: 13, key: "PromptToCodeCodeFontSize") + } + + var hideCommonPrecedingSpacesInPromptToCode: PreferenceKey { + .init(defaultValue: true, key: "HideCommonPrecedingSpacesInPromptToCode") + } } // MARK: - Suggestion diff --git a/Tool/Sources/Preferences/UserDefaults.swift b/Tool/Sources/Preferences/UserDefaults.swift index fb5244d8..1cf3b0b4 100644 --- a/Tool/Sources/Preferences/UserDefaults.swift +++ b/Tool/Sources/Preferences/UserDefaults.swift @@ -25,6 +25,10 @@ public extension UserDefaults { for: \.suggestionFeatureProvider, defaultValue: .builtIn(shared.deprecatedValue(for: \.oldSuggestionFeatureProvider)) ) + shared.setupDefaultValue( + for: \.promptToCodeCodeFontSize, + defaultValue: shared.value(for: \.suggestionCodeFontSize) + ) } } diff --git a/Tool/Sources/SharedUIComponents/CodeBlock.swift b/Tool/Sources/SharedUIComponents/CodeBlock.swift index 191d385f..c66a816a 100644 --- a/Tool/Sources/SharedUIComponents/CodeBlock.swift +++ b/Tool/Sources/SharedUIComponents/CodeBlock.swift @@ -9,6 +9,7 @@ public struct CodeBlock: View { public let highlightedCode: [NSAttributedString] public let firstLinePrecedingSpaceCount: Int public let fontSize: Double + public let droppingLeadingSpaces: Bool public init( code: String, @@ -16,12 +17,14 @@ public struct CodeBlock: View { startLineIndex: Int, colorScheme: ColorScheme, firstLinePrecedingSpaceCount: Int = 0, - fontSize: Double + fontSize: Double, + droppingLeadingSpaces: Bool ) { self.code = code self.language = language self.startLineIndex = startLineIndex self.colorScheme = colorScheme + self.droppingLeadingSpaces = droppingLeadingSpaces self.firstLinePrecedingSpaceCount = firstLinePrecedingSpaceCount self.fontSize = fontSize let padding = firstLinePrecedingSpaceCount > 0 @@ -31,7 +34,8 @@ public struct CodeBlock: View { code: padding + code, language: language, colorScheme: colorScheme, - fontSize: fontSize + fontSize: fontSize, + droppingLeadingSpaces: droppingLeadingSpaces ) commonPrecedingSpaceCount = result.commonLeadingSpaceCount highlightedCode = result.code @@ -72,14 +76,14 @@ public struct CodeBlock: View { code: String, language: String, colorScheme: ColorScheme, - fontSize: Double + fontSize: Double, + droppingLeadingSpaces: Bool ) -> (code: [NSAttributedString], commonLeadingSpaceCount: Int) { return highlighted( code: code, language: language, brightMode: colorScheme != .dark, - droppingLeadingSpaces: UserDefaults.shared - .value(for: \.hideCommonPrecedingSpacesInSuggestion), + droppingLeadingSpaces: droppingLeadingSpaces, fontSize: fontSize ) } @@ -98,7 +102,8 @@ struct CodeBlock_Previews: PreviewProvider { startLineIndex: 0, colorScheme: .dark, firstLinePrecedingSpaceCount: 0, - fontSize: 12 + fontSize: 12, + droppingLeadingSpaces: true ) } } diff --git a/Tool/Sources/SharedUIComponents/Experiment/NewCodeBlock.swift b/Tool/Sources/SharedUIComponents/Experiment/NewCodeBlock.swift index 87e716a9..21c53d0b 100644 --- a/Tool/Sources/SharedUIComponents/Experiment/NewCodeBlock.swift +++ b/Tool/Sources/SharedUIComponents/Experiment/NewCodeBlock.swift @@ -12,6 +12,7 @@ struct _CodeBlock: View { let commonPrecedingSpaceCount: Int let highlightedCode: AttributedString let colorScheme: ColorScheme + let droppingLeadingSpaces: Bool /// Create a text edit view with a certain text that uses a certain options. /// - Parameters: @@ -24,11 +25,13 @@ struct _CodeBlock: View { firstLinePrecedingSpaceCount: Int, colorScheme: ColorScheme, fontSize: Double, + droppingLeadingSpaces: Bool, selection: Binding = .constant(nil) ) { _selection = selection self.fontSize = fontSize self.colorScheme = colorScheme + self.droppingLeadingSpaces = droppingLeadingSpaces let padding = firstLinePrecedingSpaceCount > 0 ? String(repeating: " ", count: firstLinePrecedingSpaceCount) @@ -37,7 +40,8 @@ struct _CodeBlock: View { code: padding + code, language: language, colorScheme: colorScheme, - fontSize: fontSize + fontSize: fontSize, + droppingLeadingSpaces: droppingLeadingSpaces ) commonPrecedingSpaceCount = result.commonLeadingSpaceCount highlightedCode = result.code @@ -65,14 +69,14 @@ struct _CodeBlock: View { code: String, language: String, colorScheme: ColorScheme, - fontSize: Double + fontSize: Double, + droppingLeadingSpaces: Bool ) -> (code: AttributedString, commonLeadingSpaceCount: Int) { let (lines, commonLeadingSpaceCount) = highlighted( code: code, language: language, brightMode: colorScheme != .dark, - droppingLeadingSpaces: UserDefaults.shared - .value(for: \.hideCommonPrecedingSpacesInSuggestion), + droppingLeadingSpaces: droppingLeadingSpaces, fontSize: fontSize, replaceSpacesWithMiddleDots: false ) @@ -142,7 +146,7 @@ private struct _CodeBlockRepresentable: NSViewRepresentable { context.coordinator.parent = self let textView = scrollView.documentView as! STTextViewFrameObservable - + textView.onHeightChange = onHeightChange textView.showsInvisibleCharacters = true textView.textContainer.lineBreakMode = .byCharWrapping @@ -241,7 +245,10 @@ private class STTextViewFrameObservable: STTextView { var onHeightChange: ((Double) -> Void)? func recalculateSize() { var maxY = 0 as Double - textLayoutManager.enumerateTextLayoutFragments(in: textLayoutManager.documentRange, options: [.ensuresLayout]) { fragment in + textLayoutManager.enumerateTextLayoutFragments( + in: textLayoutManager.documentRange, + options: [.ensuresLayout] + ) { fragment in print(fragment.layoutFragmentFrame) maxY = max(maxY, fragment.layoutFragmentFrame.maxY) return true @@ -287,3 +294,4 @@ private final class ColumnRuler: NSRulerView { ]) } } +