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
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Agent 笔记:远程工作区按路径打开主机上的文件——工作区外只读

状态:已实现

## 先说结论

远程工作区状态栏连接菜单里的“打开主机上的文件…”从窗口顶部弹出路径框(对标 Zed 的 Open File 提示),输入 `~/` 或 `/` 开头的主机路径,边输入边经 `remote.readDir` 列出那一层的文件夹和文件。工作区文件夹里的文件照常可以编辑;文件夹以外的文件以只读方式打开(设计 S25),不能切换为可编辑,保存和新建都被拒绝。文件夹以外的文件用 Core 已有的占位路径表示(占位根旁的 `.lithe-remote-host/<主机路径>`),桌面端不另造一套路径。

## 问题

Issue #2231 的 R0 要求能打开工作区文件夹以外的主机文件(例如 `~/.bashrc`、`/etc/hosts`),用户定为“对标 Zed”。需要回答:

- 从哪里打开:项目树只列工作区文件夹,Search Everywhere 只搜工作区;
- 这些文件在本机怎样表示:编辑器、标签页、状态栏都以本机路径为键,而远程工作区的本机根是一个不能读写的空占位目录;
- 能不能编辑:文件夹外的文件不在信任范围内,也不在主机监听范围内,改了别人看不到冲突。

## 决策

### 单独的路径弹窗,不挤进 Search Everywhere

- 入口是状态栏连接菜单的“打开主机上的文件…”,没有会话时不可用。弹窗挂在窗口顶部,和 Search Everywhere 的位置一致,路径框默认 `~/`。
- 列表显示路径最后一个“/”之前的文件夹,按其后的文字不分大小写筛选名称开头,文件夹在前;只有输入以“.”开头时才列隐藏条目。上下键移动,Tab 补全,回车进入文件夹或打开文件,没有匹配时回车按输入的路径打开,Esc 关闭。
- 不用 Search Everywhere 的原因:它的第一个字符 `/` 已经被“操作”页签占用,主机路径正好都以 `/` 开头,混在一起会让 `/etc` 变成搜操作。

### 文件夹外的文件用 Core 的占位路径,只读打开

- 本机路径一律由 `remoteplaceholder.LocalPath` 求出:工作区内的在占位根下,工作区外的在占位根旁的 `.lithe-remote-host` 下。Git 返回的工作区外路径本来就用这个规则(见 `remoteplaceholder.Outside`),文件打开沿用它,所以标签页、Git 差异里同一个主机文件只有一个本机键。
- `document.hostOutside` 标记文件夹外的文档:打开时 `SetReadOnly(true)`,“切换只读”被拒绝并提示原因;`remoteDocuments.save/create` 对这类路径返回 `errRemoteReadOnly`,不发任何写请求。这是 S25 的落地:信任只覆盖工作区文件夹,文件夹外的文件不该因为打开了工作区就能改。
- 状态栏导航路径显示 `~` 开头的主机路径(不暴露占位目录),复制路径菜单不提供“复制相对路径”,因为它不在项目里。
- 主机监听只覆盖工作区文件夹,文件夹外的文件不会收到变化通知;它们和其他文档一样在重连和关闭前与主机比对,没有编辑时重新载入。

### 远程工作区不提供“在文件浏览器中显示”

占位路径在本机不存在,标签页右键、Search Everywhere 结果和 Git 变更里的“在 Finder/资源管理器中显示”在远程工作区里总是失败(ENOTDIR)。这是这次顺手修掉的既有问题:远程工作区里直接不显示这一项,而不是点了报错。

## 正确与错误示例

正确:新功能要用主机上工作区外的路径时,经 `remoteplaceholder.LocalPath(p.root, remoteRoot, host)` 换成本机键,再判断 `p.remoteOutsideDocument(path)` 决定能否写。

