Skip to content

Commit 5306a64

Browse files
committed
feat(documentation): add documentation for traits and contracts
Fixes #7
1 parent 39af957 commit 5306a64

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/main/kotlin/org/tonstudio/tact/ide/documentation/DocumentationUtils.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ object DocumentationUtils {
3434
val asStruct = loadKey(TactColor.STRUCT.textAttributesKey)
3535
val asMessage = loadKey(TactColor.MESSAGE.textAttributesKey)
3636
val asTrait = loadKey(TactColor.TRAIT.textAttributesKey)
37+
val asContract = loadKey(TactColor.CONTRACT.textAttributesKey)
3738
val asPrimitive = loadKey(TactColor.PRIMITIVE.textAttributesKey)
3839
val asConst = loadKey(TactColor.CONSTANT.textAttributesKey)
3940
val asAsmInstruction = loadKey(TactColor.ASM_INSTRUCTION.textAttributesKey)

src/main/kotlin/org/tonstudio/tact/ide/documentation/TactDocumentationProvider.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class TactDocumentationProvider : AbstractDocumentationProvider() {
1717
is TactNativeFunctionDeclaration -> element.generateDoc()
1818
is TactStructDeclaration -> element.generateDoc()
1919
is TactMessageDeclaration -> element.generateDoc()
20+
is TactTraitDeclaration -> element.generateDoc()
21+
is TactContractDeclaration -> element.generateDoc()
2022
is TactPrimitiveDeclaration -> element.generateDoc()
2123
is TactConstDefinition -> element.generateDoc()
2224
is TactVarDefinition -> element.generateDoc()

src/main/kotlin/org/tonstudio/tact/ide/documentation/documentation.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.tonstudio.tact.ide.documentation.DocumentationUtils.asBraces
2424
import org.tonstudio.tact.ide.documentation.DocumentationUtils.asBuiltin
2525
import org.tonstudio.tact.ide.documentation.DocumentationUtils.asComment
2626
import org.tonstudio.tact.ide.documentation.DocumentationUtils.asConst
27+
import org.tonstudio.tact.ide.documentation.DocumentationUtils.asContract
2728
import org.tonstudio.tact.ide.documentation.DocumentationUtils.asField
2829
import org.tonstudio.tact.ide.documentation.DocumentationUtils.asFunction
2930
import org.tonstudio.tact.ide.documentation.DocumentationUtils.asIdentifier
@@ -37,6 +38,7 @@ import org.tonstudio.tact.ide.documentation.DocumentationUtils.asStruct
3738
import org.tonstudio.tact.ide.documentation.DocumentationUtils.asTrait
3839
import org.tonstudio.tact.lang.TactSyntaxHighlighter
3940
import org.tonstudio.tact.lang.doc.psi.TactDocComment
41+
import org.tonstudio.tact.lang.psi.impl.TactPsiImplUtil.getInheritedTraitsBase
4042

4143
private fun StringBuilder.generateOwnerDpc(element: TactNamedElement) {
4244
val owner = element.getOwner() ?: return
@@ -325,6 +327,49 @@ fun TactFieldDefinition.generateDoc(): String {
325327
}
326328
}
327329

330+
fun TactContractDeclaration.generateDoc(): String {
331+
return buildString {
332+
append(DocumentationMarkup.DEFINITION_START)
333+
334+
part("contract", asKeyword)
335+
part(name, asContract)
336+
337+
val inheritedTraits = contractType.getWithClause()?.typeList ?: emptyList()
338+
formatInheritTraits(inheritedTraits)
339+
340+
append(DocumentationMarkup.DEFINITION_END)
341+
generateCommentsPart(this@generateDoc)
342+
}
343+
}
344+
345+
fun TactTraitDeclaration.generateDoc(): String {
346+
return buildString {
347+
append(DocumentationMarkup.DEFINITION_START)
348+
349+
part("trait", asKeyword)
350+
part(name, asTrait)
351+
352+
val inheritedTraits = traitType.getWithClause()?.typeList ?: emptyList()
353+
formatInheritTraits(inheritedTraits)
354+
355+
append(DocumentationMarkup.DEFINITION_END)
356+
generateCommentsPart(this@generateDoc)
357+
}
358+
}
359+
360+
private fun StringBuilder.formatInheritTraits(inheritedTraits: List<TactType>) {
361+
val filtered = inheritedTraits.filter { it.text != "BaseTrait" }
362+
if (filtered.isEmpty()) return
363+
364+
colorize("with ", asKeyword)
365+
filtered.forEachIndexed { index, trait ->
366+
colorize(trait.text, asTrait)
367+
if (index != filtered.size - 1) {
368+
append(", ")
369+
}
370+
}
371+
}
372+
328373
fun TactPrimitiveDeclaration.generateDoc(): String {
329374
return buildString {
330375
append(DocumentationMarkup.DEFINITION_START)

0 commit comments

Comments
 (0)