GithubHelp home page GithubHelp logo

bennypowers / nvim-regexplainer Goto Github PK

View Code? Open in Web Editor NEW
587.0 4.0 7.0 149 KB

Describe the regexp under the cursor

Makefile 0.71% Lua 99.29%
neovim regex regexp nvim nvim-plugin regular-expression tree-sitter lua neovim-plugin

nvim-regexplainer's Introduction

Am Yisrael Chai - עם ישראל חי

nvim-regexplainer

Lua GitHub Workflow Status Number of users on dotfyle

Describe the regular expression under the cursor.

Regexplainer.mov

Heavily inspired by the venerable atom-regexp-railroad.

👉 NOTE: Requires Neovim 0.7 👈

🚚 Installation

use { 'bennypowers/nvim-regexplainer',
      config = function() require'regexplainer'.setup() end,
      requires = {
        'nvim-treesitter/nvim-treesitter',
        'MunifTanjim/nui.nvim',
      } }

You need to install regex with nvim-treesitter, as well as the grammar for whichever host language you're using. So for example if you wish to use Regexplainer with TypeScript sources, you need to do this:

:TSInstall regex typescript

🤔 Config

-- defaults
require'regexplainer'.setup {
  -- 'narrative'
  mode = 'narrative', -- TODO: 'ascii', 'graphical'

  -- automatically show the explainer when the cursor enters a regexp
  auto = false,

  -- filetypes (i.e. extensions) in which to run the autocommand
  filetypes = {
    'html',
    'js',
    'cjs',
    'mjs',
    'ts',
    'jsx',
    'tsx',
    'cjsx',
    'mjsx',
  },

  -- Whether to log debug messages
  debug = false, 

  -- 'split', 'popup'
  display = 'popup',

  mappings = {
    toggle = 'gR',
    -- examples, not defaults:
    -- show = 'gS',
    -- hide = 'gH',
    -- show_split = 'gP',
    -- show_popup = 'gU',
  },

  narrative = {
    separator = '\n',
  },
}

display

Regexplainer offers a small variety of display modes to suit your preferences.

Split Window

Set to split to display the explainer in a window below the editor. The window will be reused, and has the filetype Regexplainer

Popup Below Cursor

Set to popup (the default) to display the explainer in a popup below the cursor. When the cursor moves, the popup closes. if auto is set, the popup will automatically display whenever the cursor moves inside a regular expression You can call show with your own display type to override your config

require'regexplainer'.show { display = 'split' }

Or use the commands RegexplainerShowSplit or RegexplainerShowPopup. RegexplainerHide and RegexplainerToggle are also available.

You can customize the popup window by specifying options.popup.border, which is a table of popup options from nui. Any options specified for options.popup will also override the defaults.

require'regexplainer'.show {
  display = 'popup',
  popup = {
    border = {
      padding = { 1, 2 },
      style = 'solid',
    },
  },
}

You could use this to, for example, set a different border based on the state of your editor.

Render Options

narrative.separator can also be a function taking the current component and returning a string clause separator. For example, to separate clauses by a new line, followed by > for each level of capture-group depth, define the following function:

narrative = {
  separator = function(component)
    local sep = '\n';
    if component.depth > 0 then
      for _ = 1, component.depth do
        sep = sep .. '> '
      end
    end
    return sep
  end
},

Input:

/zero(one(two(?<inner>three)))/;

Output:

`zero`  
capture group 1:  
> `one`  
> capture group 2:  
> > `two`  
> > named capture group 3 `inner`:  
> > > `three`

Yank

You can yank the regexplanation into any register with the yank function. The default register is ". This can be useful if you'd like to share the explanation of a regexp with your teammates, or if you'd like to report a mistake in regexplainer. The argument to yank is either a string (the register to yank to) or a table with register: string and options to show (e.g. mode = 'narrative', narrative = {}, etc.).

For example, to copy the regexplanation to your system clipboard, use either of these:

require'regexplainer'.yank'+'
require'regexplainer'.yank { register = '+' }

You can also use the command RegexplainerYank

:RegexplainerYank +

🗃️ TODO list

nvim-regexplainer's People

Contributors

amaanq avatar axieax avatar bennypowers avatar kaniyasimeji avatar yutkat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nvim-regexplainer's Issues

Support for other languages

Currently I can only reproduce the examples in the readme, and failed to get the popup in other lang, like bash or lua? Is this plugin js/ts specific?

P.S. I had already install the regex bash lua ts parsers and set them in regexplainer's config

Not usable

Hello Guys,

i wanted to try this amazing plugin.

But calling any of the provided Regexplain functions as well as the autocmd don't work.
I tried creating files with supported filetypes and several keybindings formats.

Is this error just in the latest release?

Best regards

Missing escapes, alternation terms

User donbex at neovim discourse noted that some escape chars and alternation terms were missing from explanations

to wit:

let re = /@scope\/(.*)\.(?<extension>graphql|(?:t|j|cs)s)";?/;
                       ^                               ^

