How do I change the linter/formatter in VS Code? - visual-studio-code

I'm on Ubuntu. Pushing Ctrl + Shift + i in VS Code auto formats the file.
I was editing a solidity contract and pushed Ctrl + Shift + i, VS Code didn't have a formatter configured so it asked me to pick one - I accidentally chose my JS prettifier extension instead of the solidity one. This badgered up my code by trying to use single quotes instead of double quotes (not allowed in solidity) and some other non-solidity friendly changes.
Pushing Ctrl + Shift + i now just auto formats with the wrong formatter with no option to pick a different one.
How can I change which linter/formatter is associated to which file types in VS Code?

Solution A:
Press Ctrl+Shift+P
Then type Format Document With...
At the end of the list click on Configure Default Formatter...
Now you can choose your favorite beautifier from the list.
Solution B:
go to file -> preferences -> settings search for format, on the left side, click on Text Editor, the very first item on the right side is Editor: Default Formatter from the dropdown you can pick any document formatter which you installed before.

Related

How to indent/format a selection of code in Visual Studio Code?

I want to indent a specific section of code in Visual Studio Code.
I read How do you format code in Visual Studio Code? that gives shortcuts to indent the whole code, but it doesn't work when selecting a specific section of code.
I tried Ctrl + Shift + F after selecting some line in my code, but the whole file is indented. I'm on Windows with Visual Studio Code Insider 1.8.0. How can I do it?
I want to indent a specific section of code in Visual Studio Code:
Select the lines you want to indent.
Use Ctrl + ] to indent them.
If you want to format a section (instead of indenting it):
Select the lines you want to format.
Use Ctrl + K, Ctrl + F to format them.
You can also indent a whole section (multi-lines) by selecting it and clicking
TAB
and also indent backward using Shift+TAB
And of course for auto indentation and formatting, following the language you're using, you can see which good extensions do the good job, and which formatters to install or which parameters settings to enable or set. For each language and its available tools. Just make sure to read well the documentation of the extension, to install and set all what it needs. Exemple: prettier is the most common used formatter for JavaScript and typescript. And it's widely used by all projects and code style requirements and setup. And in CI pipelines.
Up to now the indentation problem bothers me with Python when copy pasting a block of code. If that's the case, here is how you solve that: Visual Studio Code indentation for Python
On OS X, choose "Document Format", and select all lines that you need format.
Then Option + Shift + F.
(This works at least up to version 1.74.2, checked in Jan 2023)
On macOS Visual Studio Code version 1.36.1 (2019)
To auto-format the selection, use ⌘K ⌘F (the trick is that this is to be done in sequence, ⌘K first, followed by ⌘F).
To just indent (shift right) without auto-formatting, use ⌘]
As in Keyboard Shortcuts (⌘K ⌘S, or from the menu as shown below)
This should be able to set to whatever keybindings you want for indent/outdent here:
Menu File → Preferences → Keyboard Shortcuts
editor.action.indentLines
editor.action.outdentLines
F1 → open Keyboard Shortcuts → search for 'Indent Line', and change keybinding to Tab.
Right click > "Change when expression" to editorHasSelection && editorTextFocus && !editorReadonly
It will allow you to indent line when something in that line is selected (multiple lines still work).
For German keyboard layout, the standard settings are:
Indent selection: Strg + ´
Outdent selection: Strg + ß
As you've seen there are two ways to indent the code (this for Windows).
Reindenting the entire file
Shift+Alt+F
Reindenting only selected lines
First set the shortcut for Reindent Selected Lines
Menu File → Preferences → Keyboard Shortcuts → In the Search in keybindings type in Reindent Selected Lines → Select it and press Enter → Type in your own shortcut, e.g. Shift + 5, followed by Enter
Now select your code lines in the editor and use the shortcut set above, e.g. Shift + 5, to automatically indent those lines only.
On windows its "Ctrl+[" and "Ctrl+]" for indent and unindent You can find rest of the shortcuts here
For mac, you can find the shortcuts here: https://code.visualstudio.com/docs/getstarted/keybindings
For me on windows it was Ctrl+¡ , indent line. It adds a tab at the beggining of each line.
Many of the answers were not able to solve my problem too.
Just go for fn+tab
Welcome in advance.
On linux ubuntu: select text then ctrl + shift + i
This is the way I had my code before formatting...
Then I used the command like this... (Make sure to select the code part that you need to format)
Shift+ Alt+F
And I got the formatted code like this....
For me, using a mac in 2022 it was CMD + ] to indent multiple lines after selecting the desired indented lines.
Crtl + Alt + F can also formate (windows)
Windows - 2022
Shift+Alt+F

