Skip to content

Commit

Permalink
add module auto import function. [skip CI]
Browse files Browse the repository at this point in the history
  • Loading branch information
pigpigyyy committed Oct 15, 2023
1 parent 859fdcb commit 3789331
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
31 changes: 30 additions & 1 deletion Tools/dora-dora/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
47 changes: 47 additions & 0 deletions Tools/dora-dora/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,60 @@ 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);
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);
Expand Down
2 changes: 2 additions & 0 deletions Tools/dora-dora/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ i18n
},
editor: {
goToDefinition: "Go to Definition",
require: "Import this module",
},
menu: {
version: "Dorothy SSR version {{version}}",
Expand Down Expand Up @@ -187,6 +188,7 @@ i18n
},
editor: {
goToDefinition: "跳转到定义",
require: "导入该模块",
},
menu: {
version: "Dorothy SSR 版本号 {{version}}",
Expand Down

0 comments on commit 3789331

Please sign in to comment.