diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7706c09896..e6ae7a5daa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,4 +1,10 @@ -# Documentation +# Contributing + +## Submitting a new feature + +Thanks for taking the time to submit code to Telescope if you're reading this! We love having new contributors and love seeing the Neovim community come around this plugin and keep making it better If you are submitting a new PR with a feature addition, please make sure that you add the appropriate documentation. For examples of how to document a new picker for instance, check the `lua/telescope/builtin/init.lua` file to see how we write function headers for all of the pickers there. To learn how we go about writing documentation for this project, keep reading below! + +## Documentation with treesitter We are generating docs based on the tree sitter syntax tree. TJ wrote a grammar that includes the documentation in this syntax tree so we can do take this function header documentation and transform it into vim documentation. All documentation that is part of the returning module will be exported. So example: diff --git a/README.md b/README.md index 29b28c2bfd..d48a525b78 100644 --- a/README.md +++ b/README.md @@ -245,7 +245,7 @@ Many familiar mapping patterns are setup as defaults. | `/` | Next item | | `/` | Previous item | | `j/k` | Next/previous (in normal mode) | -| `` | Confirm selection | +| `` | Confirm selection | | `` | go to file selection as a split | | `` | go to file selection as a vsplit | | `` | go to a file in a new tab | @@ -292,10 +292,10 @@ require('telescope').setup{ [""] = actions.select_horizontal, -- Add up multiple actions - [""] = actions.select_default + actions.center, + [""] = actions.select_default + actions.center, -- You can perform as many actions in a row as you like - [""] = actions.select_default + actions.center + my_cool_custom_action, + [""] = actions.select_default + actions.center + my_cool_custom_action, }, n = { [""] = actions.close, @@ -366,13 +366,13 @@ Built-in functions. Ready to be bound to any key you like. :smile: ### File Pickers -| Functions | Description | -|-------------------------------------|---------------------------------------------------------------------------------------------| -| `builtin.find_files` | Lists Files in current directory. | -| `builtin.git_files` | Lists Git files in current directory. | -| `builtin.grep_string` | Searches for a string under the cursor in current directory. | -| `builtin.live_grep` | Searches in current directory files. (respecting .gitignore) | -| `builtin.file_browser` | Ivy-like file explorer. Creates files by typing in filename and pressing ``. Press `` without prompt for more info | +| Functions | Description | +|-------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------| +| `builtin.find_files` | Lists files in your current working directory, respects .gitignore | +| `builtin.git_files` | Fuzzy search through the output of `git ls-files` command, respects .gitignore, optionally ignores untracked files | +| `builtin.grep_string` | Searches for the string under your cursor in your current working directory | +| `builtin.live_grep` | Search for a string in your current working directory and get results live as you type (respecting .gitignore) | +| `builtin.file_browser` | Lists files and folders in your current working directory, open files, navigate your filesystem, and create new files and folders | #### Options for builtin.live_grep @@ -381,53 +381,65 @@ Built-in functions. Ready to be bound to any key you like. :smile: | `grep_open_files` | Restrict live_grep to currently open files, mutually exclusive with `search_dirs` | boolean | | `search_dirs` | List of directories to search in, mutually exclusive with `grep_open_files` | list | - ### Vim Pickers -| Functions | Description | -|-------------------------------------|---------------------------------------------------------------------------------------------| -| `builtin.buffers` | Lists Open buffers in the current vim instance. | -| `builtin.oldfiles` | Lists Previously open files. | -| `builtin.commands` | Lists Available plugin/user commands and run it. | -| `builtin.tags` | Lists Tags in current directory with preview (ctags -R) | -| `builtin.command_history` | Lists Commands previously ran and run it on enter. | -| `builtin.help_tags` | Lists Available help tags and open help document. | -| `builtin.man_pages` | Lists Man entries. | -| `builtin.marks` | Lists Markers and their value. | -| `builtin.colorscheme` | Lists Colorscheme and switch to it on enter. | -| `builtin.quickfix` | Lists items from quickfix. | -| `builtin.loclist` | Lists items from current window's location list. | -| `builtin.vim_options` | Lists vim options and on enter edit the options value. | -| `builtin.registers` | Lists vim registers and edit or paste selection. | -| `builtin.autocommands` | Lists vim autocommands and go to their declaration. | -| `builtin.spell_suggest` | Lists spelling suggestions for . | -| `builtin.keymaps` | Lists normal-mode mappings. | -| `builtin.filetypes` | Lists all filetypes. | -| `builtin.highlights` | Lists all highlights. | -| `builtin.current_buffer_fuzzy_find` | Searches in current buffer lines. | -| `builtin.current_buffer_tags` | Lists Tags in current buffer. | - -### LSP Pickers - -| Functions | Description | -|-------------------------------------|---------------------------------------------------------------------------------------------| -| `builtin.lsp_references` | Searches in LSP references. | -| `builtin.lsp_document_symbols` | Searches in LSP Document Symbols in the current document. | -| `builtin.lsp_workspace_symbols` | Searches in LSP all workspace symbols. | -| `builtin.lsp_code_actions` | Lists LSP action to be trigged on enter. | -| `builtin.lsp_range_code_actions` | Lists LSP range code action to be trigged on enter. | -| `builtin.lsp_document_diagnostics` | Lists LSP Diagnostics in the current document. | -| `builtin.lsp_workspace_diagnostics` | Lists LSP Diagnostics in the workspace if supported and otherwise open buffers. | -| `builtin.lsp_definitions` | Goto definition if there is only one. If there are multiple, open them up in telescope | +| Functions | Description | +|-------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `builtin.buffers` | Lists open buffers in current neovim instance | +| `builtin.oldfiles` | Lists previously open files | +| `builtin.commands` | Lists available plugin/user commands and runs them on `` | +| `builtin.tags` | Lists tags in current directory with tag location file preview (users are required to run ctags -R to generate tags or update when introducing new changes) | +| `builtin.command_history` | Lists commands that were executed recently, and reruns them on `` | +| `builtin.search_history` | Lists searches that were executed recently, and reruns them on `` | +| `builtin.help_tags` | Lists available help tags and opens a new window with the relevant help info on `` | +| `builtin.man_pages` | Lists manpage entries, opens them in a help window on `` | +| `builtin.marks` | Lists vim marks and their value | +| `builtin.colorscheme` | Lists available colorschemes and applies them on `` | +| `builtin.quickfix` | Lists items in the quickfix list | +| `builtin.loclist` | Lists items from the current window's location list | +| `builtin.vim_options` | Lists vim options, allows you to edit the current value on `` | +| `builtin.registers` | Lists vim registers, pastes the contents of the register on `` | +| `builtin.autocommands` | Lists vim autocommands and goes to their declaration on `` | +| `builtin.spell_suggest` | Lists spelling suggestions for the current word under the cursor, replaces word with selected suggestion on `` | +| `builtin.keymaps` | Lists normal mode keymappings | +| `builtin.filetypes` | Lists all available filetypes | +| `builtin.highlights` | Lists all available highlights | +| `builtin.current_buffer_fuzzy_find` | Live fuzzy search inside of the currently open buffer | +| `builtin.current_buffer_tags` | Lists all of the tags for the currently open buffer, with a preview | + +### Neovim LSP Pickers + +| Functions | Description | +|---------------------------------------------|-------------------------------------------------------------------------------------------------------------------| +| `builtin.lsp_references` | Lists LSP references for word under the cursor | +| `builtin.lsp_document_symbols` | Lists LSP document symbols in the current buffer | +| `builtin.lsp_workspace_symbols` | Lists LSP document symbols in the current workspace | +| `builtin.lsp_dynamic_workspace_symbols` | Dynamically Lists LSP for all workspace symbols | +| `builtin.lsp_code_actions` | Lists any LSP actions for the word under the cursor, that can be triggered with `` | +| `builtin.lsp_range_code_actions` | Lists any LSP actions for a given range, that can be triggered with `` | +| `builtin.lsp_document_diagnostics` | Lists LSP diagnostics for the current buffer | +| `builtin.lsp_workspace_diagnostics` | Lists LSP diagnostics for the current workspace if supported, otherwise searches in all open buffers | +| `builtin.lsp_implementations` | Goto the implementation of the word under the cursor if there's only one, otherwise show all options in Telescope | +| `builtin.lsp_definitions` | Goto the definition of the word under the cursor, if there's only one, otherwise show all options in Telescope | + +#### Pre-filtering option for LSP pickers + +For the `*_symbols` and `*_diagnostics` LSP pickers, there is a special filtering that you can use to specify your +search. When in insert mode while the picker is open, type `:` and then press `` to get an autocomplete menu +filled with all of the possible filters you can use. + +i.e. while using the `lsp_document_symbols` picker, adding `:methods:` to your query filters out any document +document symbols that are not recognized as methods by treesitter. ### Git Pickers -| Functions | Description | -|-------------------------------------|---------------------------------------------------------------------------------------------| -| `builtin.git_commits` | Lists git commits with diff preview and on enter checkout the commit. | -| `builtin.git_bcommits` | Lists buffer's git commits with diff preview and checkouts it out on enter. | -| `builtin.git_branches` | Lists all branches with log preview, checkout action (), track action () and rebase action(). | -| `builtin.git_status` | Lists current changes per file with diff preview and add action. (Multi-selection still WIP) | +| Functions | Description | +|-------------------------------------|---------------------------------------------------------------------------------------------------------------| +| `builtin.git_commits` | Lists git commits with diff preview and on enter checkout the commit. | +| `builtin.git_bcommits` | Lists buffer's git commits with diff preview and checkouts it out on enter. | +| `builtin.git_branches` | Lists all branches with log preview, checkout action ``, track action `` and rebase action`` | +| `builtin.git_status` | Lists current changes per file with diff preview and add action. (Multi-selection still WIP) | +| `builtin.git_stash` | Lists stash items in current repository with ability to apply them on `` | ### Treesitter Picker @@ -437,11 +449,11 @@ Built-in functions. Ready to be bound to any key you like. :smile: ### Lists Picker -| Functions | Description | -|-------------------------------------|---------------------------------------------------------------------------------------------| -| `builtin.planets` | Use the telescope. | -| `builtin.builtin` | Lists Built-in pickers and run them on enter. | -| `builtin.reloader` | Lists lua modules and reload them on enter. | +| Functions | Description | +|-------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `builtin.planets` | Use the telescope... | +| `builtin.builtin` | Lists Built-in pickers and run them on enter. | +| `builtin.reloader` | Lists lua modules and reload them on enter. | | `builtin.symbols` | Lists symbols inside a file `data/telescope-sources/*.json` found in your rtp. More info and symbol sources can be found [here](https://github.com/nvim-telescope/telescope-symbols.nvim) | ## Previewers @@ -607,8 +619,8 @@ function my_custom_picker(results) finder = finders.new_table(results), sorter = sorters.fuzzy_with_index_bias(), attach_mappings = function(_, map) - -- Map "" in insert mode to the function, actions.set_command_line - map('i', '', actions.set_command_line) + -- Map "" in insert mode to the function, actions.set_command_line + map('i', '', actions.set_command_line) -- If the return value of `attach_mappings` is true, then the other -- default mappings are still applies. @@ -683,11 +695,11 @@ and some other functions can be easily changed in custom pickers or built-in fun ```lua -- Disable preview for find files -nnoremap ff :lua require('telescope.builtin').find_files({previewer = false}) +nnoremap ff :lua require('telescope.builtin').find_files({previewer = false}) -- Change change prompt prefix for find_files builtin function: -nnoremap fg :lua require('telescope.builtin').live_grep({ prompt_prefix=🔍 }) -nnoremap fg :Telescope live_grep prompt_prefix=🔍 +nnoremap fg :lua require('telescope.builtin').live_grep({ prompt_prefix=🔍 }) +nnoremap fg :Telescope live_grep prompt_prefix=🔍 ``` ### How to change Telescope Highlights group? @@ -724,3 +736,9 @@ To checkout the default values of the highlight groups, checkout `plugin/telesco All contributions are welcome! Just open a pull request. Please read [CONTRIBUTING.md](./CONTRIBUTING.md) + +## Related Projects + +- [fzf.vim](https://github.com/junegunn/fzf.vim) +- [denite.nvim](https://github.com/Shougo/denite.nvim) +- [vim-clap](https://github.com/liuchengxu/vim-clap) diff --git a/doc/telescope.txt b/doc/telescope.txt index 02fe61ac45..56cb6c8ab2 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -92,22 +92,54 @@ telescope.extensions() *telescope.extensions()* ================================================================================ - *telescope.builtin* + *telescope.actions.set* -A collection of builtin pickers for telescope. +Telescope action sets are used to provide an interface for managing actions +that all primarily do the same thing, but with slight tweaks. -Meant for both example and for easy startup. +For example, when editing files you may want it in the current split, a +vertical split, etc. Instead of making users have to overwrite EACH of those +every time they want to change this behavior, they can instead replace the +`set` itself and then it will work great and they're done. -Any of these functions can just be called directly by doing: +action_set.shift_selection({prompt_bufnr}, {change})*action_set.shift_selection()* + Move the current selection of a picker {change} rows. Handles not + overflowing / underflowing the list. -:lua require('telescope.builtin').$NAME() -This will use the default configuration options. Other configuration options -are still in flux at the moment + Parameters: ~ + {prompt_bufnr} (number) The prompt bufnr + {change} (number) The amount to shift the selection by + + +action_set.select({prompt_bufnr}, {type}) *action_set.select()* + Select the current entry. This is the action set to overwrite common + actions by the user. + + By default maps to editing a file. + + + Parameters: ~ + {prompt_bufnr} (number) The prompt bufnr + {type} (string) The type of selection to make + + +action_set.edit({prompt_bufnr}, {command}) *action_set.edit()* + Edit a file based on the current selection. + + + Parameters: ~ + {prompt_bufnr} (number) The prompt bufnr + {command} (string) The command to use to open the file. + + +action_set.scroll_previewer({prompt_bufnr}, {direction})*action_set.scroll_previewer()* + Scrolls the previewer up or down -builtin.live_grep() *builtin.live_grep()* - Live grep means grep as you type. + Parameters: ~ + {prompt_bufnr} (number) The prompt bufnr + {direction} (number) The direction of the scrolling @@ -196,12 +228,630 @@ actions.toggle_selection({prompt_bufnr}) *actions.toggle_selection()* {prompt_bufnr} (number) The prompt bufnr +actions.git_create_branch({prompt_bufnr}) *actions.git_create_branch()* + Create and checkout a new git branch if it doesn't already exist + + + Parameters: ~ + {prompt_bufnr} (number) The prompt bufnr + + +actions.git_apply_stash({prompt_bufnr}) *actions.git_apply_stash()* + Applies an existing git stash + + + Parameters: ~ + {prompt_bufnr} (number) The prompt bufnr + + +actions.git_checkout({prompt_bufnr}) *actions.git_checkout()* + Checkout an existing git branch + + + Parameters: ~ + {prompt_bufnr} (number) The prompt bufnr + + +actions.git_track_branch({prompt_bufnr}) *actions.git_track_branch()* + Tell git to track the currently selected remote branch in Telescope + + + Parameters: ~ + {prompt_bufnr} (number) The prompt bufnr + + actions.open_qflist() *actions.open_qflist()* Open the quickfix list +================================================================================ + *telescope.builtin* + +Telescope Builtins is a collection of community maintained pickers to support +common workflows. It can be used as reference when writing PRs, Telescope +extensions, your own custom pickers, or just as a discovery tool for all of the +amazing pickers already shipped with Telescope! + +Any of these functions can just be called directly by doing: + +:lua require('telescope.builtin').$NAME_OF_PICKER() + +To use any of Telescope's default options or any picker-specific options, call +your desired picker by passing a lua table to the picker with all of the +options you want to use. Here's an example with the live_grep picker: + +:lua require('telescope.builtin').live_grep({ + prompt_title = 'find string in open buffers...', + grep_open_files = true + }) + +This will use the default configuration options. Other configuration options +are still in flux at the moment + +builtin.live_grep({opts}) *builtin.live_grep()* + Search for a string in your current working directory and get results live + as you type (respecting .gitignore) + + + Parameters: ~ + {opts} (table) options to pass to the picker + + Fields: ~ + {grep_open_files} (boolean) if true, restrict search to open files + only, mutually exclusive with + `search_dirs` + {search_dirs} (table) directory/directories to search in, + mutually exclusive with `grep_open_files` + + +builtin.grep_string({opts}) *builtin.grep_string()* + Searches for the string under your cursor in your current working directory + + + Parameters: ~ + {opts} (table) options to pass to the picker + + Fields: ~ + {search} (string) the query to search + {search_dirs} (table) directory/directories to search in + {use_regex} (boolean) if true, special characters won't be escaped, + allows for using regex (default is false) + + +builtin.find_files({opts}) *builtin.find_files()* + Lists files in your current working directory, respects .gitignore + + + Parameters: ~ + {opts} (table) options to pass to the picker + + Fields: ~ + {find_command} (table) command line arguments for `find_files` to + use for the search, overrides default config + {follow} (boolean) if true, follows symlinks (i.e. uses `-L` + flag for the `find` command) + {hidden} (boolean) determines whether to show hidden files or + not (default is false) + {search_dirs} (table) directory/directories to search in + + +builtin.fd() *builtin.fd()* + This is an alias for the `find_files` picker + + + +builtin.file_browser({opts}) *builtin.file_browser()* + Lists files and folders in your current working directory, open files, + navigate your filesystem, and create new files and folders + - Default keymaps: + - ``: opens the currently selected file, or navigates to the + currently selected directory + - ``: creates new file in current directory, creates new directory + if the name contains a trailing '/' + - Note: you can create files nested into several directories with + ``, i.e. `lua/telescope/init.lua` would create the file + `init.lua` inside of `lua/telescope` and will create the necessary + folders (similar to how `mkdir -p` would work) if they do not already + exist + + + Parameters: ~ + {opts} (table) options to pass to the picker + + Fields: ~ + {search_dirs} (table) directory/directories to search in + + +builtin.treesitter() *builtin.treesitter()* + Lists function names, variables, and other symbols from treesitter queries + + + Fields: ~ + {show_line} (boolean) if true, shows the row:column that the result is + found at (default is true) + + +builtin.current_buffer_fuzzy_find({opts})*builtin.current_buffer_fuzzy_find()* + Live fuzzy search inside of the currently open buffer + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.tags({opts}) *builtin.tags()* + Lists tags in current directory with tag location file preview (users are + required to run ctags -R to generate tags or update when introducing new + changes) + + + Parameters: ~ + {opts} (table) options to pass to the picker + + Fields: ~ + {ctags_file} (string) specify a particular ctags file to use + {show_line} (boolean) if true, shows the content of the line the + tag is found on in the picker (default is + true) + {shorten_path} (boolean) if true, makes file paths shown in picker + use one letter for folders (default is true) + {hide_filename} (boolean) if true, hides the name of the file in the + current picker (default is false) + + +builtin.current_buffer_tags({opts}) *builtin.current_buffer_tags()* + Lists all of the tags for the currently open buffer, with a preview + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.git_files({opts}) *builtin.git_files()* + Fuzzy search for files tracked by Git. This command lists the output of the + `git ls-files` command, respects .gitignore, and optionally ignores + untracked files + - Default keymaps: + - ``: opens the currently selected file + + + Parameters: ~ + {opts} (table) options to pass to the picker + + Fields: ~ + {show_untracked} (boolean) if true, adds `--others` flag to + command and shows untracked files + (default is true) + {recurse_submodules} (boolean) if true, adds the + `--recurse-submodules` flag to command + (default is false) + + +builtin.git_commits({opts}) *builtin.git_commits()* + Lists commits for current directory with diff preview + - Default keymaps: + - ``: checks out the currently selected commit + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.git_bcommits({opts}) *builtin.git_bcommits()* + Lists commits for current buffer with diff preview + - Default keymaps: + - ``: checks out the currently selected commit + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.git_branches({opts}) *builtin.git_branches()* + List branches for current directory, with output from `git log --oneline` + shown in the preview window + - Default keymaps: + - ``: checks out the currently selected branch + - ``: tracks currently selected branch + - ``: rebases currently selected branch + - ``: creates a new branch, with confirmation prompt before creation + - ``: deletes the currently selected branch, with confirmation + prompt before deletion + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.git_status({opts}) *builtin.git_status()* + Lists git status for current directory + - Default keymaps: + - ``: stages or unstages the currently selected file + - ``: opens the currently selected file + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.git_stash({opts}) *builtin.git_stash()* + Lists stash items in current repository + - Default keymaps: + - ``: runs `git apply` for currently selected stash + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.builtin({opts}) *builtin.builtin()* + Lists all of the community maintained pickers built into Telescope + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.planets({opts}) *builtin.planets()* + Use the telescope... + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.symbols({opts}) *builtin.symbols()* + Lists symbols inside of data/telescope-sources/*.json found in your runtime + path. Check README for more info + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.commands({opts}) *builtin.commands()* + Lists available plugin/user commands and runs them on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.quickfix({opts}) *builtin.quickfix()* + Lists items in the quickfix list, jumps to location on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.loclist({opts}) *builtin.loclist()* + Lists items from the current window's location list, jumps to location on + `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.oldfiles({opts}) *builtin.oldfiles()* + Lists previously open files, opens on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.command_history({opts}) *builtin.command_history()* + Lists commands that were executed recently, and reruns them on `` + - Default keymaps: + - ``: open the command line with the text of the currently selected + result populated in it + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.search_history({opts}) *builtin.search_history()* + Lists searches that were executed recently, and reruns them on `` + - Default keymaps: + - ``: open a search window with the text of the currently selected + search result populated in it + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.vim_options({opts}) *builtin.vim_options()* + Lists vim options, allows you to edit the current value on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.help_tags({opts}) *builtin.help_tags()* + Lists available help tags and opens a new window with the relevant help + info on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.man_pages({opts}) *builtin.man_pages()* + Lists manpage entries, opens them in a help window on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.reloader({opts}) *builtin.reloader()* + Lists lua modules and reloads them on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.buffers({opts}) *builtin.buffers()* + Lists open buffers in current neovim instance, opens selected buffer on + `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.colorscheme({opts}) *builtin.colorscheme()* + Lists available colorschemes and applies them on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.marks({opts}) *builtin.marks()* + Lists vim marks and their value, jumps to the mark on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.registers({opts}) *builtin.registers()* + Lists vim registers, pastes the contents of the register on `` + - Default keymaps: + - ``: edit the contents of the currently selected register + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.keymaps({opts}) *builtin.keymaps()* + Lists normal mode keymappings, runs the selected keymap on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.filetypes({opts}) *builtin.filetypes()* + Lists all available filetypes, sets currently open buffer's filetype to + selected filetype in Telescope on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.highlights({opts}) *builtin.highlights()* + Lists all available highlights + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.autocommands({opts}) *builtin.autocommands()* + Lists vim autocommands and goes to their declaration on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.spell_suggest({opts}) *builtin.spell_suggest()* + Lists spelling suggestions for the current word under the cursor, replaces + word with selected suggestion on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.tagstack({opts}) *builtin.tagstack()* + Lists the tag stack for the current window, jumps to tag on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + Fields: ~ + {shorten_path} (boolean) if true, makes file paths shown in picker + use one letter for folders (default is true) + {hide_filename} (boolean) if true, hides the name of the file in the + current picker (default is true) + + +builtin.jumplist({opts}) *builtin.jumplist()* + Lists items from Vim's jumplist, jumps to location on `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.lsp_references({opts}) *builtin.lsp_references()* + Lists LSP references for word under the cursor, jumps to reference on + `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + Fields: ~ + {shorten_path} (boolean) if true, makes file paths shown in picker use + one letter for folders (default is false) + + +builtin.lsp_definitions({opts}) *builtin.lsp_definitions()* + Goto the definition of the word under the cursor, if there's only one, + otherwise show all options in Telescope + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.lsp_implementations({opts}) *builtin.lsp_implementations()* + Goto the implementation of the word under the cursor if there's only one, + otherwise show all options in Telescope + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.lsp_code_actions({opts}) *builtin.lsp_code_actions()* + Lists any LSP actions for the word under the cursor which can be triggered + with `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.lsp_range_code_actions({opts}) *builtin.lsp_range_code_actions()* + Lists any LSP actions for a given range, that can be triggered with `` + + + Parameters: ~ + {opts} (table) options to pass to the picker + + +builtin.lsp_document_symbols({opts}) *builtin.lsp_document_symbols()* + Lists LSP document symbols in the current buffer + - Default keymaps: + - ``: show autocompletion menu to prefilter your query by type of + symbol you want to see (i.e. `:variable:`) + + + Parameters: ~ + {opts} (table) options to pass to the picker + + Fields: ~ + {ignore_filename} (type) string with file to ignore + + +builtin.lsp_workspace_symbols({opts}) *builtin.lsp_workspace_symbols()* + Lists LSP document symbols in the current workspace + - Default keymaps: + - ``: show autocompletion menu to prefilter your query by type of + symbol you want to see (i.e. `:variable:`) + + + Parameters: ~ + {opts} (table) options to pass to the picker + + Fields: ~ + {shorten_path} (boolean) if true, makes file paths shown in picker + use one letter for folders (default is + false) + {ignore_filename} (string) file(s) to ignore + {hide_filename} (boolean) if true, hides the name of the file in the + current picker (default is false) + + +builtin.lsp_dynamic_workspace_symbols({opts})*builtin.lsp_dynamic_workspace_symbols()* + Dynamically lists LSP for all workspace symbols + - Default keymaps: + - ``: show autocompletion menu to prefilter your query by type of + symbol you want to see (i.e. `:variable:`) + + + Parameters: ~ + {opts} (table) options to pass to the picker + + Fields: ~ + {hide_filename} (boolean) if true, hides the name of the file in the + current picker (default is false) + + +builtin.lsp_document_diagnostics({opts}) *builtin.lsp_document_diagnostics()* + Lists LSP diagnostics for the current buffer + - Default keymaps: + - ``: show autocompletion menu to prefilter your query with the + diagnostic you want to see (i.e. `:warning:`) + + + Parameters: ~ + {opts} (table) options to pass to the picker + + Fields: ~ + {hide_filename} (boolean) if true, hides the name of the file in the + current picker (default is false) + + +builtin.lsp_workspace_diagnostics({opts})*builtin.lsp_workspace_diagnostics()* + Lists LSP diagnostics for the current workspace if supported, otherwise + searches in all open buffers + - Default keymaps: + - ``: show autocompletion menu to prefilter your query with the + diagnostic you want to see (i.e. `:warning:`) + + + Parameters: ~ + {opts} (table) options to pass to the picker + + Fields: ~ + {hide_filename} (boolean) if true, hides the name of the file in the + current picker (default is false) + + + +================================================================================ + *telescope.actions.state* + +Functions to be used to determine the current state of telescope. + +Generally used from within other |telescope.actions| + +action_state.get_selected_entry() *action_state.get_selected_entry()* + Get the current entry + + + +action_state.get_current_line() *action_state.get_current_line()* + Gets the current line + + + +action_state.get_current_picker({prompt_bufnr})*action_state.get_current_picker()* + Gets the current picker + + + Parameters: ~ + {prompt_bufnr} (number) The prompt bufnr + + + ================================================================================ *telescope.previewers* diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index bee1f48b38..f78eb01628 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -234,6 +234,19 @@ actions.set_command_line = function(prompt_bufnr) vim.cmd(entry.value) end +actions.edit_search_line = function(prompt_bufnr) + local entry = action_state.get_selected_entry(prompt_bufnr) + actions.close(prompt_bufnr) + a.nvim_feedkeys(a.nvim_replace_termcodes("/" .. entry.value , true, false, true), "t", true) +end + +actions.set_search_line = function(prompt_bufnr) + local entry = action_state.get_selected_entry(prompt_bufnr) + + actions.close(prompt_bufnr) + a.nvim_feedkeys(a.nvim_replace_termcodes("/" .. entry.value .. "", true, false, true), "t", true) +end + actions.edit_register = function(prompt_bufnr) local entry = action_state.get_selected_entry(prompt_bufnr) local picker = action_state.get_current_picker(prompt_bufnr) @@ -327,6 +340,23 @@ actions.git_create_branch = function(prompt_bufnr) end end +--- Applies an existing git stash +---@param prompt_bufnr number: The prompt bufnr +actions.git_apply_stash = function(prompt_bufnr) + local selection = action_state.get_selected_entry() + actions.close(prompt_bufnr) + local _, ret, stderr = utils.get_os_command_output({ 'git', 'stash', 'apply', '--index', selection.value }) + if ret == 0 then + print("applied: " .. selection.value) + else + print(string.format( + 'Error when applying: %s. Git returned: "%s"', + selection.value, + table.concat(stderr, ' ') + )) + end +end + --- Checkout an existing git branch ---@param prompt_bufnr number: The prompt bufnr actions.git_checkout = function(prompt_bufnr) @@ -345,6 +375,32 @@ actions.git_checkout = function(prompt_bufnr) end end +-- TODO: add this function header back once the treesitter max-query bug is resolved +-- Switch to git branch +-- If the branch already exists in local, switch to that. +-- If the branch is only in remote, create new branch tracking remote and switch to new one. +--@param prompt_bufnr number: The prompt bufnr +actions.git_switch_branch = function(prompt_bufnr) + local cwd = action_state.get_current_picker(prompt_bufnr).cwd + local selection = action_state.get_selected_entry() + actions.close(prompt_bufnr) + local pattern = '^refs/remotes/%w+/' + local branch = selection.value + if string.match(selection.refname, pattern) then + branch = string.gsub(selection.refname, pattern, '') + end + local _, ret, stderr = utils.get_os_command_output({ 'git', 'switch', branch }, cwd) + if ret == 0 then + print("Switched to: " .. branch) + else + print(string.format( + 'Error when switching to: %s. Git returned: "%s"', + selection.value, + table.concat(stderr, ' ') + )) + end +end + --- Tell git to track the currently selected remote branch in Telescope ---@param prompt_bufnr number: The prompt bufnr actions.git_track_branch = function(prompt_bufnr) @@ -363,8 +419,9 @@ actions.git_track_branch = function(prompt_bufnr) end end ---- Delete the currently selected branch ----@param prompt_bufnr number: The prompt bufnr +-- TODO: add this function header back once the treesitter max-query bug is resolved +-- Delete the currently selected branch +-- @param prompt_bufnr number: The prompt bufnr actions.git_delete_branch = function(prompt_bufnr) local cwd = action_state.get_current_picker(prompt_bufnr).cwd local selection = action_state.get_selected_entry() @@ -385,8 +442,9 @@ actions.git_delete_branch = function(prompt_bufnr) end end ---- Rebase to selected git branch ----@param prompt_bufnr number: The prompt bufnr +-- TODO: add this function header back once the treesitter max-query bug is resolved +-- Rebase to selected git branch +-- @param prompt_bufnr number: The prompt bufnr actions.git_rebase_branch = function(prompt_bufnr) local cwd = action_state.get_current_picker(prompt_bufnr).cwd local selection = action_state.get_selected_entry() @@ -407,8 +465,9 @@ actions.git_rebase_branch = function(prompt_bufnr) end end ---- Stage/unstage selected file ----@param prompt_bufnr number: The prompt bufnr +-- TODO: add this function header back once the treesitter max-query bug is resolved +-- Stage/unstage selected file +-- @param prompt_bufnr number: The prompt bufnr actions.git_staging_toggle = function(prompt_bufnr) local cwd = action_state.get_current_picker(prompt_bufnr).cwd local selection = action_state.get_selected_entry() diff --git a/lua/telescope/actions/set.lua b/lua/telescope/actions/set.lua index 54abbfc47f..5b39c54575 100644 --- a/lua/telescope/actions/set.lua +++ b/lua/telescope/actions/set.lua @@ -1,3 +1,15 @@ +---@tag telescope.actions.set + +---@brief [[ +--- Telescope action sets are used to provide an interface for managing +--- actions that all primarily do the same thing, but with slight tweaks. +--- +--- For example, when editing files you may want it in the current split, +--- a vertical split, etc. Instead of making users have to overwrite EACH +--- of those every time they want to change this behavior, they can instead +--- replace the `set` itself and then it will work great and they're done. +---@brief ]] + local a = vim.api local log = require('telescope.log') @@ -8,14 +20,7 @@ local action_state = require('telescope.actions.state') local transform_mod = require('telescope.actions.mt').transform_mod ---- Telescope action sets are used to provide an interface for managing ---- actions that all primarily do the same thing, but with slight tweaks. ---- ---- For example, when editing files you may want it in the current split, ---- a vertical split, etc. Instead of making users have to overwrite EACH ---- of those every time they want to change this behavior, they can instead ---- replace the `set` itself and then it will work great and they're done. -local set = setmetatable({}, { +local action_set = setmetatable({}, { __index = function(_, k) error("'telescope.actions.set' does not have a value: " .. tostring(k)) end @@ -25,7 +30,7 @@ local set = setmetatable({}, { --- Handles not overflowing / underflowing the list. ---@param prompt_bufnr number: The prompt bufnr ---@param change number: The amount to shift the selection by -set.shift_selection = function(prompt_bufnr, change) +action_set.shift_selection = function(prompt_bufnr, change) local count = vim.v.count count = count == 0 and 1 or count count = a.nvim_get_mode().mode == "n" and count or 1 @@ -39,8 +44,8 @@ end ---@param prompt_bufnr number: The prompt bufnr ---@param type string: The type of selection to make -- Valid types include: "default", "horizontal", "vertical", "tabedit" -set.select = function(prompt_bufnr, type) - return set.edit(prompt_bufnr, action_state.select_key_to_edit_key(type)) +action_set.select = function(prompt_bufnr, type) + return action_set.edit(prompt_bufnr, action_state.select_key_to_edit_key(type)) end local edit_buffer @@ -65,7 +70,7 @@ end ---@param prompt_bufnr number: The prompt bufnr ---@param command string: The command to use to open the file. -- Valid commands include: "edit", "new", "vedit", "tabedit" -set.edit = function(prompt_bufnr, command) +action_set.edit = function(prompt_bufnr, command) local entry = action_state.get_selected_entry() if not entry then @@ -108,19 +113,18 @@ set.edit = function(prompt_bufnr, command) if entry_bufnr then edit_buffer(command, entry_bufnr) else - -- check if we didn't pick a different buffer -- prevents restarting lsp server if vim.api.nvim_buf_get_name(0) ~= filename or command ~= "edit" then filename = path.normalize(vim.fn.fnameescape(filename), vim.loop.cwd()) vim.cmd(string.format("%s %s", command, filename)) end + end - if row and col then - local ok, err_msg = pcall(a.nvim_win_set_cursor, 0, {row, col}) - if not ok then - log.debug("Failed to move to cursor:", err_msg, row, col) - end + if row and col then + local ok, err_msg = pcall(a.nvim_win_set_cursor, 0, {row, col}) + if not ok then + log.debug("Failed to move to cursor:", err_msg, row, col) end end end @@ -129,7 +133,7 @@ end ---@param prompt_bufnr number: The prompt bufnr ---@param direction number: The direction of the scrolling -- Valid directions include: "1", "-1" -set.scroll_previewer = function (prompt_bufnr, direction) +action_set.scroll_previewer = function (prompt_bufnr, direction) local status = state.get_status(prompt_bufnr) local default_speed = vim.api.nvim_win_get_height(status.preview_win) / 2 local speed = status.picker.layout_config.scroll_speed or default_speed @@ -140,5 +144,5 @@ end -- ================================================== -- Transforms modules and sets the corect metatables. -- ================================================== -set = transform_mod(set) -return set +action_set = transform_mod(action_set) +return action_set diff --git a/lua/telescope/actions/state.lua b/lua/telescope/actions/state.lua index 0752171eab..7ef9f6c7e9 100644 --- a/lua/telescope/actions/state.lua +++ b/lua/telescope/actions/state.lua @@ -1,3 +1,11 @@ +---@tag telescope.actions.state + +---@brief [[ +--- Functions to be used to determine the current state of telescope. +--- +--- Generally used from within other |telescope.actions| +---@brief ]] + local global_state = require('telescope.state') local action_state = {} @@ -13,6 +21,7 @@ function action_state.get_current_line() end --- Gets the current picker +---@param prompt_bufnr number: The prompt bufnr function action_state.get_current_picker(prompt_bufnr) return global_state.get_status(prompt_bufnr).picker end diff --git a/lua/telescope/builtin/files.lua b/lua/telescope/builtin/files.lua index 62535457a8..321e1569e6 100644 --- a/lua/telescope/builtin/files.lua +++ b/lua/telescope/builtin/files.lua @@ -93,16 +93,17 @@ files.live_grep = function(opts) }):find() end - -- Special keys: -- opts.search -- the string to search. -- opts.search_dirs -- list of directory to search in +-- opts.use_regex -- special characters won't be escaped files.grep_string = function(opts) -- TODO: This should probably check your visual selection as well, if you've got one local vimgrep_arguments = opts.vimgrep_arguments or conf.vimgrep_arguments local search_dirs = opts.search_dirs - local search = escape_chars(opts.search or vim.fn.expand("")) + local word = opts.search or vim.fn.expand("") + local search = opts.use_regex and word or escape_chars(word) local word_match = opts.word_match opts.entry_maker = opts.entry_maker or make_entry.gen_from_vimgrep(opts) @@ -133,7 +134,7 @@ files.grep_string = function(opts) end -- TODO: Maybe just change this to `find`. --- Support `find` and maybe let people do other stuff with it as well. +-- TODO: Support `find` and maybe let people do other stuff with it as well. files.find_files = function(opts) local find_command = opts.find_command local hidden = opts.hidden @@ -247,7 +248,7 @@ files.file_browser = function(opts) table.insert(data, typ == 'directory' and (entry .. os_sep) or entry) end }) - table.insert(data, 1, '../') + table.insert(data, 1, '..' .. os_sep) return finders.new_table { results = data, @@ -309,6 +310,7 @@ files.file_browser = function(opts) }):find() end +-- TODO: finish docs for opts.show_line files.treesitter = function(opts) opts.show_line = utils.get_default(opts.show_line, true) @@ -368,10 +370,14 @@ files.current_buffer_fuzzy_find = function(opts) }) end - local ok, parser = pcall(vim.treesitter.get_parser, bufnr, filetype) - if ok then - local query = vim.treesitter.get_query(filetype, "highlights") + local ts_ok, ts_parsers = pcall(require, 'nvim-treesitter.parsers') + if ts_ok then + filetype = ts_parsers.ft_to_lang(filetype) + end + local parser_ok, parser = pcall(vim.treesitter.get_parser, bufnr, filetype) + local query_ok, query = pcall(vim.treesitter.get_query, filetype, "highlights") + if parser_ok and query_ok then local root = parser:parse()[1]:root() local highlighter = vim.treesitter.highlighter.new(parser) diff --git a/lua/telescope/builtin/git.lua b/lua/telescope/builtin/git.lua index f7c0096b80..ae03019a2d 100644 --- a/lua/telescope/builtin/git.lua +++ b/lua/telescope/builtin/git.lua @@ -15,7 +15,7 @@ git.files = function(opts) local show_untracked = utils.get_default(opts.show_untracked, true) local recurse_submodules = utils.get_default(opts.recurse_submodules, false) if show_untracked and recurse_submodules then - error("Git does not suppurt both --others and --recurse-submodules") + error("Git does not support both --others and --recurse-submodules") end -- By creating the entry maker after the cwd options, @@ -57,6 +57,25 @@ git.commits = function(opts) }):find() end +git.stash = function(opts) + local results = utils.get_os_command_output({ + 'git', '--no-pager', 'stash', 'list', + }, opts.cwd) + + pickers.new(opts, { + prompt_title = 'Git Stash', + finder = finders.new_table { + results = results, + entry_maker = opts.entry_maker or make_entry.gen_from_git_stash(), + }, + previewer = previewers.git_stash_diff.new(opts), + sorter = conf.file_sorter(opts), + attach_mappings = function() + actions.select_default:replace(actions.git_apply_stash) + return true + end + }):find() +end git.bcommits = function(opts) local results = utils.get_os_command_output({ 'git', 'log', '--pretty=oneline', '--abbrev-commit', vim.fn.expand('%') @@ -180,9 +199,11 @@ git.branches = function(opts) map('i', '', actions.git_create_branch) map('n', '', actions.git_create_branch) + map('i', '', actions.git_switch_branch) + map('n', '', actions.git_switch_branch) + map('i', '', actions.git_delete_branch) map('n', '', actions.git_delete_branch) - return true end }):find() @@ -244,7 +265,10 @@ local set_opts_cwd = function(opts) local use_git_root = utils.get_default(opts.use_git_root, true) if ret ~= 0 then - error(opts.cwd .. ' is not a git directory') + local is_worktree = utils.get_os_command_output({ "git", "rev-parse", "--is-inside-work-tree" }, opts.cwd) + if is_worktree == "false" then + error(opts.cwd .. ' is not a git directory') + end else if use_git_root then opts.cwd = git_root[1] diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index b9cc99f7c9..2dd384a46a 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -1,16 +1,25 @@ ---@tag telescope.builtin ---@brief [[ ---- A collection of builtin pickers for telescope. ---- ---- Meant for both example and for easy startup. +--- Telescope Builtins is a collection of community maintained pickers to support common workflows. It can be used as +--- reference when writing PRs, Telescope extensions, your own custom pickers, or just as a discovery tool for all of +--- the amazing pickers already shipped with Telescope! --- --- Any of these functions can just be called directly by doing: --- ---- :lua require('telescope.builtin').$NAME() +--- :lua require('telescope.builtin').$NAME_OF_PICKER() +--- +--- To use any of Telescope's default options or any picker-specific options, call your desired picker by passing a lua +--- table to the picker with all of the options you want to use. Here's an example with the live_grep picker: --- ---- This will use the default configuration options. ---- Other configuration options are still in flux at the moment +---
+--- :lua require('telescope.builtin').live_grep({
+---    prompt_title = 'find string in open buffers...',
+---    grep_open_files = true
+---  })
+--- 
+--- +--- This will use the default configuration options. Other configuration options are still in flux at the moment ---@brief ]] if 1 ~= vim.fn.has('nvim-0.5') then @@ -21,56 +30,291 @@ end local builtin = {} ---- Live grep means grep as you type. +-- +-- +-- File-related Pickers +-- +-- + +--- Search for a string in your current working directory and get results live as you type (respecting .gitignore) +---@param opts table: options to pass to the picker +---@field grep_open_files boolean: if true, restrict search to open files only, mutually exclusive with `search_dirs` +---@field search_dirs table: directory/directories to search in, mutually exclusive with `grep_open_files` builtin.live_grep = require('telescope.builtin.files').live_grep +--- Searches for the string under your cursor in your current working directory +---@param opts table: options to pass to the picker +---@field search string: the query to search +---@field search_dirs table: directory/directories to search in +---@field use_regex boolean: if true, special characters won't be escaped, allows for using regex (default is false) builtin.grep_string = require('telescope.builtin.files').grep_string + +--- Lists files in your current working directory, respects .gitignore +---@param opts table: options to pass to the picker +---@field find_command table: command line arguments for `find_files` to use for the search, overrides default config +---@field follow boolean: if true, follows symlinks (i.e. uses `-L` flag for the `find` command) +---@field hidden boolean: determines whether to show hidden files or not (default is false) +---@field search_dirs table: directory/directories to search in builtin.find_files = require('telescope.builtin.files').find_files + +--- This is an alias for the `find_files` picker builtin.fd = builtin.find_files + +--- Lists files and folders in your current working directory, open files, navigate your filesystem, and create new +--- files and folders +--- - Default keymaps: +--- - ``: opens the currently selected file, or navigates to the currently selected directory +--- - ``: creates new file in current directory, creates new directory if the name contains a trailing '/' +--- - Note: you can create files nested into several directories with ``, i.e. `lua/telescope/init.lua` would +--- create the file `init.lua` inside of `lua/telescope` and will create the necessary folders (similar to how +--- `mkdir -p` would work) if they do not already exist +---@param opts table: options to pass to the picker +---@field search_dirs table: directory/directories to search in builtin.file_browser = require('telescope.builtin.files').file_browser + +--- Lists function names, variables, and other symbols from treesitter queries +---@field show_line boolean: if true, shows the row:column that the result is found at (default is true) builtin.treesitter = require('telescope.builtin.files').treesitter + +--- Live fuzzy search inside of the currently open buffer +---@param opts table: options to pass to the picker builtin.current_buffer_fuzzy_find = require('telescope.builtin.files').current_buffer_fuzzy_find + +--- Lists tags in current directory with tag location file preview (users are required to run ctags -R to generate tags +--- or update when introducing new changes) +---@param opts table: options to pass to the picker +---@field ctags_file string: specify a particular ctags file to use +---@field show_line boolean: if true, shows the content of the line the tag is found on in the picker (default is true) +---@field shorten_path boolean: if true, makes file paths shown in picker use one letter for folders (default is true) +---@field hide_filename boolean: if true, hides the name of the file in the current picker (default is false) builtin.tags = require('telescope.builtin.files').tags + +--- Lists all of the tags for the currently open buffer, with a preview +---@param opts table: options to pass to the picker builtin.current_buffer_tags = require('telescope.builtin.files').current_buffer_tags +-- +-- +-- Git-related Pickers +-- +-- + +--- Fuzzy search for files tracked by Git. This command lists the output of the `git ls-files` command, respects +--- .gitignore, and optionally ignores untracked files +--- - Default keymaps: +--- - ``: opens the currently selected file +---@param opts table: options to pass to the picker +---@field show_untracked boolean: if true, adds `--others` flag to command and shows untracked files (default is true) +---@field recurse_submodules boolean: if true, adds the `--recurse-submodules` flag to command (default is false) builtin.git_files = require('telescope.builtin.git').files + +--- Lists commits for current directory with diff preview +--- - Default keymaps: +--- - ``: checks out the currently selected commit +---@param opts table: options to pass to the picker builtin.git_commits = require('telescope.builtin.git').commits + +--- Lists commits for current buffer with diff preview +--- - Default keymaps: +--- - ``: checks out the currently selected commit +---@param opts table: options to pass to the picker builtin.git_bcommits = require('telescope.builtin.git').bcommits + +--- List branches for current directory, with output from `git log --oneline` shown in the preview window +--- - Default keymaps: +--- - ``: checks out the currently selected branch +--- - ``: tracks currently selected branch +--- - ``: rebases currently selected branch +--- - ``: creates a new branch, with confirmation prompt before creation +--- - ``: deletes the currently selected branch, with confirmation prompt before deletion +---@param opts table: options to pass to the picker builtin.git_branches = require('telescope.builtin.git').branches + +--- Lists git status for current directory +--- - Default keymaps: +--- - ``: stages or unstages the currently selected file +--- - ``: opens the currently selected file +---@param opts table: options to pass to the picker builtin.git_status = require('telescope.builtin.git').status +--- Lists stash items in current repository +--- - Default keymaps: +--- - ``: runs `git apply` for currently selected stash +---@param opts table: options to pass to the picker +builtin.git_stash = require('telescope.builtin.git').stash + +-- +-- +-- Internal and Vim-related Pickers +-- +-- + +--- Lists all of the community maintained pickers built into Telescope +---@param opts table: options to pass to the picker builtin.builtin = require('telescope.builtin.internal').builtin +--- Use the telescope... +---@param opts table: options to pass to the picker builtin.planets = require('telescope.builtin.internal').planets + +--- Lists symbols inside of data/telescope-sources/*.json found in your runtime path. Check README for more info +---@param opts table: options to pass to the picker builtin.symbols = require('telescope.builtin.internal').symbols + +--- Lists available plugin/user commands and runs them on `` +---@param opts table: options to pass to the picker builtin.commands = require('telescope.builtin.internal').commands + +--- Lists items in the quickfix list, jumps to location on `` +---@param opts table: options to pass to the picker builtin.quickfix = require('telescope.builtin.internal').quickfix + +--- Lists items from the current window's location list, jumps to location on `` +---@param opts table: options to pass to the picker builtin.loclist = require('telescope.builtin.internal').loclist + +--- Lists previously open files, opens on `` +---@param opts table: options to pass to the picker builtin.oldfiles = require('telescope.builtin.internal').oldfiles + +--- Lists commands that were executed recently, and reruns them on `` +--- - Default keymaps: +--- - ``: open the command line with the text of the currently selected result populated in it +---@param opts table: options to pass to the picker builtin.command_history = require('telescope.builtin.internal').command_history + +--- Lists searches that were executed recently, and reruns them on `` +--- - Default keymaps: +--- - ``: open a search window with the text of the currently selected search result populated in it +---@param opts table: options to pass to the picker +builtin.search_history = require('telescope.builtin.internal').search_history + +--- Lists vim options, allows you to edit the current value on `` +---@param opts table: options to pass to the picker builtin.vim_options = require('telescope.builtin.internal').vim_options + +--- Lists available help tags and opens a new window with the relevant help info on `` +---@param opts table: options to pass to the picker builtin.help_tags = require('telescope.builtin.internal').help_tags + +--- Lists manpage entries, opens them in a help window on `` +---@param opts table: options to pass to the picker builtin.man_pages = require('telescope.builtin.internal').man_pages + +--- Lists lua modules and reloads them on `` +---@param opts table: options to pass to the picker builtin.reloader = require('telescope.builtin.internal').reloader + +--- Lists open buffers in current neovim instance, opens selected buffer on `` +---@param opts table: options to pass to the picker builtin.buffers = require('telescope.builtin.internal').buffers + +--- Lists available colorschemes and applies them on `` +---@param opts table: options to pass to the picker builtin.colorscheme = require('telescope.builtin.internal').colorscheme + +--- Lists vim marks and their value, jumps to the mark on `` +---@param opts table: options to pass to the picker builtin.marks = require('telescope.builtin.internal').marks + +--- Lists vim registers, pastes the contents of the register on `` +--- - Default keymaps: +--- - ``: edit the contents of the currently selected register +---@param opts table: options to pass to the picker builtin.registers = require('telescope.builtin.internal').registers + +--- Lists normal mode keymappings, runs the selected keymap on `` +---@param opts table: options to pass to the picker builtin.keymaps = require('telescope.builtin.internal').keymaps + +--- Lists all available filetypes, sets currently open buffer's filetype to selected filetype in Telescope on `` +---@param opts table: options to pass to the picker builtin.filetypes = require('telescope.builtin.internal').filetypes + +--- Lists all available highlights +---@param opts table: options to pass to the picker builtin.highlights = require('telescope.builtin.internal').highlights + +--- Lists vim autocommands and goes to their declaration on `` +---@param opts table: options to pass to the picker builtin.autocommands = require('telescope.builtin.internal').autocommands + +--- Lists spelling suggestions for the current word under the cursor, replaces word with selected suggestion on `` +---@param opts table: options to pass to the picker builtin.spell_suggest = require('telescope.builtin.internal').spell_suggest + +--- Lists the tag stack for the current window, jumps to tag on `` +---@param opts table: options to pass to the picker +---@field shorten_path boolean: if true, makes file paths shown in picker use one letter for folders (default is true) +---@field hide_filename boolean: if true, hides the name of the file in the current picker (default is true) builtin.tagstack = require('telescope.builtin.internal').tagstack +--- Lists items from Vim's jumplist, jumps to location on `` +---@param opts table: options to pass to the picker +builtin.jumplist = require('telescope.builtin.internal').jumplist + +-- +-- +-- LSP-related Pickers +-- +-- + +--- Lists LSP references for word under the cursor, jumps to reference on `` +---@param opts table: options to pass to the picker +---@field shorten_path boolean: if true, makes file paths shown in picker use one letter for folders (default is false) builtin.lsp_references = require('telescope.builtin.lsp').references + +--- Goto the definition of the word under the cursor, if there's only one, otherwise show all options in Telescope +---@param opts table: options to pass to the picker builtin.lsp_definitions = require('telescope.builtin.lsp').definitions -builtin.lsp_document_symbols = require('telescope.builtin.lsp').document_symbols + +--- Goto the implementation of the word under the cursor if there's only one, otherwise show all options in Telescope +---@param opts table: options to pass to the picker +builtin.lsp_implementations = require('telescope.builtin.lsp').implementations + +--- Lists any LSP actions for the word under the cursor which can be triggered with `` +---@param opts table: options to pass to the picker builtin.lsp_code_actions = require('telescope.builtin.lsp').code_actions -builtin.lsp_document_diagnostics = require('telescope.builtin.lsp').diagnostics -builtin.lsp_workspace_diagnostics = require('telescope.builtin.lsp').workspace_diagnostics + +--- Lists any LSP actions for a given range, that can be triggered with `` +---@param opts table: options to pass to the picker builtin.lsp_range_code_actions = require('telescope.builtin.lsp').range_code_actions + +--- Lists LSP document symbols in the current buffer +--- - Default keymaps: +--- - ``: show autocompletion menu to prefilter your query by type of symbol you want to see (i.e. `:variable:`) +---@param opts table: options to pass to the picker +---@field ignore_filename type: string with file to ignore +builtin.lsp_document_symbols = require('telescope.builtin.lsp').document_symbols + +--- Lists LSP document symbols in the current workspace +--- - Default keymaps: +--- - ``: show autocompletion menu to prefilter your query by type of symbol you want to see (i.e. `:variable:`) +---@param opts table: options to pass to the picker +---@field shorten_path boolean: if true, makes file paths shown in picker use one letter for folders (default is false) +---@field ignore_filename string: file(s) to ignore +---@field hide_filename boolean: if true, hides the name of the file in the current picker (default is false) builtin.lsp_workspace_symbols = require('telescope.builtin.lsp').workspace_symbols + +--- Dynamically lists LSP for all workspace symbols +--- - Default keymaps: +--- - ``: show autocompletion menu to prefilter your query by type of symbol you want to see (i.e. `:variable:`) +---@param opts table: options to pass to the picker +---@field hide_filename boolean: if true, hides the name of the file in the current picker (default is false) builtin.lsp_dynamic_workspace_symbols = require('telescope.builtin.lsp').dynamic_workspace_symbols +--- Lists LSP diagnostics for the current buffer +--- - Default keymaps: +--- - ``: show autocompletion menu to prefilter your query with the diagnostic you want to see (i.e. `:warning:`) +---@param opts table: options to pass to the picker +---@field hide_filename boolean: if true, hides the name of the file in the current picker (default is false) +builtin.lsp_document_diagnostics = require('telescope.builtin.lsp').diagnostics + +--- Lists LSP diagnostics for the current workspace if supported, otherwise searches in all open buffers +--- - Default keymaps: +--- - ``: show autocompletion menu to prefilter your query with the diagnostic you want to see (i.e. `:warning:`) +---@param opts table: options to pass to the picker +---@field hide_filename boolean: if true, hides the name of the file in the current picker (default is false) +builtin.lsp_workspace_diagnostics = require('telescope.builtin.lsp').workspace_diagnostics + return builtin diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index cab145dca9..c412f69b4e 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -251,6 +251,7 @@ internal.oldfiles = function(opts) if opts.cwd_only then local cwd = vim.loop.cwd() + cwd = cwd:gsub([[\]],[[\\]]) results = vim.tbl_filter(function(file) return vim.fn.matchstrpos(file, cwd)[2] ~= -1 end, results) @@ -297,6 +298,36 @@ internal.command_history = function(opts) }):find() end +internal.search_history = function(opts) + local search_string = vim.fn.execute('history search') + local search_list = vim.split(search_string, "\n") + + local results = {} + for i = #search_list, 3, -1 do + local item = search_list[i] + local _, finish = string.find(item, "%d+ +") + table.insert(results, string.sub(item, finish + 1)) + end + + pickers.new(opts, { + prompt_title = 'Search History', + finder = finders.new_table(results), + sorter = conf.generic_sorter(opts), + + attach_mappings = function(_, map) + map('i', '', actions.set_search_line) + map('n', '', actions.set_search_line) + map('n', '', actions.edit_search_line) + map('i', '', actions.edit_search_line) + + -- TODO: Find a way to insert the text... it seems hard. + -- map('i', '', actions.insert_value, { expr = true }) + + return true + end, + }):find() +end + internal.vim_options = function(opts) -- Load vim options. local vim_opts = loadfile(utils.data_directory() .. path.separator .. 'options' .. @@ -886,16 +917,37 @@ internal.tagstack = function(opts) end pickers.new(opts, { - prompt_title = 'TagStack', - finder = finders.new_table { - results = tags, - entry_maker = make_entry.gen_from_quickfix(opts), - }, - previewer = previewers.vim_buffer_qflist.new(opts), - sorter = conf.generic_sorter(opts), - }):find() + prompt_title = 'TagStack', + finder = finders.new_table { + results = tags, + entry_maker = make_entry.gen_from_quickfix(opts), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.generic_sorter(opts), + }):find() end +internal.jumplist = function(opts) + opts = opts or {} + local jumplist = vim.fn.getjumplist()[1] + + -- reverse the list + local sorted_jumplist = {} + for i = #jumplist, 1, -1 do + jumplist[i].text = '' + table.insert(sorted_jumplist, jumplist[i]) + end + + pickers.new(opts, { + prompt_title = 'Jumplist', + finder = finders.new_table { + results = sorted_jumplist, + entry_maker = make_entry.gen_from_jumplist(opts), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.generic_sorter(opts), + }):find() +end local function apply_checks(mod) for k, v in pairs(mod) do diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua index b2ab981443..6a3492bd87 100644 --- a/lua/telescope/builtin/lsp.lua +++ b/lua/telescope/builtin/lsp.lua @@ -3,6 +3,7 @@ local action_state = require('telescope.actions.state') local finders = require('telescope.finders') local make_entry = require('telescope.make_entry') local pickers = require('telescope.pickers') +local entry_display = require('telescope.pickers.entry_display') local utils = require('telescope.utils') local a = require('plenary.async_lib') local async, await = a.async, a.await @@ -18,7 +19,12 @@ lsp.references = function(opts) local params = vim.lsp.util.make_position_params() params.context = { includeDeclaration = true } - local results_lsp = vim.lsp.buf_request_sync(0, "textDocument/references", params, opts.timeout or 10000) + local results_lsp, err = vim.lsp.buf_request_sync(0, "textDocument/references", params, opts.timeout or 10000) + if err then + vim.api.nvim_err_writeln("Error when finding references: " .. err) + return + end + local locations = {} for _, server_results in pairs(results_lsp) do if server_results.result then @@ -41,11 +47,15 @@ lsp.references = function(opts) }):find() end -lsp.definitions = function(opts) +local function list_or_jump(action, title, opts) opts = opts or {} local params = vim.lsp.util.make_position_params() - local result = vim.lsp.buf_request_sync(0, "textDocument/definition", params, opts.timeout or 10000) + local result, err = vim.lsp.buf_request_sync(0, action, params, opts.timeout or 10000) + if err then + vim.api.nvim_err_writeln("Error when executing " .. action .. " : " .. err) + return + end local flattened_results = {} for _, server_results in pairs(result) do if server_results.result then @@ -60,7 +70,7 @@ lsp.definitions = function(opts) else local locations = vim.lsp.util.locations_to_items(flattened_results) pickers.new(opts, { - prompt_title = 'LSP Definitions', + prompt_title = title, finder = finders.new_table { results = locations, entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts), @@ -71,9 +81,21 @@ lsp.definitions = function(opts) end end +lsp.definitions = function(opts) + return list_or_jump("textDocument/definition", 'LSP Definitions', opts) +end + +lsp.implementations = function(opts) + return list_or_jump("textDocument/implementation", 'LSP Implementations', opts) +end + lsp.document_symbols = function(opts) local params = vim.lsp.util.make_position_params() - local results_lsp = vim.lsp.buf_request_sync(0, "textDocument/documentSymbol", params, opts.timeout or 10000) + local results_lsp, err = vim.lsp.buf_request_sync(0, "textDocument/documentSymbol", params, opts.timeout or 10000) + if err then + vim.api.nvim_err_writeln("Error when finding document symbols: " .. err) + return + end if not results_lsp or vim.tbl_isempty(results_lsp) then print("No results from textDocument/documentSymbol") @@ -123,20 +145,56 @@ lsp.code_actions = function(opts) return end - local _, response = next(results_lsp) - if not response then - print("No code actions available") - return + local idx = 1 + local results = {} + local widths = { + idx = 0, + command_title = 0, + client_name = 0, + } + + for client_id, response in pairs(results_lsp) do + if response.result then + local client = vim.lsp.get_client_by_id(client_id) + + for _, result in pairs(response.result) do + local entry = { + idx = idx, + command_title = result.title, + client_name = client and client.name or "", + command = result, + } + + for key, value in pairs(widths) do + widths[key] = math.max(value, utils.strdisplaywidth(entry[key])) + end + + table.insert(results, entry) + idx = idx + 1 + end + end end - local results = response.result - if not results or #results == 0 then + if #results == 0 then print("No code actions available") return end - for i,x in ipairs(results) do - x.idx = i + local displayer = entry_display.create { + separator = " ", + items = { + { width = widths.idx + 1 }, -- +1 for ":" suffix + { width = widths.command_title }, + { width = widths.client_name }, + }, + } + + local function make_display(entry) + return displayer { + {entry.idx .. ":", "TelescopePromptPrefix"}, + {entry.command_title}, + {entry.client_name, "TelescopeResultsComment"}, + } end pickers.new(opts, { @@ -146,9 +204,12 @@ lsp.code_actions = function(opts) entry_maker = function(line) return { valid = line ~= nil, - value = line, - ordinal = line.idx .. line.title, - display = line.idx .. ': ' .. line.title + value = line.command, + ordinal = line.idx .. line.command_title, + command_title = line.command_title, + idx = line.idx, + client_name = line.client_name, + display = make_display, } end }, @@ -185,7 +246,11 @@ lsp.workspace_symbols = function(opts) opts.shorten_path = utils.get_default(opts.shorten_path, true) local params = {query = opts.query or ''} - local results_lsp = vim.lsp.buf_request_sync(0, "workspace/symbol", params, opts.timeout or 10000) + local results_lsp, err = vim.lsp.buf_request_sync(0, "workspace/symbol", params, opts.timeout or 10000) + if err then + vim.api.nvim_err_writeln("Error when finding workspace symbols: " .. err) + return + end local locations = {} @@ -308,6 +373,7 @@ local feature_map = { ["document_symbols"] = "document_symbol", ["references"] = "find_references", ["definitions"] = "goto_definition", + ["implementations"] = "implementation", ["workspace_symbols"] = "workspace_symbol", } diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 8729ebbe53..39106e0427 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -240,6 +240,21 @@ do end end +function make_entry.gen_from_git_stash() + return function(entry) + if entry == "" then + return nil + end + local splitted = vim.split(entry, ':') + return { + value = splitted[1], + ordinal = splitted[3], + display = splitted[3] + } + end +end + + function make_entry.gen_from_git_commits() local displayer = entry_display.create { separator = " ", @@ -339,9 +354,9 @@ function make_entry.gen_from_lsp_symbols(opts) local bufnr = opts.bufnr or vim.api.nvim_get_current_buf() local display_items = { - { width = 25 }, -- symbol - { width = 8 }, -- symbol type - { remaining = true }, -- filename{:optional_lnum+col} OR content preview + { width = opts.symbol_width or 25 }, -- symbol + { width = opts.symbol_type_width or 8 }, -- symbol type + { remaining = true }, -- filename{:optional_lnum+col} OR content preview } if opts.ignore_filename and opts.show_line then @@ -834,6 +849,8 @@ function make_entry.gen_from_vimoptions() end end +--- Special options: +--- - only_sort_tags: Only sort via tag name. Ignore filename and other items function make_entry.gen_from_ctags(opts) opts = opts or {} @@ -893,10 +910,17 @@ function make_entry.gen_from_ctags(opts) return nil end + local ordinal + + if opts.only_sort_tags then + ordinal = tag + else + ordinal = file .. ': ' .. tag + end + return { valid = true, - - ordinal = file .. ': ' .. tag, + ordinal = ordinal, display = make_display, scode = scode, tag = tag, @@ -1130,5 +1154,61 @@ function make_entry.gen_from_git_status(opts) end end +function make_entry.gen_from_jumplist(opts) + opts = opts or {} + opts.tail_path = get_default(opts.tail_path, true) + + local displayer = entry_display.create { + separator = "▏", + items = { + { width = 10 }, + { remaining = true }, + } + } + + local make_display = function(entry) + local filename + if not opts.hide_filename then + filename = entry.filename + if opts.tail_path then + filename = utils.path_tail(filename) + elseif opts.shorten_path then + filename = utils.path_shorten(filename) + end + end + + local line_info = {table.concat({entry.lnum, entry.col}, ":"), "TelescopeResultsLineNr"} + + return displayer { + line_info, + filename, + } + end + + return function(entry) + if not vim.api.nvim_buf_is_valid(entry.bufnr) then + return + end + + local filename = entry.filename or vim.api.nvim_buf_get_name(entry.bufnr) + + return { + valid = true, + + value = entry, + ordinal = ( + not opts.ignore_filename and filename + or '' + ) .. ' ' .. entry.text, + display = make_display, + + bufnr = entry.bufnr, + filename = filename, + lnum = entry.lnum, + col = entry.col, + } + end +end + return make_entry diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index 3a5baf7d23..7613daf56c 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -386,7 +386,7 @@ function Picker:find() if not last_line then last_line = 1 end if first_line > 0 or last_line > 1 then - log.debug("ON_LINES: Bad range", first_line, last_line) + log.debug("ON_LINES: Bad range", first_line, last_line, self:_get_prompt()) return end @@ -502,7 +502,7 @@ function Picker.close_windows(status) local preview_border_win = status.preview_border_win local function del_win(name, win_id, force, bdelete) - if not vim.api.nvim_win_is_valid(win_id) then + if win_id == nil or not vim.api.nvim_win_is_valid(win_id) then return end @@ -618,9 +618,15 @@ function Picker:change_prompt_prefix(new_prefix, hl_group) self:_reset_prefix_color(hl_group) end -function Picker:reset_prompt() - vim.api.nvim_buf_set_lines(self.prompt_bufnr, 0, -1, false, { self.prompt_prefix }) +function Picker:reset_prompt(text) + local prompt_text = self.prompt_prefix .. (text or '') + vim.api.nvim_buf_set_lines(self.prompt_bufnr, 0, -1, false, { prompt_text }) + self:_reset_prefix_color(self._current_prefix_hl_group) + + if text then + vim.api.nvim_win_set_cursor(self.prompt_win, {1, #prompt_text}) + end end --- opts.new_prefix: Either as string or { new_string, hl_group } @@ -634,7 +640,10 @@ function Picker:refresh(finder, opts) if opts.reset_prompt then self:reset_prompt() end self.finder:close() - if finder then self.finder = finder end + if finder then + self.finder = finder + self._multi = MultiSelect:new() + end self.__on_lines(nil, nil, nil, 0, 1) end @@ -1045,10 +1054,9 @@ function pickers.on_close_prompt(prompt_bufnr) picker.close_windows(status) end +--- Get the prompt text without the prompt prefix. function Picker:_get_prompt() - return vim.trim( - vim.api.nvim_buf_get_lines(self.prompt_bufnr, 0, 1, false)[1]:sub(#self.prompt_prefix + 1) - ) + return vim.api.nvim_buf_get_lines(self.prompt_bufnr, 0, 1, false)[1]:sub(#self.prompt_prefix + 1) end function Picker:_reset_highlights() diff --git a/lua/telescope/pickers/layout_strategies.lua b/lua/telescope/pickers/layout_strategies.lua index b4c9fda560..21994b3097 100644 --- a/lua/telescope/pickers/layout_strategies.lua +++ b/lua/telescope/pickers/layout_strategies.lua @@ -61,38 +61,7 @@ local config = require('telescope.config') local resolve = require("telescope.config.resolve") -local function get_initial_window_options(picker) - local popup_border = resolve.win_option(picker.window.border) - local popup_borderchars = resolve.win_option(picker.window.borderchars) - - local preview = { - title = picker.preview_title, - border = popup_border.preview, - borderchars = popup_borderchars.preview, - enter = false, - highlight = false - } - - local results = { - title = picker.results_title, - border = popup_border.results, - borderchars = popup_borderchars.results, - enter = false, - } - - local prompt = { - title = picker.prompt_title, - border = popup_border.prompt, - borderchars = popup_borderchars.prompt, - enter = true - } - - return { - preview = preview, - results = results, - prompt = prompt, - } -end +local p_window = require('telescope.pickers.window') -- Check if there are any borders. Right now it's a little raw as @@ -139,7 +108,7 @@ layout_strategies.horizontal = function(self, max_columns, max_lines) scroll_speed = "The speed when scrolling through the previewer", }) - local initial_options = get_initial_window_options(self) + local initial_options = p_window.get_initial_window_options(self) local preview = initial_options.preview local results = initial_options.results local prompt = initial_options.prompt @@ -237,7 +206,7 @@ end --- +--------------+ --- layout_strategies.center = function(self, columns, lines) - local initial_options = get_initial_window_options(self) + local initial_options = p_window.get_initial_window_options(self) local preview = initial_options.preview local results = initial_options.results local prompt = initial_options.prompt @@ -307,7 +276,7 @@ layout_strategies.vertical = function(self, max_columns, max_lines) scroll_speed = "The speed when scrolling through the previewer", }) - local initial_options = get_initial_window_options(self) + local initial_options = p_window.get_initial_window_options(self) local preview = initial_options.preview local results = initial_options.results local prompt = initial_options.prompt @@ -447,4 +416,67 @@ layout_strategies.current_buffer = function(self, _, _) } end +layout_strategies.bottom_pane = function(self, max_columns, max_lines) + local layout_config = validate_layout_config(self.layout_config or {}, { + height = "The height of the layout", + }) + + local initial_options = p_window.get_initial_window_options(self) + local results = initial_options.results + local prompt = initial_options.prompt + local preview = initial_options.preview + + local result_height = layout_config.height or 25 + + local prompt_width = max_columns + local col = 0 + + local has_border = not not self.window.border + if has_border then + col = 1 + prompt_width = prompt_width - 2 + end + + local result_width + if self.previewer then + result_width = math.floor(prompt_width / 2) + + local base_col = result_width + 1 + if has_border then + preview = vim.tbl_deep_extend("force", { + col = base_col + 2, + line = max_lines - result_height + 1, + width = prompt_width - result_width - 2, + height = result_height - 1, + }, preview) + else + preview = vim.tbl_deep_extend("force", { + col = base_col, + line = max_lines - result_height, + width = prompt_width - result_width, + height = result_height, + }, preview) + end + else + preview = nil + result_width = prompt_width + end + + return { + preview = preview, + prompt = vim.tbl_deep_extend("force", prompt, { + line = max_lines - result_height - 1, + col = col, + height = 1, + width = prompt_width, + }), + results = vim.tbl_deep_extend("force", results, { + line = max_lines - result_height, + col = col, + height = result_height, + width = result_width, + }), + } +end + return layout_strategies diff --git a/lua/telescope/pickers/window.lua b/lua/telescope/pickers/window.lua index 76c1fe0d09..533fe30f48 100644 --- a/lua/telescope/pickers/window.lua +++ b/lua/telescope/pickers/window.lua @@ -1,10 +1,10 @@ -local p_layouts = require('telescope.pickers.layout_strategies') +local resolve = require("telescope.config.resolve") local p_window = {} function p_window.get_window_options(picker, max_columns, max_lines) local layout_strategy = picker.layout_strategy - local getter = p_layouts[layout_strategy] + local getter = require('telescope.pickers.layout_strategies')[layout_strategy] if not getter then error("Not a valid layout strategy: " .. layout_strategy) @@ -13,5 +13,38 @@ function p_window.get_window_options(picker, max_columns, max_lines) return getter(picker, max_columns, max_lines) end +function p_window.get_initial_window_options(picker) + local popup_border = resolve.win_option(picker.window.border) + local popup_borderchars = resolve.win_option(picker.window.borderchars) + + local preview = { + title = picker.preview_title, + border = popup_border.preview, + borderchars = popup_borderchars.preview, + enter = false, + highlight = false + } + + local results = { + title = picker.results_title, + border = popup_border.results, + borderchars = popup_borderchars.results, + enter = false, + } + + local prompt = { + title = picker.prompt_title, + border = popup_border.prompt, + borderchars = popup_borderchars.prompt, + enter = true + } + + return { + preview = preview, + results = results, + prompt = prompt, + } +end + return p_window diff --git a/lua/telescope/previewers/buffer_previewer.lua b/lua/telescope/previewers/buffer_previewer.lua index d480e0c1bf..52710d8c76 100644 --- a/lua/telescope/previewers/buffer_previewer.lua +++ b/lua/telescope/previewers/buffer_previewer.lua @@ -185,7 +185,9 @@ previewers.new_buffer_previewer = function(opts) local bufnr = vim.api.nvim_create_buf(false, true) set_bufnr(self, bufnr) - vim.api.nvim_win_set_buf(status.preview_win, bufnr) + vim.schedule(function() + vim.api.nvim_win_set_buf(status.preview_win, bufnr) + end) -- TODO(conni2461): We only have to set options once. Right? vim.api.nvim_win_set_option(status.preview_win, 'winhl', 'Normal:TelescopePreviewNormal') @@ -502,6 +504,23 @@ previewers.git_branch_log = defaulter(function(opts) } end, {}) +previewers.git_stash_diff = defaulter(function(opts) + return previewers.new_buffer_previewer { + get_buffer_by_name = function(_, entry) + return entry.value + end, + + define_preview = function(self, entry, _) + putils.job_maker({ 'git', '--no-pager', 'stash', 'show', '-p', entry.value }, self.state.bufnr, { + value = entry.value, + bufname = self.state.bufname, + cwd = opts.cwd + }) + putils.regex_highlighter(self.state.bufnr, 'diff') + end + } +end, {}) + previewers.git_commit_diff = defaulter(function(opts) return previewers.new_buffer_previewer { get_buffer_by_name = function(_, entry) diff --git a/lua/telescope/previewers/init.lua b/lua/telescope/previewers/init.lua index db801ea751..853ad5b613 100644 --- a/lua/telescope/previewers/init.lua +++ b/lua/telescope/previewers/init.lua @@ -263,6 +263,7 @@ previewers.vim_buffer_qflist = buffer_previewer.qflist previewers.git_branch_log = buffer_previewer.git_branch_log previewers.git_commit_diff = buffer_previewer.git_commit_diff previewers.git_file_diff = buffer_previewer.git_file_diff +previewers.git_stash_diff = buffer_previewer.git_stash_diff previewers.ctags = buffer_previewer.ctags diff --git a/lua/telescope/themes.lua b/lua/telescope/themes.lua index eba3d119f6..f2af1a7f7b 100644 --- a/lua/telescope/themes.lua +++ b/lua/telescope/themes.lua @@ -2,10 +2,27 @@ -- Currently certain designs need a number of parameters. -- -- local opts = themes.get_dropdown { winblend = 3 } --- + +---@tag telescope.themes + +---@brief [[ +--- Themes are ways to combine several elements of styling together. +--- +--- They are helpful for managing the several differnt UI aspects for telescope and provide +--- a simple interface for users to get a particular "style" of picker. +---@brief ]] local themes = {} +--- Dropdown style theme. +---
+---
+--- Usage:
+---
+---     `local builtin = require('telescope.builtin')`
+---     `local themes = require('telescope.themes')`
+---     `builtin.find_files(themes.get_dropdown())`
+--- 
function themes.get_dropdown(opts) opts = opts or {} @@ -21,14 +38,49 @@ function themes.get_dropdown(opts) width = 80, results_height = 15, borderchars = { - { '─', '│', '─', '│', '╭', '╮', '╯', '╰'}, + { "─", "│", "─", "│", "╭", "╮", "╯", "╰"}, prompt = {"─", "│", " ", "│", "╭", "╮", "│", "│"}, results = {"─", "│", "─", "│", "├", "┤", "╯", "╰"}, - preview = { '─', '│', '─', '│', '╭', '╮', '╯', '╰'}, + preview = { "─", "│", "─", "│", "╭", "╮", "╯", "╰"}, }, } return vim.tbl_deep_extend("force", theme_opts, opts) end +--- Ivy style theme. +---
+---
+--- Usage:
+---
+---     `local builtin = require('telescope.builtin')`
+---     `local themes = require('telescope.themes')`
+---     `builtin.find_files(themes.get_ivy())`
+--- 
+function themes.get_ivy(opts) + opts = opts or {} + + return vim.tbl_deep_extend("force", { + theme = "ivy", + + sorting_strategy = "ascending", + + preview_title = "", + + layout_strategy = "bottom_pane", + layout_config = { + height = 25, + }, + + border = true, + borderchars = { + "z", + prompt = { "─", " ", " ", " ", "─", "─", " ", " " }, + results = { " " }, + -- results = { "a", "b", "c", "d", "e", "f", "g", "h" }, + preview = { "─", "│", "─", "│", "╭", "╮", "╯", "╰"}, + }, + }, opts) +end + return themes diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index 4e8c1d3f37..85e8723f3e 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -259,13 +259,14 @@ utils.strdisplaywidth = (function() return function(str, col) local startcol = col or 0 + str = tostring(str) local s = ffi.new('char[?]', #str + 1) ffi.copy(s, str) return ffi.C.linetabsize_col(startcol, s) - startcol end else return function(str, col) - return #str - (col or 0) + return #(tostring(str)) - (col or 0) end end end)() diff --git a/lua/tests/manual/all_defaults.lua b/lua/tests/manual/all_defaults.lua index ba77786589..6aea199f91 100644 --- a/lua/tests/manual/all_defaults.lua +++ b/lua/tests/manual/all_defaults.lua @@ -11,6 +11,7 @@ require('telescope.builtin').lsp_references() require('telescope.builtin').builtin() require('telescope.builtin').fd() require('telescope.builtin').command_history() +require('telescope.builtin').search_history() require('telescope.builtin').live_grep() require('telescope.builtin').loclist() diff --git a/plugin/telescope.vim b/plugin/telescope.vim index 2eea7b1073..011b6e3fe8 100644 --- a/plugin/telescope.vim +++ b/plugin/telescope.vim @@ -1,3 +1,8 @@ +if !has('nvim-0.5') + echoerr "Telescope.nvim requires at least nvim-0.5. Please update or uninstall" + finish +end + if exists('g:loaded_telescope') finish endif diff --git a/scratch/clason_finders.lua b/scratch/clason_finders.lua index bf9a9e49ba..ee25d8f773 100644 --- a/scratch/clason_finders.lua +++ b/scratch/clason_finders.lua @@ -60,7 +60,11 @@ finders.lsp_references = function(opts) local params = vim.lsp.util.make_position_params() params.context = { includeDeclaration = false } - local results_lsp = vim.lsp.buf_request_sync(0, "textDocument/references", params) + local results_lsp, err = vim.lsp.buf_request_sync(0, "textDocument/references", params) + if err then + vim.api.nvim_err_writeln("Error when finding references: " .. err) + return + end local locations = {} for _, server_results in pairs(results_lsp) do vim.list_extend(locations, vim.lsp.util.locations_to_items(server_results.result) or {}) @@ -83,7 +87,11 @@ end -- fuzzy find in document symbols finders.lsp_document_symbols = function(opts) local params = vim.lsp.util.make_position_params() - local results_lsp = vim.lsp.buf_request_sync(0, "textDocument/documentSymbol", params) + local results_lsp, err = vim.lsp.buf_request_sync(0, "textDocument/documentSymbol", params) + if err then + vim.api.nvim_err_writeln("Error when finding document symbols: " .. err) + return + end local locations = {} for _, server_results in pairs(results_lsp) do vim.list_extend(locations, vim.lsp.util.symbols_to_items(server_results.result, 0) or {}) @@ -106,7 +114,11 @@ end -- fuzzy find in all workspace symbols (may need longer timeout!) finders.lsp_workspace_symbols = function(opts) local params = {query = ''} - local results_lsp = vim.lsp.buf_request_sync(0, "workspace/symbol", params, 1000) + local results_lsp, err = vim.lsp.buf_request_sync(0, "workspace/symbol", params, 1000) + if err then + vim.api.nvim_err_writeln("Error when finding symbols: " .. err) + return + end local locations = {} for _, server_results in pairs(results_lsp) do vim.list_extend(locations, vim.lsp.util.symbols_to_items(server_results.result, 0) or {}) diff --git a/scratch/ivy.lua b/scratch/ivy.lua new file mode 100644 index 0000000000..65d90499ad --- /dev/null +++ b/scratch/ivy.lua @@ -0,0 +1,31 @@ + +RELOAD('telescope') +require('telescope.builtin').find_files(require('telescope.themes').get_ivy { previewer = false }) + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/scripts/gendocs.lua b/scripts/gendocs.lua index 7ac3b3e0f7..53423f278f 100644 --- a/scripts/gendocs.lua +++ b/scripts/gendocs.lua @@ -12,7 +12,10 @@ docs.test = function() "./lua/telescope/builtin/init.lua", "./lua/telescope/pickers/layout_strategies.lua", "./lua/telescope/actions/init.lua", + "./lua/telescope/actions/state.lua", + "./lua/telescope/actions/set.lua", "./lua/telescope/previewers/init.lua", + "./lua/telescope/themes.lua", } table.sort(input_files, function(a, b)