I have add Prettier in my VScode but I want to format my code only when I highlight my code,
say
let a = [1, 2, 3, 4]; (line1)
let b = [ 1,2 ,3,4]; (line3)
how can I just format line 1 when I highlight line 1 only and the result should be
let a = [1, 2, 3, 4]; (line1)
let b = [ 1,2 ,3,4]; (line3)
thanks
UPDATE:
I know we can format the code in a code block. But what I want to do is
const test = (a, b, c) => { (line 1)
console.log("show a", a); (line 2)
console.log("show b", b); (line 3)
}
If I highlight b, c in line 1 and format it. It only formats the code in line 1 but not 2 and 3
futher update:
this is my vscode shortcut setting
when I highlight like this,
it becomes like that
I dont know the solution yet, but there are some info that may help.
Basically, there are something wrong with the linter. ( https://github.com/prettier/prettier-vscode/issues/137 )
And your may fix it by checking out this https://prettier.io/docs/en/integrating-with-linters.html ,
I dont know how & didnt try. cuz:: [[
looks complicated (download many things) & mess up with the project structure
may not even work
some info I have no knowledge of / incompatible with my understanding
dont know what will happen to my linters
dont know what is the next step
[]
https://github.com/prettier/prettier-vscode/issues/134
[]
No, the issue is with prettier-eslint not supporting range formatting.
...
I would suggest switching to the recommended approach of integrating ESLint and Prettier
https://github.com/prettier/prettier-vscode/issues/137
[]
let Prettier do the formatting and configure the linter to not deal with formatting rules. You can find instructions on how to configure each linter on the Prettier docs site.
...
For details refer to the Prettier documentation.
https://github.com/prettier/prettier-vscode#linter-integration
[]
Linters usually contain not only code quality rules, but also stylistic rules. Most stylistic rules are unnecessary when using Prettier, but worse – they might conflict with Prettier! Use Prettier for code formatting concerns, and linters for code-quality concerns, as outlined in Prettier vs. Linters.
Luckily it’s easy to turn off rules that conflict or are unnecessary with Prettier, by using these pre-made configs:
eslint-config-prettier
stylelint-config-prettier
https://prettier.io/docs/en/integrating-with-linters.html
[]
I would like to format my code with prettier, then apply all eslint fixes. Previously, this could be achieved by setting prettier.eslintIntegration to true. Now, the extension say that this option is [DEPRECTAED] and prettier-eslint should be used instead. However, it's not clear how to use prettier-eslint in vscode.
https://github.com/prettier/prettier-vscode/issues/999
Actually, "format only selected code" is working on my end, I didnt do any fancy extra config.
What you need to pay attention to is the "syntax tree"
-- ie: dont blindly select across the scope (the bracket {}).
#eg::
given
function test() {
let a = [1, 2, 3, 4];
let b = [ 1,2 ,3,4]; // select only this line
return false
}
if you only select::
let b = [ 1,2 ,3,4];
then press ctrl+k, ctrl+f
everything is fine
if you select across the bracket (the } for function scope)::
let b = [ 1,2 ,3,4]; // select only this line
return false
}
then press ctrl+k, ctrl+f
the whole thing in the bracket (the } for function scope) gets formatted
the "syntax tree" in a class is a bit weird.
-- ie: if you select the WHOLE function AA & format it -- codes inside another function BB will also get formatted...
you may need to apply // prettier-ignore somewhere to prevent formatting
prettier-ignore
A JavaScript comment of // prettier-ignore will exclude the next node in the abstract syntax tree from formatting.
https://prettier.io/docs/en/ignore.html
(note, it seems there is no ending tag for // prettier-ignore for Javascript (at current stage of Prettier))
for the meaning of a "syntax tree", see ex below
if you place it above the code line
seems it applies to the item (the code) (-- which is a "syntax tree node") directly below ((empty lines not counted for)) the // prettier-ignore line
-- eg-below: console.log("show a", a);, and ends there.
if you place it behind the code line (inline)
seems it applies to the inline code only
#for_your_case-do_this:
const test = (a, b, c) => {
// prettier-ignore
console.log("show a", a);
// prettier-ignore
console.log("show b", b);
}
// or
const test = (a, b, c) => {
console.log("show a", a); // prettier-ignore
console.log("show b", b); // prettier-ignore
}
Select the code you want to format and press CTRL + SHIFT + P to open the command pallette. Then type in "format" and select format selected code. Or you can select your code and press right click which should bring up a context menu where you can select the option
Select the code or leave the cursor in the row you want to format and press Ctrl + K Ctrl + F.
If I have a C-file opened and I highlight var1, the editor will automatically highlight all occurrences of var1.
int main()
{
int var1=0;
int var2 = var1 + 55;
return 0;
}
However, it will not highlight all instances of int in the same manner.
Furthermore, if I open a .s file - which is set as type Assembly Source File automatically by CDT - no occurrences will be marked.
In the following assembly file, I'd like occurrences of L97 to be marked when I highlight one occurrence for easy navigation.
...
b .L97
.L98:
ldr r0, .L99
mov r1, r4
bl printf
movs r0, #0
.L97:
pop {r2, r3, r4, pc}
.L100:
...
How do I enable mark occurrences for all text and in all file types?
In Notepad++, I opened a window, typed two lines of some random text abcd, double-clicked one of them and it automatically highlighted the other without me even setting a file-type.
Notepad++ marks the occurrences of strings, whereas Eclipse marks occurrences of variables.
In the following example, if the first var1 is selected, Eclipse marks only the occurrences of this variable, but neither identically named variables in a different scope nor the string var1 in a comment:
int main()
{
int var1=0;
int var2 = var1 + 55;
{
int var1 = 42; // var1
int var11 = 43;
}
return 0;
}
int foo()
{
int var1 = 44;
return var1;
}
To search and mark a string in one or more files, you can use file search: Search > File....
To jump to the next occurrences of a selected string press Ctrl+K.
You can't do this for any file type. Occurrence highlighting requires an editor which understands the syntax of the programming language you are editing (Java, C, C++, ....). You would need to find an Eclipse editor for the particular assembly code to get occurrence highlighting.
I'd like to adjust indentation of my source code correctly at a time after I select some block of it.
Is there any function or key with which I can do it including parenthesis?
Here is original selected block of sample code I'd like to adjust indentation.
while(1)
{
func1();
if( )
{
func2();
}
}
if( x == 0 )
{
aa = 1;
}
This would be the correctly indented code how I just want to adjust.
while(1)
{
func1();
if( )
{
func2();
}
}
if( x == 0 )
{
aa = 1;
}
Select your code and press C-M-\, which should be bound to indent-region:
C-M-\
Indent all the lines in the region, as though you had typed TAB at the beginning of each line (indent-region).
If a numeric argument is supplied, indent every line in the region to that column number.
I'm using evil mode because I like vim editing keymap.
In my case, block auto indentation can be done by equal(=) key after selecting a code block.
It's very convenient to rearrange block of code in a c-default-style.
(1) install evil package
(2) Insert this code into you emacs init file.
; indentation style for c, c++, java
(setq c-default-style "linux"
c-basic-offset 4)
(3) select block using v and direction key
(4) press '='
How do I fold or collapse sections of code in Visual Studio Code?
Is this feature supported?
Folding has been rolled out and is now implemented since Visual Studio Code version 0.10.11. There are these keyboard shortcuts available:
Fold folds the innermost uncollapsed region at the cursor:
Ctrl + Shift + [ on Windows and Linux
⌥ + ⌘ + [ on macOS
Unfold unfolds the collapsed region at the cursor:
Ctrl + Shift + ] on Windows and Linux
⌥ + ⌘ + ] on macOS
Fold All folds all regions in the editor:
Ctrl + K, Ctrl + 0 (zero) on Windows and Linux
⌘ + K, ⌘ +0 (zero) on macOS
Unfold All unfolds all regions in the editor:
Ctrl + K, Ctrl + J on Windows and Linux
⌘ + K, ⌘ + J on macOS
References: https://code.visualstudio.com/docs/getstarted/keybindings
As of Visual Studio Code version 1.12.0, April 2017, see Basic Editing > Folding section in the docs.
The default keys are:
Fold All: CTRL+K, CTRL+0 (zero)
Fold Level [n]: CTRL+K, CTRL+[n]*
Unfold All: CTRL+K, CTRL+J
Fold Region: CTRL+K, CTRL+[
Unfold Region: CTRL+K, CTRL+]
*Fold Level: to fold all but the most outer classes, try CTRL+K, CTRL+1
Macs: use ⌘ instead of CTRL (thanks Prajeet)
Also see the ability to fold any arbitrary lines of code as of Insiders v1.70. That is any lines you select can be folded!
See https://stackoverflow.com/a/72954133/836330 for the command and demo.
Create Manual Folding Range from Selection
editor.createFoldingRangeFromSelection
This is bound to the above create command: Ctrl+K Ctrl+,
Remove Manual Folding Ranges
editor.removeManualFoldingRanges
This is bound to the above remove command: Ctrl+K Ctrl+.
Code folding by regions has arrived with v1.17. Folding by regions documentation. And v1.19 and 1.23.
[Generally you can add a space, for example // region and // endregion to //region and //endregion and it will also work.]
TypeScript/JavaScript: //#region and //#endregion or // #region and // #endregion
C#: #region and #endregion
C/C++: #pragma region and #pragma endregion
F#: //#region and //#endregion
PowerShell: #region and #endregion
Python: #region and #endregion
VB: #Region and #End Region
PHP: #region and #endregion
Bat: ::#region and ::#endregion or REM #region and REM #endregion
Markdown: <!-- #region --> and <!-- #endregion -->
Golang //region and //endregion or //#region and //#endregion
Java //#region and //#endregion
CSS/SCSS/Less: /* #region */ and /* #endregion */ or /*#region*/ and /*#endregion*/
SCSS/Less: // #region and // #endregion
Go: // region, // endregion and // #region, // #endregion
shellscript: # region and # endregion
Perl5 #region and #endregion or =pod and =cut
sql --#region and --#endregion
Important: If you don't see your language in the list ::
Each language also has snippets available for the markers. Type '#' and invoke code completion to see them. To have region markers configured for your language, contact the language extension provider.
So type # and then Ctrl+Space to see the region markers for any language.
This feature is available in the standard build now. To make the collapse/expand controls appears, you need to mouse over the area just to the right of the line numbers as shown in this screenshot:
You should add user settings:
{
"editor.showFoldingControls": "always",
"editor.folding": true,
"editor.foldingStrategy": "indentation",
}
ctrl + k + 0 : Fold all levels (namespace , class , method , block)
ctrl + k + 1 : namspace
ctrl + k + 2 : class
ctrl + k + 3 : methods
ctrl + k + 4 : blocks
ctrl + k + [ or ] : current cursor block
ctrl + k + j : UnFold
The default shortcut for collapse/extend are:
Ctrl + Shift + [ : "Fold"
Ctrl + Shift + Alt + [ : "Fold all"
Ctrl + Shift + ] : "Unfold"
Ctrl + Shift + Alt + ] : "Unfold all"
Or go to keybindings.json and change as you wish.
For example:
{
"key": "cmd+k cmd+m",
"command": "editor.foldAll",
"when": "editorFocus"
},
{
"key": "cmd+m cmd+k",
"command": "editor.unfoldAll",
"when": "editorFocus"
},
If none of the shortcuts are working (like for me), as a workaround you can also open the command palette (Ctrl + 3 or View -> Command Palette...) and type in fold all:
Collapsing is now supported in release 1.0:
Source Code Folding Shortcuts
There are new folding actions to collapse source code regions based on
their folding level.
There are actions to fold level 1 (Ctrl+K Ctrl+1) to level 5 (Ctrl+K
Ctrl+5). To unfold, use Unfold All (Ctrl+Shift+Alt+]).
The level folding actions do not apply to region containing the
current cursor.
I had a problem finding the ] button on my keyboard (Norwegian layout), and in my case it was the Å button. (Or two buttons left and one down starting from the backspace button.)
With JavaScript:
//#region REGION_NAME
...code here
//#endregion
This is the latest built-in(default) keyboard shortcuts for folding and unfolding the code
vscode Keyboard shortcut
Ctrl+Shift+[ Fold (collapse) region
Ctrl+Shift+] Unfold (uncollapse) region
Ctrl+K Ctrl+[ Fold (collapse) all subregions
Ctrl+K Ctrl+] Unfold (uncollapse) all subregions
Ctrl+K Ctrl+0 Fold (collapse) all regions
Ctrl+K Ctrl+J Unfold (uncollapse) all
Nb: But in some cases, your vs code extension or user will alter the keyboard binding(shortcut). So best option that Checks like this
view->command palette OR cntrl+shift+p
type "fold" .It will suggest the fold and unfold and there shortcut. You can type that shortcut instead of command-palette
eg:
Fold All
UnFold All
Just press ctrl + shift + p, and then type 'fold'.
all keybinds about (un)fold will be shown.
If ctrl k does not work, probably it's because the vim extension overrides the key.
in this case, you should modify settings.json (press ctrl + shift + p, and then type 'settings' ) with
"vim.handleKeys": {
"<C-k>": false,
},
This feature is now supported, since Visual Studio Code 1.17. To fold/collapse your code block, just add the region tags, such as //#region my block name and //#endregion if coding in TypeScript/JavaScript.
Example:
v1.42 is adding some nice refinements to how folds look and function. See https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_42.md#folded-range-highlighting:
Folded Range Highlighting
Folded ranges now are easier to discover thanks to a background color
for all folded ranges.
Fold highlight color Theme: Dark+
The feature is controled by the setting editor.foldingHighlight and
the color can be customized with the color editor.foldBackground.
"workbench.colorCustomizations": {
"editor.foldBackground": "#355000" }
Folding Refinements
Shift + Click on the folding indicator first only folds the inner
ranges. Shift + Click again (when all inner ranges are already folded)
will also fold the parent. Shift + Click again unfolds all.
When using the Fold command (kb(editor.fold))] on an already folded
range, the next unfolded parent range will be folded.
No technical tips here, just simple adjustments of the preferences of VsCode.
I managed to show code folding controls always in VsCode by going to Preferences and searching for 'folding'. Now just select to always show these controls. This works with the Typescript code and HTML of templates in the Angular 8 solution I tested it with.
This was tested with VsCode Insiders 1.37.0 running on a Windows 10 OS.
Here is the most common useful default keymap of VSCode. And you cab easily customize them with your own keymap. CTRL + K and then:
Fold All: CTRL + 0
Unfold All: CTRL + J
Fold Region: CTRL + [
Unfold Region: CTRL + ]
Fold Level 1: CTRL+ 1
Fold Level 2: CTRL+ 2
Fold Level 3: CTRL+ 3
Fold Level 1: CTRL+ 4
As of version 1.3.1 (2016-07-17), Block Collapse is much more convenient.
Any line followed by an indented line will have a '-' character to allow collapse. If the block is collapsed, it will then be replaced by a '+' character that will open the collapsed block.
The (Ctrl + Shift + Alt + ]) will still affect all blocks, closing one level. Each repeated use closed one more level. The (Ctrl + Shift + Alt + [) works in the opposite way.
Hooray, block collapse finally works usefully.
to fold/unfold current block use (ctrl+k)+(ctrl+l)
VSCode extension: Fold Level, one key fold to the level you want.
Note: these shortcuts only work as expected if you edit your keybindings.json
I wasn't happy with the default shortcuts, I wanted them to work as follow:
Fold: Ctrl + Alt + ]
Fold recursively: Ctrl + ⇧ Shift + Alt + ]
Fold all: Ctrl + k then Ctrl + ]
Unfold: Ctrl + Alt + [
Unfold recursively: Ctrl + ⇧ Shift + Alt + [
Unfold all: Ctrl + k then Ctrl + [
To set it up:
Open Preferences: Open Keyboard Shortcuts (JSON) (Ctrl + ⇧ Shift + p)
Add the following snippet to that file
Already have custom keybindings for fold/unfold? Then you'd need to replace them.
{
"key": "ctrl+alt+]",
"command": "editor.fold",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+alt+[",
"command": "editor.unfold",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+shift+alt+]",
"command": "editor.foldRecursively",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+shift+alt+[",
"command": "editor.unfoldRecursively",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+k ctrl+[",
"command": "editor.unfoldAll",
"when": "editorTextFocus && foldingEnabled"
},
{
"key": "ctrl+k ctrl+]",
"command": "editor.foldAll",
"when": "editorTextFocus && foldingEnabled"
},
On a Mac, it's the RHS Command key, ⌘K, not the left for the code folding commands.
Otherwise the left hand Command key will delete the current line, ⌘K.
I wish Visual Studio Code could handle:
#region Function Write-Log
Function Write-Log {
...
}
#endregion Function Write-Log
Right now Visual Studio Code just ignores it and will not collapse it.
Meanwhile Notepad++ and PowerGUI handle this just fine.
Update: I just noticed an update for Visual Studio Code. This is now supported!
Or, if you want to remove the folding buttons, for extra space:
"editor.folding": false
(add to your settings.json file)
The command K + command 0 does not work.
More info here: region extensionhttps://marketplace.visualstudio.com/items?itemName=maptz.regionfolder
After installing the extension and using python, it works like this:
# region ARBITRARY_REGION_NAME
code goes here...
# endregion
Also, selecting the desired area, use Ctrl+M+Ctrl+R.(that is: first hit and hold Ctrl, then hit m, let go m, then hit r, and let go all)