Okay, so today I’m gonna walk you through my little project: “monaco predictions”. Basically, I was messing around with the Monaco Editor, and thought, “Hey, wouldn’t it be cool to have some kind of predictive text thing happening here?”. So, I started hacking away.

First things first, I dove into the Monaco Editor API docs. Man, that thing’s a beast! But after a bit of digging, I figured out how to hook into the editor’s content changes. The key was using the onDidChangeModelContent
event. This thing fires every time the text in the editor changes. Super handy!
Next up, I needed a way to actually make predictions. I didn’t want to go full-blown AI on this, so I kept it simple. I decided to use a basic trie data structure to store a dictionary of words and their frequencies. I found a decent word list online (just a text file, nothing fancy), and wrote a script to load it into the trie. It was a bit slow at first, so I had to optimize the trie implementation a bit, mostly by being smarter about memory allocation.
Then came the fun part: tying it all together. In the onDidChangeModelContent
handler, I grabbed the current word the user was typing. I used a regex to find the word boundaries, nothing crazy. Then, I queried the trie for words that started with the prefix the user had typed. The trie returned a list of possible completions, sorted by frequency.
Now, displaying those completions in the editor was another challenge. Monaco has a “completion provider” API, which lets you hook into the editor’s intellisense system. I implemented a completion provider that would return the list of words from the trie. I messed around with the styling a bit to make the suggestions look decent.
It wasn’t perfect, though. Performance was a bit sluggish when the trie was large. Also, the predictions were just based on word frequency, so it didn’t really understand context. I thought about adding some simple n-gram modeling to improve context awareness, but decided to leave that for another day.

Finally, I packaged it all up into a little demo page with the Monaco Editor. You could type in the editor, and it would show you word suggestions. It was pretty janky, but it worked! I even added a little toggle to enable/disable the predictions, because sometimes it got annoying.
Here’s the rough outline of the steps I took:
- Initialized a Monaco Editor instance.
- Implemented a trie data structure to store words and their frequencies.
- Loaded a word list into the trie.
- Hooked into the
onDidChangeModelContent
event. - Extracted the current word being typed.
- Queried the trie for possible completions.
- Implemented a completion provider to display the suggestions in the editor.
- Added basic styling.
- Deployed as a simple demo page.
It’s nothing groundbreaking, but it was a fun little project to get my hands dirty with the Monaco Editor API. I learned a ton about how editors work under the hood, and got a better understanding of data structures like tries. Maybe I’ll revisit it someday and make it a bit smarter, but for now, it’s good enough.