Eclipse - How to format selected code from multiple lines to a single line?

In Eclipse, if I select some code which is split on more than one line and I want to format it on one line only, how can I do that?
I want to know if it's possible to configure the js formatter inside eclipse so to be able to use just SHIFT + CMD + F.
Code split on several lines:
$('a#pop1').fancybox({
'padding': 0,
'margin-left': 30,
'width': 728,
'height': 400,
'maxWidth': 728,
'maxHeight': 400
});
and how I would like to achieve, split on a single line.
$('a#pop1').fancybox({'padding': 0,'margin-left': 30,'width': 728,'height': 400,'maxWidth': 728,'maxHeight': 400});
You need to do couple of thing to prevent code formatting.
Follow below step :
Right click on project and open properties.
Go to Preferences > JavaScript > Code Style > Formatter
Check Enable Project Specific settings.
Click on Edit button. Change Profile Name on top of dialog.
Go to New Line tab. Un-Checked option under Object initializers section.
Now go to Line Wrapping tab. increase Maximum line width parameter value.
Click on Ok than Applly than Ok.
This will now prevent your JavaScript code to format like above.
After that select your code and press Ctrl + Shift + f key combination to format your code.
Maybe this link can be helpful for you: Using the code formatter - Eclipse
Or this one, maybe, that has got some instructions: Formatting Your Code Using the Eclipse Code Formatter
try this -
Preferences > JavaScript > Code Style > Formatter
These are the steps:
Create a new profile (since you cannot edit the builted-in one), if you haven't already, and click Edit....
Open the Line Wrapping tab.
in it edit settings for your function
Click Apply, and Ok.

How to auto-format code in Eclipse?

How do you auto-format code in Eclipse?
On Windows and Linux : Ctrl + Shift + F
On Mac : ⌘ + ⇧ + F
(Alternatively you can press Format in Main Menu > Source)
Another option is to go to Window->Preferences->Java->Editor->SaveActions and check the Format source code option. Then your source code will be formatted truly automatically each time you save it.
CTRL + SHIFT + F will auto format your code (whether it is highlighted or non highlighted).
This can also be done at the Project Level:
In the Package Explorer, right-click on the project > Properties > Java Editor > Save Actions
This might be preferable when working as a team so that everyone's code is saved with the same format settings.
Notice: It did not format the document unless I corrected all mistakes. Check your file before pressing CTRL + SHIFT + F.
Press: Ctrl + A or highlight the part of the code you wish to indent
and then press Ctrl + I.
Windows -> Preferences -> Java -> Editor -> save actions -> Format source code -> Format Edited lines (or) format all lines.
Some time when you work as a team, lead don't want you to format all lines of the code in a source file (Huge track changes will be there on commit). So, select 'Format Edited lines'. This will edit and format only the lines you modified.
Gubs
You can do with the steps below
press Ctr + A (windows) or cmd + A (Mac os)
Ctr + I in windows or cmd + I in Mac os
It will auto format your code
The secret is simple: Ctrl+Shift+F
Update your IDE with the latest PDT version for better code formatting.
On the main menu click Help -> Install New Software and then add the following URL in the Work with field:
http://download.eclipse.org/tools/pdt/updates/4.0.1
When asked for the name, give it PDT4.0.1 and then move along with the update or install.
It will see if the appropriate PDT is already installed or if it is a lower version, which then would be updated.
After restarting or applying the changes go to Windows -> Preferences
on the side bar and expand PHP -> Code Style. Here you will see an item named Formatter. Select it and choose the active profile for the code formating. Thats it.
The next time you format it, it will choose a format according to the chosen active profile.
Hope it helps.
Also note that you can also "protect" a block from being formatted with #formatter:off and #formatter:on, avoiding a reformat on a comment for example, like in:
// Master dataframe
Dataset<Row> countyStateDf = df
.withColumn(
"countyState",
split(df.col("label"), ", "));
// I could split the column in one operation if I wanted:
// #formatter:off
// Dataset<Row> countyState0Df = df
// .withColumn(
// "state",
// split(df.col("label"), ", ").getItem(1))
// .withColumn(
// "county",
// split(df.col("label"), ", ").getItem(0));
// #formatter:on
countyStateDf.sample(.01).show(5, false);
You can do this with
Ctrl + I
or
Ctrl + Shift + F
right-click on the project > Properties > Java Editor > Save Actions

