diff --git a/desktop/internal/workbench/document_observation.go b/desktop/internal/workbench/document_observation.go index 817a67c68a..a9c8815035 100644 --- a/desktop/internal/workbench/document_observation.go +++ b/desktop/internal/workbench/document_observation.go @@ -106,7 +106,7 @@ func (a *App) unwatchDocument(p *project, removed *document) { } dir := filepath.Dir(removed.path) for _, d := range p.docs.list { - if filepath.Dir(d.path) == dir { + if !d.virtual && filepath.Dir(d.path) == dir { return } } @@ -123,7 +123,7 @@ func (a *App) drainDocumentWatch(p *project, dir string, slot *documentWatch) { watch, ready, err := slot.state() markDirectory := func() { for _, d := range p.docs.list { - if filepath.Dir(d.path) == dir { + if !d.virtual && filepath.Dir(d.path) == dir { d.checkPending = true } } @@ -154,7 +154,7 @@ func (a *App) drainDocumentWatch(p *project, dir string, slot *documentWatch) { names[name] = struct{}{} } for _, d := range p.docs.list { - if filepath.Dir(d.path) != dir { + if d.virtual || filepath.Dir(d.path) != dir { continue } name := filepath.Base(d.path) @@ -174,7 +174,16 @@ func (a *App) scheduleDocumentChecks(p *project) { if a.project != p { return } + var virtualWaiters []func(bool) for _, d := range p.docs.list { + if d.virtual { + d.checkPending = false + if len(d.checkWaiters) > 0 { + virtualWaiters = append(virtualWaiters, d.checkWaiters...) + d.checkWaiters = nil + } + continue + } if p.docs.checking >= documentChecksInFlight { break } @@ -225,10 +234,14 @@ func (a *App) scheduleDocumentChecks(p *project) { waiters := d.checkWaiters d.checkWaiters = nil for _, done := range waiters { - if d.checkFailed || d.conflicted { + if d.conflicted { done(false) } else if d.dirty() || d.view.IsComposing() { - a.saveBeforeClose(p, d, done) + if d.checkFailed { + done(false) + } else { + a.saveBeforeClose(p, d, done) + } } else { done(true) } @@ -239,6 +252,9 @@ func (a *App) scheduleDocumentChecks(p *project) { } }) } + for _, done := range virtualWaiters { + done(true) + } } func (a *App) clearDocumentConflict(d *document) { diff --git a/desktop/internal/workbench/document_observation_test.go b/desktop/internal/workbench/document_observation_test.go index a9bd25af6f..b3e58494c0 100644 --- a/desktop/internal/workbench/document_observation_test.go +++ b/desktop/internal/workbench/document_observation_test.go @@ -2,6 +2,7 @@ package workbench import ( "bytes" + "context" "errors" "fmt" "os" @@ -15,6 +16,7 @@ import ( "github.com/1lck/Lithe-IDEA/desktop/internal/documentio" "github.com/1lck/Lithe-IDEA/desktop/internal/editorview" "github.com/1lck/Lithe-IDEA/desktop/internal/i18n" + "github.com/1lck/Lithe-IDEA/desktop/internal/pluginservice" "github.com/vivaui/viva/app" ) @@ -696,3 +698,141 @@ func TestRecreateRejectsFileThatAppearedAfterAcknowledgement(t *testing.T) { t.Fatal("a second deletion silently cleared the unresolved conflict") } } + +func TestVirtualDocumentClosesWithoutExternalCheckOrError(t *testing.T) { + h := newHarness(t, &testKernel{Client: core.NewClient()}) + root := t.TempDir() + h.open(root) + p := h.app.project + w := pluginWorkbench{app: h.app} + uri := "jdt://contents/java.base/java/lang/SpringApplication.class" + if _, err := callOnWindow(h, "open virtual document", func(ctx context.Context) (struct{}, error) { + return struct{}{}, w.OpenVirtualDocument(ctx, root, pluginservice.VirtualDocument{ + URI: uri, + Name: "SpringApplication.java", + Text: "public class SpringApplication {}\n", + }) + }); err != nil { + t.Fatal(err) + } + doc := p.docs.byPath(uri) + if doc == nil || !doc.virtual { + t.Fatalf("virtual document = %+v", doc) + } + readCalls := 0 + h.app.documentReader = func(path string) ([]byte, error) { + readCalls++ + return nil, errors.New("virtual URI is not a disk file") + } + doc.checkPending = true + h.app.scheduleDocumentChecks(p) + awaitDocumentState(t, h, func() bool { return !doc.checking && !doc.checkPending }) + if readCalls != 0 || doc.checkFailed { + t.Fatalf("virtual document ran disk check: readCalls=%d checkFailed=%v", readCalls, doc.checkFailed) + } + h.app.closeDocument(p, doc.id) + awaitDocumentState(t, h, func() bool { return p.docs.find(doc.id) == nil || doc.checkFailed }) + if p.docs.find(doc.id) != nil { + t.Fatalf("virtual document did not close; notice=%q checkFailed=%v", h.app.notice, doc.checkFailed) + } + if strings.Contains(h.app.notice, i18n.Shared(h.app.language(), "desktop.document.externalReadFailed")) { + t.Fatalf("unexpected external read error notice: %q", h.app.notice) + } + + secondURI := "jdt://contents/java.base/java/lang/String.class" + if _, err := callOnWindow(h, "open second virtual document", func(ctx context.Context) (struct{}, error) { + return struct{}{}, w.OpenVirtualDocument(ctx, root, pluginservice.VirtualDocument{ + URI: secondURI, + Name: "String.java", + Text: "public final class String {}\n", + }) + }); err != nil { + t.Fatal(err) + } + h.app.closeProject() + awaitDocumentState(t, h, func() bool { return h.app.project == nil || !h.app.closingProject }) + if h.app.project != nil { + t.Fatalf("closing project blocked on virtual document; notice=%q", h.app.notice) + } +} + +func TestCleanDocumentClosesWhenExternalReadFails(t *testing.T) { + h := newHarness(t, &testKernel{Client: core.NewClient()}) + root := t.TempDir() + cleanPath := filepath.Join(root, "clean.txt") + dirtyPath := filepath.Join(root, "dirty.txt") + for _, path := range []string{cleanPath, dirtyPath} { + if err := os.WriteFile(path, []byte("initial"), 0o600); err != nil { + t.Fatal(err) + } + } + h.app.openProject(root) + p := h.app.project + h.app.openFile(p, cleanPath) + h.app.openFile(p, dirtyPath) + awaitDocumentState(t, h, func() bool { return p.docs.byPath(cleanPath) != nil && p.docs.byPath(dirtyPath) != nil }) + cleanDoc := p.docs.byPath(cleanPath) + dirtyDoc := p.docs.byPath(dirtyPath) + for _, watch := range p.docs.watches { + watch.close() + } + p.docs.watches = nil + awaitDocumentState(t, h, func() bool { + return !cleanDoc.checking && !cleanDoc.checkPending && !dirtyDoc.checking && !dirtyDoc.checkPending + }) + + h.app.documentReader = func(string) ([]byte, error) { + return nil, errors.New("external read failed") + } + + h.app.closeDocument(p, cleanDoc.id) + awaitDocumentState(t, h, func() bool { + return p.docs.find(cleanDoc.id) == nil || (cleanDoc.checkFailed && !cleanDoc.checking && !cleanDoc.checkPending) + }) + if p.docs.find(cleanDoc.id) != nil { + t.Fatalf("clean document could not be closed after external read error; notice=%q", h.app.notice) + } + + // Make dirtyDoc dirty while its close check is in flight and fails. + started := make(chan struct{}) + release := make(chan struct{}) + released := false + defer func() { + if !released { + close(release) + } + }() + h.app.documentReader = func(string) ([]byte, error) { + close(started) + <-release + return nil, errors.New("external read failed") + } + h.app.closeDocument(p, dirtyDoc.id) + select { + case <-started: + case <-time.After(5 * time.Second): + t.Fatal("dirty document close check did not start") + } + input := dirtyDoc.view.TextInput(app.TextInputContext{ID: dirtyDoc.hit}) + if _, err := input.OnEvent(app.TextEvent{Kind: app.TextInsert, Text: "unsaved"}); err != nil { + t.Fatal(err) + } + close(release) + released = true + awaitDocumentState(t, h, func() bool { return dirtyDoc.checkFailed && !dirtyDoc.checking && !dirtyDoc.checkPending }) + if p.docs.find(dirtyDoc.id) != dirtyDoc { + t.Fatal("dirty document was closed even though external read check failed") + } + + // Clean up dirty edits so closeProject runs on only clean documents and still succeeds. + h.app.documentReader = func(string) ([]byte, error) { + return nil, errors.New("external read failed") + } + for dirtyDoc.view.Dirty() && dirtyDoc.view.Undo() { + } + h.app.closeProject() + awaitDocumentState(t, h, func() bool { return h.app.project == nil || !h.app.closingProject }) + if h.app.project != nil { + t.Fatalf("clean project did not close when external read failed; notice=%q", h.app.notice) + } +} diff --git a/desktop/internal/workbench/documents.go b/desktop/internal/workbench/documents.go index b38292ac6a..b4878804a9 100644 --- a/desktop/internal/workbench/documents.go +++ b/desktop/internal/workbench/documents.go @@ -259,6 +259,10 @@ func (a *App) syntaxRegistry() *syntax.Registry { // group. The file is read and its language's grammar loaded away from the window thread; a // file that cannot be read, is binary or is too large is not opened and the banner says why. func (a *App) openFile(p *project, path string) { + if path == "" { + return + } + path = filepath.Clean(path) if d := p.docs.byPath(path); d != nil { a.selectDocument(p, d.id) return @@ -346,6 +350,7 @@ func (a *App) openFileAt(p *project, path string, line int) { if path == "" { return } + path = filepath.Clean(path) if d := p.docs.byPath(path); d != nil { a.selectDocument(p, d.id) if line > 0 { @@ -367,6 +372,7 @@ func (a *App) openFileAtPosition(p *project, path string, line, column int) { if path == "" { return } + path = filepath.Clean(path) a.recordNavigationJump(p, path, line, column) if d := p.docs.byPath(path); d != nil { a.selectDocument(p, d.id) @@ -719,6 +725,10 @@ func (a *App) saveDocumentMode(p *project, id int, auto bool) { // saveBeforeClose waits asynchronously for any in-flight save and the latest edits. func (a *App) saveBeforeClose(p *project, d *document, done func(bool)) { + if d.virtual { + done(true) + return + } if d.recoveredConflict { done(false) return @@ -886,6 +896,9 @@ func (a *App) saveProjectPass(p *project, verified map[int]int, done func(bool)) } } else { for _, candidate := range p.docs.list { + if candidate.virtual { + continue + } if revision, ok := verified[candidate.id]; !ok || revision != candidate.view.Revision() || candidate.checkPending || candidate.checking { d = candidate break diff --git a/desktop/internal/workbench/editor_essentials_test.go b/desktop/internal/workbench/editor_essentials_test.go index bcbb672dfd..af7764d329 100644 --- a/desktop/internal/workbench/editor_essentials_test.go +++ b/desktop/internal/workbench/editor_essentials_test.go @@ -4,11 +4,13 @@ import ( "context" "os" "path/filepath" + "strings" "testing" "github.com/1lck/Lithe-IDEA/desktop/internal/core" "github.com/1lck/Lithe-IDEA/desktop/internal/draftstore" "github.com/1lck/Lithe-IDEA/desktop/internal/editorview" + "github.com/1lck/Lithe-IDEA/desktop/internal/i18n" "github.com/vivaui/viva/keymap" ) @@ -308,3 +310,31 @@ func TestEditorEssentialsDiscardFromListWaitsForStorageAndClosesOpenScratch(t *t } h.app.drafts.open = false } + +func TestEditorEssentialsOutsideProjectFileDoesNotFailRecentFiles(t *testing.T) { + h, p, _ := essentialsDocument(t, "inside") + outsideDir := t.TempDir() + outsidePath := filepath.Join(outsideDir, "SpringApplication.java") + if err := os.WriteFile(outsidePath, []byte("public class SpringApplication {}\n"), 0o444); err != nil { + t.Fatal(err) + } + h.app.openFile(p, outsidePath) + h.settleChain() + if p.docs.byPath(outsidePath) == nil { + t.Fatal("outside dependency file did not open") + } + if strings.Contains(h.app.notice, i18n.Shared(h.app.language(), "desktop.recentFiles.failed")) { + t.Fatalf("outside file triggered recent files failure notice: %q", h.app.notice) + } + h.app.openRecentFiles(false) + h.settleChain() + for _, entry := range h.app.recentFiles.list { + if entry.Name == "SpringApplication.java" || strings.Contains(entry.Path, "..") { + t.Fatalf("outside file leaked into recent files: %+v", h.app.recentFiles.list) + } + } + if len(h.app.recentFiles.list) != 1 || h.app.recentFiles.list[0].Name != "notes.txt" { + t.Fatalf("unexpected recent files list: %+v", h.app.recentFiles.list) + } + h.app.recentFiles.open = false +} diff --git a/desktop/internal/workbench/navigation_history.go b/desktop/internal/workbench/navigation_history.go index c22ff73722..1826f16999 100644 --- a/desktop/internal/workbench/navigation_history.go +++ b/desktop/internal/workbench/navigation_history.go @@ -3,6 +3,7 @@ package workbench import ( "context" "os" + "path/filepath" "github.com/1lck/Lithe-IDEA/desktop/internal/core" "github.com/1lck/Lithe-IDEA/desktop/internal/i18n" @@ -104,7 +105,8 @@ func (a *App) stepNavigation(p *project, forward bool) { // openStepDestination opens a history destination without recording it as a jump. func (a *App) openStepDestination(p *project, destination core.NavigationLocation) { - if d := p.docs.byPath(destination.Document); d != nil { + path := filepath.Clean(destination.Document) + if d := p.docs.byPath(path); d != nil { a.selectDocument(p, d.id) d.view.GoToNavigation(destination.Line, destination.Column, false) return @@ -112,8 +114,8 @@ func (a *App) openStepDestination(p *project, destination core.NavigationLocatio if p.docs.locations == nil { p.docs.locations = make(map[string]documentLocation) } - p.docs.locations[destination.Document] = documentLocation{line: destination.Line + 1, column: destination.Column, columnSet: true} - a.openFile(p, destination.Document) + p.docs.locations[path] = documentLocation{line: destination.Line + 1, column: destination.Column, columnSet: true} + a.openFile(p, path) } // navigationHistoryShortcut handles Control-Alt-Left and Control-Alt-Right, the chords of the diff --git a/desktop/internal/workbench/recent_files.go b/desktop/internal/workbench/recent_files.go index 5a9ebed27e..8de55aff8f 100644 --- a/desktop/internal/workbench/recent_files.go +++ b/desktop/internal/workbench/recent_files.go @@ -44,13 +44,12 @@ func (a *App) noteRecentFile(p *project, d *document, kind string) { if d.virtual || d.scratch { return } - relative, err := filepath.Rel(p.root, d.path) - if err != nil { - a.recentFailed(err) + relative, inside := projectRelative(p.root, d.path) + if !inside { return } _, caret := d.view.SelectionOffsets() - event := core.EditorRecentEvent{Kind: kind, Path: filepath.ToSlash(relative), Name: d.name} + event := core.EditorRecentEvent{Kind: kind, Path: relative, Name: d.name} a.enqueueRecent(recentRequest{project: p, event: &event, text: d.view.TextSnapshot(), caret: caret}) } func (a *App) markRecentlyEdited(p *project, d *document) { diff --git a/shared/platform-feature-matrix/features/editor-recent-files.json b/shared/platform-feature-matrix/features/editor-recent-files.json index 0bfab8b8ea..8027e065d2 100644 --- a/shared/platform-feature-matrix/features/editor-recent-files.json +++ b/shared/platform-feature-matrix/features/editor-recent-files.json @@ -4,7 +4,7 @@ "group": "文档编辑", "capability": "最近文件与最近编辑列表,保留光标位置,提供过滤和打开入口", "owner": "Editor / Core", - "notes": "Viva 共享实现;匹配、顺序和恢复决策在外部 Rust Core。macOS 无头流程与截图已覆盖;macOS 原生窗口和 Windows 原生验收未执行。", + "notes": "Viva 共享实现;匹配、顺序和恢复决策在外部 Rust Core。工作区外的文件(含 Windows 跨盘符路径)跳过最近文件记录。macOS 无头流程与截图已覆盖;macOS 原生窗口和 Windows 原生验收未执行。", "verification": "依次打开和编辑两个文件,核对访问/编辑独立顺序;过滤、键盘与点击打开,重启后保留位置。 自动化:editor_essentials_test.go 与 editor_essentials_visual_test.go。", "macos": { "implementationStatus": "implemented", diff --git a/shared/platform-feature-matrix/features/workspace-document-sync.json b/shared/platform-feature-matrix/features/workspace-document-sync.json index 3401757c3a..a58819634c 100644 --- a/shared/platform-feature-matrix/features/workspace-document-sync.json +++ b/shared/platform-feature-matrix/features/workspace-document-sync.json @@ -9,6 +9,7 @@ "evidence": [ "desktop/internal/workbench/documents.go", "desktop/internal/workbench/document_observation.go", + "desktop/internal/workbench/document_observation_test.go", "desktop/internal/documentio/" ] }, @@ -18,11 +19,12 @@ "evidence": [ "desktop/internal/workbench/documents.go", "desktop/internal/workbench/document_observation.go", + "desktop/internal/workbench/document_observation_test.go", "desktop/internal/documentio/" ] }, "owner": "Workspace", - "notes": "Viva 已接入 Core 文档状态、受保护保存、自动保存与外部修改冲突提示;硬链接保存选择、旧版完整场景和 Windows 整机验收尚未完成。Windows 原生文件读写组件已实测,符号链接用例因权限跳过。", + "notes": "Viva 已接入 Core 文档状态、受保护保存、自动保存与外部修改冲突提示;虚拟只读文档关闭前跳过磁盘变化检查与目录监听,干净且未冲突的文档在外部读取失败时不阻断关闭(已删除文件仍受冲突保护,脏文档在外部校验失败时保留)。硬链接保存选择、旧版完整场景和 Windows 整机验收尚未完成。Windows 原生文件读写组件已实测,符号链接用例因权限跳过。", "verification": "编辑未保存文件、外部修改文件并重启应用,确认冲突、保存和恢复行为;另在 Windows 于写入进行中继续连续编辑,确认更晚的正文仍被写入且未保存标记消失;再制造写入失败并关闭自动保存,确认不会自动补写。 macOS 创建两个硬链接 POM,分别验证独立保存不改别名、更新全部保留同一底层文件、取消不丢失编辑、自动保存不写盘且不弹确认、关闭/运行前保存取消会中止后续动作;自动检查尚未完成时触发手动保存,应在同一次操作里弹出确认并保存最新输入,显式编码须保留,取消/重置/关闭/移走再移回不得接续,普通权限错误不重试;确认期间更改目标/内容/链接数须拒绝覆盖。核对权限、超过 32 MB、无效编码、符号链接、目录与命名管道的中英文提示;完整产品弹窗验收仍 pending。 Windows 连续输入时自动保存在停顿 1 秒后写入,切到其他窗口、终端或其他面板时立即写入;运行前保存不受该延迟影响。", "legacy": { "revision": "5a54c41aa37a4e6e6b68918a4b05765b9dd07a36",