Skip to content

Commit fbc4e27

Browse files
committed
fix(python): 保留内置高亮并覆盖 shebang 兼容性
1 parent 7521dfa commit fbc4e27

10 files changed

Lines changed: 50 additions & 44 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,29 @@ jobs:
8989
run: |
9090
python --version || python3 --version
9191
node scripts/test.mjs
92+
93+
host-e2e:
94+
needs: package
95+
runs-on: macos-latest
96+
timeout-minutes: 45
97+
steps:
98+
- uses: actions/checkout@v4
99+
- uses: actions/setup-node@v4
100+
with:
101+
node-version: 22
102+
- uses: actions/setup-go@v7
103+
with:
104+
go-version: "1.27.3"
105+
cache: false
106+
- uses: dtolnay/rust-toolchain@1.100.0
107+
- uses: actions/setup-python@v5
108+
with:
109+
python-version: "3.12"
110+
- uses: actions/download-artifact@v4
111+
with:
112+
name: python-support-archive-darwin-arm64
113+
path: dist
114+
- name: Verify Python highlighting on Lithe 0.8.9
115+
run: node scripts/e2e-host.mjs --ref 4d1b88f03f243356c855299464baa2647cc57226 --run '^TestPythonSupportPluginGrammarHighlighting$'
116+
- name: Verify highlighting and shebang diagnostics on the pinned desktop
117+
run: node scripts/e2e-host.mjs --run '^TestPythonSupportPlugin(GrammarHighlighting|Shebang)$'

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Rust SDK 和构建提供者的 `scripts/lithe_rpc.py` 通过 stdio JSON-RPC 2.0
9191
| `backend/target/` | Rust 后端编译输出,包含按平台目标区分的产物 | 随工作树保留,可重新由 Cargo 生成 | 不共享,构建时由固定的 Cargo.lock 和 Rust 目标重建。Cargo 使用全局 registry 与 Git 依赖下载缓存。 |
9292
| `.artifacts/stage/` | 打包过程中的单平台解包暂存目录 | 每次 `scripts/package.mjs` 按平台重建(`.gitignore` 忽略) | **不可复用**(构建中间产物,毫秒级重建) |
9393
| `.artifacts/lithe-host/` | `scripts/e2e-host.mjs` 检出的宿主稀疏工作树及其编译产物 | 本地按需保留(`.gitignore` 忽略) | 其内部 `.artifacts/desktop` 在同一操作系统、CPU 架构与相同 `PINNED_CORE_REVISION` 前提下可按宿主规则复制加速,不得跨工作树软链共用;任何运行时会话日志目录(`sessions/<session>/plugins/.../files/`)与状态目录严禁跨工作树复制 |
94-
| `grammars/python.tmLanguage.json`(插件包内) | 插件版本拥有的 TextMate 首行规则,复用宿主内置的 `source.python` 高亮规则 | 构建时随 ZIP 打包,运行时只读,随插件版本更新或卸载 | 由插件源码随包生成,无运行时缓存 |
94+
| `grammars/python.first-line.json`(插件包内) | 插件的 TextMate 首行元数据。普通文件和按首行识别的脚本都沿用宿主的 `source.python` 高亮 | 构建时随 ZIP 打包,运行时只读,随插件版本更新或卸载 | 由插件源码随包生成,无运行时缓存 |
9595
| `dist/` | 最终产出的 `python-support-<version>-<platform>.zip` 发布包 | 打包输出(`.gitignore` 忽略) | 按需重新运行 `node scripts/package.mjs` 生成 |
9696

9797
### 常用命令

