Sublime Forum

Asterisks in large comment blocks


Is there a snippet or package available for automatically inserting an asterisk and indentation when commenting code?

I am talking this kind:

*Function add_user()

[can’t provide a better example for * before a word results in a bullet item]

Editors such as Kwrite, Kate and VS Code automatically add an asterisk and indentation each time the Enter key is struck. Sublime only indents to the level of the last asterisk.



The plugin linked below (written for me by @kingkeith) contains a continue_comment_on_next_line command that can do this; I use it myself for doing this very thing. DocBlokr can also do something like this, but is more heavyweight and tended to misbehave and mangle my files sometimes, so I shelved it in favor of this plugin.

For anything that’s a line comment it will automatically continue the comment into the next line when you hit enter, while for a block comment you need to provide an extra argument that tells it what to insert, which gives you full control of how things are indented.

There are example keybindings at the bottom of the plugin that show binding it to the Enter key for both regular comments as well as asterisk block comments.

The plugin as defined also has an additional command that allows for reformatting of comments (reflowing them) using an enhanced wrap_text function, and includes an event listener that uses that command for wrapping instead of the internal one as well.



Thanks for the quick response. I manually installed these plugins in Sublime but can’t access the plugin I want most of all. :frowning:



If you want to insert docblock, DoxyDoxygen, which I am currently using, may worth a try as well.



Did you also include a key binding? One of the things that plugin does is automaticaly replace the command for wrapping text with a newer one, but the one for continuing comments onto the next line needs to be manually bound to a key to be triggered.



I have a trick for this that might suit though it’s language specific. Basically when enter is hit, it’ll check the context and the preceding character. If you are currently in a comment scope and the preceding character is a space, then it’ll insert a newline and the starting character of the comment. Combined with Sublime’s auto indentation, it ought to line up with the previous comment line (at least it does for me).

If you do a lot of language switching though, this might not be the right trick to use. (Beware the trailing comma – this was one of several binds in the list.)

	// This keybind makes it so you can do continuous commenting if you end
	// have already started a comment line, and the preceding character is a
	// space.
		"keys": ["enter"],
		"command": "insert",
		"args": {"characters": "\n-- "},
		"context": [
			{"key": "selector", "operand": "comment.line.vhdl"},
			{"key": "preceding_text", "operator": "regex_contains", "operand": " $", "match_all": true}