question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I think it’s time to update old snippets and add new useful ones. Everyone who wants to contribute, please comment with all the snippets you would want to use. They should be small and either be general purpose or do common boilerplate code in normal D code. You can also suggest snippets for dub.json, dub.sdl, vibe.d .dt files, dlangui .dml files, etc.

First of all these are the snippets I actively use a lot or think are useful: (I recommend you list what you like to use aswell)

  • main
  • refp
  • gets
  • op*
  • ex - but could be improved
  • inrange <–
  • fwrange <-- my 2 favorite ones

Here are some I start with: (<> denotes an editable cursor)

imain

int main(string[] args) {<>}

cmain - -betterC main

extern (C) void main() {<>}

cimain - -betterC main

extern(C) int main(int argc, const(char)** argv) {<>}

typedef - Creates a typesafe alias not allowing implicit casting from base type, but allows implicit conversion to the base type in most cases. Therefore the implicit casting works a lot like class/interface inheritance.

enum <1:MyType> : <2:BaseType> {<3:init = 0>}

proxy - Creates a typesafe alias not allowing implicit casting to the base type, but allows implicit conversion from the base type in most cases. Basically allows copying any base type with new properties and methods as new and separate type. Imports std.typecons : Proxy

struct <1:MyType> {
	<2:BaseType> base;
	mixin Proxy!base;
}

cominterface - Win32 COM interface without implementation to talk to other applications.

interface <1:MyType> : IUnknown {
extern(Windows):<2>
}

comobject - Win32 COM interface with implementation to serve to other applications.

class <1:MyType> : ComObject {
extern(Windows):<2>
}

Range improvements: make them only include methods. Update inrange:

void popFront() {<4:>}
bool empty() @property const {<3:return true;>}
<1:auto> front() @property {<2:return myElement;>}

Update fwrange:

void popFront() {<5:>}
typeof(this) save() {<4:return this;>}
bool empty() @property const {<3:return true;>}
<1:auto> front() @property {<2:return myElement;>}

infrange:

enum bool empty = false;
void popFront() {<3:>}
<1:auto> front() @property {<2:return myElement;>}

outrange

void put(<2:MyElement> item) {<4:>}

birange

bool empty() @property const {<6:return true;>}
void popFront() {<4:>}
void popBack() {<5:>}
<1:auto> front() @property {<2:return myElement;>}
<1:auto> back() @property {<3:return myElement;>}

randrange

<1:auto> opIndex(<2:size_t> i) {<3:return myElement;>}
<2:size_t> length() @property const {<4:return 0;>}
<2:size_t> opDollar(size_t pos)() {<5:return length;>}

opslice

<1:size_t[2]> opSlice(size_t start, size_t end) {<2:return [start, end];>}
<3:auto> opIndex(size_t[2] slice) {<4:return base[slice[0] .. slice[1]];>}

Range snippets could maybe even be served by serve-d and automatically analyze which fields already exist and omit or update them accordingly.

diet snippets: html

doctype html

html(lang="en")
	head
		meta(charset="utf-8")
		meta(name="viewport", content="width=device-width, initial-scale=1")
		title <1:>
	body
		<2:>

css

link(rel="stylesheet", href="<>")

js

script(src="<>")

style

:css
	<>

script

:javascript
	<>

markdown

:markdown
	<>

= diet should also support D snippets inside embedded D code. Another reason to complete them in serve-d =

I suggest renaming some of the existing snippets too: ex should become exception er should become error tmpl should become template fore should become foreach

Typing whole words are easier to remember and it looks more like keywords are auto-completed.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dd86kcommented, Mar 19, 2019

so foreach and foreachr or foreach_r snippets?

0reactions
WebFreak001commented, Mar 28, 2019

a little bit experimental, but I think these are common boilerplate to write:

viberouter - inserts a new router instance with GET / path

auto <1:router> = new URLRouter();
<1:router>.get("/", &<2:index>);

vibeserver - inserts a basic vibe.d HTTP server structure

auto <3:settings> = new HTTPServerSettings;
<3:settings>.port = <1:3000>;
<3:settings>.bindAddresses = <2:["::1", "127.0.0.1"]>;

auto <4:router> = new URLRouter();
<4:router>.get("/", &<5:index>);

listenHTTP(<3:settings>, <4:router>);

vibeget - inserts a basic low-level GET request using vibe.d

requestHTTP(URL("<1:>"), null, (scope HTTPClientResponse res) {
	<2:// TODO: check res.statusCode and read response into parent scope variables>
});

vibegettext - inserts a basic low-level GET request using vibe.d reading the full response as text

string <1:text>;
requestHTTP(URL("<2:>"), null, (scope HTTPClientResponse res) {
	<3:// TODO: check res.statusCode>
	<1:text> = res.bodyReader.readAllUTF8();
});

vibegetjson - inserts a basic low-level GET request using vibe.d reading a JSON response

Json <1:json>;
requestHTTP(URL("<2:>"), null, (scope HTTPClientResponse res) {
	<3:// TODO: check res.statusCode>
	<1:json> = res.readJson(); // TODO: probably want to .deserializeJson!T
});

vibepost - inserts a basic low-level POST request using vibe.d

requestHTTP(URL("<1:>"), (scope HTTPClientRequest req) {
	req.method = HTTPMethod.POST;
	<2:// TODO: write request body>
}, (scope HTTPClientResponse res) {
	<3:// TODO: check res.statusCode and read response into parent scope variables>
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Snippets in Visual Studio Code
Snippets in Visual Studio Code. Code snippets are templates that make it easier to enter repeating code patterns, such as loops or conditional-statements....
Read more >
Snippets - GitLab Docs
With GitLab snippets, you can store and share bits of code and text with other users. You can comment on, clone, and use...
Read more >
Code snippets - Visual Studio (Windows) - Microsoft Learn
Code snippets are small blocks of reusable code that you can add to a code file by using the right-click context menu command...
Read more >
Google Shows New Snippets For Quoted Searches
Google Shows New Snippets For Quoted Searches. Google changes how search results are displayed for queries that contain text in quotes.
Read more >
Create and use snippets - HubSpot Knowledge Base
Learn how to create a snippet and use it in an email template or in a ... In the top right on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found