snippets

package
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2023 License: MIT Imports: 11 Imported by: 0

README

snippets

import "github.com/slalombuild/fusion/snippets"

Package snippets implements support for building snippets for vscode

Index

Variables

go:embed package.json

var EXTENSION_PACKAGE_FILE []byte

func JSON

func JSON(w io.Writer, v interface{}) error

JSON renders the snippets to a pretty-printed object at the destination io.Writer (w).

func XML

func XML(w io.Writer, v interface{}) error

XML renders the live templates to a pretty-printed object at the destination io.Writer (w).

type Generator

type Generator interface {
    Generate(io.Writer, embed.FS) error
}

type IntelliJ

type IntelliJ struct{}
func (IntelliJ) AddTemplate
func (i IntelliJ) AddTemplate(target *IntelliJTemplateGroup, template IntelliJTemplate)

AddTemplate adds a Tempate to the target TemplateSet struct

func (IntelliJ) Generate
func (i IntelliJ) Generate(w io.Writer, filesystem embed.FS) error
Generate generates an XML file which contains

an IntelliJ live template group built from the fusion templates

Generate supports rendering output to an io.Writer.

func (IntelliJ) NewTemplate
func (i IntelliJ) NewTemplate(name string, template string, opts ...IntelliJOption) *IntelliJTemplate

NewTemplate creates a new live template Template, the building block for IntelliJ live templates.

All Template bodies are generated from parsing the provided Go template.

func (IntelliJ) WithDescription
func (i IntelliJ) WithDescription(description string) IntelliJOption

WithDescription applies a description to a Template.

func (IntelliJ) WithValue
func (i IntelliJ) WithValue(value string) IntelliJOption

WithValue applies the live template value lines to a Template.

func (IntelliJ) WithVariables
func (i IntelliJ) WithVariables(vars []string) IntelliJOption

type IntelliJOption

IntelliJOption is a modifying function that applies a property to an Template.

type IntelliJOption func(*IntelliJTemplate)

type IntelliJTemplate

Templates contain information about the live template including information about any variables it uses, the base template itself, and other metadata

type IntelliJTemplate struct {
    // XMLName serves as the XML tag which this struct will populate
    XMLName xml.Name `xml:"template"`

    // Name serves as the abbreviation for a template in the IDE
    Name string `xml:"name,attr"`

    // Value is the template as a string, which is to be encoded to XML at a later time
    Value string `xml:"value,attr"`

    // Description is an optional description of the live template displayed by the IDE
    Description string `xml:"description,attr"`

    // Variable definitions are needed in order to allow their usage within templates
    // - see Variable struct
    Variables []templateVariable `xml:"variable"`

    ToReformat string `xml:"toReformat,attr"`

    ToShortenFQNames string `xml:"toShortenFQNames,attr"`

    // Context contains Options for a Template - see Context struct
    Context templateContext `xml:"context"`
}

type IntelliJTemplateGroup

The IntelliJTemplateGroup contains all of the Templates that will be contained within the Template group

type IntelliJTemplateGroup struct {
    XMLName   xml.Name           `xml:"templateSet"`
    Group     string             `xml:"group,attr"`
    Templates []IntelliJTemplate `xml:"template"`
}

type VSCode

type VSCode struct{}
func (VSCode) AddItem
func (v VSCode) AddItem(target *map[string]*VSCodeSnippet, snippet *VSCodeSnippet)

AddItem adds an Item to the target Snippet map.

func (VSCode) Generate
func (v VSCode) Generate(w io.Writer, filesystem embed.FS) error

Generate generates a snippets file from all Go template files in the provided filesystem.

Generate supports rendering output to an io.Writer.

func (VSCode) NewItem
func (v VSCode) NewItem(name string, template string, opts ...VSCodeOption) *VSCodeSnippet

NewItem creates a new snippet Item, the building block for VSCode snippets.

All Item bodies are generated from parsing the provided Go template.

Example

package main

import (
	"github.com/slalombuild/fusion/snippets"
	"github.com/slalombuild/fusion/templates/aws"
)

func main() {
	// Generate a new snippet item from an existing
	// Go template
	v := snippets.VSCode{}
	v.NewItem("IAM Policy", aws.TEMPLATE_AWS_IAM_POLICY)
}

func (VSCode) NewSnippetMap
func (v VSCode) NewSnippetMap() map[string]*VSCodeSnippet

NewSnippetMap creates a new VSCode snippet map.

SnippetMaps are used to build a vscode snippet file using a map of named Items.

Example

package main

import (
	"github.com/slalombuild/fusion/snippets"
	"github.com/slalombuild/fusion/templates/aws"
	"log"
	"os"
)

