Skip to content

Commit e0abd50

Browse files
committed
Change it to an actual plugin
1 parent 41bc22e commit e0abd50

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

README.md

+24-25
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,49 @@
11
# Neovim Sourcegraph Integration
22

3-
This repository contains a Lua script (`sourcegraph.lua`) that integrates
3+
This repository contains a Lua plugin that integrates
44
[Sourcegraph](https://sourcegraph.com/) search into Neovim. This tool helps
55
developers search for code snippets directly from their Neovim environment.
66

77
## Features
88

9-
- Fetch selected code within Neovim
10-
- URL encode the fetched code for search
11-
- Construct a Sourcegraph URL including the selected code
129
- Open the URL in the default browser to show search results on Sourcegraph
1310
- Contextual search based on the file type of the current buffer
1411

1512
## Usage
1613

1714
After you've installed the script (see Installation section below), you can use
18-
it by simply selecting text in visual mode in Neovim and pressing `<leader>S`.
15+
it by simply selecting text in visual mode in Neovim and pressing `<leader>S`
16+
(default - see below to change keybinding).
1917
This will construct a Sourcegraph search URL with the selected text and open it
2018
in your default web browser.
2119

22-
## Installation
23-
24-
To install the script, follow these steps:
25-
26-
1. Clone the repository or download the `sourcegraph.lua` file.
27-
2. Move the `sourcegraph.lua` file to your Neovim configuration directory (usually `~/.config/nvim/lua`).
28-
3. Add the following line to your Neovim configuration file (`init.vim` or `init.lua`): `require'sourcegraph'`.
29-
4. Change the mapping to the location of your path
30-
31-
E.g.
20+
### Customizing the Key Binding
3221

33-
```sh
34-
vnoremap <leader>S :lua require'philiplinell.sourcegraph'.search_sourcegraph()<CR>
22+
By default, the key binding to trigger a Sourcegraph search is `<leader>S`. If
23+
you want to use a different key binding, you can set the `g:sourcegraph_key`
24+
variable in your `.vimrc` or `init.vim` file. For example, to use `<leader>sg`
25+
as the key binding, add this line:
3526

27+
```vim
28+
let g:sourcegraph_key = '<leader>sg'
3629
```
3730

38-
My path is `config/nvim/lua/philiplinell/sourcegraph.lua`.
31+
## Installation
3932

40-
## Contributing
33+
Using vim-plug, add the following to your `init.vim` or `.vimrc`:
4134

42-
Contributions are welcome! Please submit a pull request or open an issue if you have any improvements or suggestions.
35+
`Plug 'philiplinell/sourcegraph.lua'`
4336

44-
## Disclaimer
37+
Save the file and run the `:PlugInstall` command in Neovim.
4538

46-
I am not used to the neovim API, lua or sourcegraph!
39+
## Disclaimer
4740

48-
The majority of `sourcegraph.lua` was written with the help of ChatGPT, as well
49-
as the majority of this README.
41+
This is my first Neovim plugin, and I'm still learning the Neovim API, Lua, and
42+
Sourcegraph. I've had a lot of help from ChatGPT, OpenAI's language model, both
43+
for writing the sourcegraph.lua script and for creating this README.
5044

51-
These are some of the prompts that I used
45+
I've listed some of the prompts I used with ChatGPT below, to give an idea of
46+
how it helped create this plugin:
5247

5348
```
5449
I would like to create a neovim plugin, in lua, that takes the text I have selected in visual mode and open that in a browser.
@@ -61,3 +56,7 @@ https://sourcegraph.com/search?q=context:global+QUERY&patternType=standard&sm=1
6156
```
6257
I'll create a repository that has the sourcegraph.lua file. Can you suggest a readme for the repository?
6358
```
59+
60+
If you have any suggestions or improvements, or if you find any issues, please
61+
don't hesitate to open an issue or a pull request. Any contributions are very
62+
welcome!

sourcegraph.lua lua/sourcegraph.lua

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--[[
22
This script allows you to quickly search for a selected piece of code
33
in Sourcegraph directly from Neovim. When you highlight text in visual
4-
mode and press <leader>S, the script fetches the selected text, URL encodes
4+
mode and press <leader>S (default), the script fetches the selected text, URL encodes
55
it, and constructs a Sourcegraph search URL. The search is also tailored
66
to the file type you're currently working with in Neovim. The script then
77
opens the constructed URL in your default web browser, taking you straight
@@ -107,9 +107,4 @@ function M.search_sourcegraph()
107107
end
108108
end
109109

110-
-- Map the function to the <leader>S key in visual mode.
111-
vim.api.nvim_exec([[
112-
vnoremap <leader>S :lua require'philiplinell.sourcegraph'.search_sourcegraph()<CR>
113-
]], false)
114-
115110
return M

plugin/sourcegraph.vim

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
" Default keybinding
2+
if !exists('g:sourcegraph_key')
3+
let g:sourcegraph_key = '<leader>S'
4+
endif
5+
6+
if has('nvim')
7+
execute 'vnoremap' g:sourcegraph_key ':lua require"sourcegraph".search_sourcegraph()<CR>'
8+
endif
9+

0 commit comments

Comments
 (0)