Skip to content

Commit

Permalink
Added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-ludwig committed Nov 16, 2023
1 parent 0b4d2a9 commit 4ee1921
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion SearchKitDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@
children = (
617658832AF161AB000C5197 /* SearchKitDemoApp.swift */,
617658872AF161AB000C5197 /* ContentView.swift */,
61CF23122B062247003B3C48 /* String+AppearancesOfSubstring.swift */,
61CF23102B061FBF003B3C48 /* SearchManager.swift */,
61CA4C7C2AFAC205005AC971 /* SidebarView.swift */,
61FE68302B03FEFF0059E0FD /* FileTabItemView.swift */,
61FE68322B0400720059E0FD /* SearchResultsFileTabItem.swift */,
617658B32AF16B04000C5197 /* FileViewModel.swift */,
61A4A33F2B013DC6004802D7 /* SearchResultsViewModel.swift */,
61FE682E2B03FD7D0059E0FD /* String+SafeOffset.swift */,
61CF23122B062247003B3C48 /* String+AppearancesOfSubstring.swift */,
61E2E77E2AF3EDC700E3AA4A /* Indexer */,
617658892AF161AC000C5197 /* Assets.xcassets */,
6176588E2AF161AD000C5197 /* Info.plist */,
Expand Down
24 changes: 20 additions & 4 deletions SearchKitDemo/SearchManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class SearchManager: ObservableObject {
}
}

/// Indexes the content of the given files using the specified indexer.
///
/// The function iterates through each file in the `files` array, adds its content to the indexer, and then flushes the indexer to ensure the data is processed.
func index() {
let startTime = Date()
guard let indexer = indexer else {
Expand All @@ -89,6 +92,13 @@ class SearchManager: ObservableObject {
print(Date().timeIntervalSince(startTime))
}

/// Deletes a file from the index and removes it from the files array.
///
/// - Parameters:
/// - url: The URL of the file to be deleted.
///
/// - Returns:
/// `true` if the file was successfully deleted from the indexer and removed from the files array; otherwise, returns `false`.
func delete(url: URL) -> Bool {
guard let indexer = indexer else {
return false
Expand All @@ -104,6 +114,10 @@ class SearchManager: ObservableObject {
return success
}

/// Searches for a given query within the indexed documents and updates the search results.
///
/// - Parameters:
/// - searchQuery: The search query string.
public func search(searchQuery: String) {
guard let indexer = indexer else {
return
Expand All @@ -125,10 +139,13 @@ class SearchManager: ObservableObject {
searchResults = newSearchResults
print(Date().timeIntervalSince(startTime))
}



/// Addes line matchings to a `SearchResultsViewModel` array. That means if a search result is a file, and the search term appears in the file, the function will add the line number, line content, and keyword range to the `SearchResultsViewModel`.
///
/// - Parameters:
/// - query: The search query string.
/// - searchResults: An inout parameter containing the array of `SearchResultsViewModel` to be evaluated. It will be modified to include line matches.
private func evaluateResults(query: String, searchResults: inout [SearchResultsViewModel]) {

searchResults = searchResults.map { result in
var newResult = result
var newMatches = [SearchResultLineMatchesModel]()
Expand All @@ -147,7 +164,6 @@ class SearchManager: ObservableObject {
for match in matches {
newMatches.append(SearchResultLineMatchesModel(file: result.url, lineNumber: match[0] as! Int, lineContent: match[1] as! String, keywordRange: match[2] as! Range<String.Index>))
}
// result.lineMatches.append(contentsOf: newMatches)
}
}
newMatches.forEach { match in
Expand Down

0 comments on commit 4ee1921

Please sign in to comment.