mirror of
https://github.com/tiennm99/serena.git
synced 2026-07-16 20:18:31 +00:00
Move "search for pattern" functionality from LS to Project,
adding all necessary related functionality: * Handling of ignored files * Enumeration of source files Functions removed from SolidLanguageServer: * index_repository * request_parsed_files * search_files_for_pattern Implement SearchForPatternTool without language server
This commit is contained in:
committed by
Dominik Jain
parent
34db0f1446
commit
c4d89f21e7
@@ -9,6 +9,7 @@ import os
|
||||
|
||||
import pytest
|
||||
|
||||
from serena.project import Project
|
||||
from serena.text_utils import LineType
|
||||
from solidlsp import SolidLanguageServer
|
||||
from solidlsp.ls_config import Language
|
||||
@@ -185,42 +186,42 @@ class TestLanguageServerBasics:
|
||||
else:
|
||||
assert line.match_type == LineType.AFTER_MATCH
|
||||
|
||||
@pytest.mark.parametrize("language_server", [Language.PYTHON], indirect=True)
|
||||
def test_search_files_for_pattern(self, language_server: SolidLanguageServer) -> None:
|
||||
|
||||
class TestProjectBasics:
|
||||
@pytest.mark.parametrize("project", [Language.PYTHON], indirect=True)
|
||||
def test_search_files_for_pattern(self, project: Project) -> None:
|
||||
"""Test search_files_for_pattern with various patterns and glob filters."""
|
||||
# Test 1: Search for class definitions across all files
|
||||
class_pattern = r"class\s+\w+\s*(?:\([^{]*\)|:)"
|
||||
matches = language_server.search_files_for_pattern(class_pattern)
|
||||
matches = project.search_files_for_pattern(class_pattern)
|
||||
assert len(matches) > 0
|
||||
# Should find multiple classes like User, Item, BaseModel, etc.
|
||||
assert len(matches) >= 5
|
||||
|
||||
# Test 2: Search for specific class with include glob
|
||||
user_class_pattern = r"class\s+User\s*(?:\([^{]*\)|:)"
|
||||
matches = language_server.search_files_for_pattern(user_class_pattern, paths_include_glob="**/models.py")
|
||||
matches = project.search_files_for_pattern(user_class_pattern, paths_include_glob="**/models.py")
|
||||
assert len(matches) == 1 # Should only find User class in models.py
|
||||
assert matches[0].source_file_path is not None
|
||||
assert "models.py" in matches[0].source_file_path
|
||||
|
||||
# Test 3: Search for method definitions with exclude glob
|
||||
method_pattern = r"def\s+\w+\s*\([^)]*\):"
|
||||
matches = language_server.search_files_for_pattern(method_pattern, paths_exclude_glob="**/models.py")
|
||||
matches = project.search_files_for_pattern(method_pattern, paths_exclude_glob="**/models.py")
|
||||
assert len(matches) > 0
|
||||
# Should find methods in services.py but not in models.py
|
||||
assert all(match.source_file_path is not None and "models.py" not in match.source_file_path for match in matches)
|
||||
|
||||
# Test 4: Search for specific method with both include and exclude globs
|
||||
create_user_pattern = r"def\s+create_user\s*\([^)]*\)(?:\s*->[^:]+)?:"
|
||||
matches = language_server.search_files_for_pattern(
|
||||
create_user_pattern, paths_include_glob="**/*.py", paths_exclude_glob="**/models.py"
|
||||
)
|
||||
matches = project.search_files_for_pattern(create_user_pattern, paths_include_glob="**/*.py", paths_exclude_glob="**/models.py")
|
||||
assert len(matches) == 1 # Should only find create_user in services.py
|
||||
assert matches[0].source_file_path is not None
|
||||
assert "services.py" in matches[0].source_file_path
|
||||
|
||||
# Test 5: Search for a pattern that should appear in multiple files
|
||||
init_pattern = r"def\s+__init__\s*\([^)]*\):"
|
||||
matches = language_server.search_files_for_pattern(init_pattern)
|
||||
matches = project.search_files_for_pattern(init_pattern)
|
||||
assert len(matches) > 1 # Should find __init__ in multiple classes
|
||||
# Should find __init__ in both models.py and services.py
|
||||
assert any(match.source_file_path is not None and "models.py" in match.source_file_path for match in matches)
|
||||
@@ -228,5 +229,5 @@ class TestLanguageServerBasics:
|
||||
|
||||
# Test 6: Search with a pattern that should have no matches
|
||||
no_match_pattern = r"def\s+this_method_does_not_exist\s*\([^)]*\):"
|
||||
matches = language_server.search_files_for_pattern(no_match_pattern)
|
||||
matches = project.search_files_for_pattern(no_match_pattern)
|
||||
assert len(matches) == 0
|
||||
|
||||
Reference in New Issue
Block a user