Archive | Writing Code RSS feed for this section

Customizing Structural Highlighting in Delphi

It’s possible that Structural Highlighting is Castalia’s most popular feature. It’s certainly one of the first things that you notice when you try Castalia for the first time, and one of the things that people tell me over and over again they love.

Of course, code formatting can be quite a touchy subject, and by extension, the “best” way to highlight that formatting has been the subject of more than a few debates over the years. Fortunately, the advanced syntax highlighting that Castalia adds to the Delphi IDE is versatile and easy to customize.

To customize Structural Highlighting in your Delphi editor, open the Castalia menu and choose “Castalia Options….” Drill down the options tree through Castalia Options | Editor | Smart Highlighting | Structural Highlighting, and you’ll find the structural highlighting options pane.

The first checkbox, “Highlight Code Structure,” turns structural highlighting on and off. If, for some reason, you don’t want to see the structural highlighting lines, this is where to turn them off and on.

Next is the checkbox for “Mark Hanging Blocks.” This controls what happens if the beginning and end of a structural statement aren’t aligned vertically in the code editor. You can see this with the example given in the options window.

Notice that there are two begin/end pairs in the example. The outer one, marked in orange, has the begin/end aligned vertically, but the inner one, marked in purple, does not. If “Mark Hanging Blocks” is checked, the structural highlighting lines will always be drawn to align vertically with the “end” keyword.

Here’s what it looks like if “Mark Hanging Blocks” is turned off.

In this case, Castalia draws the structural lines more literally, exactly marking the “begin” and “end,” and only drawing the connecting line if they are vertically aligned (otherwise you would have diagonal lines running through your code, making it harder to read, not easier).

So, if you’re one who aligns your begin/end pairs vertically, this option doesn’t really matter for you – Castalia will behave the same way whether it’s on or off. On the other hand, if you’re one who puts “begin” on the same line as your conditional statement (which is very common with curly-brace languages like C and Java), you’ll want to decide how you prefer to see the structure. Personally, I think having hanging blocks ON is better, but that’s just my opinion.

The final set of options is the color buttons. Castalia’s Structural Highlighting uses four colors (if your nested structure is more than four levels deep, the colors will be reused again in the same order – it’s a simple Color = StructuralDepth mod 4).

I think the default colors are nice, but not everybody likes them, especially people who don’t use a white background as their code editor. You can customize the colors by clicking the color button. You’ll get a standard Delphi color picker dialog, and you can customize your four colors to your liking.

Tomorrow, I’ll write about customizing Castalia’s Parenthesis Matching for the Delphi IDE, and why it works differently (and is better) than the parenthesis matching built in to the Delphi editor. Then, later this week, I’ll write about Flow Control Highlighting.

For now, have fun customizing your Structural Highlighting. If you don’t already have Castalia installed, come download a free trial.

Read full story · Comments { 2 }

Delphi Style Guide

About once a year or so, I seem to get involved in some discussion of whether there’s a “correct” way to format Delphi code. For me, whenever possible, questions about code formatting always end with the Object Pascal Style Guide originally written by Charlie Calvert and published by Embarcadero.

Castalia‘s internal code formatting routines adhere to the standards in the Object Pascal Style Guide, and I consider it to be an authoritative voice on the “correct” way to format most common Delphi code structures.

Read full story · Comments { 2 }

Castalia’s Inline Variable Declaration

One of the biggest complaints I hear about Delphi syntax is the need to declare variables at the top of a function before they can be used. Other languages, like C++ or java, let you declare a variable at any point in your code. I have mixed feelings about this.

On the one hand, I believe that Delphi’s approach (require all variables to be declared in a VAR section at the beginning of a function) is stronger and more readable, and that it gives you a quick at-a-glance feeling for the data being used in your function. It also contributes to Delphi’s lightning-fast compile times, for reasons that are beyond the scope of this post.

On the other hand, it can be an annoyance to have to jump back into the VAR section of the Delphi function every time you need another variable. It breaks up your mental “flow” and, in particularly large and complex functions, can cost time and effort in getting back to where you left off.

This is the reason I created Inline Variable Declaration in Castalia for Delphi.

Inline Variable Declaration gives you the best of both worlds. It lets you write a VAR declaration anywhere in your function, and then automatically moves that VAR declaration into the variable declaration section of your function without breaking your flow or losing your place.

Inline Variable Declaration isn’t a very discoverable feature the Castalia adds to Delphi – you have to know it’s there, but once you do, it can save you a little time and a lot of mental effort.

For more information on Inline Variable Declaration in Castalia for Delphi, see

Read full story · Comments { 0 }

Castalia Code Templates – A Primer

Castalia’s code template system for the Delphi IDE is actually a scripting system. Borrowing from web programming systems like PHP or ASP, Castalia’s templates are a combination of text to be inserted directly into the editor and of scripting code, contained between {% and %} markers.

The scripting language is Delphi-based, so there’s no syntax to learn as you write really powerful code templates. Here’s an example of what the ifb template looks like (ifb inserts an if statement with a begin..end block):

    if {% InsertCursor(0) %} then
    {%= Indent %}{% InsertCursor (1) %}

Everything not between {% %} markers is inserted directly into the Delphi code editor. Code between the {% %} markers is executed. This example shows the InsertCursor(AOrder: Integer) procedure, which drops a stack bookmark at that point in the text. The bookmark with AOrder = 0 will be the location of the cursor after the template is done executing. The bookmarks are then picked up in the order indicated by the AOrder parameter.

Also notice the {%= marker, which means “insert the result of this code into the editor.” Indent returns a string of spaces matching your indent size.

Here’s an example of a template to insert the current method name as a string:

    {% if ClassName <> ” then
    %}{%= ClassName %}.{% end %}{%= ProcedureName %}

Notice the Delphi code right in the template to check whether there is a current class and insert the “ClassName.” before the method name.

Castalia comes with quite a few built-in templates, but the real power is in writing your own.

Castalia Delphi code template editor

Want to give it a try? Download Castalia today and see what you can do with Castalia’s powerful code templates for the Delphi IDE.

Read full story · Comments { 1 }

For programmers, by a programmer

Hi. My name is Jacob, and I'm the creator of Castalia.

I starting programming in 1986, learning Lightspeed Pascal on a Mac Classic. Today, I'm a professional programmer, teacher, and entrepreneur.

I have a Master's Degree in Computer Science, and I still love Pascal and Delphi.

I believe that writing code is the heart and soul of software development, and I love helping programmers write code more effectively.