Skip to content

Commit

Permalink
feat: add smartcase and globless path search
Browse files Browse the repository at this point in the history
Known issues: failing tests, sluggish autocomplete
  • Loading branch information
CyanVoxel committed Jan 28, 2025
1 parent 83b5464 commit 70f568a
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions tagstudio/src/core/library/alchemy/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import structlog
from sqlalchemy import ColumnElement, and_, distinct, func, or_, select, text
from sqlalchemy.orm import Session
from sqlalchemy.sql.operators import ilike_op
from src.core.media_types import FILETYPE_EQUIVALENTS, MediaCategories
from src.core.query_lang import BaseVisitor
from src.core.query_lang.ast import ANDList, Constraint, ConstraintType, Not, ORList, Property

from .joins import TagEntry
from .models import Entry, Tag, TagAlias

# workaround to have autocompletion in the Editor
# Only import for type checking/autocompletion, will not be imported at runtime.
if TYPE_CHECKING:
from .library import Library
else:
Expand Down Expand Up @@ -97,7 +98,26 @@ def visit_constraint(self, node: Constraint) -> ColumnElement[bool]:
elif node.type == ConstraintType.TagID:
return self.__entry_matches_tag_ids([int(node.value)])
elif node.type == ConstraintType.Path:
return Entry.path.op("GLOB")(node.value)
smartcase = False
glob = False

if node.value == node.value.lower():
smartcase = True
if "*" in node.value:
glob = True

if smartcase and glob:
logger.info("ConstraintType.Path", smartcase=True, glob=True)
return Entry.path.op("GLOB")(ilike_op(Entry.path, f"%{node.value}%"))
elif smartcase:
logger.info("ConstraintType.Path", smartcase=True, glob=False)
return ilike_op(Entry.path, f"%{node.value}%")
elif glob:
logger.info("ConstraintType.Path", smartcase=False, glob=True)
return Entry.path.op("GLOB")(node.value)
else:
logger.info("ConstraintType.Path", smartcase=False, glob=False)
return Entry.path.regexp_match(rf"\b{node.value}\b")
elif node.type == ConstraintType.MediaType:
extensions: set[str] = set[str]()
for media_cat in MediaCategories.ALL_CATEGORIES:
Expand Down

0 comments on commit 70f568a

Please sign in to comment.