chrisgrieser/nvim-puppeteer

github github
editing-supportplugin
stars 47
issues 2
subscribers 1
forks 1
CREATED

2023-09-18

UPDATED

23 days ago


nvim-puppeteer 🎎

Master of strings. Automatically convert strings to f-strings or template strings and back.

Features

  • When typing {} in a Python string automatically converts it to an f-string.
  • Adding ${} or a line break in a JavaScript string automatically converts it to a template string. (Also works in related languages like JS-React or Typescript.)
  • Typing %s in a non-pattern Lua string automatically converts it to a formatted string. (Opt-in, as this has some caveats.)
  • Removing the {}, ${}, or %s converts it back to a regular string.
  • Also works with multi-line strings and undos.
  • Zero configuration. Just install and you are ready to go.

Requirements

  • nvim 0.9 or higher.
  • The respective Treesitter parsers: :TSInstall python javascript typescript. (Installing them requires nvim-treesitter.)

Installation

-- lazy.nvim
{ 
    "chrisgrieser/nvim-puppeteer",
    lazy = false, -- plugin lazy-loads itself. Can also load on filetypes.
},

-- packer
use { "chrisgrieser/nvim-puppeteer" }

There is no .setup() call. The plugin already automatically loads as little as possible.

Special Case: Formatted Strings in Lua

Through string.format, there are also formatted strings in Lua. However, auto-conversions are far more difficult in lua %s is used as a placeholder for string.format and as class in lua patterns at the same time. While it is possible to identify in some cases whether a lua string is used as pattern, there are certain cases where that is not possible:

-- desired: conversion to format string when typing the placeholder "%s"
local str = "foobar %s baz" -- before
local str = ("foobar %s baz"):format() -- after

-- problem case that can be dealt with: "%s" used as class in lua pattern
local found = str:find("foobar %s")

-- problem case that cannot be dealt with: "%s" in string, which
-- is only later used as pattern
local pattern = "foobar %s baz"
-- some code…
str:find(pattern)

Since the auto-conversion of lua strings can result in undesired false conversions, the feature is opt-in only. This way, you can decide for yourself whether the occasional false positive is worth it for you or not.

-- enable auto-conversion of lua strings (default: false)
vim.g.puppeteer_lua_format_string = true

[!NOTE] After enabling, you can also set the variable to false temporarily to pause the auto-conversion. This can be useful if only one specific string gives you trouble.

Credits

About Me
In my day job, I am a sociologist studying the social mechanisms underlying the digital economy. For my PhD project, I investigate the governance of the app economy and how software ecosystems manage the tension between innovation and compatibility. If you are interested in this subject, feel free to get in touch.

Blog
I also occasionally blog about vim: Nano Tips for Vim

Profiles

Buy Me a Coffee