feat: ✨ Search context provider with ripgrep
This commit is contained in:
parent
40da28cb29
commit
4b9dd4cf8e
3 changed files with 104 additions and 1 deletions
12
continuedev/poetry.lock
generated
12
continuedev/poetry.lock
generated
|
|
@ -1264,6 +1264,16 @@ urllib3 = ">=1.21.1,<1.27"
|
|||
socks = ["PySocks (>=1.5.6,!=1.5.7)"]
|
||||
use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"]
|
||||
|
||||
[[package]]
|
||||
name = "ripgrepy"
|
||||
version = "2.0.0"
|
||||
description = ""
|
||||
optional = false
|
||||
python-versions = "*"
|
||||
files = [
|
||||
{file = "ripgrepy-2.0.0.tar.gz", hash = "sha256:6dd871bafe859301097354d1f171540fbc9bd38d3f8f52f8a196dc28522085da"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "six"
|
||||
version = "1.16.0"
|
||||
|
|
@ -1767,4 +1777,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p
|
|||
[metadata]
|
||||
lock-version = "2.0"
|
||||
python-versions = "^3.8.1"
|
||||
content-hash = "482e5ed42e46d4b422302ab5718a52c6765f5471155ee8ba0885f9436a5be565"
|
||||
content-hash = "aacc9ed670fbe067caccc88bf35f1a36541ed3b9446f64fdff7f36fe9b7b558a"
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ psutil = "^5.9.5"
|
|||
pygithub = "^1.59.0"
|
||||
meilisearch-python-async = "^1.4.8"
|
||||
socksio = "^1.0.0"
|
||||
ripgrepy = "^2.0.0"
|
||||
|
||||
[tool.poetry.scripts]
|
||||
typegen = "src.continuedev.models.generate_json_schema:main"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,92 @@
|
|||
import os
|
||||
from typing import List
|
||||
from ripgrepy import Ripgrepy
|
||||
|
||||
from .util import remove_meilisearch_disallowed_chars
|
||||
from ...core.main import ContextItem, ContextItemDescription, ContextItemId
|
||||
from ...core.context import ContextProvider
|
||||
|
||||
|
||||
class SearchContextProvider(ContextProvider):
|
||||
title = "search"
|
||||
|
||||
SEARCH_CONTEXT_ITEM_ID = "search"
|
||||
|
||||
workspace_dir: str = None
|
||||
|
||||
@property
|
||||
def BASE_CONTEXT_ITEM(self):
|
||||
return ContextItem(
|
||||
content="",
|
||||
description=ContextItemDescription(
|
||||
name="Search",
|
||||
description="Enter a query to search the current workspace",
|
||||
id=ContextItemId(
|
||||
provider_title=self.title,
|
||||
item_id=self.SEARCH_CONTEXT_ITEM_ID
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
def _get_rg_path(self):
|
||||
if os.name == 'nt':
|
||||
rg_path = f"C:\\Users\\{os.getlogin()}\\AppData\\Local\\Programs\\Microsoft VS Code\\resources\\app\\node_modules.asar.unpacked\\vscode-ripgrep\\bin\\rg.exe"
|
||||
elif os.name == 'posix':
|
||||
if 'darwin' in os.sys.platform:
|
||||
rg_path = "/Applications/Visual Studio Code.app/Contents/Resources/app/node_modules.asar.unpacked/vscode-ripgrep/bin/rg"
|
||||
else:
|
||||
rg_path = "/usr/share/code/resources/app/node_modules.asar.unpacked/vscode-ripgrep/bin/rg"
|
||||
else:
|
||||
rg_path = "rg"
|
||||
|
||||
if not os.path.exists(rg_path):
|
||||
rg_path = "rg"
|
||||
|
||||
return rg_path
|
||||
|
||||
async def _search(self, query: str) -> str:
|
||||
rg = Ripgrepy(query, self.workspace_dir, rg_path=self._get_rg_path())
|
||||
results = rg.I().context(2).run()
|
||||
return f"Search results in workspace for '{query}':\n\n{results}"
|
||||
|
||||
# Custom display below - TODO
|
||||
|
||||
# Gather results per file
|
||||
file_to_matches = {}
|
||||
for result in results:
|
||||
if result["type"] == "match":
|
||||
data = result["data"]
|
||||
filepath = data["path"]["text"]
|
||||
if filepath not in file_to_matches:
|
||||
file_to_matches[filepath] = []
|
||||
|
||||
line_num_and_line = f"{data['line_number']}: {data['lines']['text']}"
|
||||
file_to_matches[filepath].append(line_num_and_line)
|
||||
|
||||
# Format results
|
||||
content = f"Search results in workspace for '{query}':\n\n"
|
||||
for filepath, matches in file_to_matches.items():
|
||||
content += f"{filepath}\n"
|
||||
for match in matches:
|
||||
content += f"{match}\n"
|
||||
content += "\n"
|
||||
|
||||
return content
|
||||
|
||||
async def provide_context_items(self, workspace_dir: str) -> List[ContextItem]:
|
||||
self.workspace_dir = workspace_dir
|
||||
return [self.BASE_CONTEXT_ITEM]
|
||||
|
||||
async def get_item(self, id: ContextItemId, query: str) -> ContextItem:
|
||||
if not id.item_id == self.SEARCH_CONTEXT_ITEM_ID:
|
||||
raise Exception("Invalid item id")
|
||||
|
||||
query = query.lstrip("search ")
|
||||
results = await self._search(query)
|
||||
|
||||
ctx_item = self.BASE_CONTEXT_ITEM.copy()
|
||||
ctx_item.content = results
|
||||
ctx_item.description.name = f"Search: '{query}'"
|
||||
ctx_item.description.id.item_id = remove_meilisearch_disallowed_chars(
|
||||
query)
|
||||
return ctx_item
|
||||
Loading…
Add table
Reference in a new issue