Ben Rohlfs | 4deb8df5 | 2024-05-10 12:53:19 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2024 Google LLC |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 6 | export interface AutocompletionContext { |
| 7 | draftContent: string; |
| 8 | draftContentLength?: number; |
| 9 | commentCompletion: string; |
| 10 | commentCompletionLength?: number; |
| 11 | |
| 12 | isFullCommentPrediction?: boolean; |
| 13 | draftInSyncWithSuggestionLength?: number; |
| 14 | modelVersion?: string; |
Ben Rohlfs | 8b0e91a | 2024-05-23 14:42:23 +0200 | [diff] [blame] | 15 | outcome?: number; |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 16 | requestDurationMs?: number; |
| 17 | |
| 18 | commentId?: string; |
| 19 | commentNumber?: number; |
| 20 | filePath?: string; |
| 21 | fileExtension?: string; |
| 22 | |
| 23 | similarCharacters?: number; |
| 24 | maxSimilarCharacters?: number; |
| 25 | acceptedSuggestionsCount?: number; |
| 26 | totalAcceptedCharacters?: number; |
| 27 | savedDraftLength?: number; |
Ben Rohlfs | 40f41af | 2024-05-22 12:57:34 +0200 | [diff] [blame] | 28 | |
| 29 | hasDraftChanged?: boolean; |
Ben Rohlfs | 4deb8df5 | 2024-05-10 12:53:19 +0200 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Caching for autocompleting text, e.g. comments. |
| 34 | * |
| 35 | * If the user continues typing text that matches the completion hint, then keep the hint. |
| 36 | * |
| 37 | * If the user backspaces, then continue using previous hint. |
| 38 | */ |
| 39 | export class AutocompleteCache { |
| 40 | /** |
| 41 | * We are using an ordered list instead of a map here, because we want to evict the oldest |
| 42 | * entries, if the capacity is exceeded. And we want to prefer newer entries over older |
| 43 | * entries, if both match the criteria for being reused. |
| 44 | */ |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 45 | private cache: AutocompletionContext[] = []; |
Ben Rohlfs | 4deb8df5 | 2024-05-10 12:53:19 +0200 | [diff] [blame] | 46 | |
| 47 | constructor(private readonly capacity = 10) {} |
| 48 | |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 49 | get(content: string): AutocompletionContext | undefined { |
Ben Rohlfs | 4deb8df5 | 2024-05-10 12:53:19 +0200 | [diff] [blame] | 50 | if (content === '') return undefined; |
| 51 | for (let i = this.cache.length - 1; i >= 0; i--) { |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 52 | const cachedContext = this.cache[i]; |
| 53 | const completionContent = cachedContext.draftContent; |
| 54 | const completionHint = cachedContext.commentCompletion; |
Ben Rohlfs | 4deb8df5 | 2024-05-10 12:53:19 +0200 | [diff] [blame] | 55 | const completionFull = completionContent + completionHint; |
| 56 | if (completionContent.length > content.length) continue; |
| 57 | if (!completionFull.startsWith(content)) continue; |
| 58 | if (completionFull === content) continue; |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 59 | const hint = completionFull.substring(content.length); |
| 60 | return { |
| 61 | ...cachedContext, |
| 62 | draftContent: content, |
| 63 | commentCompletion: hint, |
| 64 | draftInSyncWithSuggestionLength: |
| 65 | content.length - completionContent.length, |
| 66 | }; |
Ben Rohlfs | 4deb8df5 | 2024-05-10 12:53:19 +0200 | [diff] [blame] | 67 | } |
| 68 | return undefined; |
| 69 | } |
| 70 | |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 71 | set(context: AutocompletionContext) { |
| 72 | const index = this.cache.findIndex( |
| 73 | c => c.draftContent === context.draftContent |
| 74 | ); |
Ben Rohlfs | 4deb8df5 | 2024-05-10 12:53:19 +0200 | [diff] [blame] | 75 | if (index !== -1) { |
| 76 | this.cache.splice(index, 1); |
| 77 | } else if (this.cache.length >= this.capacity) { |
| 78 | this.cache.shift(); |
| 79 | } |
Ben Rohlfs | dd88cd9 | 2024-05-08 13:29:39 +0200 | [diff] [blame] | 80 | this.cache.push(context); |
Ben Rohlfs | 4deb8df5 | 2024-05-10 12:53:19 +0200 | [diff] [blame] | 81 | } |
| 82 | } |