From de839c2848f158d88870737b3568f54b36af716e Mon Sep 17 00:00:00 2001 From: Rahul Yadav Date: Sat, 2 Mar 2024 12:17:45 +0530 Subject: [PATCH] Include Tact code snippets --- src/assets/ton/tact/snippets.ts | 107 ++++++++++++++++++ .../workspace/Editor/EditorOnMount.ts | 27 +++++ 2 files changed, 134 insertions(+) create mode 100644 src/assets/ton/tact/snippets.ts diff --git a/src/assets/ton/tact/snippets.ts b/src/assets/ton/tact/snippets.ts new file mode 100644 index 0000000..47c77b4 --- /dev/null +++ b/src/assets/ton/tact/snippets.ts @@ -0,0 +1,107 @@ +export const tactSnippets = [ + { + label: 'spdx', + code: '// SPDX-License-Identifier: MIT', + description: 'SPDX License', + }, + { + label: 'con', + code: 'contract ${1:Name} {\n\t$0\n}', + description: 'A template for contract.', + }, + { + label: 'imp', + code: "import '${1:contract}';", + description: 'A template for import contract.', + }, + { + label: 'impdep', + code: 'import "@stdlib/deploy";', + description: 'A template for import deploy.', + }, + { + label: 'impown', + code: 'import "@stdlib/ownable";', + description: 'A template for import ownable.', + }, + { + label: 'impstop', + code: 'import "@stdlib/stoppable";', + description: 'A template for import stoppable.', + }, + { + label: 'map', + code: 'map[${1:type1}]${2:type2};', + description: 'mapping declaration', + }, + { + label: 'init', + code: 'init () {\n\t$0\n}', + description: 'init with 0 param declaration', + }, + { + label: 'init1', + code: 'init (${1:name}: ${2:type}) {\n\t$0\n}', + description: 'init with 1 param declaration', + }, + { + label: 'init2', + code: 'init (${1:name}: ${2:type}, ${3:name}: ${4:type}) {\n\t$0\n}', + description: 'init with 2 param declaration', + }, + { + label: 'fun', + code: 'fun ${1:name}(${2:name}: ${3:type}) {\n\t$0\n}', + description: 'function declaration', + }, + { + label: 'funr', + code: 'fun ${1:name}(${2:name}: ${3:type}): ${4:type} {\n\t$0\n}', + description: 'function return declaration', + }, + { + label: 'if', + code: 'if (${1:condition}) {\n\t$0\n}', + description: 'if statement', + }, + { + label: 'ife', + code: 'if (${1:condition}) {\n\t$2\n} else {\n\t$0\n}', + description: 'if else statement', + }, + { + label: 'while', + code: 'while (${1:condition}) {\n\t$0\n}', + description: 'while statement', + }, + { + label: 'until', + code: 'do {\n\t$0\n} until (${1:condition});', + description: 'until statement', + }, + { + label: 'repeat', + code: 'repeat(${1:index}) {\n\t$0\n}', + description: 'repeat statement', + }, + { + label: 'rec', + code: 'receive(${1:name}) {\n\t$0\n}', + description: 'receive declaration', + }, + { + label: 'ctx', + code: 'let ctx: Context = context();', + description: 'context declaration', + }, + { + label: 'doc', + code: '//\n/// @notice What does it do?;\n///\n/// @param name - description;\n/// @return Struct - description;', + description: 'documentation for function', + }, + { + label: 'req', + code: 'require(${1:Bool}, ${2:String});', + description: 'require expression', + }, +]; diff --git a/src/components/workspace/Editor/EditorOnMount.ts b/src/components/workspace/Editor/EditorOnMount.ts index 1d92b9c..16abaca 100644 --- a/src/components/workspace/Editor/EditorOnMount.ts +++ b/src/components/workspace/Editor/EditorOnMount.ts @@ -1,3 +1,4 @@ +import { tactSnippets } from '@/assets/ton/tact/snippets'; import { editor } from 'monaco-editor'; import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'; @@ -36,6 +37,32 @@ export const editorOnMount = async ( const messageMethods = ['recv_internal', 'recv_external']; + monaco.languages.registerCompletionItemProvider('tact', { + provideCompletionItems: (model, position) => { + var word = model.getWordUntilPosition(position); + var range = { + startLineNumber: position.lineNumber, + endLineNumber: position.lineNumber, + startColumn: word.startColumn, + endColumn: word.endColumn, + }; + return { + suggestions: tactSnippets.map((snippet) => { + return { + label: snippet.label, + kind: monaco.languages.CompletionItemKind.Snippet, + insertTextRules: + monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet, + insertText: snippet.code, + documentation: snippet.description || '', + detail: snippet.description || '', + range, + }; + }), + }; + }, + }); + monaco.languages.registerCompletionItemProvider('func', { provideCompletionItems: (model, position) => { var word = model.getWordUntilPosition(position);