feat: virtual scrolling with chunked buffer for large files
- Add ChunkedBuffer for 64KB chunked file access with dirty-chunk eviction protection - Add LineIndex for precise byte-offset-to-line-number mapping - Refactor IO task system with context cancellation, typed priorities, and new task types (ReadChunk, BuildLineIndex, StatFile) - Add ReadFileAt to FileSystem interface (mock + real implementations) - Integrate virtual scrolling into editor layout - Add comprehensive tests for chunked buffer eviction, dirty-chunk safety, and full edit lifecycle
This commit is contained in:
@@ -9,6 +9,7 @@ type FileSystem interface {
|
||||
DirExists(path string) bool
|
||||
FileExists(path string) bool
|
||||
ReadFile(path string) ([]byte, error)
|
||||
ReadFileAt(path string, offset, size int) ([]byte, error)
|
||||
WriteFile(path string, content []byte) error
|
||||
WriteFileAtomic(path string, content []byte) error
|
||||
DeleteFile(path string) error
|
||||
|
||||
@@ -181,6 +181,34 @@ func (fs *FileSystem) ReadFile(path string) ([]byte, error) {
|
||||
return content, nil
|
||||
}
|
||||
|
||||
// ReadFileAt reads a specific range of bytes from a file at the given path.
|
||||
func (fs *FileSystem) ReadFileAt(path string, offset, size int) ([]byte, error) {
|
||||
fs.mu.Lock()
|
||||
|
||||
if fs.delay > 0 {
|
||||
fs.mu.Unlock()
|
||||
time.Sleep(fs.delay)
|
||||
fs.mu.Lock()
|
||||
}
|
||||
|
||||
f, ok := fs.files[path]
|
||||
if !ok {
|
||||
fs.mu.Unlock()
|
||||
return nil, fmt.Errorf("file not found: %s", path)
|
||||
}
|
||||
if offset >= len(f.Content) {
|
||||
fs.mu.Unlock()
|
||||
return []byte{}, nil
|
||||
}
|
||||
end := offset + size
|
||||
if end > len(f.Content) {
|
||||
end = len(f.Content)
|
||||
}
|
||||
content := append([]byte(nil), f.Content[offset:end]...)
|
||||
fs.mu.Unlock()
|
||||
return content, nil
|
||||
}
|
||||
|
||||
// WriteFile writes or replaces the content of a file at the given path.
|
||||
// This is the non-atomic path — use WriteFileAtomic for safety.
|
||||
func (fs *FileSystem) WriteFile(path string, content []byte) error {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package real
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"pad/internal/io/pool/types"
|
||||
@@ -37,6 +38,21 @@ func (fs *RealFileSystem) ReadFile(path string) ([]byte, error) {
|
||||
return os.ReadFile(filepath.Join(fs.Root, path))
|
||||
}
|
||||
|
||||
func (fs *RealFileSystem) ReadFileAt(path string, offset, size int) ([]byte, error) {
|
||||
fullPath := filepath.Join(fs.Root, path)
|
||||
f, err := os.Open(fullPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
buf := make([]byte, size)
|
||||
n, err := f.ReadAt(buf, int64(offset))
|
||||
if err != nil && err != io.EOF {
|
||||
return nil, err
|
||||
}
|
||||
return buf[:n], nil
|
||||
}
|
||||
|
||||
func (fs *RealFileSystem) WriteFile(path string, content []byte) error {
|
||||
// Simple write for backward compatibility if needed,
|
||||
// but defer to Atomic implementation.
|
||||
|
||||
+524
-375
File diff suppressed because it is too large
Load Diff
@@ -307,8 +307,8 @@ func TestTask_TaskType_String(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
if string(tt.taskType) != tt.want {
|
||||
t.Errorf("TaskType %v = %q, want %q", tt.taskType, string(tt.taskType), tt.want)
|
||||
if tt.taskType.String() != tt.want {
|
||||
t.Errorf("TaskType %v = %q, want %q", tt.taskType, tt.taskType.String(), tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package types
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// DirEntry interface defines the structure for directory entries.
|
||||
type DirEntry interface {
|
||||
@@ -8,3 +11,39 @@ type DirEntry interface {
|
||||
IsDir() bool
|
||||
Info() (os.FileInfo, error)
|
||||
}
|
||||
|
||||
// LineIndex maps line numbers to byte offsets within a file.
|
||||
// Built as a low-priority background task; cached on disk.
|
||||
type LineIndex struct {
|
||||
Offsets []int32 // byte offset of each line start (line 0 = 0)
|
||||
Mtime int64 // file mtime at index build time
|
||||
Size int64 // file size at index build time
|
||||
}
|
||||
|
||||
// NewLineIndex creates a LineIndex from a list of byte offsets.
|
||||
func NewLineIndex(offsets []int32, mtime int64, size int64) *LineIndex {
|
||||
return &LineIndex{
|
||||
Offsets: offsets,
|
||||
Mtime: mtime,
|
||||
Size: size,
|
||||
}
|
||||
}
|
||||
|
||||
// LineCount returns the number of lines in the index.
|
||||
func (li *LineIndex) LineCount() int {
|
||||
return len(li.Offsets)
|
||||
}
|
||||
|
||||
// ByteOffset returns the byte offset of the given line number.
|
||||
// Returns 0 if the line number is out of range.
|
||||
func (li *LineIndex) ByteOffset(line int) int {
|
||||
if line < 0 || line >= len(li.Offsets) {
|
||||
return 0
|
||||
}
|
||||
return int(li.Offsets[line])
|
||||
}
|
||||
|
||||
// String returns a string representation of the LineIndex.
|
||||
func (li *LineIndex) String() string {
|
||||
return fmt.Sprintf("LineIndex{lines=%d, size=%d, mtime=%d}", len(li.Offsets), li.Size, li.Mtime)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user