|
| 1 | +# WIT Syntax Highlighting Example |
| 2 | + |
| 3 | +This page demonstrates WIT (WebAssembly Interface Types) syntax highlighting in VitePress. |
| 4 | + |
| 5 | +## Basic Package Definition |
| 6 | + |
| 7 | +```wit |
| 8 | +package example:[email protected]; |
| 9 | +
|
| 10 | +interface types { |
| 11 | + /// A person with name and age |
| 12 | + record person { |
| 13 | + name: string, |
| 14 | + age: u32, |
| 15 | + } |
| 16 | + |
| 17 | + /// Get a greeting for a person |
| 18 | + greet: func(p: person) -> string; |
| 19 | +} |
| 20 | +
|
| 21 | +world hello { |
| 22 | + export types; |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +## Resource Example |
| 27 | + |
| 28 | +```wit |
| 29 | +package example:[email protected]; |
| 30 | +
|
| 31 | +interface database { |
| 32 | + /// A database connection resource |
| 33 | + resource connection { |
| 34 | + /// Create a new connection |
| 35 | + constructor(url: string); |
| 36 | + |
| 37 | + /// Execute a query |
| 38 | + query: func(sql: string) -> result<list<string>, string>; |
| 39 | + |
| 40 | + /// Close the connection |
| 41 | + close: func(); |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +## Async Operations |
| 47 | + |
| 48 | +```wit |
| 49 | +package example:[email protected]; |
| 50 | +
|
| 51 | +interface io { |
| 52 | + use wasi:io/[email protected].{input-stream, output-stream}; |
| 53 | + |
| 54 | + /// Read data asynchronously |
| 55 | + read-async: func(stream: borrow<input-stream>) -> future<result<list<u8>, error-code>>; |
| 56 | + |
| 57 | + /// Write data asynchronously |
| 58 | + write-async: func(stream: borrow<output-stream>, data: list<u8>) -> future<result<_, error-code>>; |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +## Complex Types |
| 63 | + |
| 64 | +```wit |
| 65 | +package example:[email protected]; |
| 66 | +
|
| 67 | +interface types { |
| 68 | + /// Boolean values |
| 69 | + type flag = bool; |
| 70 | + |
| 71 | + /// Variant type |
| 72 | + variant color { |
| 73 | + rgb(u8, u8, u8), |
| 74 | + rgba(u8, u8, u8, u8), |
| 75 | + named(string), |
| 76 | + } |
| 77 | + |
| 78 | + /// Flags for permissions |
| 79 | + flags permissions { |
| 80 | + read, |
| 81 | + write, |
| 82 | + execute, |
| 83 | + } |
| 84 | + |
| 85 | + /// Enum for log levels |
| 86 | + enum log-level { |
| 87 | + debug, |
| 88 | + info, |
| 89 | + warning, |
| 90 | + error, |
| 91 | + } |
| 92 | + |
| 93 | + /// Option type |
| 94 | + type maybe-string = option<string>; |
| 95 | + |
| 96 | + /// Result type |
| 97 | + type io-result = result<u32, string>; |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +## World with Imports and Exports |
| 102 | + |
| 103 | +```wit |
| 104 | +package example:[email protected]; |
| 105 | +
|
| 106 | +world application { |
| 107 | + import wasi:filesystem/[email protected]; |
| 108 | + import wasi:http/[email protected]; |
| 109 | + import wasi:clocks/[email protected]; |
| 110 | + |
| 111 | + export run: func() -> result<_, string>; |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +## Feature Gates |
| 116 | + |
| 117 | +```wit |
| 118 | +package example:[email protected]; |
| 119 | +
|
| 120 | +interface experimental { |
| 121 | + @since(version = 0.2.0) |
| 122 | + @unstable(feature = new-api) |
| 123 | + new-function: func() -> string; |
| 124 | + |
| 125 | + @deprecated(since = 0.3.0) |
| 126 | + old-function: func() -> string; |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +The WIT syntax highlighting is provided by the grammar built from the [vscode-wit](https://github.com/bytecodealliance/vscode-wit) TextMate grammar, making it consistent with VS Code's WIT extension. |
0 commit comments