‎docs/feature-matrix.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
| 能力域 | 功能项 | `darwin-arm64` | `darwin-x64` | `windows-x64` / `windows-arm64` | `linux-x64` / `linux-arm64` | 最近实机验证 | 实现与测试证据路径(本仓库) |
1414
| --- | --- | --- | --- | --- | --- | --- | --- |
1515
| 语言基础 (`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`<br>`scripts/test.mjs`<br>`e2e/host/desktop/internal/workbench/python_support_plugin_e2e_test.go`<br>`e2e/host/desktop/internal/workbench/python_shebang_e2e_test.go` |
16-
| 无后缀 Python 脚本 | 支持 TextMate 首行匹配的桌面按解释器路径或 `env` shebang 打开,提供 Python 高亮、注释与 Pyrefly 诊断 | ✅ 已验证 | ⏳ 待实机验证 | ⏳ 待实机验证 | ⏳ 待实机验证 | `2026-11-20`(`darwin-arm64`,桌面 `79b97fa64`) | `io.github.lithe-idea.python-support/grammars/python.tmLanguage.json`<br>`scripts/test.mjs`<br>`e2e/host/desktop/internal/workbench/python_shebang_e2e_test.go` |
16+
| 无后缀 Python 脚本 | 支持 TextMate 首行匹配的桌面按解释器路径或 `env` shebang 打开,提供 Python 高亮、注释与 Pyrefly 诊断 | ✅ 已验证 | ⏳ 待实机验证 | ⏳ 待实机验证 | ⏳ 待实机验证 | `2026-11-20`(`darwin-arm64`,桌面 `3dd5a79e9`) | `io.github.lithe-idea.python-support/grammars/python.first-line.json`<br>`scripts/test.mjs`<br>`e2e/host/desktop/internal/workbench/python_shebang_e2e_test.go` |
1717
| 插件后端与语言服务器启动解析 (`backend` + `languageServers`) | 包内 Rust SDK 后端协商 `litheLogging` 0 与 `litheFunctions` 0。解析 `pyrefly lsp` 启动,传入宿主提供的解释器,缺失时使用 Pyrefly 原生检测。 | ✅ 已验证(桌面主线) | ⏳ 归档已校验 | ⏳ 待 CI 与宿主验收 | ⏳ 待 CI 与宿主验收 | 2026-11-20(macOS ARM64,桌面主线) | `backend/src/pyrefly.rs`<br>`scripts/test.mjs`<br>`e2e/host/desktop/internal/workbench/python_support_plugin_e2e_test.go`<br>`e2e/host/desktop/internal/workbench/python_editor_debug_e2e_test.go` |
1818
| 工具链与环境自动发现 (`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`<br>`scripts/test.mjs`<br>`e2e/host/desktop/internal/workbench/python_support_plugin_e2e_test.go` |
1919
| 构建模型与运行 (`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`<br>`io.github.lithe-idea.python-support/scripts/build_provider.py`<br>`io.github.lithe-idea.python-support/plugin.json`<br>`scripts/test.mjs`<br>`e2e/host/desktop/internal/workbench/python_support_plugin_e2e_test.go` |
@@ -29,6 +29,7 @@
2929

3030
- 插件要求宿主声明 `toolchainProviders: 0`、`buildSystems: 1` 和 `languages: 0`。桌面 0.8.9 支持安装与原有 Python 功能。支持 TextMate 首行匹配的桌面会额外识别无后缀脚本。
3131
- 现有 M3 在兼容桌面主线 `761e6f882`(#2192、#2193)独立验过。`e2e/host/lithe-host.json` 固定支持 TextMate 首行匹配的桌面候选版本。
32+
- 插件 CI 用同一个 macOS ARM64 ZIP 验证 0.8.9 与候选桌面的普通 Python 高亮,并在候选版验证无后缀脚本编辑后收到 Pyrefly 诊断。
3233
- macOS ARM64 的工作台测试从 ZIP 安装插件,改变项目版本要求以切换外部解释器,检查全局候选和项目 `.venv` 信任,精确重跑一个参数化 pytest 用例并核对 JUnit。
3334
- 无头工作台打开 `.py` 后,Pyrefly 返回所选环境独有模块的类型诊断。debugpy 使用同一环境执行当前文件,并将日志写入宿主分配目录。关闭后,已安装插件的文件清单和 SHA-256 与启动前一致。
3435
- 宿主按 Core 的文件事实校验安装。外部虚拟环境里指向环境外的 Python 链接不可作为提供者候选,项目内环境由宿主信任流程单独处理。

‎e2e/host/desktop/internal/workbench/python_shebang_e2e_test.go‎

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ func TestPythonSupportPluginGrammarHighlighting(t *testing.T) {
5656
t.Fatal(err)
5757
}
5858
grammar := h.app.syntaxRegistry().SyntaxForPath(path)
59-
if grammar == nil || grammar.Language().Scope != "source.python.lithe-support" {
60-
t.Fatal("installed Python grammar was not loaded")
59+
if grammar == nil || grammar.Language().Scope != "source.python" {
60+
t.Fatal("installed plugin did not preserve the built-in Python grammar")
6161
}
6262
h.app.openFile(p, path)
6363
h.settle()
@@ -76,21 +76,9 @@ func TestPythonSupportPluginGrammarHighlighting(t *testing.T) {
7676
t.Fatalf("line %d %q highlights = %v, bundled Python = %v", lineIndex+1, line, got, want)
7777
}
7878
for i, token := range got {
79-
if token == want[i] {
80-
continue
79+
if token != want[i] {
80+
t.Errorf("line %d %q token = %v, bundled Python = %v", lineIndex+1, line, token, want[i])
8181
}
82-
end := len(line)
83-
if i+1 < len(want) {
84-
end = int(want[i+1].Start)
85-
}
86-
// Both registries tokenize operators through the bundled grammar. The
87-
// plugin root uses the generic classifier, styling those symbols as keywords.
88-
if token.Start == want[i].Start && token.Name.String() == "keyword" &&
89-
want[i].Name.String() == "delimiter" &&
90-
strings.Trim(line[token.Start:end], "=+<>") == "" {
91-
continue
92-
}
93-
t.Errorf("line %d %q token = %v, bundled Python = %v", lineIndex+1, line, token, want[i])
9482
}
9583
}
9684
}
@@ -99,7 +87,7 @@ func TestPythonSupportPluginShebang(t *testing.T) {
9987
h := openPythonGrammarWorkbench(t)
10088
p := h.app.project
10189
path := filepath.Join(p.root, "script")
102-
const body = "#!/usr/bin/env -S python3 -I\ncount: int = 42\n"
90+
const body = "#!/usr/bin/env -S /usr/bin/python3 -I\ncount: int = 42\n"
10391
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
10492
t.Fatal(err)
10593
}

‎e2e/host/lithe-host.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"repository": "https://github.com/1lck/Lithe-IDEA.git",
3-
"revision": "79b97fa6428df3c28d2c338c56cfdf4153c14008",
3+
"revision": "3dd5a79e97d0c4ac2eedddd06baaf327a20487d0",
44
"packages": {
55
"./internal/projectgen": "^TestPythonPlugin",
66
"./internal/workbench": "^TestPythonSupportPlugin"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"firstLineMatch": "^#![ \\t]*/(?:[^ \\t]*/)?(?:env[ \\t]+(?:-S[ \\t]+)?(?:/(?:[^ \\t]*/)?)?)?(?:python(?:[0-9]+(?:\\.[0-9]+)*t?)?|pypy[0-9]*)(?:[ \\t]|$)"
3+
}

‎io.github.lithe-idea.python-support/grammars/python.tmLanguage.json‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎io.github.lithe-idea.python-support/plugin.json‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@
5252
"id": "python",
5353
"displayName": "Python",
5454
"fileExtensions": ["py", "pyi", "pyw"],
55-
"grammar": {
56-
"kind": "textmate",
57-
"path": "grammars/python.tmLanguage.json"
58-
},
5955
"projectFileNames": [
6056
"pyproject.toml",
6157
"setup.py",

‎scripts/package/archives.test.mjs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,16 @@ test("every built archive has safe paths, no bytecode or symlinks, and valid pla
123123

124124
assert.equal(manifest.requires.languages, 0);
125125
const pythonLanguage = manifest.languages.find(({ id }) => id === "python");
126-
assert.deepEqual(pythonLanguage.grammar, { kind: "textmate", path: "grammars/python.tmLanguage.json" });
126+
assert.equal(pythonLanguage.grammar, undefined);
127127
assert.equal("firstLineMatch" in pythonLanguage, false);
128-
const grammarPath = `${PACKAGE_ID}/${pythonLanguage.grammar.path}`;
129-
const grammarEntry = entries.find(({ name }) => name === grammarPath);
130-
assert.ok(grammarEntry, `${archive.name}: missing declared grammar ${grammarPath}`);
128+
const metadataPath = `${PACKAGE_ID}/grammars/python.first-line.json`;
129+
const metadataEntry = entries.find(({ name }) => name === metadataPath);
130+
assert.ok(metadataEntry, `${archive.name}: missing ${metadataPath}`);
131131
// Packaged JSON can have CRLF when a Windows runner assembles the ZIP.
132132
assert.deepEqual(
133-
JSON.parse(grammarEntry.data.toString("utf8")),
134-
JSON.parse(readFileSync(grammarPath, "utf8")),
135-
`${archive.name}: packaged grammar differs from ${grammarPath}`,
133+
JSON.parse(metadataEntry.data.toString("utf8")),
134+
JSON.parse(readFileSync(metadataPath, "utf8")),
135+
`${archive.name}: packaged metadata differs from ${metadataPath}`,
136136
);
137137

138138
assert.deepEqual(manifest.backend, { launch: { kind: "packaged", path: backendPath(archive.platform) } });

‎scripts/test.mjs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,14 @@ try {
310310
const pluginManifest = JSON.parse(readFileSync(join(unpackedPluginDir, "plugin.json"), "utf8"));
311311

312312
await check("Unix and Windows plugin.json + zh-Hans.json satisfy M3 contract invariants", () => {
313-
const grammar = JSON.parse(readFileSync(join(unpackedPluginDir, "grammars", "python.tmLanguage.json"), "utf8"));
314-
assert.deepEqual(grammar.patterns, [{ include: "source.python" }]);
315-
const firstLine = new RegExp(grammar.firstLineMatch);
313+
const metadata = JSON.parse(readFileSync(join(unpackedPluginDir, "grammars", "python.first-line.json"), "utf8"));
314+
assert.deepEqual(Object.keys(metadata), ["firstLineMatch"]);
315+
const firstLine = new RegExp(metadata.firstLineMatch);
316316
for (const line of [
317317
"#!/usr/bin/python",
318318
"#!/usr/bin/env python3",
319319
"#!/usr/bin/env -S python3.13 -I",
320+
"#!/usr/bin/env -S /usr/bin/python3 -I",
320321
"#!/opt/venv/bin/python3.13t",
321322
"#!/usr/bin/env pypy3",
322323
]) {
@@ -383,7 +384,7 @@ try {
383384
});
384385

385386
const pyLang = variant.languages[0];
386-
assert.deepEqual(pyLang.grammar, { kind: "textmate", path: "grammars/python.tmLanguage.json" });
387+
assert.equal(pyLang.grammar, undefined);
387388
assert.equal("firstLineMatch" in pyLang, false);
388389
assert.deepEqual(pyLang.fileExtensions, ["py", "pyi", "pyw"]);
389390
assert.deepEqual(pyLang.dependencies.projectDependencyPaths, [".venv", "venv", ".conda"]);

0 commit comments

Comments
 (0)