diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ae36c7c..37f49c2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -89,3 +89,29 @@ jobs: run: | python --version || python3 --version node scripts/test.mjs + + host-e2e: + needs: package + runs-on: macos-latest + timeout-minutes: 45 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 + - uses: actions/setup-go@v7 + with: + go-version: "1.27.3" + cache: false + - uses: dtolnay/rust-toolchain@1.100.0 + - uses: actions/setup-python@v5 + with: + python-version: "3.12" + - uses: actions/download-artifact@v4 + with: + name: python-support-archive-darwin-arm64 + path: dist + - name: Verify Python highlighting on Lithe 0.8.9 + run: node scripts/e2e-host.mjs --ref 4d1b88f03f243356c855299464baa2647cc57226 --run '^TestPythonSupportPluginGrammarHighlighting$' + - name: Verify highlighting and shebang diagnostics on the pinned desktop + run: node scripts/e2e-host.mjs --run '^TestPythonSupportPlugin(GrammarHighlighting|Shebang)$' diff --git a/README.md b/README.md index 2c9c265..6a3f3ce 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Python language support plugin for Lithe (`schemaVersion: 0`, `pluginAPIVersion: 1. **语言与编辑器基础(`languages: python`)** - 识别 `.py`、`.pyi`、`.pyw`,配置 `#` 行注释与括号/引号自动配对,声明 `pyproject.toml` / `setup.py` / `setup.cfg` / `requirements.txt` / `Pipfile` / `pyrefly.toml` 项目标记及 `.venv` / `venv` / `.conda` 项目依赖目录。 + - Lithe 0.8.9 可以安装此插件。支持 TextMate 首行匹配的桌面版本还会按 shebang 识别无后缀脚本,提供 Python 高亮和 Pyrefly 诊断。 2. **后端解析的语言服务器启动与解释器自动注入(`backend` + `languageServers: python` / Pyrefly `1.4.1`)** - 插件包内的 Rust 后端直接启动 Pyrefly,无需先找到 Python。宿主会传入可用于自动启动的解释器路径,缺失时由 Pyrefly 按自身规则发现项目环境。构建、测试与调试仍用宿主选择的 Python。 - 构建期按平台固定版本与 SHA-256 打包 `pyrefly 1.4.1`(Unix:`bin/pyrefly`,Windows:`bin/pyrefly.exe`),每个包也包含同一目标架构的 `bin/lithe-python-backend`(Windows 为 `.exe`)。 @@ -90,6 +91,7 @@ Rust SDK 和构建提供者的 `scripts/lithe_rpc.py` 通过 stdio JSON-RPC 2.0 | `backend/target/` | Rust 后端编译输出,包含按平台目标区分的产物 | 随工作树保留,可重新由 Cargo 生成 | 不共享,构建时由固定的 Cargo.lock 和 Rust 目标重建。Cargo 使用全局 registry 与 Git 依赖下载缓存。 | | `.artifacts/stage/` | 打包过程中的单平台解包暂存目录 | 每次 `scripts/package.mjs` 按平台重建(`.gitignore` 忽略) | **不可复用**(构建中间产物,毫秒级重建) | | `.artifacts/lithe-host/` | `scripts/e2e-host.mjs` 检出的宿主稀疏工作树及其编译产物 | 本地按需保留(`.gitignore` 忽略) | 其内部 `.artifacts/desktop` 在同一操作系统、CPU 架构与相同 `PINNED_CORE_REVISION` 前提下可按宿主规则复制加速,不得跨工作树软链共用;任何运行时会话日志目录(`sessions//plugins/.../files/`)与状态目录严禁跨工作树复制 | +| `grammars/python.first-line.json`(插件包内) | 插件的 TextMate 首行元数据。普通文件和按首行识别的脚本都沿用宿主的 `source.python` 高亮 | 构建时随 ZIP 打包,运行时只读,随插件版本更新或卸载 | 由插件源码随包生成,无运行时缓存 | | `dist/` | 最终产出的 `python-support--.zip` 发布包 | 打包输出(`.gitignore` 忽略) | 按需重新运行 `node scripts/package.mjs` 生成 | ### 常用命令 @@ -112,9 +114,12 @@ node --test --test-timeout=60000 scripts/package/*.test.mjs cargo test --locked --manifest-path backend/Cargo.toml node scripts/test.mjs -# 使用 e2e/host/lithe-host.json 固定的兼容桌面主线提交 +# 使用 e2e/host/lithe-host.json 固定的兼容桌面提交 node scripts/e2e-host.mjs +# 用同一个 ZIP 检查 0.8.9 能安装插件并正常高亮 .py +node scripts/e2e-host.mjs --source https://github.com/1lck/Lithe-IDEA.git --ref desktop-v0.8.9 --run '^TestPythonSupportPluginGrammarHighlighting$' + # 也可使用另一个兼容的本地检出和提交 node scripts/e2e-host.mjs --source /path/to/Lithe-IDEA --ref COMPATIBLE_HOST_COMMIT ``` diff --git a/docs/feature-matrix.md b/docs/feature-matrix.md index 33f6caf..65d69db 100644 --- a/docs/feature-matrix.md +++ b/docs/feature-matrix.md @@ -12,7 +12,8 @@ | 能力域 | 功能项 | `darwin-arm64` | `darwin-x64` | `windows-x64` / `windows-arm64` | `linux-x64` / `linux-arm64` | 最近实机验证 | 实现与测试证据路径(本仓库) | | --- | --- | --- | --- | --- | --- | --- | --- | -| 语言基础 (`languages`) | `.py` / `.pyi` / `.pyw` 识别、`#` 行注释、括号/引号配对、项目标记与 `.venv` / `venv` / `.conda` 依赖目录 | ✅ 已验证 | ⏳ pending(归档与清单已校验) | ⏳ pending(归档与 Windows 清单已校验) | ⏳ pending(归档与清单已校验) | `2026-11-18`(`darwin-arm64`) | `io.github.lithe-idea.python-support/plugin.json`
`scripts/test.mjs`
`e2e/host/desktop/internal/workbench/python_support_plugin_e2e_test.go` | +| 语言基础 (`languages`) | `.py` / `.pyi` / `.pyw` 识别、`#` 行注释、括号/引号配对、项目标记与 `.venv` / `venv` / `.conda` 依赖目录 | ✅ 已验证 | ⏳ pending(归档与清单已校验) | ⏳ pending(归档与 Windows 清单已校验) | ⏳ pending(归档与清单已校验) | `2026-11-20`(`darwin-arm64`,0.8.9 复验 `.py` 高亮) | `io.github.lithe-idea.python-support/plugin.json`
`scripts/test.mjs`
`e2e/host/desktop/internal/workbench/python_support_plugin_e2e_test.go`
`e2e/host/desktop/internal/workbench/python_shebang_e2e_test.go` | +| 无后缀 Python 脚本 | 支持 TextMate 首行匹配的桌面按解释器路径或 `env` shebang 打开,提供 Python 高亮、注释与 Pyrefly 诊断 | ✅ 已验证 | ⏳ 待实机验证 | ⏳ 待实机验证 | ⏳ 待实机验证 | `2026-11-20`(`darwin-arm64`,桌面 `3dd5a79e9`) | `io.github.lithe-idea.python-support/grammars/python.first-line.json`
`scripts/test.mjs`
`e2e/host/desktop/internal/workbench/python_shebang_e2e_test.go` | | 插件后端与语言服务器启动解析 (`backend` + `languageServers`) | 包内 Rust SDK 后端协商 `litheLogging` 0 与 `litheFunctions` 0。解析 `pyrefly lsp` 启动,传入宿主提供的解释器,缺失时使用 Pyrefly 原生检测。 | ✅ 已验证(桌面主线) | ⏳ 归档已校验 | ⏳ 待 CI 与宿主验收 | ⏳ 待 CI 与宿主验收 | 2026-11-20(macOS ARM64,桌面主线) | `backend/src/pyrefly.rs`
`scripts/test.mjs`
`e2e/host/desktop/internal/workbench/python_support_plugin_e2e_test.go`
`e2e/host/desktop/internal/workbench/python_editor_debug_e2e_test.go` | | 工具链与环境自动发现 (`toolDefinitions`) | `python` 4 层解析:项目绑定 → `.venv` / `venv` / `.conda`(受工作区信任门控,保留字面软链路径)→ 全局设置 → `VIRTUAL_ENV` + `PATH` → 16 条跨平台自动发现规则(覆盖 `UV_PYTHON_INSTALL_DIR`、`XDG_DATA_HOME`、`PYENV_ROOT`、`CONDA_ENVS_PATH`、`CONDA_PREFIX`、`WORKON_HOME`、`LOCALAPPDATA`、`APPDATA`、Homebrew、Framework、`~/.pyenv`、`~/.local/share/uv`、`*conda*/envs/*`、`miniforge*/envs/*`) | ✅ 已验证 | ⏳ pending(清单与 Core 规则已校验) | ⏳ pending(`Scripts/python.exe` / `python.exe` 布局与环境变量发现规则已通过 Core 校验) | ⏳ pending(清单与 Core 规则已校验) | `2026-11-18`(`darwin-arm64`) | `io.github.lithe-idea.python-support/plugin.json`
`scripts/test.mjs`
`e2e/host/desktop/internal/workbench/python_support_plugin_e2e_test.go` | | 构建模型与运行 (`buildSystems: python` + `provider`) | `scripts/build_provider.py`(`python -I -B`,`lithe/build/model`)复用 `scripts/build_model.py` 纯 AST + `tomllib` / `vendor/tomli` 静态提取 `[project.scripts]`、包 `__main__.py`、顶层 `__main__` 脚本、Django `manage.py`、FastAPI ASGI、Flask WSGI 并派生 7 类声明式 `run` 配置;支持损坏 `pyproject.toml` 降级与修复后恢复诊断,并在协商 `litheBuild` 1 时支持 `lithe/build/runEntrypoints` | ✅ 已验证 | ⏳ pending(归档已校验) | ⏳ pending(归档已校验) | ⏳ pending(归档已校验) | `2026-11-18`(`darwin-arm64`) | `io.github.lithe-idea.python-support/scripts/build_model.py`
`io.github.lithe-idea.python-support/scripts/build_provider.py`
`io.github.lithe-idea.python-support/plugin.json`
`scripts/test.mjs`
`e2e/host/desktop/internal/workbench/python_support_plugin_e2e_test.go` | @@ -26,8 +27,9 @@ ## 宿主版本与验证 -- 插件要求宿主声明 `toolchainProviders: 0` 和 `buildSystems: 1`。桌面稳定版 `desktop-v0.8.8` 缺少这组能力。 -- `e2e/host/lithe-host.json` 固定桌面主线 `761e6f882848d754515ca16c5a78431d85d3b154`(#2192、#2193),其 Core 为 `6a2c0826`。 +- 插件要求宿主声明 `toolchainProviders: 0`、`buildSystems: 1` 和 `languages: 0`。桌面 0.8.9 支持安装与原有 Python 功能。支持 TextMate 首行匹配的桌面会额外识别无后缀脚本。 +- 现有 M3 在兼容桌面主线 `761e6f882`(#2192、#2193)独立验过。`e2e/host/lithe-host.json` 固定支持 TextMate 首行匹配的桌面候选版本。 +- 插件 CI 用同一个 macOS ARM64 ZIP 验证 0.8.9 与候选桌面的普通 Python 高亮,并在候选版验证无后缀脚本编辑后收到 Pyrefly 诊断。 - macOS ARM64 的工作台测试从 ZIP 安装插件,改变项目版本要求以切换外部解释器,检查全局候选和项目 `.venv` 信任,精确重跑一个参数化 pytest 用例并核对 JUnit。 - 无头工作台打开 `.py` 后,Pyrefly 返回所选环境独有模块的类型诊断。debugpy 使用同一环境执行当前文件,并将日志写入宿主分配目录。关闭后,已安装插件的文件清单和 SHA-256 与启动前一致。 - 宿主按 Core 的文件事实校验安装。外部虚拟环境里指向环境外的 Python 链接不可作为提供者候选,项目内环境由宿主信任流程单独处理。 diff --git a/docs/releases/v1.0.0.md b/docs/releases/v1.0.0.md index 86e76aa..70672be 100644 --- a/docs/releases/v1.0.0.md +++ b/docs/releases/v1.0.0.md @@ -1,21 +1,23 @@ ## 中文 +- 支持 TextMate 首行匹配的 Lithe 可按 shebang 识别无后缀 Python 脚本,提供高亮、注释和 Pyrefly 诊断。 - Toolchains 可从 Conda 环境登记表、uv 自定义安装目录和其他受支持的环境位置发现 Python。项目的 `.python-version` 和 `requires-python` 参与解释器选择。 - Pyrefly 与宿主选定的解释器保持一致。插件包内的后端可以在尚未安装 Python 时启动 Pyrefly。 - Build 面板可用 pytest 收集测试,按已发现的套件和用例精确运行。运行 pytest 和 debugpy 仍需要可用的项目解释器。 ### 升级与兼容性 -升级需要支持 `toolchainProviders` 0 和 `buildSystems` 1 的 Lithe 桌面版。确认桌面已支持这两项能力后,再安装对应操作系统和架构的插件 ZIP。使用较早桌面版本时,可继续使用 Python Support v0.1.1。 +Lithe 0.8.9 支持安装此插件并使用普通 Python 文件功能。无后缀脚本的自动识别需要支持 TextMate 首行匹配的桌面版本。请选择对应操作系统和架构的 ZIP。使用较早桌面版本时,可继续使用 Python Support v0.1.1。 --- ## English +- On Lithe versions that support TextMate first-line matching, open extensionless Python scripts with a shebang to get highlighting, comment commands, and Pyrefly diagnostics. - Toolchains can find Python from the Conda environment registry, a custom uv installation directory, and other supported environment locations. The project's `.python-version` and `requires-python` help select an interpreter. - Pyrefly uses the interpreter selected in Lithe. The bundled backend can start Pyrefly before Python is installed. - The Build panel can collect tests with pytest and run an exact discovered suite or case. Running pytest and debugpy still requires an available project interpreter. ### Upgrade and compatibility -This upgrade requires a Lithe desktop version that supports `toolchainProviders` 0 and `buildSystems` 1. Once your desktop version supports both capabilities, install the plugin ZIP for your operating system and architecture. Python Support v0.1.1 remains available for earlier versions of Lithe. +Lithe 0.8.9 can install this plugin and use its standard Python support. Automatic recognition of extensionless scripts requires a desktop version with TextMate first-line matching. Choose the ZIP for your operating system and architecture. Python Support v0.1.1 remains available for earlier versions of Lithe. diff --git a/e2e/host/desktop/internal/workbench/python_shebang_e2e_test.go b/e2e/host/desktop/internal/workbench/python_shebang_e2e_test.go new file mode 100644 index 0000000..a6f8200 --- /dev/null +++ b/e2e/host/desktop/internal/workbench/python_shebang_e2e_test.go @@ -0,0 +1,123 @@ +package workbench + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/1lck/Lithe-IDEA/desktop/internal/core" + "github.com/1lck/Lithe-IDEA/desktop/internal/editor/rope" + "github.com/1lck/Lithe-IDEA/desktop/internal/editor/syntax" + "github.com/1lck/Lithe-IDEA/desktop/internal/pluginservice" + "github.com/1lck/Lithe-IDEA/desktop/internal/store" + "github.com/vivaui/viva/app" +) + +func openPythonGrammarWorkbench(t *testing.T) *harness { + t.Helper() + archive := pythonArchiveForE2E(t) + ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second) + defer cancel() + kernel := core.NewClient() + preferences := store.At(t.TempDir()) + service, err := pluginservice.Open(kernel, preferences, t.TempDir()) + if err != nil { + t.Fatal(err) + } + installPythonArchive(t, ctx, service, archive) + if err := os.WriteFile(filepath.Join(preferences.Dir(), settingsFile), []byte(`{"theme":"dark"}`), 0o600); err != nil { + t.Fatal(err) + } + h := newHarnessWithOptions(t, Options{ + Kernel: kernel, PluginKernel: kernel, PluginRoot: service.Root, Store: preferences, Languages: []string{"en"}, + Logs: &pluginFilesLog{directory: t.TempDir()}, LogKernel: kernel, + }) + h.app.reloadPluginLanguages() + h.settle() + h.open(t.TempDir()) + return h +} + +func TestPythonSupportPluginGrammarHighlighting(t *testing.T) { + h := openPythonGrammarWorkbench(t) + p := h.app.project + path := filepath.Join(p.root, "sample.py") + const body = `class Counter: + def greet(self, name: str): + count = 42 + 1 + if count >= 1 and name: + return f"Hi {name.upper()}, {count + 1}" # hello + return None +` + if err := os.WriteFile(path, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + grammar := h.app.syntaxRegistry().SyntaxForPath(path) + if grammar == nil || grammar.Language().Scope != "source.python" { + t.Fatal("installed plugin did not preserve the built-in Python grammar") + } + h.app.openFile(p, path) + h.settle() + doc := p.docs.byPath(path) + if doc == nil || doc.languageID != "python" { + t.Fatal("sample.py did not open as Python") + } + + bundled := syntax.NewRegistry().SyntaxForPath(path) + highlighter := syntax.NewHighlighter(bundled, rope.New([]byte(body)), syntax.HighlighterOptions{}) + t.Cleanup(highlighter.Close) + for lineIndex, line := range strings.Split(body, "\n") { + want := highlighter.Line(nil, lineIndex) + got := doc.view.SyntaxLine(nil, lineIndex) + if len(got) != len(want) { + t.Fatalf("line %d %q highlights = %v, bundled Python = %v", lineIndex+1, line, got, want) + } + for i, token := range got { + if token != want[i] { + t.Errorf("line %d %q token = %v, bundled Python = %v", lineIndex+1, line, token, want[i]) + } + } + } +} + +func TestPythonSupportPluginShebang(t *testing.T) { + h := openPythonGrammarWorkbench(t) + p := h.app.project + path := filepath.Join(p.root, "script") + const body = "#!/usr/bin/env -S /usr/bin/python3 -I\ncount: int = 42\n" + if err := os.WriteFile(path, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + h.app.openFile(p, path) + h.settle() + doc := p.docs.byPath(path) + if doc == nil || doc.languageID != "python" { + t.Fatal("extensionless script did not open as Python") + } + if len(doc.view.SyntaxLine(nil, 1)) == 0 { + t.Fatal("extensionless script has no Python syntax tokens") + } + awaitStateWithin(t, h, 15*time.Second, "Pyrefly to open the extensionless document", func() bool { + if failure := h.app.pluginServerFailed["python"]; failure != nil { + t.Fatalf("Pyrefly start: %s", failure.message) + } + return doc.sync.opened + }) + if !doc.view.GoToNavigation(1, 13, false) { + t.Fatal("could not place the caret before the integer literal") + } + input := doc.view.TextInput(app.TextInputContext{ID: doc.hit}) + if _, err := input.OnEvent(app.TextEvent{Kind: app.TextInsert, Text: `"bad" # `}); err != nil { + t.Fatal(err) + } + if !strings.Contains(doc.view.TextSnapshot().String(), `count: int = "bad" # 42`) { + t.Fatal("extensionless script edit was not applied") + } + awaitStateWithin(t, h, 15*time.Second, "Pyrefly to diagnose the edited extensionless script", func() bool { + return diagnosticsForFile(h.app, path) > 0 && + problemMessagesContain(h.app, "int") && problemMessagesContain(h.app, "bad") + }) +} diff --git a/e2e/host/lithe-host.json b/e2e/host/lithe-host.json index f5317a6..56a3939 100644 --- a/e2e/host/lithe-host.json +++ b/e2e/host/lithe-host.json @@ -1,6 +1,6 @@ { "repository": "https://github.com/1lck/Lithe-IDEA.git", - "revision": "761e6f882848d754515ca16c5a78431d85d3b154", + "revision": "3dd5a79e97d0c4ac2eedddd06baaf327a20487d0", "packages": { "./internal/projectgen": "^TestPythonPlugin", "./internal/workbench": "^TestPythonSupportPlugin" diff --git a/io.github.lithe-idea.python-support/grammars/python.first-line.json b/io.github.lithe-idea.python-support/grammars/python.first-line.json new file mode 100644 index 0000000..02f2a36 --- /dev/null +++ b/io.github.lithe-idea.python-support/grammars/python.first-line.json @@ -0,0 +1,3 @@ +{ + "firstLineMatch": "^#![ \\t]*/(?:[^ \\t]*/)?(?:env[ \\t]+(?:-S[ \\t]+)?(?:/(?:[^ \\t]*/)?)?)?(?:python(?:[0-9]+(?:\\.[0-9]+)*t?)?|pypy[0-9]*)(?:[ \\t]|$)" +} diff --git a/scripts/package/archives.test.mjs b/scripts/package/archives.test.mjs index 27a3380..7c8c70d 100644 --- a/scripts/package/archives.test.mjs +++ b/scripts/package/archives.test.mjs @@ -121,6 +121,20 @@ test("every built archive has safe paths, no bytecode or symlinks, and valid pla assert.deepEqual(manifest.requires, baseManifest.requires); assert.deepEqual(manifest.vendor, baseManifest.vendor); + assert.equal(manifest.requires.languages, 0); + const pythonLanguage = manifest.languages.find(({ id }) => id === "python"); + assert.equal(pythonLanguage.grammar, undefined); + assert.equal("firstLineMatch" in pythonLanguage, false); + const metadataPath = `${PACKAGE_ID}/grammars/python.first-line.json`; + const metadataEntry = entries.find(({ name }) => name === metadataPath); + assert.ok(metadataEntry, `${archive.name}: missing ${metadataPath}`); + // Packaged JSON can have CRLF when a Windows runner assembles the ZIP. + assert.deepEqual( + JSON.parse(metadataEntry.data.toString("utf8")), + JSON.parse(readFileSync(metadataPath, "utf8")), + `${archive.name}: packaged metadata differs from ${metadataPath}`, + ); + assert.deepEqual(manifest.backend, { launch: { kind: "packaged", path: backendPath(archive.platform) } }); assert.deepEqual(manifest.toolchainProviders, baseManifest.toolchainProviders); assert.equal(manifest.requires.toolchainProviders, 0); diff --git a/scripts/test.mjs b/scripts/test.mjs index eeec627..ab5ee9e 100644 --- a/scripts/test.mjs +++ b/scripts/test.mjs @@ -310,6 +310,28 @@ try { const pluginManifest = JSON.parse(readFileSync(join(unpackedPluginDir, "plugin.json"), "utf8")); await check("Unix and Windows plugin.json + zh-Hans.json satisfy M3 contract invariants", () => { + const metadata = JSON.parse(readFileSync(join(unpackedPluginDir, "grammars", "python.first-line.json"), "utf8")); + assert.deepEqual(Object.keys(metadata), ["firstLineMatch"]); + const firstLine = new RegExp(metadata.firstLineMatch); + for (const line of [ + "#!/usr/bin/python", + "#!/usr/bin/env python3", + "#!/usr/bin/env -S python3.13 -I", + "#!/usr/bin/env -S /usr/bin/python3 -I", + "#!/opt/venv/bin/python3.13t", + "#!/usr/bin/env pypy3", + ]) { + assert.match(line, firstLine); + } + for (const line of [ + "#!/usr/bin/env bash", + "#!/usr/bin/env python-wrapper", + "#!/usr/bin/python3.13.exe", + "#!/usr/bin/env node python3", + "# Mention #!/usr/bin/env python3", + ]) { + assert.doesNotMatch(line, firstLine); + } for (const platform of Object.keys(lock.pyrefly.platforms)) { const variant = manifestForPlatform(sourceManifest, platform, lock); assert.equal(variant.schemaVersion, 0); @@ -362,7 +384,8 @@ try { }); const pyLang = variant.languages[0]; - assert.equal("shebang" in pyLang, false); + assert.equal(pyLang.grammar, undefined); + assert.equal("firstLineMatch" in pyLang, false); assert.deepEqual(pyLang.fileExtensions, ["py", "pyi", "pyw"]); assert.deepEqual(pyLang.dependencies.projectDependencyPaths, [".venv", "venv", ".conda"]);