Convert Intellij Live Template to vscode Snippet

Page content

If you are in the need of converting your custom intellij live tempate, you can use this method to transform it.

Locate the template file

The first thing you need to is is to locate the template file. You can find it inside the intellij settings directory which is vary per operating system. See this documentation to locate it.

For example for my golang templte in linux, I find it under

1/home/username/.config/JetBrains/IntelliJIdea2020.1/jba_config/templates/Golang.xml

Convert it into vscode snippet

You can use tihs gocode

  1package main
  2
  3import (
  4	"encoding/json"
  5	"encoding/xml"
  6	"fmt"
  7	"io/ioutil"
  8	"log"
  9	"os"
 10	"regexp"
 11	"strings"
 12)
 13
 14type TemplateSet struct {
 15	XMLName  xml.Name       `xml:"templateSet"`
 16	Text     string         `xml:",chardata"`
 17	Group    string         `xml:"group,attr"`
 18	Template []ideaTemplate `xml:"template"`
 19}
 20
 21type ideaTemplate struct {
 22	Text             string `xml:",chardata"`
 23	Name             string `xml:"name,attr"`
 24	Value            string `xml:"value,attr"`
 25	Description      string `xml:"description,attr"`
 26	ToReformat       string `xml:"toReformat,attr"`
 27	ToShortenFQNames string `xml:"toShortenFQNames,attr"`
 28	Variable         []struct {
 29		Text         string `xml:",chardata"`
 30		Name         string `xml:"name,attr"`
 31		Expression   string `xml:"expression,attr"`
 32		DefaultValue string `xml:"defaultValue,attr"`
 33		AlwaysStopAt string `xml:"alwaysStopAt,attr"`
 34	} `xml:"variable"`
 35	Context struct {
 36		Text   string `xml:",chardata"`
 37		Option struct {
 38			Text  string `xml:",chardata"`
 39			Name  string `xml:"name,attr"`
 40			Value string `xml:"value,attr"`
 41		} `xml:"option"`
 42	} `xml:"context"`
 43}
 44
 45// "Print to console": {
 46// 	"prefix": "log",
 47// 	"body": [
 48// 		"console.log('$1');",
 49// 		"$2"
 50// 	],
 51// 	"description": "Log output to console"
 52// }
 53
 54type snippet struct {
 55	Prefix      string   `json:"prefix,omitempty"`
 56	Body        []string `json:"body,omitempty"`
 57	Description string   `json:"description,omitempty"`
 58}
 59
 60func main() {
 61	f, err := os.Open("Golang.xml")
 62	if err != nil {
 63		log.Fatal(err)
 64	}
 65
 66	defer f.Close()
 67
 68	raw, err := ioutil.ReadAll(f)
 69	if err != nil {
 70		log.Fatal(err)
 71	}
 72
 73	var ts TemplateSet
 74	xml.Unmarshal(raw, &ts)
 75
 76	snippets := map[string]snippet{}
 77	for i, t := range ts.Template {
 78		s := snippet{
 79			Prefix:      t.Name,
 80			Body:        getBody(t),
 81			Description: t.Description,
 82		}
 83		if _, ok := snippets[t.Description]; ok {
 84			log.Fatalf("duplicate key %d:%s %s", i, t.Description, t.Name)
 85		}
 86		snippets[t.Description] = s
 87	}
 88
 89	raw, err = json.MarshalIndent(snippets, "", "\t")
 90	if err != nil {
 91		log.Fatal(err)
 92	}
 93	fmt.Printf("%s", raw)
 94}
 95
 96var (
 97	rx = regexp.MustCompile(`\$\w+\$`)
 98)
 99
100func getBody(t ideaTemplate) []string {
101	s := t.Value
102
103	params := map[string]string{}
104	match := rx.FindAllString(s, -1)
105	varIdx := 1
106
107MATCH:
108	for _, v := range match {
109		if _, ok := params[v]; ok {
110			// already process the parameter
111			continue
112		}
113		varname := strings.ReplaceAll(v, "$", "")
114
115		for _, ideaVar := range t.Variable {
116			if ideaVar.Name == varname {
117				defaultValue := ideaVar.DefaultValue
118				if defaultValue != "" {
119					// process default
120					replacement := fmt.Sprintf("${%d:another}", varIdx)
121					varIdx++
122					params[v] = replacement
123					continue MATCH
124				}
125
126				// no variable found
127				replacement := fmt.Sprintf("$%d", varIdx)
128				varIdx++
129				params[v] = replacement
130			}
131		}
132
133	}
134
135	// replace all parameters
136	for varname, p := range params {
137		s = strings.ReplaceAll(s, varname, p)
138	}
139	s = strings.ReplaceAll(s, "$END$", "")
140
141	return strings.Split(s, "\n")
142}

Copy the output of the file. On vscode, Ctrl+p to bring the command and search for Configure User Snippet. Chose the language and then paste it.