Skip to content

Commit b1a738d

Browse files
cyfung1031CodFrmCopilot
authored
✨ 优化 Monaco Editor 设定,加 /* global xxx */ 修正 (#1012)
* 优化 Monaco Editor 设定,加 `/* global xxx */` 修正 * Update src/pkg/utils/monaco-editor/config.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: wangyizhi <i@xloli.top> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 7b55547 commit b1a738d

File tree

5 files changed

+416
-21
lines changed

5 files changed

+416
-21
lines changed

src/pages/components/CodeEditor/index.tsx

Lines changed: 92 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,106 @@ const CodeEditor: React.ForwardRefRenderFunction<{ editor: editor.IStandaloneCod
4545
}
4646
let edit: editor.IStandaloneDiffEditor | editor.IStandaloneCodeEditor;
4747
const inlineDiv = document.getElementById(id) as HTMLDivElement;
48-
// @ts-ignore
48+
const commonEditorOptions = {
49+
folding: true,
50+
foldingStrategy: "indentation",
51+
automaticLayout: true,
52+
scrollbar: { alwaysConsumeMouseWheel: false },
53+
overviewRulerBorder: false,
54+
scrollBeyondLastLine: false,
55+
56+
glyphMargin: true,
57+
unicodeHighlight: {
58+
ambiguousCharacters: false,
59+
},
60+
61+
// https://code.visualstudio.com/docs/editing/intellisense
62+
63+
// Controls whether suggestions should be accepted on commit characters. For example, in JavaScript, the semi-colon (`;`) can be a commit character that accepts a suggestion and types that character.
64+
acceptSuggestionOnCommitCharacter: true,
65+
66+
// Controls if suggestions should be accepted on 'Enter' - in addition to 'Tab'. Helps to avoid ambiguity between inserting new lines or accepting suggestions. The value 'smart' means only accept a suggestion with Enter when it makes a textual change
67+
acceptSuggestionOnEnter: "on",
68+
69+
// Controls the delay in ms after which quick suggestions will show up.
70+
quickSuggestionsDelay: 10,
71+
72+
// Controls if suggestions should automatically show up when typing trigger characters
73+
suggestOnTriggerCharacters: true,
74+
75+
// Controls if pressing tab inserts the best suggestion and if tab cycles through other suggestions
76+
tabCompletion: "off",
77+
78+
// Controls whether sorting favours words that appear close to the cursor
79+
suggest: {
80+
localityBonus: true,
81+
preview: true,
82+
},
83+
84+
// Controls how suggestions are pre-selected when showing the suggest list
85+
suggestSelection: "first",
86+
87+
// Enable word based suggestions
88+
wordBasedSuggestions: "matchingDocuments",
89+
90+
// Enable parameter hints
91+
parameterHints: {
92+
enabled: true,
93+
},
94+
95+
// https://qiita.com/H-goto16/items/43802950fc5c112c316b
96+
// https://zenn.dev/udonj/articles/ultimate-vscode-customization-2024
97+
// https://github.com/is0383kk/VSCode
98+
99+
quickSuggestions: {
100+
other: "inline",
101+
comments: true,
102+
strings: true,
103+
},
104+
105+
fastScrollSensitivity: 10,
106+
smoothScrolling: true,
107+
inlineSuggest: {
108+
enabled: true,
109+
},
110+
guides: {
111+
indentation: true,
112+
},
113+
renderLineHighlightOnlyWhenFocus: true,
114+
snippetSuggestions: "top",
115+
116+
cursorBlinking: "phase",
117+
cursorSmoothCaretAnimation: "off",
118+
119+
autoIndent: "advanced",
120+
wrappingIndent: "indent",
121+
wordSegmenterLocales: ["ja", "zh-CN", "zh-Hant-TW"] as string[],
122+
123+
renderLineHighlight: "gutter",
124+
renderWhitespace: "selection",
125+
renderControlCharacters: true,
126+
dragAndDrop: false,
127+
emptySelectionClipboard: false,
128+
copyWithSyntaxHighlighting: false,
129+
bracketPairColorization: {
130+
enabled: true,
131+
},
132+
mouseWheelZoom: true,
133+
links: true,
134+
accessibilitySupport: "off",
135+
largeFileOptimizations: true,
136+
colorDecorators: true,
137+
} as const;
49138
if (diffCode) {
50139
edit = editor.createDiffEditor(inlineDiv, {
51140
hideUnchangedRegions: {
52141
enabled: true,
53142
},
54143
enableSplitViewResizing: false,
55144
renderSideBySide: false,
56-
folding: true,
57-
foldingStrategy: "indentation",
58-
automaticLayout: true,
59-
scrollbar: { alwaysConsumeMouseWheel: false },
60-
overviewRulerBorder: false,
61-
scrollBeyondLastLine: false,
62145
readOnly: true,
63146
diffWordWrap: "off",
64-
glyphMargin: true,
65-
unicodeHighlight: {
66-
ambiguousCharacters: false,
67-
},
147+
...commonEditorOptions,
68148
});
69149
edit.setModel({
70150
original: editor.createModel(diffCode, "javascript"),
@@ -74,17 +154,8 @@ const CodeEditor: React.ForwardRefRenderFunction<{ editor: editor.IStandaloneCod
74154
edit = editor.create(inlineDiv, {
75155
language: "javascript",
76156
theme: document.body.getAttribute("arco-theme") === "dark" ? "vs-dark" : "vs",
77-
folding: true,
78-
foldingStrategy: "indentation",
79-
automaticLayout: true,
80-
scrollbar: { alwaysConsumeMouseWheel: false },
81-
overviewRulerBorder: false,
82-
scrollBeyondLastLine: false,
83157
readOnly: !editable,
84-
glyphMargin: true,
85-
unicodeHighlight: {
86-
ambiguousCharacters: false,
87-
},
158+
...commonEditorOptions,
88159
});
89160
edit.setValue(code);
90161

src/pkg/utils/monaco-editor/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const config = {
99
// https://github.com/suren-atoyan/monaco-react/issues/75#issuecomment-1890761086
1010
allowNonTsExtensions: true,
1111
allowJs: true,
12+
checkJs: true, // 启用 JS 类型检查以提供更好的智能提示
13+
noUnusedLocals: false, // 用户脚本中可能有意声明但未使用的变量,避免无用变量警告
14+
noFallthroughCasesInSwitch: false, // 允许 switch 穿透,用户脚本常见模式,减少警告
15+
noImplicitThis: false, // 用户脚本中 this 上下文可能不明确,避免相关警告
1216
strict: true,
1317
} as languages.typescript.CompilerOptions;
1418

src/pkg/utils/monaco-editor/index.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { globalCache, systemConfig } from "@App/pages/store/global";
22
import EventEmitter from "eventemitter3";
33
import { languages } from "monaco-editor";
4+
import { findGlobalInsertionInfo, updateGlobalCommentLine } from "./utils";
45

56
// 注册eslint
67
const linterWorker = new Worker("/src/linter.worker.js");
@@ -517,6 +518,10 @@ export default function registerEditor() {
517518
// 判断有没有修复方案
518519
const val = context.markers[i];
519520
const code = typeof val.code === "string" ? val.code : val.code!.value;
521+
522+
// =============================
523+
// 1) eslint-fix logic
524+
// =============================
520525
const fix = eslintFix.get(
521526
`${code}|${val.startLineNumber}|${val.endLineNumber}|${val.startColumn}|${val.endColumn}`
522527
);
@@ -539,6 +544,69 @@ export default function registerEditor() {
539544
isPreferred: true,
540545
});
541546
}
547+
548+
// =============================
549+
// 2) /* global XXX */ fix
550+
// =============================
551+
552+
// message format usually like: "'XXX' is not defined.ESLint (no-undef)"
553+
if (code === "no-undef") {
554+
const message = val.message || "";
555+
const match = message.match(/^[^']*'([^']+)'[^']*$/);
556+
const globalName = match && match[1];
557+
558+
if (globalName) {
559+
const { insertLine, globalLine } = findGlobalInsertionInfo(model);
560+
let textEdit: languages.IWorkspaceTextEdit["textEdit"];
561+
562+
if (globalLine != null) {
563+
// there is already a /* global ... */ line → update it
564+
const oldLine = model.getLineContent(globalLine);
565+
const newLine = updateGlobalCommentLine(oldLine, globalName);
566+
567+
textEdit = {
568+
range: {
569+
startLineNumber: globalLine,
570+
startColumn: 1,
571+
endLineNumber: globalLine,
572+
endColumn: oldLine.length + 1,
573+
},
574+
text: newLine,
575+
};
576+
} else {
577+
// no global line yet → insert a new one
578+
textEdit = {
579+
range: {
580+
startLineNumber: insertLine,
581+
startColumn: 1,
582+
endLineNumber: insertLine,
583+
endColumn: 1,
584+
},
585+
text: `/* global ${globalName} */\n`,
586+
};
587+
}
588+
589+
actions.push(<languages.CodeAction>{
590+
title: `将 '${globalName}' 声明为全局变量 (/* global */)`,
591+
diagnostics: [val],
592+
kind: "quickfix",
593+
edit: {
594+
edits: [
595+
{
596+
resource: model.uri,
597+
textEdit,
598+
versionId: undefined,
599+
},
600+
],
601+
},
602+
isPreferred: false,
603+
});
604+
}
605+
}
606+
607+
// =============================
608+
// 3) disable-next-line / disable fixes
609+
// =============================
542610
// 添加eslint-disable-next-line和eslint-disable
543611
actions.push(<languages.CodeAction>{
544612
title: multiLang.addEslintDisableNextLine,

0 commit comments

Comments
 (0)