4 ms·
Something I have been experimenting with what I call "persistent snippets", over frustration with boilerplate. Basically when inserting a snippet into an editor
by benjaminjackman 8y ago
Something I have been experimenting with what I call "persistent snippets", over frustration with boilerplate. Basically when inserting a snippet into an editor it leaves behind a magic comment, that lists the parameters of the snippet, and wraps the snippet code in a //#region to enable folding in editors and languages that support that.
e.g.:
import {insertSnippet} from "polish-and-release-this-someday"
import {mstBoilerplate} from "./some-local-snippet-store"
insertSnippet(mstBoilerplate, {name : "XCardModel"})
//#region mstBoilerplate 20180911
export interface XCardModelInstance extends Instance<typeof XCardModelImpl> {}
export interface XCardModelCreation extends TypeHelp.Id<typeof XCardModelImpl["CreationType"]> {}
export interface XCardModelSnapshot extends TypeHelp.Id<typeof XCardModelImpl["SnapshotType"]> {}
export interface XCardModel extends TypeHelp.Id<typeof XCardModelImpl> {
Type: XCardModelInstance
CreationType: XCardModelCreation
SnapshotType: XCardModelSnapshot
}
export const XCardModel: XCardModel = XCardModelImpl
//#endregion mstBoilerplate 20180911
This means a simple editor extension and very simple command-line tools can be written to expand / refresh the "insertSnippet" lines as needed. mstBoilerplate, basically is a template literal string in typescript / javascript land but you can use whatever makes sense for your language. So this is useful for language that don't want to / can't support macros natively. You end up getting a lot of the boilerplate reduction power, and everything just works.
I feel for a lot of cases this works better than macros, the expanded code is checked into version control so there is nothing hidden going on, debugging works, if the code ends up needing to be specialized a bit, it's pretty easy to erase the insertSnippet line / comment lines and start modifying.
- hinkley 8y agoInteresting. I had the opposite idea a while back, inspired by a coworker who struggled greatly with indirection. In my scenario I’d like to extend the code folding mechanism to show inherited methods and macro expansions inline, folded by default. Then you could drill into a five function and/or macro code flow, without ever leaving the current editor window, if the code has good cohesion. And if it doesn’t, then this is more incentive to fix your code.