Code Monkey home page Code Monkey logo

new-file-template.nvim's Introduction

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

  • The templates are separated by filetype.
  • The user templates are in lua/templates/{filetype}.lua inside your neovim configuration.
  • To create a new template, just run :NewFileTemplate filetype (if you don't pass any args, it will create a template for the current file filetype)

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:

  • Return the template string if it found it
  • Return false is don't find nothing

The utils.find_entry function receives an table with:

  • pattern: the pattern that triggers the template
  • content: a function that return the template string

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

  • Convert snake_case to camelCase

camel_to_snake

  • Convert camelCase to snake_case

snake_to_class_camel

  • Convert snake_case to ClassCamelCase

class_camel_to_snake

  • Convert ClassCamelCase to snake_case

camel_to_kebab

  • Convert camelCase to kebab-case

kebab_to_camel

  • Convert kebab-case to camelCase

pluralize

  • Pluralize a word

singularize

  • Singularize a word

split(string, delimiter)

  • Split a string into an array

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.

new-file-template.nvim's People

Contributors

otavioschwanck avatar jensenqi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.