|
| 1 | +--[[ |
| 2 | + This script allows you to quickly search for a selected piece of code |
| 3 | + 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 |
| 5 | + it, and constructs a Sourcegraph search URL. The search is also tailored |
| 6 | + to the file type you're currently working with in Neovim. The script then |
| 7 | + opens the constructed URL in your default web browser, taking you straight |
| 8 | + to the search results on Sourcegraph. This helps you swiftly find usage |
| 9 | + examples, definitions, and other relevant information about the selected |
| 10 | + piece of code on Sourcegraph. |
| 11 | +]] |
| 12 | + |
| 13 | +-- Disclaimer: Written with the help of ChatGPT 4 |
| 14 | + |
| 15 | +-- Create a module M. |
| 16 | +local M = {} |
| 17 | + |
| 18 | +-- Function to convert a character to its hexadecimal value. |
| 19 | +local function char_to_hex(c) |
| 20 | + return string.format("%%%02X", string.byte(c)) |
| 21 | +end |
| 22 | + |
| 23 | +-- Function to URL encode a string. |
| 24 | +-- From https://gist.github.com/liukun/f9ce7d6d14fa45fe9b924a3eed5c3d99 |
| 25 | +local function url_encode(url) |
| 26 | + -- If url is nil, return nil. |
| 27 | + if url == nil then |
| 28 | + return |
| 29 | + end |
| 30 | + |
| 31 | + -- Replace newline characters with carriage return + newline. |
| 32 | + url = url:gsub("\n", "\r\n") |
| 33 | + |
| 34 | + -- URL encode non-alphanumeric characters. |
| 35 | + url = url:gsub("([^%w ])", char_to_hex) |
| 36 | + |
| 37 | + -- Replace spaces with '+'. |
| 38 | + url = url:gsub(" ", "+") |
| 39 | + return url |
| 40 | +end |
| 41 | + |
| 42 | +-- Main function for searching Sourcegraph with the selected text. |
| 43 | +function M.search_sourcegraph() |
| 44 | + -- Get the start and end position of the selected text. |
| 45 | + local _, vs_line, vs_col, _ = unpack(vim.fn.getpos("'<")) |
| 46 | + local _, ve_line, ve_col, _ = unpack(vim.fn.getpos("'>")) |
| 47 | + |
| 48 | + -- Get the selected text. |
| 49 | + local selected_text = vim.fn.getline(vs_line, ve_line) |
| 50 | + |
| 51 | + -- If the selected text is only one line, just get the selected part. |
| 52 | + -- If it's multiple lines, concatenate them with newline characters. |
| 53 | + if #selected_text == 1 then |
| 54 | + selected_text = selected_text[1]:sub(vs_col, ve_col) |
| 55 | + else |
| 56 | + selected_text[1] = selected_text[1]:sub(vs_col) |
| 57 | + selected_text[#selected_text] = selected_text[#selected_text]:sub(1, ve_col) |
| 58 | + selected_text = table.concat(selected_text, '\n') |
| 59 | + end |
| 60 | + |
| 61 | + -- If no text was selected, print an error message and return. |
| 62 | + if not selected_text then |
| 63 | + print("Error: No text selected.") |
| 64 | + return |
| 65 | + end |
| 66 | + |
| 67 | + -- URL encode the selected text. |
| 68 | + local encoded_text = url_encode(selected_text) |
| 69 | + |
| 70 | + -- Get the filetype of the current buffer. |
| 71 | + local filetype = vim.bo.filetype |
| 72 | + local lang_query = "" |
| 73 | + |
| 74 | + -- Lookup table for matching Neovim filetypes to Sourcegraph language names. |
| 75 | + local filetype_to_sourcegraph_lang = { |
| 76 | + go = "Go", |
| 77 | + python = "Python", |
| 78 | + javascript = "JavaScript", |
| 79 | + cpp = "C++", |
| 80 | + markdown = "Markdown", |
| 81 | + lua = "Lua", |
| 82 | + html = "HTML" |
| 83 | + -- Add more file types as needed |
| 84 | + } |
| 85 | + |
| 86 | + -- If the filetype is found in the lookup table, append it to the Sourcegraph query. |
| 87 | + if filetype_to_sourcegraph_lang[filetype] then |
| 88 | + lang_query = "+lang:" .. filetype_to_sourcegraph_lang[filetype] |
| 89 | + else |
| 90 | + -- If not, print a warning message. |
| 91 | + print("Warning: Filetype " .. filetype .. " not found in Sourcegraph language dictionary.") |
| 92 | + end |
| 93 | + |
| 94 | + -- Construct the Sourcegraph URL. |
| 95 | + local url = "https://sourcegraph.com/search?q=context:global+" .. encoded_text .. lang_query .. "&patternType=standard&sm=1" |
| 96 | + |
| 97 | + -- Determine the operating system and use the appropriate command to open the URL in the default browser. |
| 98 | + local OS = string.lower(jit.os) |
| 99 | + if OS == "osx" then |
| 100 | + os.execute('open ' .. url .. ' &') |
| 101 | + elseif OS == "linux" then |
| 102 | + os.execute('xdg-open ' .. url .. ' &') |
| 103 | + elseif OS == "windows" then |
| 104 | + os.execute('start ' .. url) |
| 105 | + else |
| 106 | + print("Error: Unrecognized OS.") |
| 107 | + end |
| 108 | +end |
| 109 | + |
| 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 | + |
| 115 | +return M |
0 commit comments