plum

#treesitter#compiler#wasm

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

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


4f59df3 — Peter John 2026-11-10T16:07:30+05:30
LSP: complete the built-ins after @
compiler/lsp/src/completion.rs CHANGED
@@ -54,6 +54,56 @@ const KEYWORDS: &[&str] = &[
54
54
  "false",
55
55
  ];
56
56
 
57
+ /// The start (in chars) of the built-in name being typed when the cursor at `col` of `line`
58
+ /// follows an `@` and the identifier characters after it (`x := @me|`); `None` anywhere else.
59
+ pub fn builtinPrefixStart(line: &str, col: usize) -> Option<usize> {
60
+ let chars: Vec<char> = line.chars().take(col).collect();
61
+ let mut at = chars.len();
62
+ while at > 0 && (chars[at - 1].is_alphanumeric() || chars[at - 1] == '_') {
63
+ at -= 1;
64
+ }
65
+ if at == 0 || chars[at - 1] != '@' {
66
+ return None;
67
+ }
68
+ // An `@` inside a string or a comment is text.
69
+ let before: String = chars[..at - 1].iter().collect();
70
+ if before.contains('#') || before.matches('"').count() % 2 == 1 {
71
+ return None;
72
+ }
73
+ Some(at - 1)
74
+ }
75
+
76
+ /// The built-ins (`@typeName`, `@sameRef`, the `@mem..` family) and `@native`, each replacing
77
+ /// the text from the `@` at char `start` of `lineNo` to the cursor at char `col`.
78
+ pub fn builtinItems(lineText: &str, lineNo: u32, start: usize, col: usize) -> Vec<CompletionItem> {
79
+ let utf16 = |chars: usize| lineText.chars().take(chars).map(char::len_utf16).sum::<usize>() as u32;
80
+ let range = Range::new(Position::new(lineNo, utf16(start)), Position::new(lineNo, utf16(col)));
81
+ let native = ("native", "fun f(self) -> Int = @native", "the whole body of a std function the compiler supplies");
82
+ let what = |takes: plum_core::compile_builtins::Takes| match takes {
83
+ plum_core::compile_builtins::Takes::OneType => "takes one type".to_string(),
84
+ plum_core::compile_builtins::Takes::Values(0) => "takes no arguments".to_string(),
85
+ plum_core::compile_builtins::Takes::Values(1) => "takes one value".to_string(),
86
+ plum_core::compile_builtins::Takes::Values(n) => format!("takes {n} values"),
87
+ };
88
+ plum_core::compile_builtins::BUILTINS
89
+ .iter()
90
+ .map(|(name, takes, eg)| (*name, *eg, what(*takes)))
91
+ .chain(std::iter::once((native.0, native.1, native.2.to_string())))
92
+ .map(|(name, eg, doc)| {
93
+ let label = format!("@{name}");
94
+ CompletionItem {
95
+ label: label.clone(),
96
+ kind: Some(if name == "native" { CompletionItemKind::KEYWORD } else { CompletionItemKind::FUNCTION }),
97
+ detail: Some(eg.to_string()),
98
+ documentation: Some(Documentation::String(format!("built-in: {doc}"))),
99
+ filter_text: Some(label.clone()),
100
+ text_edit: Some(CompletionTextEdit::Edit(TextEdit { range, new_text: label })),
101
+ ..Default::default()
102
+ }
103
+ })
104
+ .collect()
105
+ }
106
+
57
107
  pub fn keywordItems() -> Vec<CompletionItem> {
58
108
  KEYWORDS
59
109
  .iter()
compiler/lsp/src/main.rs CHANGED
@@ -122,6 +122,11 @@ impl Backend {
122
122
  let tree = Self::parseTree(&sanitized);
123
123
  let own_source = plum_core::AstParser::new(&sanitized).parseSourceRaw(tree.root_node());
124
124
 
125
+ // After `@`: the built-ins, and nothing else.
126
+ if let Some(start) = completion::builtinPrefixStart(line_text, pos.character as usize) {
127
+ return completion::builtinItems(line_text, pos.line, start, pos.character as usize);
128
+ }
129
+
125
130
  if let Some(receiver) = completion::receiverBeforeDot(line_text, pos.character as usize) {
126
131
  let Ok(path) = uri.to_file_path() else {
127
132
  return Vec::new();
@@ -246,7 +251,7 @@ impl LanguageServer for Backend {
246
251
  TextDocumentSyncKind::FULL,
247
252
  )),
248
253
  completion_provider: Some(CompletionOptions {
249
- trigger_characters: Some(vec![".".to_string()]),
254
+ trigger_characters: Some(vec![".".to_string(), "@".to_string()]),
250
255
  ..Default::default()
251
256
  }),
252
257
  hover_provider: Some(HoverProviderCapability::Simple(true)),
@@ -1056,6 +1061,43 @@ mod tests {
1056
1061
  }
1057
1062
 
1058
1063
  /// Completion scanners get a char index: after "é😀" the client's column 6 is char 5.
1064
+ /// After `@` the built-ins are offered: `@typeName`, `@sameRef`, the eight `@mem..` and
1065
+ /// `@native`, each replacing what was typed from the `@` on.
1066
+ #[test]
1067
+ fn builtinsAreOfferedAfterAt() {
1068
+ let line = " ok := @sa";
1069
+ let col = line.chars().count();
1070
+ let start = completion::builtinPrefixStart(line, col).expect("after @");
1071
+ assert_eq!(start, 8);
1072
+ let items = completion::builtinItems(line, 3, start, col);
1073
+ let labels: Vec<&str> = items.iter().map(|i| i.label.as_str()).collect();
1074
+ let want = [
1075
+ "@typeName", "@sameRef", "@memSize", "@memGrow", "@memStoreI32", "@memLoadI32", "@memStoreU8",
1076
+ "@memLoadU8", "@memStoreF32", "@memLoadF32", "@native",
1077
+ ];
1078
+ assert_eq!(labels.len(), want.len(), "{labels:?}");
1079
+ for w in want {
1080
+ assert!(labels.contains(&w), "missing {w} in {labels:?}");
1081
+ }
1082
+ let same = items.iter().find(|i| i.label == "@sameRef").unwrap();
1083
+ assert_eq!(same.detail.as_deref(), Some("@sameRef(a, b)"));
1084
+ let Some(CompletionTextEdit::Edit(edit)) = &same.text_edit else { panic!("no edit: {same:?}") };
1085
+ assert_eq!(edit.new_text, "@sameRef");
1086
+ assert_eq!((edit.range.start, edit.range.end), (Position::new(3, 8), Position::new(3, 11)));
1087
+ // Right after the `@`, and with wide characters before it (UTF-16 columns in the edit).
1088
+ assert_eq!(completion::builtinPrefixStart(" @", 3), Some(2));
1089
+ let wide = " é😀 := @m";
1090
+ let n = wide.chars().count();
1091
+ let s = completion::builtinPrefixStart(wide, n).unwrap();
1092
+ let it = &completion::builtinItems(wide, 0, s, n)[0];
1093
+ let Some(CompletionTextEdit::Edit(e)) = &it.text_edit else { panic!() };
1094
+ assert_eq!((e.range.start.character, e.range.end.character), (9, 11));
1095
+ // Not after a plain name, an `@` in a string or in a comment.
1096
+ assert_eq!(completion::builtinPrefixStart(" x := sa", 9), None);
1097
+ assert_eq!(completion::builtinPrefixStart(" s := \"mail@ex", 15), None);
1098
+ assert_eq!(completion::builtinPrefixStart(" # see @sa", 11), None);
1099
+ }
1100
+
1059
1101
  #[test]
1060
1102
  fn completionColumnsAreConvertedFromUtf16() {
1061
1103
  let line = " s := \"é😀\".le";
docs/tools.md CHANGED
@@ -64,7 +64,7 @@ A program that crashes prints one line, such as `crash: division by zero in div
64
64
 
65
65
  ## Editors
66
66
 
67
- `plum editor helix` and `plum editor vscode` install syntax highlighting and the language server, which gives you errors as you type and completion. Everything is built into the `plum` binary, so they work from any folder. Reload your editor afterwards.
67
+ `plum editor helix` and `plum editor vscode` install syntax highlighting and the language server, which gives you errors as you type and completion (names, members after `.`, and the built-ins after `@`). Everything is built into the `plum` binary, so they work from any folder. Reload your editor afterwards.
68
68
 
69
69
  ## Building from source
70
70