Writing Code in TwinBasic: Part 1

To learn more about twinBASIC, join me at this year's virtual Access DevCon where I will be presenting this exciting new project from vbWatchdog creator, Wayne Phillips.

A great development environment makes the code-writing experience enjoyable.  In yesterday's article, I wrote about some of the great features of Visual Studio Code itself.  In this article, I want to dive into how TwinBasic leverages those features.

Writing Code - The Language Server Protocol

Let's start by focusing on the writing of the code itself.  We'll discuss the debugging aspects in a separate article.  As I wrote yesterday, the Language Server Protocol (LSP) standardizes the interfaces between programming languages and code-writing tools.  

This approach allows for language and tool implementations to be developed independently of each other.  In theory, any language that implements the protocol will work with any development tool that implements the other side of the protocol.

IntelliSense

One of the first things you notice when working with TwinBasic is the massive improvement in IntelliSense compared to VBA.  

To begin with, the autocomplete dropdown appears as soon as you start typing in TwinBasic.  In VBA, you need to press [Ctrl] + [Spacebar] to force it to appear while you are still typing.

Let's do a quick side-by-side comparison.  Here is the trusty VBA autocomplete that appears when I type the characters Ms and then press [Ctrl] + [Spacebar]:

Only public methods and constants that begin with "Ms" are shown

Notice how the only matches that appear are those public methods and constants that begin with the letters "ms".  

Now, here's what I get when I type "ms" in TwinBasic (no need for the [Ctrl] + [Spacebar] shortcut):

Public methods and constants match if "ms" appears anywhere in their names.

The first thing you probably notice is that the full declaration of the MsgBox method appears on the right side of the dropdown.  Nice!

The other thing I want to point out is the expanded pattern matching on the autocomplete list.  There are several results beyond those that start with "ms."  The relevance priority list appears to go something like this:

  • Matches that start with "ms" (e.g., MsgBox)
  • Camel-case matches where the first "word" begins with "M" and the second "word" begins with "S" (e.g., MacScript)
  • Matches that contain "ms" (e.g., vbMsgBoxHelpButton)
  • Camel-case matches where one word begins with "M" and the letter "s" appears later (e.g., IsMissing)

The one oddity is the trailing underscore in the MsgBox_ procedure name.  Wayne listed this as a bug fix in an earlier release he sent me, but this latest version seems to have reintroduced it.  Or maybe VS Code cached the old version of the TwinBasic extension and the problem is on my end.  Either way, I expect this will be cleaned up by the time you get your hands on it.

Go to definition

Just like the View > Definition feature in VBA (Shift + F2), TwinBasic has an analogous Go to Definition feature.  Taking advantage of the VS Code capabilities, TwinBasic steps it up a notch by also offering a Peek Definition option:

Sometimes all you need is a quick peek.

When you use the "Peek Definition" feature, the method being called is displayed in place without jumping you out of the code you're currently editing.  What's more, the "peeked" code appears in a resizable window.  As you resize the window, it remains in between the lines of code in the calling routine.  That means none of the code you are working on gets obscured:

The "peeked code" appears between lines 8 and 9 of the calling routine.

Semantic highlighting

Semantic highlighting is a fancy way of saying the font appears in different colors depending on what sort of thing it represents.  VBA has this feature, but it's limited to three sorts of things:

  • Comments: green
  • Keywords: blue
  • Everything else: black

TwinBasic expands on the concept, providing the ability to customize to your heart's content.  Here is a side-by-side comparison of the same code in VBA vs. TwinBasic.  

Light side: VBA | Dark side: TwinBasic (no luring-you-to-the-dark-side-metaphor intended)

I am running Dark+, the default dark theme of VS Code.  Out of the box, TwinBasic currently has colors defined for the following themes:

  • Monokai Dimmed
  • Default Dark+
  • Default Light+

There are a total of about forty different sorts of things whose styling can be customized.  Honestly, I think it might be overkill.  I actually prefer the VBA approach to reducing things into only three buckets.  I think that results in a strong signal to noise ratio when glancing at my code.  

That said, I've often wished that I could add a fourth bucket to VBA's semantic highlighting so that I could use a different color for literal strings.  With TwinBasic, I would have the flexibility to customize my environment to do that.

Writing Code in TwinBasic: Part 2

I did not intend for this to be a two-part article, but after nearly two hours I'm closing in on about 1,000 words.  Check back tomorrow where I will cover the following additional code writing features:

  • Code folding
  • Inline parameter hints
  • Outline view

Image by Bruno /Germany from Pixabay