(ctx context.Context, data wshrpc.FetchSuggestionsData)
| 358 | } |
| 359 | |
| 360 | func fetchFileSuggestions(ctx context.Context, data wshrpc.FetchSuggestionsData) (*wshrpc.FetchSuggestionsResponse, error) { |
| 361 | // Only support file suggestions. |
| 362 | if data.SuggestionType != "file" { |
| 363 | return nil, fmt.Errorf("unsupported suggestion type: %q", data.SuggestionType) |
| 364 | } |
| 365 | |
| 366 | // Resolve the base directory, query prefix (for display) and search term. |
| 367 | baseDir, queryPrefix, searchTerm, err := resolveFileQuery(data.FileCwd, data.Query) |
| 368 | if err != nil { |
| 369 | return nil, fmt.Errorf("error resolving base dir: %w", err) |
| 370 | } |
| 371 | |
| 372 | // Use a cancellable context for directory listing. |
| 373 | listingCtx, cancelFn := context.WithCancel(ctx) |
| 374 | defer cancelFn() |
| 375 | |
| 376 | entriesCh, err := listDirectory(listingCtx, data.WidgetId, baseDir, 1000) |
| 377 | if err != nil { |
| 378 | return nil, fmt.Errorf("error listing directory: %w", err) |
| 379 | } |
| 380 | |
| 381 | const maxEntries = MaxSuggestions // top-k entries |
| 382 | |
| 383 | // Always use a heap. |
| 384 | var topHeap scoredEntryHeap |
| 385 | heap.Init(&topHeap) |
| 386 | |
| 387 | var patternRunes []rune |
| 388 | if searchTerm != "" { |
| 389 | patternRunes = []rune(strings.ToLower(searchTerm)) |
| 390 | } |
| 391 | |
| 392 | var slab util.Slab |
| 393 | var index int // used for ordering when searchTerm is empty |
| 394 | |
| 395 | // Process each directory entry. |
| 396 | for result := range entriesCh { |
| 397 | if result.Err != nil { |
| 398 | return nil, fmt.Errorf("error reading directory: %w", result.Err) |
| 399 | } |
| 400 | de := result.Entry |
| 401 | fileName := de.Name() |
| 402 | var score int |
| 403 | var candidatePositions []int |
| 404 | |
| 405 | if searchTerm != "" { |
| 406 | // Perform fuzzy matching. |
| 407 | candidate := strings.ToLower(fileName) |
| 408 | text := util.ToChars([]byte(candidate)) |
| 409 | matchResult, positions := algo.FuzzyMatchV2(false, true, true, &text, patternRunes, true, &slab) |
| 410 | if matchResult.Score <= 0 { |
| 411 | index++ |
| 412 | continue |
| 413 | } |
| 414 | score = matchResult.Score |
| 415 | if positions != nil { |
| 416 | candidatePositions = *positions |
| 417 | } |
no test coverage detected