PrintDebugger
A Sublime Text plugin for convenient print-debugging.
Overview
PrintDebugger is the first Sublime Text plugin I created. It lets users insert print and debug statements like print
or fmt.Println
in one keystroke, speeding up the development of programs. The design of the plugin makes it fully customizable: users can configure it to work with any programming language in the world and adjust the exact syntax of the inserted print/debug statements.
Motivation
In 2021, not long after I started learning to code and create programming projects, I noticed that the act of printing things to the console was very common when debugging programs. Yet, it was also a very manual process: I had to type out lines like System.out.println
or console.log
over and over again, along with the message or variable I wanted to output.
To automate this process, I first created snippets in Sublime Text for print statements I used a lot. pl
triggers System.out.println
, cl
triggers console.log
, etc. While this did speed things up quite a bit, I wasn’t satisfied: I still had to manually type or copy and paste the string or variable I wanted to print out.
My next step was browsing Package Control for an existing plugin that automates the whole process. After some research, I did find a plugin that lets users quickly insert print statements. However, while it does have some cool features like the ability to comment out or remove all print statements in a file, it comes with a deal-breaker for me: it only supports 4 programming languages, and the plugin’s design makes adding new languages a very cumbersome process. Furthermore, even for the supported languages, it is difficult to fully customize the syntax of the print statements that are used.
Without an existing solution, I decided to develop my own plugin from the ground up so I can design it to meet my vision. The result was PrintDebugger, an elegant and fully customizable plugin that allows users to effortlessly insert print and debug statements.
How It Works
The plugin, which was developed using Python and the Sublime Text API, provides the print_debug
command.
If the user triggers the command with no text selected and while their cursor is not on a word, the plugin is in print mode: an empty print statement is inserted and the cursor is placed in the brackets, ready for the user to start typing.
If the user triggers the command with some text selected or while their cursor is placed on a word, the plugin is in debug mode: a debug statement is inserted on a new line below the current line. Optionally, if the user enables the before_and_after
mode, then 2 debug statements are inserted: one above the focused line and one below. This variant is useful when a developer wants to see if a line actually mutates the value of a variable as intended.
Demo
In these videos, the plugin’s debug command and its before_and_after
variation are triggered via the command palette. Alternatively, users can easily add keybindings to achieve the same effect through keyboard shortcuts.
For instance, I personally configure the plugin to be triggered by Alt + D on Windows and Option + D on macOS.
Customization
From the very beginning of this project, I focused on developing the plugin to be fully customizable. I wanted users to have the ability to adapt the plugin to any programming language they want, and also adjust the syntax of the print/debug statements as needed.
Ultimately, my goal was: if you invent a brand new programming language right now, you can configure PrintDebugger to work with that language in mere seconds.
Users can define syntaxes in the settings file. Each syntax corresponds to a programming language, and is applied when the user triggers the plugin in the specified scope or in a file with one of the specified file extensions.
The JSON object below is the syntax defined for Python by default (users can override it should they wish).
{
"name": "Python",
"scope": "source.python",
"extensions": ["py"],
"debug": "print(f'{$0 = }')",
"print": "print($0)"
}
name
is a human-friendly label for users to easily locate syntaxes.
scope
is the Sublime Text scope in which the syntax should take effect. In Sublime Text, scopes are context labels automatically assigned to texts, indicating the type of content under the user’s cursor. In this case, when a user is editing a Python file, the active scope contains the source.python
selector, so the Python print/debug syntax defined above is matched and used.
extensions
is another way for syntaxes to be matched. In this case, the array specifies that the Python syntax should be used whenever the user is editing a .py
file.
debug
defines the syntax for debug statements. The $0
marker indicates where the variable should be inserted. For the syntax above, if the user selects a variable foo
, then the inserted debug statement is print(f'{foo = }')
. To adjust the syntax of a debug statement, all users need to do is edit this debug
string. For instance, modifying it to print('$0 = ' + $0)
would change the inserted debug statement to print('foo = ' + foo)
. This way, users can easily adjust the format of each debug statement based on preference.
print
is similar to debug
, but instead controls the syntax of inserted print statements. The $0
marker is where the cursor ends up after the print statement is inserted. With the syntax defined above, print()
would be the print statement inserted, and the cursor would be placed in between the parentheses, ready for the user to continue typing without interrupting their workflow.
Pull Request
PrintDebugger is significant in my software development journey because not only is it the first Sublime Text plugin I created, but it was also the first time I made an open-source contribution to someone else’s repository through a pull request.
After I finished creating the plugin, I greatly enjoyed using it in my day-to-day coding workflow. Just as I have benefited from the open-source plugins made by other Sublime Text community members, I wanted to share my plugin with the world so others can also benefit from its functionalities.
To submit PrintDebugger to the Package Control platform, I opened a pull request so my plugin could be reviewed and approved.
In this review process, I exchanged comments with veteran developers maintaining Package Control, who taught me best practices in distributing software products to users and guided me to learn more about the Sublime Text API. The feedback I received also enlightened me on how to clearly communicate product features to users. I realized that settings and instructions that make perfect sense to me as the developer may not make any sense to users who are just looking for an intuitive experience, so I changed parts of the plugin’s design accordingly. Since then, I have always tried to keep the user’s perspective in mind when developing software.
Learn More
The code for this plugin can be found here. Its Package Control listing is located here.
To learn more about my other Sublime Text plugins, visit this page.