comments in C++ in Sublime Text 2 - c++

I'm trying to write some C++ in Sublime Text 2. If I begin a line with a double forward slash (//) the text in that line grays out as if it were commented out, but it causes a build error when I compile, so clearly it isn't. If I begin the line with a pound sign (#) that line is commented out but doesn't change in appearance. I want to be able to tell what lines are comments and what lines are actually part of my program. How is this done?

In c++ comments look like this
// one line comment or
/* comment
over multiple
lines */
If your compiler is not recognizing these, chances are, it's not compiling c++. This seems even more likely seeing how lines beginning with # will be ignored like you'd expected for some other languages (for example python)
Make sure to check what the "build" button in your IDE actually calls/does.

You can try this:
/*
I am a comment!
I am another comment!
*/
I hope this helps.

Related

FORTRAN 77 comments starting at column 7

If I understand correctly, any line that starts with C in column 1 is automatically a comment. So why do some code samples I see start the comment text at column 7? Is this just to make all text line up a little nicer, or was it required by some compilers?
Where the text of the comments start is completely irrelevant to the compiler, the compiler just ignores the line or the punch card.
It is just up to the programmer to format the comments based on the reasons that made him/her to create such comment. Often, programmers will want the comments to be somehow aligned, but it is not necessary.
For example, if a line of code that contained a numeric label is commented out, it will often start earlier than column 7.

How to set up clang-format comment pragmas so multiline doxygen comments don't get touched?

I am trying to introduce clang-format to a couple of our projects at work (C and C++), but I am having trouble getting it to format multi-line Doxygen comments the way I want.
All comments have the same format:
/*! #brief Some text
*
* Some more text
*
* #verbatim
*
* A very long line of text that exceeds the clang-format column width but should not be touched
*
* #endverbatim
*/
I want clang-format to leave the verbatim blocks alone and not reflow them. I am using clang-format-6.0
Turning ReflowComments off is not an option as non-doxygen comments must be taken care of by clang-format
I have tried various regular expressions in the CommentPragmas config item but to no avail:
#verbatim(.*\n)*.*#endverbatim to treat the entire verbatim block as a comment pragma. This is the ideal situation, as any other part of the Doxygen comment I do not mind being broken into multiple lines.
#brief(.*\n)+ to match the entire comment block as the pragma. I've also tried this with an arbitrary token at the end of the comment to act as an explicit end-of-block marker. This isn't ideal as it doesn't force the non-verbatim part of the comment to conform, but is a compromise I'm willing to live with if I have to.
Various other regexes I've seen in other discussions, adapted to fit our Doxygen markup.
All I've managed to get it to do so far is to leave the first line of the multi-line comment alone, if it happens to exceed the column limit. However, any following long line is still broken up.
The only other tool I have left in my box is to use // clang-format off and // clang-format on around these comments but again I'd like to avoid it if I can because:
a) it'll be quite tedious to add them throughout the code base
b) I'll have to surround the entire comments with these, rather than just the verbatim blocks (I haven't figured out if you can disable it just for a portion of a multi-line comment - I've only managed to get it working for an entire one, and even if that was possible the clang-format directives would end up in the generated Doxygen docs which is unacceptable)
c) I don't really like the way it looks in code.
Any help is appreciated. Thanks.
Ran into this issue also, and the only work around found was to use clang-format on/off.
clang-format re flowing comments tends to:
break #page, #section, etc titles, and links generated from them (in rare cases).
break #startuml blocks, which have a specific syntax.
break #verbatim blocks.
See an example of usage in MySQL:
https://github.com/mysql/mysql-server/blob/8.0/storage/perfschema/pfs.cc
Update:
Filed a feature request on clang-format itself:
https://bugs.llvm.org/show_bug.cgi?id=44486

How to do block comment in fortran?

