A simple VSCode extension that allows navigating marked comments as if they were code symbols, similar to Xcode's // MARK:
syntax.
This simple extension allows you to use line comments prefixed with a specific mark (e.g. // MARK: My symbol name
) to turn that comment into a navigable symbol, which, as illustrated by Figure 1 and Figure 2, will then appear in the breadcrumbs section, in the document outline, as well as in the command pallette (when typing @
).
The extension also contributes the following (also shown in Figure 3) three simple commands:
Insert Mark
: Will insert a symbol comment a the cursor's current position, with a placeholder for the symbol name. NOTE: Obviously, one could easily do away with this command an simply write// MARK: Symbol name
manually, but the real advantage of the command over typing is that the former can be bound to special keys (i.e., keyboard shortcut).Go To Previous Mark
: Will move the cursor to the previous symbol comment, relative to the cursor's current position.Go To Next Mark
: Will move the cursor to the next symbol comment, relative to the cursor's current position.
The Go To Previous Mark
and Go To Next Mark
commands listed above can also be triggered through "code actions" (see Figure 4). There is also a code action named Edit Mark Label
, which will conveniently allow the user to edit the symbol name using an input box (not that directly modifying the comment inline is a difficult task, however).
Technically, this extension can be used with any languages. The extension does come with support for a few popular languages (see the list below), but it is very easy to add support for a new language using the extension's settings interface.
- TypeScript
- C
- C++
- C#
- Java
- Go
- JavaScript
- TypeScript
- PHP
- Python
- Rust
- Shell scripts
- HTML
- XML
- CSS
Support for a new language can be added by modifying either the user or the workspace settings, using the symbolCommentNavigator.languages.custom
property, which latter corresponds to an array of items with the following properties: languageId: string
, lineComment: string
, and closing?: string
. Note that the closing
property is optional and should only be provided for languages that don't support "line comments" (e.g., HTML). Figure 5 and Figure 6 illustrate how to add support for both Swift and HTML (even though support for HTML is already provided by the extension). It should be noted that a custom language (i.e., a user-provided language) will override a native language when both use the same language ID.
This extension contributes the following settings:
symbolCommentNavigator.mark
: Themark
, which defaults toMARK
, used to determine if the line comment is to be treated as a symbol (which allows for comment navigation). For instance,MARK
will be used as// MARK: [symbol name here]
to navigate JavaScript code comments.symbolCommentNavigator.decorationsEnabled
: Whether the marked comment should be decorated (i.e., styled) for easier visual identification in the document. The default istrue
.symbolCommentNavigator.symbolLineNumbersEnabled
: Whether the line number should be included in the symbol's name. The default isfalse
.symbolCommentNavigator.languages.disabled
: A list of language IDs for which to disable symbol comments.symbolCommentNavigator.languages.custom
: A list of custom languages. Each item contains information about the language's ID, the language's line comment syntax, and, optionally, for a language without line comments, the language's closing comment syntax. See Figure 6 for an example.
- This extension uses the start up activation event (i.e.,
"activationEvents": ["*"]
; see package.json) for its activation. This means that, when enabled, the extension will be activated right away for a workspace. Using language-specific activation events here is not sufficient, because we don't know about all languages in advance (i.e., the extension comes with a handful of supported languages, but most languages will be added by the user). That being said, users concerned by that could disable the extension globally, and only enable it on a "per workspace basis", where it is needed. - This application implements the DocumentSymbolProvider interface in order to provide support for symbol comments navigation. The SymbolInformation objects it generates need to specify a SymbolKind. Since we are dealing with comments, which technically aren't code, the most appropriate type I was able to find in that enumeration was
Null
. - DISCLAIMER: This is the first version of this extension, and it still needs testing. It might contain code sections that are not optimal. So please let me know if you find anything wrong that needs fixing.
If you have any questions, if you find bugs, or if you have suggestions for this project, please feel free to contact me by opening an issue on the repository.
This project is released under an MIT License with Commons Clause.
Copyright (c) 2024 BB-301 (fw3dg3@gmail.com)
Initial release