was missing the escaped . after the first anonymous capture group, and the s following the (cs) capture group

cannot get it to open - even on the test files.

I have just installed nvim-regexplainer but I cannot get it to actually show up.
I use lunarvim (nvim with config from lunarvim.org) but the config etc is the usual lua way:

{ 'bennypowers/nvim-regexplainer',
    requires = {
      'nvim-lua/plenary.nvim',
      'MunifTanjim/nui.nvim',
    },
    config = function() require'regexplainer'.setup({
   -- 'narrative'
  mode = 'narrative', -- TODO: 'ascii', 'graphical'

  -- automatically show the explainer when the cursor enters a regexp
  auto = true,

  -- Whether to log debug messages
  debug = false,

  -- 'split', 'popup'
  display = 'popup',

  mappings = {
    toggle = 'gR',
    -- examples, not defaults:
    -- show = 'gS',
    -- hide = 'gH',
    show_split = 'gP',
    show_popup = 'gU',
  },

  narrative = {
    separator = '\n',
  },
  })
  end,
}

I can see that it is loaded (the gR, gH etc mappings are available and also the :RegexplainerShow etc) but nothing happens when I call any of them. Nothing in :messages - even when I call :RegexplainerDebug

nvim version 0.7.0-dev

any idea?

Needs tests

The project has grown to the point that manual testing is no longer sufficient

We need automated tests that will cover at least the following:

  • testing narrative results for any given regexp
  • testing options like split, pop-up, display options

I've briefly glanced at busted but that seems insufficient on it's own, since so much of what the plugin does relies on treesitter and nui. What we need are proper integration tests.

Transparency with `winblend`

Hi, it would be nice to be able to set transparency of the popup window using vim.api.nvim_win_set_option(winnr, "winblend", ?), where ? can be passed in as a config option :))

`RegexplainerToggle` support

Hi, I think it'll be a very beneficial feature to be able to set one keybinding for both hiding and showing the explainer based on its current state, or at least having a default binding to be able to exit from open windows with <Esc>.

Can't show split if auto is set and display is popup

with this config, one can't show a split using :RegexplainerShowSplit

require'regexplainer'.setup {
  auto = true,
  display = 'popup',
}

I believe that what's happening is the split is open with the command, but that triggers the autocommand which closes the split, then reopens the popup

Remove nui dependency

I'd like to use this plugin but as someone averse to clutter installing nui.nvim for this single plugin just does not warrant it.

If you are ok with the addition of this feature I could make the PR myself.

Incorrect interpretation of a negated character list `[^p]`

Awesome idea!
I tried a simple regex in a scratch buffer:

let reggie = /^p[^p]*p/

Regexplainer incorrectly explains it like so:

START
`p`
One of ^, or p (>= 0x)
`p`

image

The first caret is correctly explained as the "start" anchor, but the second caret is interpreted as part of the character list.
The first caret inside square brackets actually negates the character list.

So maybe the output of regexplainer would be:

START
`p`
Any character not in the list: `p` (>= 0x)
`p`

And for a character list with multiple characters, e.g. /^p[^p^a]*p/:

START
`p`
Any character not in the list: `p`, `^`, `a` (>= 0x)
`p`

This is how regex101 treats that last example: https://regex101.com/r/mGQXlb/1

FR: Config to change the border style

regexplainer seems to use nui popups that always have the "shadow" border style. Since all my other popups have a different border style, it would be nice if they were consistent, meaning an option to change the popup border style would be very useful.

Rexexplainer: loop exceeded 1000 at node start_tag

in certain HTML files, Regexplainer can try too hard to find a pattern in start_tag, end_tag, or text nodes. I don't have a minimal repro, but check rh-audio-player detailed-transcript.html demo, particularly in the cue elements

\d\.\d\.\d

/elements@\d\.\d\.\d\/elements\/.*lightdom\.css$/;

EXPECTED:

`elements@`
**0-9**
`.`
**0-9**
`.`
**0-9**
`/elements/`
**ANY** (_>= 0x_)
`lightdom.css`
**END**

ACTUAL:

`elements@`
**d.**
**d.**
`\\d/elements/`
**ANY** (_>= 0x_)
`lightdom.css`
**END**

Backreferences

/(['"]).*?\1/g

EXPECTED:

capture group 1:
> One of `'`, or `"`
**ANY** (_>= 0x_) (_lazy_)
(capture group 1)

ACTUAL:

capture group 1:
> One of `'`, or `"`
**ANY** (_>= 0x_) (_lazy_)
`1`

auto-hide popup after manual show/toggle

Hi, love the plugin! Will make writing regex so much better. I'm wondering if this is either a bug or needs implementing; I want to have auto = false for the config, so I have to manually show the popup, but then when I move my cursor away on to a different line, it auto hides. Currently the popup persists until I hide it/toggle it off again. Not a deal breaker but more keystrokes than necessary!

Error about the auto option

It seems that when the initial position of the cursor is on the regular expression when the program starts, the auto option causes an error.

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.