Skip to content

Commit

Permalink
add open files to context with priority
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmacarthy committed Nov 26, 2024
1 parent 8da6789 commit 2862b04
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ export interface InteractionItem {
name: string | null | undefined
sessionLength: number
visits: number | null | undefined
isOpen?: boolean
relevanceScore: number | null | undefined
isOpen: boolean
relevanceScore?: number | null | undefined
activeLines: {
line: number
character: number
Expand Down
96 changes: 70 additions & 26 deletions src/extension/file-interaction.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as vscode from "vscode"

import { InteractionItem } from "../common/types"

import { LRUCache } from "./cache"
Expand All @@ -8,15 +10,34 @@ export class FileInteractionCache {
private _interactions = new LRUCache<InteractionItem>(20)
private _sessionPauseTime: Date | null = null
private _sessionStartTime: Date | null = null
private readonly _inactivityThreshold = 5 * 60 * 1000 // 5 minutes
private readonly _disposables: vscode.Disposable[] = []
private readonly _inactivityThreshold = 5 * 60 * 1000
private readonly FILTER_REGEX = /\.git|git|package.json|.hg/
private static readonly KEY_STROKE_WEIGHT = 2
private static readonly OPEN_FILE_WEIGHT = 10
private static readonly RECENCY_WEIGHT = 2.1
private static readonly SESSION_LENGTH_WEIGHT = 1
private static readonly VISIT_WEIGHT = 0.5

constructor() {
this._disposables.push(
vscode.workspace.onDidCloseTextDocument((doc) => {
const item = this._interactions.get(doc.fileName)
if (item) {
this._interactions.set(doc.fileName, { ...item, isOpen: false })
}
})
)

this.resetInactivityTimeout()
this.addOpenFilesWithPriority()
}

dispose() {
if (this._inactivityTimeout) {
clearTimeout(this._inactivityTimeout)
}
this._disposables.forEach((d) => d.dispose())
}

resetInactivityTimeout() {
Expand Down Expand Up @@ -50,33 +71,46 @@ export class FileInteractionCache {
private calculateRelevanceScore(interaction: InteractionItem | null): number {
if (!interaction) return 0

const recency = Date.now() - (interaction.lastVisited || 0)
const score =
const recencyInHours =
(Date.now() - (interaction.lastVisited || 0)) / (1000 * 60 * 60)
const recencyScore = Math.max(0, 24 - recencyInHours) // Caps at 24 hours

return (
(interaction.keyStrokes || 0) * FileInteractionCache.KEY_STROKE_WEIGHT +
(interaction.visits || 0) * FileInteractionCache.VISIT_WEIGHT +
(interaction.sessionLength || 0) *
FileInteractionCache.SESSION_LENGTH_WEIGHT -
recency * FileInteractionCache.RECENCY_WEIGHT

return (
score + (interaction.isOpen ? FileInteractionCache.OPEN_FILE_WEIGHT : 0)
FileInteractionCache.SESSION_LENGTH_WEIGHT +
recencyScore * FileInteractionCache.RECENCY_WEIGHT +
(interaction.isOpen ? FileInteractionCache.OPEN_FILE_WEIGHT : 0)
)
}

getAll(): InteractionItem[] {
return Array.from(this._interactions.getAll())
.map(([name, interaction]) => ({
name,
keyStrokes: interaction?.keyStrokes || 0,
visits: interaction?.visits || 0,
sessionLength: interaction?.sessionLength || 0,
lastVisited: interaction?.lastVisited || 0,
activeLines: interaction?.activeLines || [],
relevanceScore: this.calculateRelevanceScore(interaction),
isOpen: interaction?.isOpen || false,
name,
relevanceScore: this.calculateRelevanceScore(interaction)
}))
.sort((a, b) => b.relevanceScore - a.relevanceScore)
}

addOpenFilesWithPriority(): void {
const openFiles = vscode.workspace.textDocuments
.filter((doc) => !doc.isUntitled)
.map((doc) => doc.fileName)
.filter((fileName) => !fileName.match(this.FILTER_REGEX))

for (const filePath of openFiles) {
this.putOpenFile(filePath)
}
}

getCurrentFile(): string | null {
return this._currentFile
}
Expand All @@ -88,7 +122,7 @@ export class FileInteractionCache {
this._interactions.set(this._currentFile, {
...item,
visits: (item.visits || 0) + 1,
lastVisited: Date.now(),
lastVisited: Date.now()
})
}

Expand All @@ -102,9 +136,9 @@ export class FileInteractionCache {
keyStrokes: (item.keyStrokes || 0) + 1,
activeLines: [
...item.activeLines,
{ line: currentLine, character: currentCharacter },
{ line: currentLine, character: currentCharacter }
],
lastVisited: Date.now(),
lastVisited: Date.now()
})

this.resumeSession()
Expand All @@ -113,7 +147,7 @@ export class FileInteractionCache {

startSession(filePath: string): void {
this._sessionStartTime = new Date()
this.put(filePath)
this.putOpenFile(filePath)
this.incrementVisits()
this.resetInactivityTimeout()
}
Expand All @@ -137,7 +171,7 @@ export class FileInteractionCache {
this._interactions.set(this._currentFile, {
...item,
sessionLength: (item.sessionLength || 0) + sessionLength,
lastVisited: Date.now(),
lastVisited: Date.now()
})
}

Expand All @@ -151,22 +185,32 @@ export class FileInteractionCache {
this._interactions.delete(filePath)
}

put(filePath: string): void {
this._currentFile = filePath.replace(".git", "").replace(".hg", "")
const fileExtension = this._currentFile.split(".").pop()
if (this._interactions.get(this._currentFile)) {
this.incrementVisits()
return
}
if (this._currentFile.includes(".") && fileExtension) {
this._interactions.set(this._currentFile, {
name: this._currentFile,
putOpenFile(filePath: string): void {
this.putFile(filePath, true)
}

putClosedFile(filePath: string): void {
this.putFile(filePath, false)
}

putFile(filePath: string, isOpen: boolean): void {
if (filePath.match(this.FILTER_REGEX)) return

this._currentFile = filePath

const fileExtension = filePath.split(".").pop()

if (this._interactions.get(filePath)) return

if (filePath.includes(".") && fileExtension) {
this._interactions.set(filePath, {
name: filePath,
keyStrokes: 0,
visits: 1,
sessionLength: 0,
activeLines: [],
lastVisited: Date.now(),
relevanceScore: 0,
isOpen
})
}
}
Expand Down
1 change: 1 addition & 0 deletions src/extension/providers/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ export class CompletionProvider
}

private async getFileInteractionContext() {
this._fileInteractionCache.addOpenFilesWithPriority()
const interactions = this._fileInteractionCache.getAll()
const currentFileName = this._document?.fileName || ""

Expand Down

0 comments on commit 2862b04

Please sign in to comment.