#VisualStudioCode – Create and use Code Snippets in @code, and create custom snippets just for your workspace

Buy Me A Coffee

Hi !

Code Snippets are super useful, and create your custom Code Snippets is super easy in Visual Studio Code. The official Documentation (see references) explains how to create snippets, with the full details of the JSON file, sections, contents and more. However there are some extra tips on top of this information that are super useful.

Snippet Creator

This is an amazing Visual Studio Code Extension. Just search for [snippet creator] on the Extension and install the extension.

visual studio code snippet creator extension

Once the extension is installed, the way to use this is super simple.

  • Select some code
  • CTRL + SHIFT + P, and type [create snippet]
  • Follow the steps, define snippet prefix, and optionally description
  • That’s it!
visual studio code snippet creator add new snippet

I created a custom snippet with the prefix [cameraCalcFPS] and now the snippet is available for any python file !

visual studio code custom code snippet available

In order to check all the custom code snippets

  • CTRL + SHIFT + P
  • Type [configure user snippets]
  • Open the specific one, for this sample python.json

The code snippets file has all the custom created snippets

visual studio code open custom snippets for python

Workspace specific snippets

There is also another specific scenario which I found very interesting:

The need for specific snippets inside a workspace

In order to do this, just create a file [mysnippets.code-snippets] and paste your own snippets here. This snippets will be only available for this workspace!

snippets for a workspace

Happy coding!

Greetings

El Bruno


References

#PowerBI – Sending information to Streaming Datasets from a UWP App with C#

Hello!

Today the post is a simple reminder of a piece of code I’ve written from scratch several times, and that’s it, I’m writing this down here! The scenario is simple:

In a PowerBI Instance I have a pair of Streaming Datasets and I want to send information to them from a Windows Store App.

Well, in Hands On Labs that has shared Ozzie (see references), describes the steps to create the Streaming Dataset from scratch and to feed it. However, a bit of code is pending for Windows 10 and C#.

My Streaming DataSet value definition is very simple:

i!

And the source code is even simpler!

private async void ReportVisitorsAsync(string pushUrl, int totalVisitors, int totalVisitorsIdentified, string centerName)
{
var visitorsMeasurement = new
{
TotalVisitors = totalVisitors,
TotalRecognizedVisitors = totalVisitorsIdentified,
timeStamp = DateTime.Now,
CenterName = centerName
};
var jsonString = JsonConvert.SerializeObject(visitorsMeasurement);
var request = WebRequest.Create(pushUrl);
request.Method = "POST";
request.ContentType = "application/json";
using (var requestWriter = new StreamWriter(await request.GetRequestStreamAsync()))
{
requestWriter.Write(jsonString);
requestWriter.Flush();
}
await request.GetResponseAsync();
}

Happy Coding!

Greetings @ Toronto

El Bruno

References

#PowerBI – Enviando información a Streaming Datasets desde una app UWP con C#

Hola!

Hoy el post es un simple recordatorio de una porción de código que ya he escrito desde cero varias veces. El escenario es simple:

En una instancia de PowerBI tengo un par de Streaming Datasets y quiero enviar información a los mismos desde una Windows Store App.

Pues bien, en el Hands On Labs que ha compartido Ozzie (ver referencias), se describen los pasos para crear desde cero el Streaming Dataset y para alimentar el mismo. Sin embargo, queda pendiente un poco de código para Windows 10 y C#.

Mi Streaming DataSet es muy simple:

i!

¡Y el código que utilizo es más simple aun!

private async void ReportVisitorsAsync(string pushUrl, int totalVisitors, int totalVisitorsIdentified, string centerName)
{
var visitorsMeasurement = new
{
TotalVisitors = totalVisitors,
TotalRecognizedVisitors = totalVisitorsIdentified,
timeStamp = DateTime.Now,
CenterName = centerName
};
var jsonString = JsonConvert.SerializeObject(visitorsMeasurement);
var request = WebRequest.Create(pushUrl);
request.Method = "POST";
request.ContentType = "application/json";
using (var requestWriter = new StreamWriter(await request.GetRequestStreamAsync()))
{
requestWriter.Write(jsonString);
requestWriter.Flush();
}
await request.GetResponseAsync();
}

Happy Coding!

Saludos @ Toronto

El Bruno

References

#VS2015 – PostHaste, share your code snippets with just one click! (to a #Raspberry Pi ;) #VSGallery

Hola!

Today is Visual Studio 2015 extension friday’s one more time. You surely know PasteBin or GistHub, as repositories for code snippets. Today, thanks to this extension, I added one more to the list: HasteBin.

The operation of the extension is very simple:

  • Select a portion of code
  • Display the context menu and select Post Haste
  • This action will publish the code in HasteBin and copy to the Clipboard the Url of publication
  • Then you can browse the code snippet

An live sample

The interesting thing about this service is that it is based on node.js and consumes few resources. For example; there are articles about how to host it in a Raspberry Pi 2 😉

By default HasteBin treats the code as text snippets with format Mark Down, but you can also work as plain text. You can also upload, modify, or delete code snippets from command line.

Enjoy your weekend!

Saludos @ Madrid

/El Bruno

References

– Post Haste https://visualstudiogallery.msdn.microsoft.com/f3ccaa45-c306-44bd-b435-495132d3383f

– John Crepezzi @hastebin https://twitter.com/hastebin

– PasteBin http://pastebin.com/

– GistHub https://gist.github.com/elbruno

#VS2015 – PostHaste, comparte tus code snippets con un solo click ! (a una #Raspberry Pi ;) #VSGallery

Hola!

Viernes de extensiones para Visual Studio 2015. Seguramente conoces PasteBin o GistHub, como repositorios de code snippets. Hoy gracias a esta extensión he agregado una más a la lista: HasteBin.

El funcionamiento de la extensión es muy simple:

– Seleccionas una sección de código

– Menú contextual y seleccionas Post Haste

– Esto publica el código en HasteBin y copia al portapapeles la Url de publicación

– Luego se puede navegar el code snippet

Un ejemplo en acción

Lo interesante de este servicio, es que está basado en node.js y consume pocos recursos. Por ejemplo< existen artículos sobre como hostear el mismo en una Raspberry Pi 2.

Por defecto HasteBin trata a los fragmentos de código como texto con formato Mark Down, pero se puede trabajar también como texto sin formato. Además se pueden subir, modificar o eliminar code snippets desde línea de comandos.

Enjoy your weekend!

Saludos @ Madrid

/El Bruno

References

– Post Haste https://visualstudiogallery.msdn.microsoft.com/f3ccaa45-c306-44bd-b435-495132d3383f

– John Crepezzi @hastebin https://twitter.com/hastebin

– PasteBin http://pastebin.com/

– GistHub https://gist.github.com/elbruno