diff --git a/Tools/dora-dora/src/App.tsx b/Tools/dora-dora/src/App.tsx index b871d741c..1697a4cee 100644 --- a/Tools/dora-dora/src/App.tsx +++ b/Tools/dora-dora/src/App.tsx @@ -1267,7 +1267,7 @@ export default function PersistentDrawerLeft() { monaco.KeyCode.F12 | monaco.KeyMod.WinCtrl, ], contextMenuGroupId: "navigation", - contextMenuOrder: 1.5, + contextMenuOrder: 1, run: function(ed) { const position = ed.getPosition(); if (position === null) return; @@ -1307,6 +1307,35 @@ export default function PersistentDrawerLeft() { }); }, }); + if (lang === "tl" || lang === "lua") { + editor.addAction({ + id: "dora-action-require", + label: t("editor.require"), + keybindings: [ + monaco.KeyCode.F1 | monaco.KeyMod.CtrlCmd, + monaco.KeyCode.F1 | monaco.KeyMod.WinCtrl, + ], + contextMenuGroupId: "navigation", + contextMenuOrder: 2, + run: function(ed) { + const position = ed.getPosition(); + if (position === null) return; + const model = ed.getModel(); + if (model === null) return; + const word = model.getWordAtPosition(position); + if (word === null) return; + model.pushEditOperations(null, [{ + text: `local ${word.word} = require("${word.word}")\n`, + range: { + startLineNumber: 1, + startColumn: 0, + endLineNumber: 1, + endColumn: 0 + } + }], () => {return null}); + }, + }); + } } const model = editor.getModel(); if (model) { diff --git a/Tools/dora-dora/src/Editor.tsx b/Tools/dora-dora/src/Editor.tsx index dcefad221..8336b7778 100644 --- a/Tools/dora-dora/src/Editor.tsx +++ b/Tools/dora-dora/src/Editor.tsx @@ -307,6 +307,52 @@ const signatureHelpProvider = (signatureHelpTriggerCharacters: string[], lang: S } as monaco.languages.SignatureHelpProvider; }; +const codeActionProvider = { + resolveCodeAction(codeAction, token) { + console.log(codeAction, token); + return undefined; + }, + provideCodeActions(model, range, context, token) { + if (context.only !== "quickfix") { + return undefined; + } + const marker = context.markers.find(m => { + return m.message.startsWith("unknown variable:"); + }); + if (marker === undefined) { + return undefined; + } + const moduleName = marker.message.replace("unknown variable: ", ""); + const message = Info.locale.match(/^zh/) ? "导入模块" : "Require"; + return { + actions: [ + { + title: `${message} ${moduleName}`, + edit: { + edits: [ + { + resource: model.uri, + textEdit: { + text: `local ${moduleName} = require("${moduleName}")\n`, + range: { + startLineNumber: 1, + startColumn: 0, + endLineNumber: 1, + endColumn: 0 + } + }, + } + ] + }, + isPreferred: true, + kind: "quickfix" + } + ], + dispose() { }, + } as monaco.languages.CodeActionList; + }, +} as monaco.languages.CodeActionProvider; + monaco.languages.register({id: 'tl'}); monaco.languages.setLanguageConfiguration("tl", teal.config); monaco.languages.setMonarchTokensProvider("tl", teal.language); @@ -314,6 +360,7 @@ const tlComplete = completionItemProvider([".", ":"], "tl"); monaco.languages.registerCompletionItemProvider("tl", tlComplete); monaco.languages.registerHoverProvider("tl", hoverProvider("tl")); monaco.languages.registerSignatureHelpProvider("tl", signatureHelpProvider(["(", ","], "tl")); +monaco.languages.registerCodeActionProvider("tl", codeActionProvider) const luaComplete = completionItemProvider([".", ":"], "lua"); monaco.languages.setLanguageConfiguration("lua", lua.config); diff --git a/Tools/dora-dora/src/i18n.ts b/Tools/dora-dora/src/i18n.ts index 51e401a97..3210bd8d2 100644 --- a/Tools/dora-dora/src/i18n.ts +++ b/Tools/dora-dora/src/i18n.ts @@ -86,6 +86,7 @@ i18n }, editor: { goToDefinition: "Go to Definition", + require: "Import this module", }, menu: { version: "Dorothy SSR version {{version}}", @@ -187,6 +188,7 @@ i18n }, editor: { goToDefinition: "跳转到定义", + require: "导入该模块", }, menu: { version: "Dorothy SSR 版本号 {{version}}",