Skip to content

feat(editor): 优化 Monaco quick fix 与用户脚本元数据提示#1461

Open
cyfung1031 wants to merge 29 commits into
release/v1.4from
improve/editor/hints/001
Open

feat(editor): 优化 Monaco quick fix 与用户脚本元数据提示#1461
cyfung1031 wants to merge 29 commits into
release/v1.4from
improve/editor/hints/001

Conversation

@cyfung1031
Copy link
Copy Markdown
Collaborator

@cyfung1031 cyfung1031 commented May 23, 2026

Summary

优化 Monaco 编辑器的 CodeAction / Hover 生成逻辑,新增 ScriptCat 用户脚本元数据检查、警告与 quick fix,并调整 ESLint 规则配置,使编辑器提示更贴合用户脚本场景。

Changes

  • 重构 Monaco 编辑器 quick fix 生成逻辑

    • 抽离通用的文本编辑 action 创建逻辑
    • 将 ScriptCat metadata quick fix 与 ESLint quick fix 分开处理
    • 仅对 ESLint 来源的诊断生成 ESLint 修复、disable 与全局变量声明操作
  • 新增 ScriptCat 元数据检查与快速修复

    • 检查 metadata value 对齐,并提供一键对齐 quick fix
    • @connect *.example.com 提示移除通配符并生成修复
    • @match *://example.*/* 提示改用 @include,并支持 .tld 修复建议
    • 对可安全转换的 @include 提供改回 @match 的 quick fix
    • 检查 @grant none 与 GM API 同时使用的冲突
  • 优化 Hover 提示与多语言文案

    • 新增 @grant 取值说明,覆盖常见 GM / CAT API 与 window.* 权限
    • 补充 ScriptCat metadata 字段提示,例如 scriptUrlstorageNametagcloudCatcloudServerexportValueexportCookie
    • 扩展 quick fix 与 warning 文案,覆盖 zh-CN、en-US、zh-TW、ja-JP、de-DE、vi-VN、ru-RU
  • 调整 ESLint 配置以适配 ScriptCat 场景

    • 将部分规则从 error 调整为 warn,或放宽常见用户脚本写法
    • 关闭由 Monaco 自定义逻辑接管的 userscripts 规则,例如 metadata 对齐、@include / @match 建议、metadata header 位置检查
    • 新增 allFrames 兼容 header
  • 修复多个编辑器实例下的 ESLint fix 缓存问题

    • ESLint fix key 加入 model URI,避免不同编辑器的相同 marker 互相覆盖
    • 更新和销毁 model 时只清理当前 model 的 fix 缓存
    • 新增 eslintFixCache 单元测试覆盖 key 隔离与定向清理
  • 类型与实现清理

    • require 改为 TypeScript import
    • 补充 eslint-linter-browserify 类型声明
    • 收窄 Monaco environment、marker、fix、metadata 相关类型

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.ts
    • 覆盖不同 model 的 fix key 不冲突
    • 覆盖清理 fix 缓存时仅移除当前 model 的条目

Notes

  • 本 PR 主要影响编辑器提示、metadata warning / quick fix、ESLint 配置与相关多语言文案。
  • PR 中保留了现有截图,用于展示 metadata warning / quick fix 效果。

Screenshot 2026-05-24 at 7 56 13 Screenshot 2026-05-24 at 7 56 57
Screenshot 2026-05-24 at 7 57 28 Screenshot 2026-05-24 at 7 57 53 Screenshot 2026-05-24 at 7 58 37
Screenshot 2026-05-24 at 7 59 06 Screenshot 2026-05-24 at 7 58 59

@cyfung1031 cyfung1031 linked an issue May 23, 2026 that may be closed by this pull request
@cyfung1031 cyfung1031 marked this pull request as draft May 23, 2026 10:39
@cyfung1031 cyfung1031 changed the title 优化 Monaco 编辑器 quick-fix ,新增元数据警告与 quick-fix feat(editor): 优化 Monaco quick fix 与用户脚本元数据提示 May 23, 2026
@cyfung1031 cyfung1031 marked this pull request as ready for review May 23, 2026 23:03
@CodFrm CodFrm requested a review from Copilot May 24, 2026 04:21
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 规则,并新增 allFrames header 兼容。

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 thread packages/eslint/linter-config.ts Outdated
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"] = [];
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in 53f1700

Comment thread packages/eslint/linter-config.ts Outdated
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"] = [];
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in 53f1700

Comment thread src/pkg/utils/monaco-editor/index.ts Outdated

const getIncludeSpacing = (spacing: string, tag: string) => {
const lenDiff = "include".length - tag.length;
return lenDiff > 0 && spacing.length > lenDiff ? spacing.slice(0, -lenDiff) : spacing;
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in 53f1700

Comment thread src/pkg/utils/monaco-editor/index.ts Outdated
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(
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in 53f1700

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);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in 53f1700

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

加入合适格式警告提醒不规范的 UserScript metablock

2 participants