func main() {
	v := snippets.VSCode{}
	// Create a new snippet map
	sm := v.NewSnippetMap()

	// Build a list of snippets to add to the map
	s := []*snippets.VSCodeSnippet{
		v.NewItem("IAM Policy", aws.TEMPLATE_AWS_IAM_POLICY),
		v.NewItem("Lambda function", aws.TEMPLATE_AWS_LAMBDA_FUNCTION),
	}

	// Append all the snippets
	for _, snippet := range s {
		v.AddItem(&sm, snippet)
	}

	// Render the snippet file as json
	err := snippets.JSON(os.Stdout, s)
	if err != nil {
		log.Fatal(err)
	}

	// OR write the snippet json to a file
	f, err := os.Create("fusion-snippets.json")
	if err != nil {
		log.Fatal(err)
	}

	// The JSON method can write to stdout,stderr, a file
	// or anything else that implements io.Writer
	err = snippets.JSON(f, s)
	if err != nil {
		log.Fatal(err)
	}
}

func (VSCode) WithBody
func (v VSCode) WithBody(body []string) VSCodeOption

WithBody applies the snippet body lines to an Item.

func (VSCode) WithDescription
func (v VSCode) WithDescription(description string) VSCodeOption

WithDescription applies a description to an Item.

func (VSCode) WithPrefix
func (v VSCode) WithPrefix(prefix string) VSCodeOption

WithPrefix applies an intellisense prefix to an Item.

type VSCodeOption

VSCodeOption is a modifying function that applies a property to an Item.

type VSCodeOption func(*VSCodeSnippet)

type VSCodeSnippet

Item is the content of a named VSCode snippet. Items are placed into maps of map[string]*VSCodeSnippet to create a full named VSCodeSnippet.

type VSCodeSnippet struct {

    // Scope scopes the snippet so that only relevant snippets are suggested.
    Scope string `json:"scope,omitempty"`

    // Prefix defines one or more trigger words that
    // display the snippet in IntelliSense. Substring
    // matching is performed on prefixes.
    Prefix string `json:"prefix"`

    // Description is an optional description of the snippet
    // displayed by IntelliSense.
    Description string `json:"description"`

    // Body is one or more lines of content, which will be joined as multiple lines upon insertion.
    // Newlines and embedded tabs will be formatted according to the context in which the snippet is inserted.
    Body []string `json:"body"`
    // contains filtered or unexported fields
}

Generated by gomarkdoc

Documentation

Overview

Package snippets implements support for building snippets for vscode

Index

Examples

Constants

This section is empty.

Variables

View Source
var EXTENSION_PACKAGE_FILE []byte

Functions

func JSON

func JSON(w io.Writer, v interface{}) error

JSON renders the snippets to a pretty-printed object at the destination io.Writer (w).

func XML

func XML(w io.Writer, v interface{}) error

XML renders the live templates to a pretty-printed object at the destination io.Writer (w).

Types

type Generator

type Generator interface {
	Generate(io.Writer, embed.FS) error
}

type IntelliJ

type IntelliJ struct{}

func (IntelliJ) AddTemplate

func (i IntelliJ) AddTemplate(target *IntelliJTemplateGroup, template IntelliJTemplate)

AddTemplate adds a Tempate to the target TemplateSet struct

func (IntelliJ) Generate

func (i IntelliJ) Generate(w io.Writer, filesystem embed.FS) error

Generate generates an XML file which contains an IntelliJ live template group built from the fusion templates

Generate supports rendering output to an io.Writer.

func (IntelliJ) NewTemplate

func (i IntelliJ) NewTemplate(name string, template string, opts ...IntelliJOption) *IntelliJTemplate

NewTemplate creates a new live template Template, the building block for IntelliJ live templates.

All Template bodies are generated from parsing the provided Go template.

func (IntelliJ) WithDescription

func (i IntelliJ) WithDescription(description string) IntelliJOption

WithDescription applies a description to a Template.

func (IntelliJ) WithValue

func (i IntelliJ) WithValue(value string) IntelliJOption

WithValue applies the live template value lines to a Template.

func (IntelliJ) WithVariables

func (i IntelliJ) WithVariables(vars []string) IntelliJOption

type IntelliJOption

type IntelliJOption func(*IntelliJTemplate)

IntelliJOption is a modifying function that applies a property to an Template.

type IntelliJTemplate

