Modify default C++ snippets in VS Code - c++

I would like to modify the default code snippets for C++ in Visual Studio Code. I've found a similar question here, but they actually show how to create your own snippets - and not how to modify the default snippets.
The reason I want to do this is because I'm used to doing some things a bit differently than how they are done in the snippets. For example, this is what appears when I use the if snippet:
if (/* condition */)
{
/* code */
}
And I would like it to be like this:
if(/* condition */){
/* code */
}
Is there any way to achieve what I want to do? Thank you.

Press Ctrl + ,(opens vs code settings);
Type "snippet" in settings search bar;
In Extentions List choose C/C++
Untick "Suggest Snippets" button;
Close settings tab.
Go to settings again and create new User Snippets 😊as you liked.enter image description here

Related

Keep open braces on the same line in on saving Visual Studio Code [duplicate]

I used to write C code with Visual Studio, and whenever I wrote "for" and then pressed TAB, it was automatically completed to an entire for loop, i.e.
for (size_t i = 0; i < length; i++)
{
}
Is there a way to enable that in VSCode as well? Even by using some extension?
Thanks!
Is there a way to enable that in VSCode as well?
Yes, you can add snippets and customize them according to your needs if the corresponding snippet is not already available, as shown below for the for loop shown in your question.
Step 1
Go to Files -> Preferences -> User Snippets
Step 2
After clicking on the User Snippets you will be prompted with a menu with different options as shown in the screen shot attached. Click on the option saying: New Global Snippets File
Step 3
When you click on New Global Snippets File, a file will open where you can add your required snippet. Since you've given the for loop that you want in C++, I will write the content that you want to place in that file:
{
"For Loop": {
"prefix": ["for", "for-const"],
"body": ["for (size_t i = ${1:0} ;i < ${2:length}; i++)", "{\t${0://add code here}", "}"],
"description": "A for loop."
}
}
Step 4
Save this file with the content shown above and then you will be able to use this snippet. For example, next time you write for you will be prompted with different options and you can press TAB for selecting that option and that snippet will be used at that point, as shown in the below screenshot :

How can i disable or hide default cancel button in QFileDialog?

I'm trying to use QFileDialog as a widget, to use QFileDialog as a widget the final step for my is to disable the cancel button.
Have you an idea of how can i disable this button.
PS : I am using Qt 5.5.0
You should be able to access the various standard buttons via the QDialogButtonBox and, from there, do what you want with the Cancel button.
The following example code appears to works as expected...
QFileDialog fd;
/*
* Find the QDialogButtonBox.
*/
if (auto *button_box = fd.findChild<QDialogButtonBox *>()) {
/*
* Now find the `Cancel' button...
*/
if (auto *cancel_button = button_box->button(QDialogButtonBox::Cancel)) {
/*
* ...and remove it (or disable it if you prefer).
*/
button_box->removeButton(cancel_button);
}
}
fd.show();
The QFileDialog Class doesn't seem to have any option for this.
However, you could make your own file browser using QTreeModel and QTreeView (It's not too difficult).
There is a tutorial on how to do just that here.
It would take a while to type out all the code (sorry, I'm a slow typer), but this tutorial should allow you to have the flexibility you need to do what you want to do.
I understand that this answer isn't exactly what you asked, but I hope it is a good alternative.
EDIT: Accidentally pasted wrong link for QFileDialog Class

How to stop Clion tab key behaviour

When on a C++ line of code like the following
aType.aMethod(
std::make_shared< T_1>();
^^^^^-- Press tab here
)
Clion tries to move to the next parameter(i guess), but being the only parameter it goes nowhere. I want to have the tab to just insert characters(tab or space that is) and not to try to cycle the cursor among the method parameter. Is there a way to stop this alternative functionality?
I searched to no avail in
Settings|Editor|CodeStyle|C/C++
Thank you
"Try changing the "Next parameter" and "Previous parameter" keybindings to something else than Tab."
– Eldar Abusalimov Jul 5 '17 at 9:02
In addition to the accepted answer, i found that tab was assigned to Next Live Template Parameter Under : Main Menu | Navigate | Navigate in File (This is in the keymap section in settings, not the actual main menu). So when i generated definitions for the methods in my class and it jumped into the cpp if there were any auto generated functions with return initializer; as the method body it would jump to these instead of letting me indent code so i turned that off too and now i can happily implement the methods in order and fix those up when i get to them. Alternatively if you like that setting and want to keep it turned on, Hopefully knowing that you have to tidy up all instances of return initializer; before the tab key will indent code again is useful to you, i found it very confusing.
EDIT: i realise this wasn't part of the original question, but this is where googles top result brought me, so i hope you don't mind me adding this info here as its still related to the tab key doing weird things in CLion.

Visual Studios 2013 indenting code

It's not necessary I fix this 'issue' since it's a more aesthetic thing. I was just wondering if it's possible to indent code that comes under the "public:" deceleration after indenting the deceleration itself.
So it would look like this:
class Myclass{
public:
void test(){
}
}
At the moment the void part would be directly underneath the public modifier. I have looked through the formatting setting and yet cannot find how to do this.
In visual studio open Tools/Options. In the menu on the left choose Text Editor->C/C++->Formatting. There you can change a lot of things like indentation.

Creating a simple VS2008 visualizer inside autoexp.dat (problem with casting)

I have a large project of mixed C/C++. I've created a simple visualizer for the ICU UnicodeString class as follows...
[inside autoexp.dat]
icu_4_2::UnicodeString {
preview ([$c.fUnion.fFields.fArray,su])
}
...and that works fine. Inside the debugger wherever I see the object I now see the text inside in the preview line.
I then created a wrapper class containing one of these objects as follows...
class StringHandleData
{
public:
icu_4_2::UnicodeString str;
};
...and then created another visualizer for this...
[inside autoexp.dat]
StringHandleData {
preview ([$c.str.fUnion.fFields.fArray,su])
}
...which again, works fine. Whenever I see a StringHandleData object in the debugger I see the text inside in the string.
However, my problem comes when I define a typedef I can use inside C code like this...
typedef void* StringHandle;
...which under the hood is actually just a ptr to a StringHandleData object. So when I try and create a visualizer for the StringHandle type like this...
[inside autoexp.dat]
StringHandle {
preview ([((StringHandleData)$c).str.fUnion.fFields.fArray,su])
}
...it doesn't work. I've tried lots of other ways of casting the object too but with no luck so far. If I go to my watch window and cast a StringHandle like this... (StringHandleData*)stringHandle then the debugger makes the cast and previews correctly - but I just can't seem to get it to do it automatically from inside autoexp.dat
Thanks for any help.
Visual studio's visualiser is blind to typedefs and will think StringHandle is a void *.