Home

Awesome

Introduction

A simple, but, powerful plugin that allows you to easily create multiple templates for new files using lua for your projects (using regexp to decide which template should be used). It also come with some pre-configured templates that you can use.

image 1

Setup

Lazy

{ "otavioschwanck/new-file-template.nvim", opts = {} }

Packer

use {
  'otavioschwanck/new-file-template.nvim', 
  config = function() 
    require('new-file-template').setup() 
  end
}

Setup Options

{ 
 disable_insert = false, -- Enter in insert mode after inserting the template?,
 disable_autocmd = false, -- Disable the autocmd that creates the template.  You can use manually by calling :InsertTemplateFile,
 disable_filetype = {}, -- Disable templates for a filetype (disable only default templates.  User templates will work).
 disable_specific = {}, -- Disable specific regexp for the default templates.  Example: { ruby = { ".*" } }.  To see the regexps, just look into lua/templates/{filetype}.lua for the regexp being used.
 suffix_as_filetype = false, -- use suffix of filename rather than vim.bo.filetype as filetype
}

Creating new templates

Intro

An example of a basic template for solidity:

local utils = require("new-file-template.utils")

local function base_template(path, filename)
	local contract_name = vim.split(filename, "%.")[1]
	local solidity_version = vim.g.solc_version or "0.8.14"

	return [[
// SPDX-License-Identifier: UNLICENSED

pragma solidity ]] .. solidity_version .. [[;

contract ]] .. contract_name .. [[ {
  |cursor|
}]]
end

local function helper_template(path, filename) -- just an example
  return "Created a file in " .. path .. " called " .. filename
end

return function(opts)
	local template = {
		{ pattern = "helper/.*", content = helper_template },
		{ pattern = ".*", content = base_template },
	}

	return utils.find_entry(template, opts)
end

For a more sophisticated template, check the ruby template

How it works?

The template file always should return a function that:

The utils.find_entry function receives an table with:

The content function will always receive the path to the file and the filename as parameters. It should ALWAYS return a string.

If the content return |cursor| inside the string, it will position the cursor there.

What we can do with it?

The content function can do anything. Can ask the user for some input, can call an API to get the content. The sky is the limit.

Quickly creating a new template

Here is an example of me creating a JavaScript template.

image 2

Utils to help you creating your own templates

The utils module contains some useful functions to help you creating your own templates.

snake_to_camel

camel_to_snake

snake_to_class_camel

class_camel_to_snake

camel_to_kebab

kebab_to_camel

pluralize

singularize

split(string, delimiter)

Troubleshoot

empty lines are not being respected

If for some reason, the template ignores your empty lines, just add \n on the previous line that will work

Contributing

This plugin is in its early stages, and we welcome your help! If you use some framework that is not in the defaults, please open a PR to contribute to the project. Any help is welcome.

If you use some framework that is not on the defaults, please, open an PR to help the project! Any help is welcome.