Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions desktop/internal/workbench/document_observation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -239,6 +252,9 @@ func (a *App) scheduleDocumentChecks(p *project) {
}
})
}
for _, done := range virtualWaiters {
done(true)
}
}

func (a *App) clearDocumentConflict(d *document) {
Expand Down
140 changes: 140 additions & 0 deletions desktop/internal/workbench/document_observation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package workbench

import (
"bytes"
"context"
"errors"
"fmt"
"os"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
13 changes: 13 additions & 0 deletions desktop/internal/workbench/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions desktop/internal/workbench/editor_essentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
8 changes: 5 additions & 3 deletions desktop/internal/workbench/navigation_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -104,16 +105,17 @@ 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
}
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
Expand Down
7 changes: 3 additions & 4 deletions desktop/internal/workbench/recent_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
]
},
Expand All @@ -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",
Expand Down
Loading