错误:在桌面端拼 `filepath.Join(os.TempDir(), host)` 之类的本机路径,或者从占位路径里截字符串还原主机路径——前者会让同一个主机文件出现两个键,后者在 Windows 上会把 `\` 和盘符混进主机路径。

## 考虑过的备选方案

- 在 Search Everywhere 里加“主机文件”页签:被否,`/` 前缀冲突,而且它按项目索引搜索,不适合逐层浏览。
- 文件夹外的文件也允许编辑并带版本保存:被否,S25 要求信任边界之外只读;以后真有需要,应当由用户把那个文件夹作为另一个远程工作区打开并单独信任。
- 打开文件夹外的文件时扩大主机监听:被否,监听整个主目录或根目录代价太大,比对已经能在重连和关闭前发现变化。

## 后果

- 收益:工作区外的主机文件和 Git 返回的工作区外路径共用同一个本机键;信任边界之外不会被写;不用为它扩大主机监听。
- 代价:工作区外的文件只能看不能改,确实要改时需要把那个文件夹另开为远程工作区;主机上对它的修改要等重连或关闭标签前比对才发现。
- 列出文件夹每次都是一次 `remote.readDir` 往返,深层路径边输入边列出,网络慢时列表会短暂显示“正在加载”。

## 验证

- `desktop/internal/workbench/remote_host_files_test.go` 的 `TestRemoteOpenFileOnHost`:列表筛选、隐藏条目、进入文件夹、Tab 补全、工作区内可编辑、工作区外只读且保存/新建被拒、状态栏主机路径、非绝对路径提示、日志不含主机和路径。
- `desktop/internal/remoteplaceholder` 的 `TestHostAndLocalPathsMapBothWays` 覆盖 `Outside`。
- `TestVisualRemoteWorkspace` 的 `host-file`、`host-file-readonly` 无头截图(`docs/assets/remote-workspace/`)。
- 真实 SSH 主机上的整机验收和 Windows 上的验证尚未完成。

```bash
scripts/desktop/desktop-go.sh test ./internal/remoteplaceholder ./internal/workbench -run 'TestRemoteOpenFileOnHost|TestHostAndLocalPathsMapBothWays' -count=1
```

## 适用范围

`desktop/internal/workbench/remote_host_files.go`、`remote_host_files_view.go`、`remote_documents.go` 中的 `writable`/`errRemoteReadOnly`、`documents.go` 的 `hostOutside`,以及 `desktop/internal/remoteplaceholder` 的 `Outside`。以后远程工作区里任何打开主机文件的入口(例如远程运行输出里的路径链接)都要经 `remoteplaceholder.LocalPath` 求本机键、按 `hostOutside` 决定只读,不要另开一条可写路径。
18 changes: 18 additions & 0 deletions desktop/internal/i18n/shared.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions desktop/internal/remoteplaceholder/remoteplaceholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ func HostPath(root, remoteRoot, local string) (string, bool) {
return "", false
}

// Outside says whether local stands for a host path outside the workspace whose local root is
// root: one below root's sibling (HostSuffix), not below root itself.
func Outside(root, local string) bool {
_, ok := below(root+HostSuffix, local)
return ok
}

// LocalPath is the path that stands for host, an absolute host path, in the same workspace: the
// inverse of HostPath. ok is false for a path that is not absolute, and on Windows for one with a
// name holding "\", which would name another path there.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ func TestHostAndLocalPathsMapBothWays(t *testing.T) {
if !ok || host != tc.host {
t.Errorf("HostPath(%q) = %q, %v; want %q", tc.local, host, ok, tc.host)
}
// Only the paths below the sibling stand for host files outside the workspace.
if outside, want := Outside(root, tc.local), strings.HasPrefix(tc.local, sibling); outside != want {
t.Errorf("Outside(%q) = %v; want %v", tc.local, outside, want)
}
}
if Outside(root, root+"x") || Outside(root, filepath.Dir(root)) {
t.Error("Outside took a path that names no host path")
}
if local, ok := LocalPath(Root(anchor, "c1", "/"), "/", "/etc/hosts"); !ok || local != filepath.Join(Root(anchor, "c1", "/"), "etc", "hosts") {
t.Errorf("LocalPath below the host's root = %q, %v", local, ok)
Expand Down
14 changes: 9 additions & 5 deletions desktop/internal/workbench/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"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/1lck/Lithe-IDEA/desktop/internal/remoteplaceholder"
"github.com/1lck/Lithe-IDEA/desktop/internal/winpath"
"github.com/vivaui/viva/gfx"
"github.com/vivaui/viva/ui"
Expand Down Expand Up @@ -136,6 +137,9 @@ type document struct {
// watched, saved or synchronized with a language server, and the host keeps no
// copy once it closes.
virtual bool
// hostOutside marks a remote workspace's file outside its host folder, which opens
// read-only and stays so (remote_host_files.go, S25).
hostOutside bool
// encoding is the id of the text encoding the file is read and written in; "" is plain
// UTF-8. hasBOM says a byte order mark leads the file. diskBytes are the bytes the file
// held when it was last read or written, kept only for a document whose bytes differ from
Expand Down Expand Up @@ -279,9 +283,9 @@ func (a *App) openFile(p *project, path string) {
return
}
if p.remote != nil {
// A remote workspace edits the files of its host folder; the viewers still read this
// computer's files.
if _, ok := remoteHostPathBelow(p.root, p.remote.binding.RemoteRoot(), path); !ok && a.refuseRemote(p, "open_file") {
// A remote workspace edits the files of its host folder and shows other host files
// read-only (remote_host_files.go); the viewers still read this computer's files.
if _, ok := remoteplaceholder.HostPath(p.root, p.remote.binding.RemoteRoot(), path); !ok && a.refuseRemote(p, "open_file") {
return
}
if _, ok := viewerKindOf(path); ok && a.refuseRemote(p, "open_viewer") {
Expand Down Expand Up @@ -546,7 +550,7 @@ func (a *App) placeDocumentAs(p *project, path, name string, content editorview.
return
}
p.docs.next = id
d := &document{id: id, path: path, name: name, languageID: languageID, grammar: grammar, virtual: virtual}
d := &document{id: id, path: path, name: name, languageID: languageID, grammar: grammar, virtual: virtual, hostOutside: !virtual && p.remoteOutsideDocument(path)}
d.view = editorview.New(editorview.Config{
Label: name,
Content: content,
Expand All @@ -558,7 +562,7 @@ func (a *App) placeDocumentAs(p *project, path, name string, content editorview.
a.mirrorDocumentEdit(p, d, edit)
},
})
d.view.SetReadOnly(virtual)
d.view.SetReadOnly(virtual || d.hostOutside)
d.view.SetVimExit(func(cmd string) bool { return a.vimExit(p, d, cmd) })
setupFolding(d)
d.breakpointText = d.view.TextSnapshot()
Expand Down
5 changes: 5 additions & 0 deletions desktop/internal/workbench/editor_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ func (a *App) onKey(event gfx.KeyEvent) gfx.EventResult {
return result
}
}
if p := a.project; p != nil && p.remote != nil && p.remote.hostFiles.open {
if result := a.remoteHostFilesKey(p, event); result.Handled {
return result
}
}
if event.Release {
return gfx.EventResult{}
}
Expand Down
3 changes: 2 additions & 1 deletion desktop/internal/workbench/gitreview_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ func (a *App) changeMenuItems(c ui.C, p *project, change core.GitChange) func(*u
c.After(func() { a.openTreeRemoval(p, []string{rel}) })
}
menu.Separator()
if !deleted && menu.Item(revealLabel(lang)).Clicked() {
// The placeholder path of a remote workspace's file is nothing the file manager can show.
if !deleted && p.remote == nil && menu.Item(revealLabel(lang)).Clicked() {
c.After(func() {
a.performed("git.change.reveal", surfaceContextMenu)
a.revealPath(full)
Expand Down
Loading
Loading