Repository: CodeEditApp/CodeEdit
File: CodeEdit/Features/Settings/Pages/SearchSettings/SearchSettingsView.swift (Swift)

Code before:

    @State private var selection: GlobPattern?

    func addPattern() {
        searchSettingsModel.ignoreGlobPatterns.append(GlobPattern(value: ""))
    }

    func removePattern(_ pattern: GlobPattern?) {
        let selectedIndex = searchSettingsModel.ignoreGlobPatterns.firstIndex {
            $0 == selection
        }

        let removeIndex = searchSettingsModel.ignoreGlobPatterns.firstIndex {
            $0 == selection
        }

        searchSettingsModel.ignoreGlobPatterns.removeAll {
            pattern == $0
        }

        if selectedIndex == removeIndex && !searchSettingsModel.ignoreGlobPatterns.isEmpty && selectedIndex != nil {
            selection = searchSettingsModel.ignoreGlobPatterns[
                selectedIndex == 0 ? 0 : (selectedIndex ?? 1) - 1
            ]
        }
    }

    var body: some View {
        List(selection: $selection) {
            ForEach(
                Array(searchSettingsModel.ignoreGlobPatterns.enumerated()),
                id: \.element
            ) { index, ignorePattern in
                IgnorePatternListItem(
                    pattern: $searchSettingsModel.ignoreGlobPatterns[index],
                    selectedPattern: $selection,
                    addPattern: addPattern,
                    removePattern: removePattern,
                    focusedField: $focusedField,
                    isLast: searchSettingsModel.ignoreGlobPatterns.count == index+1
                )
                .onAppear {
                    if ignorePattern.value.isEmpty {
                        focusedField = ignorePattern.id.uuidString
                    }
                }
            }
            .onMove { fromOffsets, toOffset in
                searchSettingsModel.ignoreGlobPatterns.move(fromOffsets: fromOffsets, toOffset: toOffset)
            }
        }

Review comment (suggestion):
In SwiftUI, I prefer organizing a struct with properties at the top, followed by the body, and then the methods at the bottom. It adds a nice flow to the code! Like here:
```swift
struct SomeView: View {
    @State private var x: Int = 0

    var body: some View {
        Text(hi())
    }

    func hi() -> String {
        return "hi"
    }
}
```

Code after:

    @State private var selection: GlobPattern?

    var body: some View {
        List(selection: $selection) {
            ForEach(
                Array(searchSettingsModel.ignoreGlobPatterns.enumerated()),
                id: \.element
            ) { index, ignorePattern in
                IgnorePatternListItem(
                    pattern: $searchSettingsModel.ignoreGlobPatterns[index],
                    selectedPattern: $selection,
                    addPattern: addPattern,
                    removePattern: removePattern,
                    focusedField: $focusedField,
                    isLast: searchSettingsModel.ignoreGlobPatterns.count == index+1
                )
                .onAppear {
                    if ignorePattern.value.isEmpty {
                        focusedField = ignorePattern.id.uuidString
                    }
                }
            }
            .onMove { fromOffsets, toOffset in
                searchSettingsModel.ignoreGlobPatterns.move(fromOffsets: fromOffsets, toOffset: toOffset)
            }
        }
        .frame(minHeight: 96)
        .contextMenu(
            forSelectionType: GlobPattern.self,
            menu: { selection in
                if let pattern = selection.first {
                    Button("Edit") {
                        focusedField = pattern.id.uuidString
                    }
                    Button("Add") {
                        addPattern()
                    }
                    Divider()
                    Button("Remove") {
                        if !searchSettingsModel.ignoreGlobPatterns.isEmpty {
                            removePattern(pattern)
                        }
                    }
                }
            },
            primaryAction: { selection in
                if let pattern = selection.first {
                    focusedField = pattern.id.uuidString
                }
            }