Skip to content

Commit

Permalink
add option for regex (jupyterlab-contrib#18)
Browse files Browse the repository at this point in the history
* add option for regex

* Update -F flag default usage

Co-authored-by: Frédéric Collonval <fcollonval@users.noreply.github.com>

Co-authored-by: Frédéric Collonval <fcollonval@users.noreply.github.com>
  • Loading branch information
madhur-tandon and fcollonval committed Feb 25, 2022
1 parent bad83b3 commit 9962071
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 38 deletions.
10 changes: 9 additions & 1 deletion jupyterlab_search_replace/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ async def get(self, path: str = ""):
whole_word = self.get_query_argument("whole_word", False)
include = self.get_query_argument("include", None)
exclude = self.get_query_argument("exclude", None)
use_regex = self.get_query_argument("use_regex", False)
r = await self._engine.search(
query, path, max_count, case_sensitive, whole_word, include, exclude
query,
path,
max_count,
case_sensitive,
whole_word,
include,
exclude,
use_regex,
)

if r.get("code") is not None:
Expand Down
12 changes: 9 additions & 3 deletions jupyterlab_search_replace/search_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
MAX_LOG_OUTPUT = 6000 # type: int


def construct_command(query, max_count, case_sensitive, whole_word, include, exclude):
command = ["rg", "-e", query, "--json", f"--max-count={max_count}"]
def construct_command(
query, max_count, case_sensitive, whole_word, include, exclude, use_regex
):
command = ["rg", "-F", query, "--json", f"--max-count={max_count}"]
if use_regex:
command.remove("-F")
if not case_sensitive:
command.append("--ignore-case")
if whole_word:
Expand All @@ -36,6 +40,7 @@ def construct_command(query, max_count, case_sensitive, whole_word, include, exc
if exclude and type(exclude) == str:
command.append("-g")
command.append(f"!{exclude}")

return command


Expand Down Expand Up @@ -109,11 +114,12 @@ async def search(
whole_word: bool = False,
include: Optional[str] = None,
exclude: Optional[str] = None,
use_regex: bool = False,
):
""""""
# JSON output is described at https://docs.rs/grep-printer/0.1.0/grep_printer/struct.JSON.html
command = construct_command(
query, max_count, case_sensitive, whole_word, include, exclude
query, max_count, case_sensitive, whole_word, include, exclude, use_regex
)
cwd = os.path.join(self._root_dir, url2path(path))
code, output = await self._execute(command, cwd=cwd)
Expand Down
2 changes: 1 addition & 1 deletion jupyterlab_search_replace/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_content(jp_root_dir):
test_file.write_text(
"\n".join(
[
"Unicode histrange file, very strange",
"Unicode histrange file, very str.*ange",
"ü notebook with λ",
"Is that Strange enough?",
]
Expand Down
122 changes: 89 additions & 33 deletions jupyterlab_search_replace/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async def test_search_get(test_content, schema, jp_fetch):
payload = json.loads(response.body)
validate(instance=payload, schema=schema)
assert len(payload["matches"]) == 2
assert len(payload["matches"][0]["matches"]) == 3
assert len(payload["matches"][0]["matches"]) == 2
assert len(payload["matches"][1]["matches"]) == 3
assert sorted(payload["matches"], key=lambda x: x["path"]) == [
{
Expand Down Expand Up @@ -47,28 +47,20 @@ async def test_search_get(test_content, schema, jp_fetch):
"path": "test_lab_search_replace/text_1.txt",
"matches": [
{
"line": "Unicode histrange file, very strange\n",
"line": "Unicode histrange file, very str.*ange\n",
"match": "strange",
"start": 10,
"end": 17,
"line_number": 1,
"absolute_offset": 0,
},
{
"line": "Unicode histrange file, very strange\n",
"match": "strange",
"start": 29,
"end": 36,
"line_number": 1,
"absolute_offset": 0,
},
{
"line": "Is that Strange enough?",
"match": "Strange",
"start": 8,
"end": 15,
"line_number": 3,
"absolute_offset": 57,
"absolute_offset": 59,
},
],
},
Expand Down Expand Up @@ -102,7 +94,7 @@ async def test_search_case_sensitive(test_content, schema, jp_fetch):
"start": 8,
"end": 15,
"line_number": 3,
"absolute_offset": 57,
"absolute_offset": 59,
},
],
}
Expand All @@ -117,7 +109,7 @@ async def test_search_whole_word(test_content, schema, jp_fetch):
payload = json.loads(response.body)
validate(instance=payload, schema=schema)
assert len(payload["matches"]) == 2
assert len(payload["matches"][0]["matches"]) == 2
assert len(payload["matches"][0]["matches"]) == 1
assert len(payload["matches"][1]["matches"]) == 3
assert sorted(payload["matches"], key=lambda x: x["path"]) == [
{
Expand Down Expand Up @@ -152,21 +144,13 @@ async def test_search_whole_word(test_content, schema, jp_fetch):
{
"path": "test_lab_search_replace/text_1.txt",
"matches": [
{
"line": "Unicode histrange file, very strange\n",
"match": "strange",
"start": 29,
"end": 36,
"line_number": 1,
"absolute_offset": 0,
},
{
"line": "Is that Strange enough?",
"match": "Strange",
"start": 8,
"end": 15,
"line_number": 3,
"absolute_offset": 57,
"absolute_offset": 59,
},
],
},
Expand All @@ -181,34 +165,26 @@ async def test_search_include_files(test_content, schema, jp_fetch):
payload = json.loads(response.body)
validate(instance=payload, schema=schema)
assert len(payload["matches"]) == 1
assert len(payload["matches"][0]["matches"]) == 3
assert len(payload["matches"][0]["matches"]) == 2
assert sorted(payload["matches"], key=lambda x: x["path"]) == [
{
"path": "test_lab_search_replace/text_1.txt",
"matches": [
{
"line": "Unicode histrange file, very strange\n",
"line": "Unicode histrange file, very str.*ange\n",
"match": "strange",
"start": 10,
"end": 17,
"line_number": 1,
"absolute_offset": 0,
},
{
"line": "Unicode histrange file, very strange\n",
"match": "strange",
"start": 29,
"end": 36,
"line_number": 1,
"absolute_offset": 0,
},
{
"line": "Is that Strange enough?",
"match": "Strange",
"start": 8,
"end": 15,
"line_number": 3,
"absolute_offset": 57,
"absolute_offset": 59,
},
],
},
Expand Down Expand Up @@ -265,3 +241,83 @@ async def test_search_include_and_exclude_files(test_content, schema, jp_fetch):
method="GET",
)
assert str(excinfo.value) == "HTTP 500: Internal Server Error"


async def test_search_literal(test_content, schema, jp_fetch):
response = await jp_fetch("search", params={"query": "str.*"}, method="GET")
assert response.code == 200
payload = json.loads(response.body)
validate(instance=payload, schema=schema)
assert len(payload["matches"]) == 1
assert len(payload["matches"][0]["matches"]) == 1
assert sorted(payload["matches"], key=lambda x: x["path"]) == [
{
"path": "test_lab_search_replace/text_1.txt",
"matches": [
{
"line": "Unicode histrange file, very str.*ange\n",
"match": "str.*",
"start": 29,
"end": 34,
"line_number": 1,
"absolute_offset": 0,
},
],
},
]


async def test_search_regex(test_content, schema, jp_fetch):
response = await jp_fetch(
"search", params={"query": "str.*", "use_regex": True}, method="GET"
)
assert response.code == 200
payload = json.loads(response.body)
validate(instance=payload, schema=schema)
assert len(payload["matches"]) == 2
assert len(payload["matches"][0]["matches"]) == 2
assert len(payload["matches"][1]["matches"]) == 2
assert sorted(payload["matches"], key=lambda x: x["path"]) == [
{
"path": "test_lab_search_replace/subfolder/text_sub.txt",
"matches": [
{
"line": "Unicode strange sub file, very strange\n",
"match": "strange sub file, very strange",
"start": 8,
"end": 38,
"line_number": 1,
"absolute_offset": 0,
},
{
"line": "Is that λ strange enough?",
"match": "strange enough?",
"start": 11,
"end": 26,
"line_number": 3,
"absolute_offset": 57,
},
],
},
{
"path": "test_lab_search_replace/text_1.txt",
"matches": [
{
"line": "Unicode histrange file, very str.*ange\n",
"match": "strange file, very str.*ange",
"start": 10,
"end": 38,
"line_number": 1,
"absolute_offset": 0,
},
{
"line": "Is that Strange enough?",
"match": "Strange enough?",
"start": 8,
"end": 23,
"line_number": 3,
"absolute_offset": 59,
},
],
},
]

0 comments on commit 9962071

Please sign in to comment.