type IntelliJTemplate struct {
	// XMLName serves as the XML tag which this struct will populate
	XMLName xml.Name `xml:"template"`

	// Name serves as the abbreviation for a template in the IDE
	Name string `xml:"name,attr"`

	// Value is the template as a string, which is to be encoded to XML at a later time
	Value string `xml:"value,attr"`

	// Description is an optional description of the live template displayed by the IDE
	Description string `xml:"description,attr"`

	// Variable definitions are needed in order to allow their usage within templates
	// - see Variable struct
	Variables []templateVariable `xml:"variable"`

	ToReformat string `xml:"toReformat,attr"`

	ToShortenFQNames string `xml:"toShortenFQNames,attr"`

	// Context contains Options for a Template - see Context struct
	Context templateContext `xml:"context"`
}

Templates contain information about the live template including information about any variables it uses, the base template itself, and other metadata

type IntelliJTemplateGroup

type IntelliJTemplateGroup struct {
	XMLName   xml.Name           `xml:"templateSet"`
	Group     string             `xml:"group,attr"`
	Templates []IntelliJTemplate `xml:"template"`
}

The IntelliJTemplateGroup contains all of the Templates that will be contained within the Template group

type VSCode

type VSCode struct{}

func (VSCode) AddItem

func (v VSCode) AddItem(target *map[string]*VSCodeSnippet, snippet *VSCodeSnippet)

AddItem adds an Item to the target Snippet map.

func (VSCode) Generate

func (v VSCode) Generate(w io.Writer, filesystem embed.FS) error

Generate generates a snippets file from all Go template files in the provided filesystem.

Generate supports rendering output to an io.Writer.

func (VSCode) NewItem

func (v VSCode) NewItem(name string, template string, opts ...VSCodeOption) *VSCodeSnippet

NewItem creates a new snippet Item, the building block for VSCode snippets.

All Item bodies are generated from parsing the provided Go template.

Example
package main

import (
	"github.com/slalombuild/fusion/snippets"
	"github.com/slalombuild/fusion/templates/aws"
)

func main() {
	// Generate a new snippet item from an existing
	// Go template
	v := snippets.VSCode{}
	v.NewItem("IAM Policy", aws.TEMPLATE_AWS_IAM_POLICY)
}
Output:

func (VSCode) NewSnippetMap

func (v VSCode) NewSnippetMap() map[string]*VSCodeSnippet

NewSnippetMap creates a new VSCode snippet map.

SnippetMaps are used to build a vscode snippet file using a map of named Items.

Example
package main

import (
	"log"
	"os"

	"github.com/slalombuild/fusion/snippets"
	"github.com/slalombuild/fusion/templates/aws"
)

func main() {
	v := snippets.VSCode{}
	// Create a new snippet map
	sm := v.NewSnippetMap()

	// Build a list of snippets to add to the map
	s := []*snippets.VSCodeSnippet{
		v.NewItem("IAM Policy", aws.TEMPLATE_AWS_IAM_POLICY),
		v.NewItem("Lambda function", aws.TEMPLATE_AWS_LAMBDA_FUNCTION),
	}

	// Append all the snippets
	for _, snippet := range s {
		v.AddItem(&sm, snippet)
	}

	// Render the snippet file as json
	err := snippets.JSON(os.Stdout, s)
	if err != nil {
		log.Fatal(err)
	}

	// OR write the snippet json to a file
	f, err := os.Create("fusion-snippets.json")
	if err != nil {
		log.Fatal(err)
	}

	// The JSON method can write to stdout,stderr, a file
	// or anything else that implements io.Writer
	err = snippets.JSON(f, s)
	if err != nil {
		log.Fatal(err)
	}
}
Output:

func (VSCode) WithBody

func (v VSCode) WithBody(body []string) VSCodeOption

WithBody applies the snippet body lines to an Item.

func (VSCode) WithDescription

func (v VSCode) WithDescription(description string) VSCodeOption

WithDescription applies a description to an Item.

func (VSCode) WithPrefix

func (v VSCode) WithPrefix(prefix string) VSCodeOption

WithPrefix applies an intellisense prefix to an Item.

type VSCodeOption

type VSCodeOption func(*VSCodeSnippet)

VSCodeOption is a modifying function that applies a property to an Item.

type VSCodeSnippet

type VSCodeSnippet struct {

	// Scope scopes the snippet so that only relevant snippets are suggested.
	Scope string `json:"scope,omitempty"`

	// Prefix defines one or more trigger words that
	// display the snippet in IntelliSense. Substring
	// matching is performed on prefixes.
	Prefix string `json:"prefix"`

	// Description is an optional description of the snippet
	// displayed by IntelliSense.
	Description string `json:"description"`

	// Body is one or more lines of content, which will be joined as multiple lines upon insertion.
	// Newlines and embedded tabs will be formatted according to the context in which the snippet is inserted.
	Body []string `json:"body"`
	// contains filtered or unexported fields
}

Item is the content of a named VSCode snippet. Items are placed into maps of map[string]*VSCodeSnippet to create a full named VSCodeSnippet.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL