plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


2c7e59b — Peter John 2026-11-10T15:29:16+05:30
Doc snippets that import are type-checked
Files changed (2) hide show
  1. compiler/lsp/src/main.rs +22 -1
  2. docs/game.md +29 -0
compiler/lsp/src/main.rs CHANGED
@@ -865,7 +865,9 @@ mod tests {
865
865
  }
866
866
 
867
867
  /// Every ```plum block in the README and docs parses without a syntax diagnostic
868
- /// (statement fragments are wrapped in a function first).
868
+ /// (statement fragments are wrapped in a function first), and every block that is a
869
+ /// whole program (it starts with `import`) also type-checks. A deliberate fragment stays
870
+ /// syntax-only by not starting with `import`.
869
871
  #[test]
870
872
  fn docSnippetsHaveNoSyntaxDiagnostics() {
871
873
  let files = [
@@ -876,6 +878,10 @@ mod tests {
876
878
  "docs/tools.md",
877
879
  "docs/performance.md",
878
880
  ];
881
+ let dir = std::env::temp_dir().join(format!("plum_lsp_docs_{}", std::process::id()));
882
+ std::fs::create_dir_all(&dir).unwrap();
883
+ let mut checked = 0;
884
+ let mut failures = Vec::new();
879
885
  let mut n = 0;
880
886
  for file in files {
881
887
  let text = std::fs::read_to_string(repoRoot().join(file)).unwrap();
@@ -941,10 +947,25 @@ mod tests {
941
947
  } else {
942
948
  assert!(d.is_empty(), "{file} snippet has syntax errors:\n{block}");
943
949
  }
950
+ if block.starts_with("import ") {
951
+ let path = dir.join(format!("snippet{n}.plum"));
952
+ std::fs::write(&path, block).unwrap();
953
+ let diags = typeDiagnosticsFor(&path, &repoRoot(), block);
954
+ if !diags.is_empty() {
955
+ failures.push(format!(
956
+ "{file} snippet has type errors {:?}:\n{block}",
957
+ diags.iter().map(|d| (d.range.start.line + 1, &d.message)).collect::<Vec<_>>()
958
+ ));
959
+ }
960
+ checked += 1;
961
+ }
944
962
  n += 1;
945
963
  }
946
964
  }
965
+ let _ = std::fs::remove_dir_all(&dir);
966
+ assert!(failures.is_empty(), "{} of {checked} checked doc snippets fail:\n{}", failures.len(), failures.join("\n"));
947
967
  assert!(n > 20, "found only {n} doc snippets");
968
+ assert!(checked > 10, "type-checked only {checked} doc snippets");
948
969
  }
949
970
 
950
971
  /// A type error is published at the position its `Diagnostic` reports (1-based there,
docs/game.md CHANGED
@@ -123,6 +123,24 @@ Only the current screen is shown, gets input and steps. The others are paused an
123
123
  During a transition other than `Cut`, both screens are drawn, the one going out fading or sliding out and the one coming in fading or sliding in over `secs` seconds of ticks. Only the one coming in gets input and steps, and reacts to messages; the one going out is frozen. `Game` animates them with a parent of each screen that has an offset and an opacity, so the screens' actors see it in their `ctx` too (and a sliding screen is hit where it is drawn). A transition that starts on a tick is first drawn with the old screen fully shown; `game.shift` is the transition in progress.
124
124
 
125
125
  ```plum
126
+ import std/Int
127
+ import std/Float
128
+ import std/Str
129
+ import std/Option
130
+ import std/Io
131
+ import std/math/Vec2
132
+ import std/game/Color
133
+ import std/game/gfx
134
+ import std/game/input
135
+ import std/game/ui
136
+ import std/game/Store
137
+ import std/game/Game
138
+ import std/collections/RoseTree
139
+ import std/game/Place
140
+ import std/game/Actor
141
+ import std/game/Ctx
142
+ import std/game/SceneTree
143
+
126
144
  enum Msg = Start | Back
127
145
 
128
146
  # The state names the screen shown, by the screen's own name.
@@ -258,6 +276,17 @@ Each kind of actor is its own struct, with only its own fields, that `is Actor[M
258
276
  An actor the pointer can hit says so with a second trait, `Clickable` ([`Clickable.plum`](../std/game/Clickable.plum)): `struct Target(..) is Actor[Msg], Clickable =`, with `hit(self, p) -> Bool`, whether a point in its own coordinates is on it. Only a `Clickable` actor that says it is hit gets a pointer event (and its parents after it, clickable or not), and is found by `tree.hit(x, y)` and `tree.hitId(x, y)`, topmost first. The tree's pointer walk skips every other actor with one type-tag test, and computes the transform of one only when it has children (who may be clickable). A `hit` on an actor that isn't `Clickable` is a plain method the tree never calls, and nothing warns about it: if an actor gets no clicks, check its `is` list.
259
277
 
260
278
  ```plum
279
+ import std/Int
280
+ import std/Float
281
+ import std/math/Vec2
282
+ import std/game/input
283
+ import std/game/Place
284
+ import std/game/Actor
285
+ import std/game/Clickable
286
+ import std/game/Ctx
287
+
288
+ enum Msg = Hit(id: Int)
289
+
261
290
  struct Target(id: Int, x: Float, y: Float) is Actor[Msg], Clickable =
262
291
  fun key(self) -> Int = self.id
263
292