From eb47614bcd6304dc0bc2edd9da1ffc47d05edc72 Mon Sep 17 00:00:00 2001 From: Shx Guo Date: Wed, 27 Mar 2024 15:37:53 +0800 Subject: [PATCH 1/4] Add promptToCodeCodeFontSize key --- Tool/Sources/Preferences/Keys.swift | 4 ++++ Tool/Sources/Preferences/UserDefaults.swift | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/Tool/Sources/Preferences/Keys.swift b/Tool/Sources/Preferences/Keys.swift index d020accf..428daef1 100644 --- a/Tool/Sources/Preferences/Keys.swift +++ b/Tool/Sources/Preferences/Keys.swift @@ -294,6 +294,10 @@ public extension UserDefaultPreferenceKeys { var enableSenseScopeByDefaultInPromptToCode: PreferenceKey { .init(defaultValue: false, key: "EnableSenseScopeByDefaultInPromptToCode") } + + var promptToCodeCodeFontSize: PreferenceKey { + .init(defaultValue: 13, key: "PromptToCodeCodeFontSize") + } } // 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) + ) } } From c4a6c02503ea18f0a957ffc06496018205f17a56 Mon Sep 17 00:00:00 2001 From: Shx Guo Date: Wed, 27 Mar 2024 15:48:33 +0800 Subject: [PATCH 2/4] Add hideCommonPrecedingSpacesInPromptToCode key --- Tool/Sources/Preferences/Keys.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Tool/Sources/Preferences/Keys.swift b/Tool/Sources/Preferences/Keys.swift index 428daef1..a7588692 100644 --- a/Tool/Sources/Preferences/Keys.swift +++ b/Tool/Sources/Preferences/Keys.swift @@ -298,6 +298,10 @@ public extension UserDefaultPreferenceKeys { var promptToCodeCodeFontSize: PreferenceKey { .init(defaultValue: 13, key: "PromptToCodeCodeFontSize") } + + var hideCommonPrecedingSpacesInPromptToCode: PreferenceKey { + .init(defaultValue: true, key: "HideCommonPrecedingSpacesInPromptToCode") + } } // MARK: - Suggestion From 5dd8ee0dc3d30cbb2886c388fea46eb4e844d3cf Mon Sep 17 00:00:00 2001 From: Shx Guo Date: Wed, 27 Mar 2024 15:49:02 +0800 Subject: [PATCH 3/4] Update CodeBlock to accept droppingLeadingSpaces in init --- .../CodeBlockSuggestionPanel.swift | 4 +++- .../PromptToCodePanel.swift | 6 ++++-- .../SharedUIComponents/CodeBlock.swift | 17 ++++++++++------ .../Experiment/NewCodeBlock.swift | 20 +++++++++++++------ 4 files changed, 32 insertions(+), 15 deletions(-) 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/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 { ]) } } + From c339de7acb19ce0ea6ee4ee95bd94ec0fe596c8f Mon Sep 17 00:00:00 2001 From: Shx Guo Date: Wed, 27 Mar 2024 15:49:26 +0800 Subject: [PATCH 4/4] Add settings field --- .../PromptToCodeSettingsView.swift | 20 +++++++++---------- .../Sources/SuggestionWidget/WidgetView.swift | 10 ---------- 2 files changed, 10 insertions(+), 20 deletions(-) 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/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()