Repository: CodeEditApp/CodeEdit
File: CodeEdit/Features/Documents/WorkspaceDocument+Search.swift (Swift)

Code before:

            guard let url = workspace.fileURL else {
                return
            }

            indexStatus = .indexing(progress: 0.0)

            Task.detached {
                let filePaths = self.getFileURLs(at: url)
                let asyncController = SearchIndexer.AsyncManager(index: indexer)
                var lastProgress: Double = 0

                for await (file, index) in AsyncFileIterator(fileURLs: filePaths) {
                    _ = await asyncController.addText(files: [file], flushWhenComplete: false)
                    let progress = Double(index) / Double(filePaths.count)

                    // Send only if difference is > 0.5%, to keep updates from sending too frequently
                    if progress - lastProgress > 0.005 || index == filePaths.count - 1 {
                        lastProgress = progress
                        await MainActor.run {
                            self.indexStatus = .indexing(progress: progress)
                        }
                    }
                }
                asyncController.index.flush()
                asyncController.index.compact()

                await MainActor.run {
                    self.indexStatus = .done
                }
            }
        }

        /// Retrieves an array of file URLs within the specified directory URL.
        ///
        /// - Parameter url: The URL of the directory to search for files.
        ///
        /// - Returns: An array of file URLs found within the specified directory.
        func getFileURLs(at url: URL) -> [URL] {
            let enumerator = FileManager.default.enumerator(
                at: url,
                includingPropertiesForKeys: [.isRegularFileKey],
                options: [.skipsHiddenFiles, .skipsPackageDescendants]
            )
            return enumerator?.allObjects as? [URL] ?? []
        }

        /// Creates a search term based on the given query and search mode.
        ///
        /// - Parameter query: The original user query string.
        ///

Review comment (suggestion):
I'm uncertain about the necessity of calling `index.compact()` here; it should be invoked only if the index becomes significantly fragmented.
**Apple Docs:**
Over time, as document URL objects (of type SKDocument) and associated contents get added to and removed from an index, the index’s disk or memory footprint may grow due to fragmentation.
Compacting can take a significant amount of time. Do not call SKIndexCompact(_:) on the main thread in an application with a user interface. Call it only if the index is significantly fragmented and according to the needs of your application.
Calling SKIndexCompact(_:) changes the block allocation for an index’s backing store. Close all clients of an index before calling this function.

Code after:

            guard let url = workspace.fileURL else {
                return
            }

            indexStatus = .indexing(progress: 0.0)

            Task.detached {
                let filePaths = self.getFileURLs(at: url)
                let asyncController = SearchIndexer.AsyncManager(index: indexer)
                var lastProgress: Double = 0

                for await (file, index) in AsyncFileIterator(fileURLs: filePaths) {
                    _ = await asyncController.addText(files: [file], flushWhenComplete: false)
                    let progress = Double(index) / Double(filePaths.count)

                    // Send only if difference is > 0.5%, to keep updates from sending too frequently
                    if progress - lastProgress > 0.005 || index == filePaths.count - 1 {
                        lastProgress = progress
                        await MainActor.run {
                            self.indexStatus = .indexing(progress: progress)
                        }
                    }
                }
                asyncController.index.flush()

                await MainActor.run {
                    self.indexStatus = .done
                }
            }
        }

        /// Retrieves an array of file URLs within the specified directory URL.
        ///
        /// - Parameter url: The URL of the directory to search for files.
        ///
        /// - Returns: An array of file URLs found within the specified directory.
        func getFileURLs(at url: URL) -> [URL] {
            let enumerator = FileManager.default.enumerator(
                at: url,
                includingPropertiesForKeys: [.isRegularFileKey],
                options: [.skipsHiddenFiles, .skipsPackageDescendants]
            )
            return enumerator?.allObjects as? [URL] ?? []
        }

        /// Creates a search term based on the given query and search mode.
        ///
        /// - Parameter query: The original user query string.
        ///
        /// - Returns: A modified search term according to the specified search mode.