Are there any tools in IDEs to automatically fix comment formatting? - visual-studio

/* Suppose I have a multi-line comment with hard line-breaks
* that are roughly uniform on the right side of the text,
* and I want to add text to a line in order to make the
* comment a bit more descriptive.
*/
Now, most unfortunately, I need to add text to one of the top lines.
/* Suppose I have a multi-line comment with hard line-breaks (here is some added text for happy fun time)
* that are roughly uniform on the right side of the text,
* and I want to add text to a line in order to make the
* comment a bit more descriptive.
*/
It takes O(n) time (n being the number of lines) to fix each line so that they roughly line up again. The computer should do this, not me.
Are there tools to deal with this in our IDEs? What are they called?

emacs supports the command fill-paragraph which is typically mapped to meta-q.
Output from fill-paragraph on your second paragraph of text:
/* Suppose I have a multi-line comment with hard line-breaks (here is
* some added text for happy fun time) that are roughly uniform on the
* right side of the text, and I want to add text to a line in order
* to make the comment a bit more descriptive.
*/

Eclipse has this built in (at least, I think it's what you want). When you type a comment, you then type Ctrl+Shift+F and it will format either all your code, or just the section of code that you have highlighted.
I just tested it now and it aligned all my comments for me.

Related

Detect cursor is over comment or string in Scintilla NET

Is there any built in function in Scintilla.NET to detect the cursor is over a comment or string? I'd want to avoid the autocompletion to work when the user is typing comments or strings.
I'm aware I can scan the whole text backwards, searching for //, /* */ and pairs of " " but I'm almost sure there must be a built-in function to do that.
Thanks!
If you're using a lexer, you can get the style number at the current caret postion and check to see if it corresponds with a string or comment. The Scintilla API for retrieving the style number is:
SCI_GETSTYLEAT(int pos)
The Scintilla.NET documentation states there are already some convenience APIs for detecting comments:
ScintillaNET.Scintilla.PositionIsOnComment(System.Int32)
ScintillaNET.Scintilla.PositionIsOnComment(System.Int32,ScintillaNET.Lexer)
But there does not seem to be anything equivalent for strings - so it looks like you'll have to roll your own by using the above Scintilla message with one of the ScintillaNET.Scintilla.SendMessageDirect() methods.

Multilined commenting in vim using /** ... */

Note: When I'm describing /** ... */ comment blocks, I'm referring to the following types of comment blocks
/**
* This is a comment block that
* spans multiple lines.
*
* Each line is prefixed with a "\t* " string
*/
I'm used to the very automated commenting of comment blocks in IDEs, but it doesn't seem that vim supports these kinds of comments too well.
Specifically, I'm looking for this kind of automated functionality. Say you have the following comment
/**
* This is a comment and I plan to run to the next line.
In vim's insert mode, when my cursor is at the period and I hit "enter", I want another "\t* " string to be placed on the new line.
Furthermore, when I hit "/" to end the comment block on a brand new line starting with "\t* ", it would delete the space and place "/" on this new line.
For example, if I have
/**
* This is a comment and I plan to run to the next line.
*
(with a space after the '*' character), and I hit "/", I should produce
/**
* This is a comment and I plan to run to the next line.
*/
Any plug in or vimscript that would help me support this?
Note that I've looked at the nerd commenter already, but the comments created by the nerd commenter don't seem automated (ie: I have to press a keystroke or two to activate commenting.)
Also, whenever I make block comments, I always start with "/**".
Vim can do this by itself. Just add
filetype plugin indent on
to your vimrc.
After this is done the defualt ftplugins should enable it for you. If it doesn't I believe adding to ~/.vim/after/ftplugin/{filetype}.vim
setlocal formatoptions+=r
should enable it.

Is there a way to delete all comments in a file using Notepad++?

Notepad++ obviously recognizes all comments as such. Is there a way to simply delete all?
Edit: Stat-R's bookmark method has helped greatly, not only for removing comments but for conditionally removing lines in general.
For a general file, first of all you need to know the comment operator of the language you are writing the file in. For example, in java script the comment operator is //.
For the following code...
In NP++, you need to
Mark the lines that contains '//'. Make sure the bookmark option is enabled.
Then, choose from NP++ menu Search>Bookmark>Remove Bookmarked lines
EDIT:
Another solution after #Chris Mirno 's suggestion is as follows:
Use regular expression. See the image below. It is self explanatory
To understand it better, refer to these
In the Find & Replace Dialog, put the following regex and adjust the search options as depicted.
/\*.*?\*/
Replace with: (empty)
Select Mode: Regular Expression AND .(dot) matches newline
This should remove all your C style comments spanned across lines.
Star-R and Chris Mirno Answer are also Correct and Good.
But For Line Comment:
//.*?(?=\r?$)
Explanation:
// will be the Starting Position
.*? Will be any character
(?=\r?$) will search to the end of the line (as it is required in line comment)
Note:
But Still check each of the line because for example if your code contains soap format like
//www.w3.org/2001/XMLSchema-instance\x2......");
it will capture this line because the starting is // and it goes to end of the line so watch out for this :)
Warning to all using Stat-R's solution:
This method will remove lines of code if formatted like this:
echo "hello"; //This comment will be detected
Following his method, the entire line will be removed.
Therefore make sure to go through and make these comments, their own line before doing this method.
I have had some luck running a macro for the above. Basically:
search for // (F3)
select to end of line (shift+end)
delete (delete)
Put // into the search dialog by just searching for it once. Then record the three steps in a macro, then play it back until EOF.
The first time I did it I had a problem, but then it worked, not sure what I did differently.
Anton Largiader's answer was the most reliable one, including complex inline comments.
However, it will leave many empty lines, including ones with empty characters (space, tabs...) so I would just add another step to make it almost perfect:
After running the macro, just do:
Edit > Line Operations > Remove Empty Lines
OR
Edit > Line Operations > Remove Empty Lines (Containing Blank Characters)
1st option is good if you wish to remove only really empty lines
2nd options will remove every empty line even containing space etc. so there will be no more actual spacing left between code blocks. 1st option might be the safest with some manual cleanup afterwards.
As someone suggested in another post, the simplest and most reliable is maybe to export the all text in .RTF format using Menu Plugin-->NppExport-->Export to RTF and then:
-Open the newly created file in Word
-Select any part of any comment
-On the top-right side of Word clic Select--> Select all texts with similar formatting
-Remove the selected comments all at once (del or cut if doesn't work)
To remove Powershell comments if someone find it handy:
Removing Comment in a Powershell using Notepad ++
To find just lines beginning with # (and not with # elsewhere in the line).
Notepad++ SEARCH Menu > Find
‘Mark‘ Tab – fill in as below.
Select ‘Mark All’ (clear all marks if used previously).
Regex ^[#}
enter image description here
SEARCH Menu > bookmark > Remove (or do anything on the list with
them)
Clear all marks to reset
You can select no comments just code by doing the following:
Regex ^[^#}
enter image description here
Enter ctrl+shift+K to remove comment

Do these sort of commenting lines have a name?

//
// These are my comments at the beginning...
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- <--- These?
SETUP;
DRAW;
END;
I did some searching but I found myself not knowing what to call them.
If such a line must be given a name, call it an ASCII Art divider.
Such lines help guide the eye when flipping through a large printout, but only take up screen space in a modern editor, where syntax colouring is more effective.
(The multi-line comment as a whole is called a block comment, with or without such dividers.)

How do you do block comments in YAML?

This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
How do I comment a block of lines in YAML?
YAML supports inline comments, but does not support block comments.
From Wikipedia:
Comments begin with the number sign ( # ), can start anywhere on a line, and continue until the end of the line
A comparison with JSON, also from Wikipedia:
The syntax differences are subtle and seldom arise in practice: JSON allows extended charactersets like UTF-32, YAML requires a space after separators like comma, equals, and colon while JSON does not, and some non-standard implementations of JSON extend the grammar to include Javascript's /* ... */ comments. Handling such edge cases may require light pre-processing of the JSON before parsing as in-line YAML.
# If you want to write
# a block-commented Haiku
# you'll need three pound signs
The specification only describes one way of marking comments:
An explicit comment is marked by a “#” indicator.
That's all. There aren't any block comments.
I am not trying to be smart about it, but if you use Sublime Text for your editor, the steps are:
Select the block
Cmd + / on Mac or Ctrl + / on Linux and Windows
Profit
I'd imagine that other editors have similar functionality too. Which one are you using? I'd be happy to do some digging.
In Vim you can do one of the following:
Comment all lines: :%s/^/#
Comment lines 10 - 15: :10,15s/^/#
Comment line 10 to current line: :10,.s/^/#
Comment line 10 to end: :10,$s/^/#
or using visual block:
Select a multiple-line column after entering visual block via Ctrl+v.
Press r followed by # to comment out the multiple-line block replacing the selection, or Shift+i#Esc to insert comment characters before the selection.
An alternative approach:
If
your YAML structure has well defined fields to be used by your app
AND you may freely add additional fields that won't mess up with your app
then
at any level you may add a new block text field named like "Description" or "Comment" or "Notes" or whatever
Example:
Instead of
# This comment
# is too long
use
Description: >
This comment
is too long
or
Comment: >
This comment is also too long
and newlines survive from parsing!
More advantages:
If the comments become large and complex and have a repeating pattern, you may promote them from plain text blocks to objects
Your app may -in the future- read or update those comments
One way to block commenting in YAML is by using a text editor like Notepad++ to add a # (comment) tag to multiple lines at once.
In Notepad++ you can do that using the "Block Comment" right-click option for selected text.
Emacs has comment-dwim (Do What I Mean) - just select the block and do a:
M-;
It's a toggle - use it to comment AND uncomment blocks.
If you don't have yaml-mode installed you will need to tell Emacs to use the hash character (#).
If you are using Eclipse with the YEdit plugin (an editor for .yaml files), you can comment-out multiple lines by:
selecting lines to be commented, and then
Ctrl + Shift + C
And to uncomment, follow the same steps.
For RubyMine users on Windows:
Open the file in the editor.
Select the block and press:
Ctrl + /,
And you will have the selected block starting with #.
Now if you want to uncomment the commented block, press the same key combination Ctrl + forward slash again.
In the Azure DevOps browser (pipeline YAML editor),
Ctrl + K + C Comment Block
Ctrl + K + U Uncomment Block
There also a 'Toggle Block Comment' option, but this did not work for me.
There are other 'weird' ways too: Right-click to see 'Command Palette' or F1
Then choose a cursor option.
Now it is just a matter of #.
Or even smarter [Ctrl + K] + [Ctrl + C]
In a .gitlab-ci.yml file, the following works:
To comment out a block (multiline): Select the whole block section >
Ctrl K C
To uncomment already commented out block (multiline): Select the
whole block section > Ctrl K U

Resources