feat(editor): 优化 Monaco quick fix 与用户脚本元数据提示#1461
Open
cyfung1031 wants to merge 29 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
该 PR 主要围绕 ScriptCat 内置 Monaco 编辑器的诊断提示体验做增强:将 ESLint 的 quick fix 与 ScriptCat 用户脚本 metadata 的提示/警告/quick fix 解耦,并补齐多语言 Hover 文案与 metadata 规则适配,从而让编辑器提示更贴近用户脚本场景、并修复多编辑器实例下 ESLint fix 缓存互相覆盖的问题。
Changes:
- 重构 Monaco CodeAction/Hover:仅对 ESLint 来源的 marker 生成 ESLint 修复/disable/global 声明,同时新增 ScriptCat metadata 的对齐/规范化警告与 quick fix。
- 新增并测试
eslintFixCache:fix key 引入 model URI,支持按 model 定向清理,避免多编辑器实例冲突。 - 调整 ESLint 配置与 compat headers:放宽部分规则、关闭由 Monaco 自定义逻辑接管的 userscripts 规则,并新增
allFramesheader 兼容。
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/types/eslint-linter-browserify.d.ts | 补充 eslint-linter-browserify 的类型声明以支持 TS import。 |
| src/pkg/utils/monaco-editor/langs.ts | 扩展多语言文案:metadata 字段提示、@grant 取值说明、quick fix/warning 文案等。 |
| src/pkg/utils/monaco-editor/index.ts | Monaco Hover/CodeAction 重构;新增 ScriptCat metadata 检查(对齐、@connect/@match/@include 建议、@grant 冲突)与 quick fix 生成逻辑。 |
| src/pkg/utils/monaco-editor/eslintFixCache.ts | 新增 ESLint fix cache key 与按 model 定向清理工具函数。 |
| src/pkg/utils/monaco-editor/eslintFixCache.test.ts | 新增单测覆盖 fix key 隔离与定向清理行为。 |
| src/pages/components/CodeEditor/index.tsx | ESLint fix 缓存改为按 model 更新与销毁时定向清理,避免多实例覆盖。 |
| src/linter.worker.ts | 将 worker 内的 require 改为 TS import,并更严格地定义/注册 userscripts 规则。 |
| packages/eslint/linter-config.ts | 调整默认 ESLint 规则严重级别与 userscripts 规则启用策略,以适配 ScriptCat 场景。 |
| packages/eslint/compat-headers.js | 增加 metadata header allFrames 的兼容映射。 |
Comment on lines
+94
to
+103
| config.rules["userscripts/align-attributes"] = []; | ||
| config.rules["userscripts/require-download-url"] = ["warn"]; | ||
|
|
||
| // ScriptCat 不适用 - 有必要存在的用法 | ||
| // 不是所有 @include 都要改为 @match。改用自定义处理 | ||
| config.rules["userscripts/better-use-match"] = []; | ||
| // 不是 @name @name:en @name:zh-CN @name:zh-TW @name:ja 都要放在最前。这个连 warning 也很无谓 | ||
| config.rules["userscripts/require-name"] = []; | ||
| // ScriptCat 不用指定 ==UserScript== 放最前。在 ==UserScript== 前面可以写其他注释, 例如是 License。 不视为 invalid | ||
| config.rules["userscripts/no-invalid-metadata"] = []; |
Comment on lines
+94
to
+103
| config.rules["userscripts/align-attributes"] = []; | ||
| config.rules["userscripts/require-download-url"] = ["warn"]; | ||
|
|
||
| // ScriptCat 不适用 - 有必要存在的用法 | ||
| // 不是所有 @include 都要改为 @match。改用自定义处理 | ||
| config.rules["userscripts/better-use-match"] = []; | ||
| // 不是 @name @name:en @name:zh-CN @name:zh-TW @name:ja 都要放在最前。这个连 warning 也很无谓 | ||
| config.rules["userscripts/require-name"] = []; | ||
| // ScriptCat 不用指定 ==UserScript== 放最前。在 ==UserScript== 前面可以写其他注释, 例如是 License。 不视为 invalid | ||
| config.rules["userscripts/no-invalid-metadata"] = []; |
|
|
||
| const getIncludeSpacing = (spacing: string, tag: string) => { | ||
| const lenDiff = "include".length - tag.length; | ||
| return lenDiff > 0 && spacing.length > lenDiff ? spacing.slice(0, -lenDiff) : spacing; |
Comment on lines
+379
to
+383
| if (wildcardNormalizedHost.split(".").every((hostSegment) => hostSegment === "*" || /^[\w-]+$/.test(hostSegment))) { | ||
| const includeSpacing = getIncludeSpacing(spacing, normalizedTag); | ||
| const titleTemplate = currentEditorLang.replaceIncludeWithMatch; | ||
| return [ | ||
| createMetadataFix( |
Comment on lines
+759
to
+768
| const registerScriptcatMetadataMarkerProvider = () => { | ||
| const registerMetadataModel = (model: editor.ITextModel) => { | ||
| updateScriptcatMetadataMarkers(model); | ||
| model.onDidChangeContent(() => { | ||
| updateScriptcatMetadataMarkers(model); | ||
| }); | ||
| }; | ||
|
|
||
| editor.getModels().forEach(registerMetadataModel); | ||
| editor.onDidCreateModel(registerMetadataModel); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
优化 Monaco 编辑器的 CodeAction / Hover 生成逻辑,新增 ScriptCat 用户脚本元数据检查、警告与 quick fix,并调整 ESLint 规则配置,使编辑器提示更贴合用户脚本场景。
Changes
重构 Monaco 编辑器 quick fix 生成逻辑
ESLint来源的诊断生成 ESLint 修复、disable 与全局变量声明操作新增 ScriptCat 元数据检查与快速修复
@connect *.example.com提示移除通配符并生成修复@match *://example.*/*提示改用@include,并支持.tld修复建议@include提供改回@match的 quick fix@grant none与 GM API 同时使用的冲突优化 Hover 提示与多语言文案
@grant取值说明,覆盖常见 GM / CAT API 与window.*权限scriptUrl、storageName、tag、cloudCat、cloudServer、exportValue、exportCookie调整 ESLint 配置以适配 ScriptCat 场景
@include/@match建议、metadata header 位置检查allFrames兼容 header修复多个编辑器实例下的 ESLint fix 缓存问题
eslintFixCache单元测试覆盖 key 隔离与定向清理类型与实现清理
require改为 TypeScript importeslint-linter-browserify类型声明Motivation
原本 Monaco 的 quick fix 逻辑把 ESLint 修复、disable 操作和 ScriptCat metadata 场景混在一起,容易出现非 ESLint warning 也生成 ESLint quick fix、多个编辑器实例共享 fix map 互相覆盖,以及部分 ScriptCat warning 无法提供有效修复的问题。
同时,
eslint-plugin-userscripts的部分规则不完全符合 ScriptCat 的 metadata 约定。本 PR 将这些场景交给 Monaco 侧自定义检查处理,保留更贴近 ScriptCat 行为的提示和修复体验。Testing
src/pkg/utils/monaco-editor/eslintFixCache.test.tsNotes