Code Monkey home page Code Monkey logo

nvim-cmp-buffer-lines's Introduction

nvim-cmp Buffer Lines

Contents

Preview

Introduction

nvim-cmp Buffer Lines is a completion source for nvim-cmp that provides a source for all the lines in the current buffer. This is especially useful for C programmers. It uses tree-sitter if you have it installed on your system. tree-sitter is optional but recommended.

Installation

require "packer".startup(function(use)
    use "amarakon/nvim-cmp-buffer-lines"
end)

Setup

require "cmp".setup {
    sources = {
        {
            name = "buffer-lines",
            option = { … }
        }
    }
}

Options

Option Type Default Description
words Boolean false Include words
comments Boolean false Include comments
line_numbers Boolean false Include line numbers in the completion menu (does not apply on selection/confirmation)
line_number_separator String " " The separator between the line number and the line text (only used if line_numbers is set.
leading_whitespace Boolean true Include leading whitespace in the completion menu (does not apply on selection/confirmation)
max_indents Number 0 Maximum indentation level lines can be shown (0-indexed). For example, lines with one or more indents will not be shown when this is set to 1. Set to 0 to show an unlimited amount of indents.
max_size Number 100 Maximum file size (in kB) for which this plugin will be activated

Command-line

You can use this source for searching for patterns in the command-line. I recommend using it in conjunction with cmp-buffer for a bread-and-butter combination. The following code block is the configuration I use and recommend.

-- Enable `buffer` and `buffer-lines` for `/` and `?` in the command-line
require "cmp".setup.cmdline({ "/", "?" }, {
    mapping = require "cmp".mapping.preset.cmdline(),
    sources = {
        {
            name = "buffer",
            option = { keyword_pattern = [[\k\+]] }
        },
        { name = "buffer-lines" }
    }
})

Only for certain file types

-- Only enable `buffer-lines` for C and C++
require "cmp".setup.filetype({ "c", "cpp" }, {
    sources = {
        { name = "buffer-lines" }
    }
})

TODO

  • Automatically update the source
  • Cut comments from lines
    • Test it to prove it works in all use cases
    • Find a more efficient implementation with tree-sitter or LSP (Language Server Protocol)
  • Omit duplicate lines
  • Add an option to show line numbers
  • Show indentation level in the completion menu, but not when selecting or confirming
  • Add an option to choose the maximum indentation level lines will be shown
  • Make the plugin more efficient for editing large files
  • Add an option to set a file size limit
  • Add configuration options
  • Add syntax highlighting
  • Use the current buffer instead of buffer 0
    • Add an option to use all buffers

nvim-cmp-buffer-lines's People

Contributors

amarakon avatar tummetott 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

Watchers

 avatar  avatar

nvim-cmp-buffer-lines's Issues

Plugin does not work in files where treesitter is not enabled if treesitter is installed

In contrast to my last issues (sorry for spamming your repo with issues xD ) I have treesitter installed currently.
Then I open a boring text file without file type, and when I now press ^X^L with this configuration:

                    ['<C-x><C-l>'] = cmp.mapping.complete({ -- manual ^X^L is broken,
                        config = {
                            sources = {
                                { name = 'buffer-lines' },
                                option = {
                                    words = false, -- include (true) or skip (false) lines that are a single word
                                    comments = true, -- include (true) or strip (false) comments on insertion (probably only trailing comments? like `command --comment`)
                                    leading_whitespace = true, -- show leading whitespace, is stripped on buffer insertion in any case.
                                    max_size = 100000, -- kB
                                },
                            },
                        },
                    }),

I get the following error message:

E5108: Error executing lua: Failed to load parser: uv_dlopen: /home/bagohart/.local/share/nvim/site/pack/packer/start/nvim-treesitter/parser/.: cannot read file data: Is a directory
stack traceback:
	[C]: in function '_ts_add_language'
	/usr/share/nvim/runtime/lua/vim/treesitter/language.lua:36: in function 'require_language'
	/usr/share/nvim/runtime/lua/vim/treesitter.lua:38: in function '_create_parser'
	/usr/share/nvim/runtime/lua/vim/treesitter.lua:96: in function 'get_parser'
	/usr/share/nvim/runtime/lua/vim/treesitter.lua:287: in function 'get_node_at_pos'
	.../nvim-cmp-buffer-lines/lua/cmp-buffer-lines/complete.lua:31: in function 'rmcomments'
	.../nvim-cmp-buffer-lines/lua/cmp-buffer-lines/complete.lua:110: in function 'generate'
	.../nvim-cmp-buffer-lines/lua/cmp-buffer-lines/complete.lua:158: in function 'complete'
	...re/nvim/site/pack/packer/opt/nvim-cmp/lua/cmp/source.lua:325: in function 'complete'
	...hare/nvim/site/pack/packer/opt/nvim-cmp/lua/cmp/core.lua:289: in function 'complete'
	...hare/nvim/site/pack/packer/opt/nvim-cmp/lua/cmp/init.lua:77: in function 'complete'
	...site/pack/packer/opt/nvim-cmp/lua/cmp/config/mapping.lua:113: in function 'on_keymap'
	...hare/nvim/site/pack/packer/opt/nvim-cmp/lua/cmp/core.lua:145: in function 'callback'
	...m/site/pack/packer/opt/nvim-cmp/lua/cmp/utils/keymap.lua:133: in function <...m/site/pack/packer/opt/nvim-cmp/lua/cmp/utils/keymap.lua:127>

Looks like this plugin tries to use treesitter when it's installed even if it's not active for the current filetype.
An obvious workaround is to only enable this plugin for certain filetypes (the readme describes how) but I would expect it in this case to just not use treesitter.
I'm new to treesitter, so I have no idea if there's an easy way to query it if it's active in the current buffer, but I would assume there should be. Maybe like this?
https://neovim.discourse.group/t/check-if-treesitter-is-enabled-in-the-current-buffer/902

A way to set the size of the line

I tried this and it's great but the size of the lines makes the popup too big and the doc popup is squashed on the left.
Is there a way to set (here or in the cmp config) a maximum number of characters to contain the popup size?

[Function request] Automatic escape of pattern characters in lines on selection

For example, if I want to search for line 115 in the document, I should use very nomagic
\V[system="Linux" class="Alacritty" modifier="(AltLeft ShiftLeft)"] or \[system="Linux" class="Alacritty" modifier="(AltLeft ShiftLeft)"] Instead of [system="Linux" class="Alacritty" modifier="(AltLeft ShiftLeft)"]

#TODO 支持向进程的标准输入发送字符串,比如向终端发送字符串【ctrl-shift-c】,
#然后在vim中就可以映射: map 《c-s-c》 key/command
#查看键码的网站
#https://www.toptal.com/developers/keycode/for/a

#F系列键的终端ascii转义序列
KF1 = Escape Shift{O} Shift{P}
KF2 = Escape Shift{O} Shift{Q}
KF3 = Escape Shift{O} Shift{R}
KF4 = Escape Shift{O} Shift{S}
KF5 = Escape BracketLeft 1 5 Shift{Backquote}
KF6 = Escape BracketLeft 1 7 Shift{Backquote}
KF7 = Escape BracketLeft 1 8 Shift{Backquote}
KF8 = Escape BracketLeft 1 9 Shift{Backquote}
KF9 = Escape BracketLeft 2 0 Shift{Backquote}
KF10 = Escape BracketLeft 2 1 Shift{Backquote}
KF11 = Escape BracketLeft 2 3 Shift{Backquote}
KF12 = Escape BracketLeft 2 4 Shift{Backquote}
KF13 = Escape BracketLeft 1 Semicolon 2 Shift{P}
KF14 = Escape BracketLeft 1 Semicolon 2 Shift{Q}
KF15 = Escape BracketLeft 1 Semicolon 2 Shift{R}
KF16 = Escape BracketLeft 1 Semicolon 2 Shift{S}
KF17 = Escape BracketLeft 1 5 Semicolon 2 Shift{Backquote}
KF18 = Escape BracketLeft 1 7 Semicolon 2 Shift{Backquote}
KF19 = Escape BracketLeft 1 8 Semicolon 2 Shift{Backquote}
KF20 = Escape BracketLeft 1 9 Semicolon 2 Shift{Backquote}
KF21 = Escape BracketLeft 2 0 Semicolon 2 Shift{Backquote}
KF22 = Escape BracketLeft 2 1 Semicolon 2 Shift{Backquote}
KF23 = Escape BracketLeft 2 3 Semicolon 2 Shift{Backquote}
KF24 = Escape BracketLeft 2 4 Semicolon 2 Shift{Backquote}
KF25 = Escape BracketLeft 1 Semicolon 5 Shift{P}
KF26 = Escape BracketLeft 1 Semicolon 5 Shift{Q}
KF27 = Escape BracketLeft 1 Semicolon 5 Shift{R}
KF28 = Escape BracketLeft 1 Semicolon 5 Shift{S}
KF29 = Escape BracketLeft 1 5 Semicolon 5 Shift{Backquote}
KF30 = Escape BracketLeft 1 7 Semicolon 5 Shift{Backquote}
KF31 = Escape BracketLeft 1 8 Semicolon 5 Shift{Backquote}
KF32 = Escape BracketLeft 1 9 Semicolon 5 Shift{Backquote}
KF33 = Escape BracketLeft 2 0 Semicolon 5 Shift{Backquote}
KF34 = Escape BracketLeft 2 1 Semicolon 5 Shift{Backquote}
KF35 = Escape BracketLeft 2 3 Semicolon 5 Shift{Backquote}
KF36 = Escape BracketLeft 2 4 Semicolon 5 Shift{Backquote}
KF37 = Escape BracketLeft 1 Semicolon 6 Shift{P}
KF38 = Escape BracketLeft 1 Semicolon 6 Shift{Q}
KF39 = Escape BracketLeft 1 Semicolon 6 Shift{R}
KF40 = Escape BracketLeft 1 Semicolon 6 Shift{S}
KF41 = Escape BracketLeft 1 5 Semicolon 6 Shift{Backquote}
KF42 = Escape BracketLeft 1 7 Semicolon 6 Shift{Backquote}
KF43 = Escape BracketLeft 1 8 Semicolon 6 Shift{Backquote}
KF44 = Escape BracketLeft 1 9 Semicolon 6 Shift{Backquote}
KF45 = Escape BracketLeft 2 0 Semicolon 6 Shift{Backquote}
KF46 = Escape BracketLeft 2 1 Semicolon 6 Shift{Backquote}
KF47 = Escape BracketLeft 2 3 Semicolon 6 Shift{Backquote}
KF48 = Escape BracketLeft 2 4 Semicolon 6 Shift{Backquote}
KF49 = Escape BracketLeft 1 Semicolon 3 Shift{P}
KF50 = Escape BracketLeft 1 Semicolon 3 Shift{Q}
KF51 = Escape BracketLeft 1 Semicolon 3 Shift{R}
KF52 = Escape BracketLeft 1 Semicolon 3 Shift{S}
KF53 = Escape BracketLeft 1 5 Semicolon 3 Shift{Backquote}
KF54 = Escape BracketLeft 1 7 Semicolon 3 Shift{Backquote}
KF55 = Escape BracketLeft 1 8 Semicolon 3 Shift{Backquote}
KF56 = Escape BracketLeft 1 9 Semicolon 3 Shift{Backquote}
KF57 = Escape BracketLeft 2 0 Semicolon 3 Shift{Backquote}
KF58 = Escape BracketLeft 2 1 Semicolon 3 Shift{Backquote}
KF59 = Escape BracketLeft 2 3 Semicolon 3 Shift{Backquote}
KF60 = Escape BracketLeft 2 4 Semicolon 3 Shift{Shift{Backquote}}
KF61 = Escape BracketLeft 1 Semicolon 4 Shift{P}
KF62 = Escape BracketLeft 1 Semicolon 4 Shift{Q}
KF63 = Escape BracketLeft 1 Semicolon 4 Shift{R}

[system="Linux" class="Alacritty" modifier="(MetaLeft ShiftLeft)"]
H >> KF63
J >> KF62
K >> KF61
L >> KF60
Equal >> KF59
Minus >> KF58
Enter >> KF57
Period >> KF56

[system="Linux" class="Alacritty" modifier="MetaLeft"]
H >> KF55
J >> KF54
K >> KF53
L >> KF52
U >> KF51
Equal >> KF50
Minus >> KF49
0 >> KF48
Enter >> KF47
Period >> KF46
O >> KF45

[system="Linux" class="Alacritty" modifier="(ControlLeft ShiftLeft)"]
Semicolon >> KF44
Comma >> KF43
Period >> KF42

[system="Linux" class="Alacritty"]
(ControlLeft ShiftLeft){Any} >> (ControlLeft ShiftLeft){Any}
(ControlLeft ShiftRight){Any} >> (ControlLeft ShiftRight){Any}
#BUG 下面映射理论上可以替代上面的两条映射,但是不起作用
#(ControlLeft Any){Any} >> (ControlLeft Any){Any}

[system="Linux" class="Alacritty" modifier="ControlLeft"]
Semicolon >> KF41
Comma >> KF40
Enter >> KF39
Period >> KF38
BracketLeft >> KF37
Quote >> KF36
Space >> KF35
Any >> ControlLeft{Any}

[system="Linux" class="Alacritty" modifier="(AltLeft ShiftLeft)"]
Semicolon >> KF34
Comma >> KF33

[system="Linux" class="Alacritty"]
(AltLeft ShiftLeft){Any} >> (AltLeft ShiftLeft){Any}
(AltLeft ShiftRight){Any} >> (AltLeft ShiftRight){Any}

[system="Linux" class="Alacritty" modifier="(AltLeft Backquote)"]
Enter >> KF32
Space >> KF31
#J >> KF30

[system="Linux" class="Alacritty" modifier="(AltLeft CapsLock)"]
J >> KF9
K >> KF8

[system="Linux" class="Alacritty" modifier="CapsLock"]
P >> KF7
J >> KF6
K >> KF5
N >> KF4

[system="Linux" class="Alacritty" modifier="AltLeft"]
Semicolon >> KF29
Comma >> KF28
Enter >> KF27
Period >> KF26
Quote >> KF25
Space >> KF24
Any >> AltLeft{Any}

[system="Linux" class="Alacritty" modifier="Backquote"]
Enter >> KF23
Space >> KF22
J >> KF21
K >> KF20
L >> KF19
U >> KF18
Period >> KF17
R >> KF16
V >> KF15
O >> KF14

#BUG 在中文输入法下shiftleft+, shiftleft+. 有问题
[system="Linux" class="Alacritty" modifier="ShiftLeft"]
# Enter >> KF13
Space >> KF12
Any >> ShiftLeft{Any}

[system="Linux" class="Alacritty"]
#TODO 实现类似xcape的效果,在按住超过500ms后就不发送了
ControlLeft >> KF11
AltLeft >> KF10

#warpd
[system="Linux" modifier="CapsLock"]
H >> (AltLeft MetaLeft){C}
J >> (AltLeft MetaLeft){C}
K >> (AltLeft MetaLeft){C}
L >> (AltLeft MetaLeft){C}
N >> (AltLeft MetaLeft){C}
P >> (AltLeft MetaLeft){C}
M >> (AltLeft MetaLeft){X}
O >> (AltLeft MetaLeft){L}
Semicolon >> (AltLeft MetaLeft){H}
I >> (AltLeft MetaLeft){S}

[default]
CapsLock{Space} >> CapsLock
CapsLock >> Escape
Escape >> CapsLock
Space{Any} >> MetaLeft{Any}
(Space ShiftLeft){Any} >> (MetaLeft ShiftLeft){Any}

cmp-buffer-lines breaks LSP

I have installed cmp-buffer-lines with:

use ({"amarakon/nvim-cmp-buffer-lines", config = function()
	require("setup/buffer-lines")
end})

setup/buffer-lines.lua looks like:

require"cmp".setup {
	sources = {
		{ name = "buffer-lines" }
	}
}

LSP servers stop autocomplete your code.
In pyright, it displays errors, and warnings for unused variables, but it doesn't autocomplete the code.
In clangd, it doesn't see functions from STL, and sees cout as an error (despite the fact gcc has no problem to compile it).

Tested with clangd and pyright servers.
Here is my config: https://github.com/redve-dev/dotfiles/tree/master/nvim/lua
Tested with neovim v0.9.0

Fails on lines with indentation.

I am trying to use this plugin to replace the default .

In this example, the plugin does not give any suggestions. (This is also my config)
image

At the beginning of the line, it works like a charm.
image

I think It would be better to ignore whitespaces/indetations at the start of the line.

Plugin crashes because treesitter is not available.

Hi,
I just tried this plugin because nvim-cmp breaks manual ^X^L completion for me.
I don't use treesitter.
I configured it like this:

                    ['<C-x><C-l>'] = cmp.mapping.complete({ -- manual ^X^L is broken, so use this instead...
                        config = {
                            sources = {
                                { name = 'buffer-lines' },
                                option = {
                                    words = true,
                                    comments = true,
                                    leading_whitespace = false,
                                },
                            },
                        },
                    }),

When I press ^X^L I get:

E5108: Error executing lua: .../nvim-cmp-buffer-lines/lua/cmp-buffer-lines/complete.lua:19: module 'nvim-treesitter.ts_utils' not found:
	no field package.preload['nvim-treesitter.ts_utils']
	no file './nvim-treesitter/ts_utils.lua'
	no file '/usr/share/luajit-2.1.0-beta3/nvim-treesitter/ts_utils.lua'
	no file '/usr/local/share/lua/5.1/nvim-treesitter/ts_utils.lua'
	no file '/usr/local/share/lua/5.1/nvim-treesitter/ts_utils/init.lua'
	no file '/usr/share/lua/5.1/nvim-treesitter/ts_utils.lua'
	no file '/usr/share/lua/5.1/nvim-treesitter/ts_utils/init.lua'
	no file '/home/felix/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/nvim-treesitter/ts_utils.lua'
	no file '/home/felix/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/nvim-treesitter/ts_utils/init.lua'
	no file '/home/felix/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/nvim-treesitter/ts_utils.lua'
	no file '/home/felix/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/nvim-treesitter/ts_utils/init.lua'
	no file './nvim-treesitter/ts_utils.so'
	no file '/usr/local/lib/lua/5.1/nvim-treesitter/ts_utils.so'
	no file '/usr/lib/lua/5.1/nvim-treesitter/ts_utils.so'
	no file '/usr/local/lib/lua/5.1/loadall.so'
	no file '/home/felix/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/lua/5.1/nvim-treesitter/ts_utils.so'
	no file './nvim-treesitter.so'
	no file '/usr/local/lib/lua/5.1/nvim-treesitter.so'
	no file '/usr/lib/lua/5.1/nvim-treesitter.so'
	no file '/usr/local/lib/lua/5.1/loadall.so'
	no file '/home/felix/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/lua/5.1/nvim-treesitter.so'
stack traceback:
	[C]: in function 'require'
	.../nvim-cmp-buffer-lines/lua/cmp-buffer-lines/complete.lua:19: in function 'rmcomments'
	.../nvim-cmp-buffer-lines/lua/cmp-buffer-lines/complete.lua:110: in function 'generate'
	.../nvim-cmp-buffer-lines/lua/cmp-buffer-lines/complete.lua:158: in function 'complete'
	...re/nvim/site/pack/packer/opt/nvim-cmp/lua/cmp/source.lua:325: in function 'complete'
	...hare/nvim/site/pack/packer/opt/nvim-cmp/lua/cmp/core.lua:289: in function 'complete'
	...hare/nvim/site/pack/packer/opt/nvim-cmp/lua/cmp/init.lua:77: in function 'complete'
	...site/pack/packer/opt/nvim-cmp/lua/cmp/config/mapping.lua:113: in function 'on_keymap'
	...hare/nvim/site/pack/packer/opt/nvim-cmp/lua/cmp/core.lua:145: in function 'callback'
	...m/site/pack/packer/opt/nvim-cmp/lua/cmp/utils/keymap.lua:133: in function <...m/site/pack/packer/opt/nvim-cmp/lua/cmp/utils/keymap.lua:127>

My suspicion is that the condition in complete.lua is wrong:

local function rmcomments(nmbr, line)
	-- Use tree-sitter if is installed
	if not (vim.fn.exists(":TSUpdate") >= 2) then
		local ts_utils = require "nvim-treesitter.ts_utils"

Cyrillic chars in lines.

Hello! This is a great plugin for C programming. But it does not support lines with Cyrillic, only if I press <C>-<X>-o manually.

Some lines will not be displayed

Please see, The first line of the first document is not shown in the complementary menu and the third line of the second document is not shown in the complementary menu

gg
zzz

mini config

local buffer_lines = {
    name = "buffer-lines",
    option = {
        comments = true,
    },
}
local global_source = {
    buffer_lines,
}
cmp.setup({
    sources = global_source,
})

document

    if ₁ = ₂ then
end
        abcdefg


end
        abcdefg

[system="Linux" class="Alacritty" modifier="(MetaLeft ShiftLeft)"]


Only trigger line-completion with CTRL-X CTRL-L

Hey there,

I'm wondering if it's possible to turn off automatic completion for this cmp source and instead activate it only when using a special key combination, like the native CTRL-X CTRL-L. When I trigger it manually, I'd like to see only completions from this source, and I don't want any other cmp sources to appear in the popup menu.

Thanks!

How to use this plugin with large files?

This plugin works now, but not for large files.
I don't care if it's not very fast, but it should at least work at all.
I press <C-x><C-l> and then nothing happens if the file is large. (2530 lines)
This is my configuration:

                    ['<C-x><C-l>'] = cmp.mapping.complete({ -- manual ^X^L is broken,↴
                        config = {
                            sources = {
                                { name = 'buffer-lines' },
                                option = {
                                    words = true, -- not sure what this does...
                                    comments = true, -- not sure what this does...
                                    leading_whitespace = true, -- not sure. is stripped on buffer insertion anyway.
                                    max_size = 10000000, -- kB
                                },
                            },
                        },
                    }),

This line should govern execution conditional on file size:

if vim.fn.getfsize(vim.fn.expand("%")) > (opts.max_size * 1000) then
		return
	end

and the output of getfsize(expand("%")) is 117943, so the whole expression should evaluate to false.

I also tried to set

            cmp.setup({
                performance = {
                    fetching_timeout = 100000,
                },
-- ...

but it didn't make any difference.

Is there a way to make this work?

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.