I have seen /* block comment */ for block commenting in C++. I know I can do line commenting by using ! or c, but is there any option for block commenting in Fortran?
No, the strange concept of block comments is alien to Fortran. Your editor or development environment might provide a way to comment a block of lines in one go.
You can do a little hack though:
go to 100
! CHUNK OF CODE YOU WANT TO COMMENT OUT
100 continue
Yeah, I know it's horrible but it works. :)
If your FORTRAN compiler supports preprocessor macros then a popular method is to use (What exactly does an #if 0 ..... #endif block do?)
#if 0
...
Your comments ...
go here ...
...
#endif
A line with a c, C, *, d, D, or ! in column one is a comment line; except that if the -xld option is set, then the lines starting with D or d are compiled as debug lines. The d, D, and ! are nonstandard.
If you put an exclamation mark (!) in any column of the statement field, except within character literals, then everything after the ! on that line is a comment.
It is much simpler to use a text editor which allows for multiline commenting using "SHIFT + /"!
Do the following:
Go to the first line among all the multiple block lines to comment and place the typing position to the first column,
Press Alt+Shift in the keyboard,
Select all the lines to comment,
Unpress Alt+Shift from the keyboard
Type C/c
It will work then . Check that out and tell me in the comments. Cheers!
In Sublime text editor it can be used Toggle Comment (Ctrl+7) or Toggle Block Comment.

Easily comment (C++) code in vim

I have looked at the following question:
How to comment out a block of Python code in Vim
But that does not seem to work for me. How do I comment code easily without resorting to plugins/scripts?
Use ctrl-V to do a block selection and then hit I followed by //[ESC].
Alternatively, use shift-V to do a line-based select and then type :s:^://[Enter]. The latter part could easily go into a mapping. eg:
:vmap // :s:^://<CR>
Then you just shift-V, select the range, and type // (or whatever you bind it to).
You can add this to your .vimrc file
map <C-c> :s/^/\/\//<Enter>
Then when you need to comment a section just select all lines (Shift-V + movement) and then press CtrlC.
To un-comment you can define in a similar way
map <C-u> :s/^\/\///<Enter>
that removes a // at begin of line from the selected range when pressing CtrlU.
You can use the NERD commenter plugin for vim, which has support for a whole bunch of languages (I'm sure C++ is one of them). With this installed, to comment/uncomment any line, use <Leader>ci. To do the same for a block of text, select text by entering the visual mode and use the same command as above.
There are other features in this such as comment n lines by supplying a count before the command, yank before comment with <Leader>cy, comment to end of line with <Leader>c$, and many others, which you can read about in the link. I've found this plugin to be extremely useful and is one of my 'must have' plugins.
There's always #ifdef CHECK_THIS_LATER ... #endif which has the advantage of not causing problems with nested C-style comments (if you use them) and is easy to find and either uncomment or remove completely later.

Vim different textwidth for multiline C comments?

In our C++ code base we keep 99 column lines but 79-some-odd column multiline comments. Is there a good strategy to do this automagically? I assume the modes are already known because of smart comment line-joining and leading * insertion.
Apparently both code and comments use the same textwidth option. As far as I can see, the only trick is to set this option dynamically:
:autocmd CursorMoved,CursorMovedI * :if match(getline(.), '^\s*\*') == 0 | :setlocal textwidth=79 | :else | :setlocal textwidth=99 | :endif
Here the critical part is detecting when we are in a comment. If you only format comments this way:
/*
* my comment
*/
my regex should work... unless you have lines in the code starting with * (which I guess can happen in C, less frequently in C++). If you use comments like this:
// comment line 1
// comment line 2
the regex is even simpler to write. If you want to cover all possible situations, including corner cases, well... I guess the best thing would be to define a separate detection function and call that from the :autocmd instead of match().
I came across this same problem and think that I have found a suitable solution.
What I wanted my comments to word wrap so that when I'm typing I don't have to worry about formating text. This works well with comment text. But I wasn't comfortable with having vim format my code. So I wanted vim to highlight every thing in red after x column.
To do this with only cpp code you would add the following to your ~/.vim/ftdetect/cpp.vim file.
set textwidth=79
match ErrorMsg '\%>99v.\+'
note: You may have to create the file and folders if they don't exist.
If you have problems with this make sure that you have formatoptions set to:
formatoptions=croql
You can see this by running :set formatoptions inside of vim.