Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/font size adjustment prompt to code #472

Merged
merged 4 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 10 additions & 10 deletions Core/Sources/HostApp/FeatureSettings/PromptToCodeSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -101,7 +102,8 @@ struct CodeBlockSuggestionPanel: View {
language: suggestion.language,
startLineIndex: suggestion.startLineIndex,
colorScheme: colorScheme,
fontSize: fontSize
fontSize: fontSize,
droppingLeadingSpaces: hideCommonPrecedingSpaces
)
.frame(maxWidth: .infinity)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ extension PromptToCodePanel {
struct Content: View {
let store: StoreOf<PromptToCode>
@Environment(\.colorScheme) var colorScheme
@AppStorage(\.suggestionCodeFontSize) var fontSize
@AppStorage(\.promptToCodeCodeFontSize) var fontSize
@AppStorage(\.hideCommonPrecedingSpacesInPromptToCode) var hideCommonPrecedingSpaces

struct CodeContent: Equatable {
var code: String
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 0 additions & 10 deletions Core/Sources/SuggestionWidget/WidgetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions Tool/Sources/Preferences/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@ public extension UserDefaultPreferenceKeys {
var enableSenseScopeByDefaultInPromptToCode: PreferenceKey<Bool> {
.init(defaultValue: false, key: "EnableSenseScopeByDefaultInPromptToCode")
}

var promptToCodeCodeFontSize: PreferenceKey<Double> {
.init(defaultValue: 13, key: "PromptToCodeCodeFontSize")
}

var hideCommonPrecedingSpacesInPromptToCode: PreferenceKey<Bool> {
.init(defaultValue: true, key: "HideCommonPrecedingSpacesInPromptToCode")
}
}

// MARK: - Suggestion
Expand Down
4 changes: 4 additions & 0 deletions Tool/Sources/Preferences/UserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public extension UserDefaults {
for: \.suggestionFeatureProvider,
defaultValue: .builtIn(shared.deprecatedValue(for: \.oldSuggestionFeatureProvider))
)
shared.setupDefaultValue(
for: \.promptToCodeCodeFontSize,
defaultValue: shared.value(for: \.suggestionCodeFontSize)
)
}
}

Expand Down
17 changes: 11 additions & 6 deletions Tool/Sources/SharedUIComponents/CodeBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ public struct CodeBlock: View {
public let highlightedCode: [NSAttributedString]
public let firstLinePrecedingSpaceCount: Int
public let fontSize: Double
public let droppingLeadingSpaces: Bool

public init(
code: String,
language: String,
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
Expand All @@ -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
Expand Down Expand Up @@ -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
)
}
Expand All @@ -98,7 +102,8 @@ struct CodeBlock_Previews: PreviewProvider {
startLineIndex: 0,
colorScheme: .dark,
firstLinePrecedingSpaceCount: 0,
fontSize: 12
fontSize: 12,
droppingLeadingSpaces: true
)
}
}
Expand Down
20 changes: 14 additions & 6 deletions Tool/Sources/SharedUIComponents/Experiment/NewCodeBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -24,11 +25,13 @@ struct _CodeBlock: View {
firstLinePrecedingSpaceCount: Int,
colorScheme: ColorScheme,
fontSize: Double,
droppingLeadingSpaces: Bool,
selection: Binding<NSRange?> = .constant(nil)
) {
_selection = selection
self.fontSize = fontSize
self.colorScheme = colorScheme
self.droppingLeadingSpaces = droppingLeadingSpaces

let padding = firstLinePrecedingSpaceCount > 0
? String(repeating: " ", count: firstLinePrecedingSpaceCount)
Expand All @@ -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
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -287,3 +294,4 @@ private final class ColumnRuler: NSRulerView {
])
}
}