eclipse multiple text selection like sublime text 2

is there an option or plugin for eclipse which would enable multiple simultaneous selections in the same editor.
In sublime text, selecting some text and then pressing Ctrl+d will add next instance of the same text to the selection if possible. After selecting the instances needed the editor has multiple carrets (not necessarily on the same or adjacent columns and rows). In this mode it is possible to move all cursors forward or back simultaneously and to edit all instances of text simuntaneously.
I find this feature very usefull and miss it sorely in eclipse..
This Eclipse plugin attempts to provide this feature: https://github.com/caspark/eclipse-multicursor. From the README:
What is this?
A work-in-progress attempt to provide Sublime-Text-like
multi cursor support for text editors in the Eclipse IDE.
What works?
Multiple identical lines can be edited simultaneously using Eclipse
linked mode editing (similar to existing "rename in file"
functionality)
Next steps
"select next" functionality + associated editing using Eclipse linked
mode
"find next" + associated editing
editing of non-identical text / editing without using linked mode
split selection to lines
regexp support for find next
This feature is available in LiClipse.
See it in action (more towards the end of the video).
It supports linking with Ctrl+K, unlink with Shift+Alt+K, Ctrl+Alt+mouse double click to select words or Ctrl+Alt+Mouse to make a selection of a region (or just end lines).
Preferences>General>keys>Rename - refactoring
I changed the binding to command + shift + R when > Editing Text.
Sorry for bringing up an old question, stumbled upon it after searching google for the problem
Alt + Shift + A, then you can hold shift and use the cursor in multiple lines.
Like Ctrl+D I could not find, but like Alt+F3 in sublime (multiselects all matches), you can do by pressing Alt+Shift+R, or select text > right click > refactor > rename.
Must say that this does not work with any kind of text. It works with names of variables, functions, classes etc.
Tested on Eclipse 3.8.1
ALT + SHIFT + F worked for me.
You can see shortcuts for all here:
Goto -> Window -> Preferences -> General -> Keys and search for replace then you will see binding for Find and replace. In the bottom of that window, you can add your key to Binding text box. There you can add or edit any keys as shortcut.
If you want to replace selected word's matching words or find selected words, use below keys because you do not need to select all words in eclipse:
Ctrl+F gives me Find/Replace dialog box.
Or you can,
First Alt+A
Next Alt+F
Then press on Replace or Search button occurding to your need.

Eclipse does not format this if statement correctly

This should be easy to answer:
I have Eclipse set to wrap Java code over 80 chars to respect my margin.
This code never wraps:
if (expressionItem.type.isTypeCompatibleWith(containingNameLink.type) == false) {
reportParsingError("expression type incompatible with containing "
+ "context.");
}
Question: how do I set my code formatter preferences so that the margin is respected?
Did you press CTRL + SHIFT + F (or right click > source > format) ? Here it works fine. The code is not automatically formatted as you type, you need to tell eclipse to format it.
Click Windows > Preferences. Go to Java > Editor > Save Action Enable Perform the selected action on save then enable Formatt source code option.
Refer below pic:
Click on Formatter link to edit settings for Formatter.