DEV Community

adro.codes
adro.codes

Posted on

45 2 1

VS Code define a #region

just show me

Visual Studio Code is definitely one of my favourite code editors and I use it all the time. Recently I stumbled upon the #region keyword. Using this you are able to wrap a section of code that will be collapsed together. This makes organising code a lot easier and allows you to focus on the functionality you're writing and ignore everything else.

Folding Guide

Example time

I will be showing off how to do this in JavaScript, but it is available in quite a very languages. See the guide above.

function add(a, b) {
  return a + b
}

function minus(a, b) {
  return a - b
}

function multiply(a, b) {
  return a * b
}

function divide(a, b) {
  return a / b
}
Enter fullscreen mode Exit fullscreen mode

Without folding, the best you can do, in terms of folding, is the following;

function add(a, b) {...
}

function minus(a, b) {...
}

function multiply(a, b) {...
}

function divide(a, b) {...
}
Enter fullscreen mode Exit fullscreen mode

Not bad but if you add appropriate jsdoc blocks the functions still take up a decent footprint. With regions, you are able to do the following;

// #region Math functions
function add(a, b) {
  return a + b
}

function minus(a, b) {
  return a - b
}

function multiply(a, b) {
  return a * b
}

function divide(a, b) {
  return a / b
}
// #endregion
Enter fullscreen mode Exit fullscreen mode

Now you are able to collapse the code at the // #region definition, collapsing the code down to;

// #region Math functions ...
Enter fullscreen mode Exit fullscreen mode

Documentation

Stay safe out there ❤️

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (5)

Collapse
 
kasir-barati profile image
Mohammad Jawad (Kasir) Barati

Not working in Dockerfiles unfortunately :sad: and I believe it is thanks to how we can write comments in Dockerfiles, so the same goes for yaml files and bash script files

Collapse
 
tsmmark profile image
Mark Allen

have you tried the maptz.regionfolder extension ? marketplace.visualstudio.com/items...

Collapse
 
okineadev profile image
Okinea Dev

Thanks for the clear explanation!

Collapse
 
jayawardanajth profile image
JayawardanaJTH

Thanks

Collapse
 
rahulto profile image
rahulto

nice

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay