|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Copyright © 2025 TON Core |
| 3 | +import {CompletionItemKind, InsertTextFormat} from "vscode-languageserver-types" |
| 4 | + |
| 5 | +import type {CompletionProvider} from "@server/completion/CompletionProvider" |
| 6 | +import type {CompletionContext} from "@server/languages/tolk/completion/CompletionContext" |
| 7 | +import {CompletionResult, CompletionWeight} from "@server/completion/WeightedCompletionItem" |
| 8 | +import {parentOfType} from "@server/psi/utils" |
| 9 | + |
| 10 | +export class ContractFieldCompletionProvider implements CompletionProvider<CompletionContext> { |
| 11 | + public isAvailable(ctx: CompletionContext): boolean { |
| 12 | + return ctx.contractTopLevel |
| 13 | + } |
| 14 | + |
| 15 | + public addCompletion(ctx: CompletionContext, result: CompletionResult): void { |
| 16 | + const contract = parentOfType(ctx.element.node, "contract_declaration") |
| 17 | + if (!contract) return |
| 18 | + |
| 19 | + const body = contract.childForFieldName("body") |
| 20 | + if (!body) return |
| 21 | + |
| 22 | + const existingFields: Set<string> = new Set() |
| 23 | + for (const child of body.children) { |
| 24 | + if (child?.type === "contract_field") { |
| 25 | + const name = child.childForFieldName("name")?.text |
| 26 | + if (name) existingFields.add(name) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + const fields: Record<string, string> = { |
| 31 | + author: "Author of the contract", |
| 32 | + version: "Version of the contract", |
| 33 | + description: "Description of the contract", |
| 34 | + symbolsNamespace: "Namespace for contract symbols", |
| 35 | + incomingMessages: "Allowed incoming messages type", |
| 36 | + incomingExternal: "Allowed incoming external messages type", |
| 37 | + storage: "Persistent storage structure", |
| 38 | + storageAtDeployment: "Storage structure at deployment", |
| 39 | + forceAbiExport: "List of symbols to additionally export to ABI", |
| 40 | + } |
| 41 | + |
| 42 | + for (const [field, description] of Object.entries(fields)) { |
| 43 | + if (existingFields.has(field)) continue |
| 44 | + |
| 45 | + result.add({ |
| 46 | + label: field, |
| 47 | + labelDetails: { |
| 48 | + detail: `: ${description}`, |
| 49 | + }, |
| 50 | + kind: CompletionItemKind.Field, |
| 51 | + insertText: `${field}: $0`, |
| 52 | + insertTextFormat: InsertTextFormat.Snippet, |
| 53 | + weight: CompletionWeight.CONTEXT_ELEMENT, |
| 54 | + }) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments