PathCompleter
Base classes
| Name |
Children |
Inherits |
CompletionProvider slashed.completion Base class for completion providers. |
|
|
⋔ Inheritance diagram
graph TD
31592764["completers.PathCompleter"]
2851341576["completion.CompletionProvider"]
925645236["abc.ABC"]
2751680898["builtins.object"]
2851341576 --> 31592764
925645236 --> 2851341576
2751680898 --> 925645236
🛈 DocStrings
Bases: CompletionProvider
Provides filesystem path completions.
Source code in src/slashed/completers.py
| class PathCompleter(CompletionProvider):
"""Provides filesystem path completions."""
def __init__(
self,
file_patterns: list[str] | None = None,
*,
directories: bool = True,
files: bool = True,
show_hidden: bool = False,
expanduser: bool = True,
base_path: JoinablePathLike | None = None,
) -> None:
"""Initialize path completer.
Args:
file_patterns: Optional glob patterns to filter files
directories: Whether to include directories
files: Whether to include files
show_hidden: Whether to show hidden files
expanduser: Whether to expand user directory (~)
base_path: Optional base path to resolve relative paths against
"""
self.file_patterns = file_patterns
self.directories = directories
self.files = files
self.show_hidden = show_hidden
self.expanduser = expanduser
self.base_path = UPath(base_path).resolve() if base_path else None
async def get_completions(
self,
context: CompletionContext[Any],
) -> AsyncIterator[CompletionItem]:
"""Get path completions."""
word = context.current_word or "."
try:
# Handle absolute paths
if UPath(word).is_absolute():
path = UPath(word)
# Handle user paths
elif self.expanduser and word.startswith("~"):
path = UPath(word).expanduser()
# Handle relative paths
else:
# If base_path is set, resolve relative to it
base = self.base_path or UPath.cwd()
path = (base / word).resolve()
# If path doesn't exist, use its parent for listing
if not path.exists():
completion_dir = path.parent
prefix = path.name
else:
completion_dir = path
prefix = ""
# List directory contents
for entry in completion_dir.iterdir():
# Skip hidden unless enabled
if not self.show_hidden and entry.name.startswith("."):
continue
# Skip if doesn't match prefix
if prefix and not entry.name.startswith(prefix):
continue
# Apply type filters
if entry.is_dir():
if not self.directories:
continue
elif entry.is_file():
if not self.files:
continue
if self.file_patterns and not any(
entry.match(pattern) for pattern in self.file_patterns
):
continue
# Get relative path if using base_path
if self.base_path:
try:
display = str(entry.relative_to(self.base_path)) # pyright: ignore[reportArgumentType]
except ValueError:
display = str(entry)
else:
display = str(entry)
# Create completion
name = str(entry)
if entry.is_dir():
name = f"{name}{os.sep}"
kind = "directory"
else:
kind = utils.get_file_kind(entry)
meta = utils.get_metadata(entry)
yield CompletionItem(text=name, display=display, kind=kind, metadata=meta)
except Exception as e: # noqa: BLE001
# Log error but don't raise
logger.debug("Path completion error: %s", e)
|
__init__
__init__(
file_patterns: list[str] | None = None,
*,
directories: bool = True,
files: bool = True,
show_hidden: bool = False,
expanduser: bool = True,
base_path: JoinablePathLike | None = None,
) -> None
Initialize path completer.
Parameters:
| Name |
Type |
Description |
Default |
file_patterns
|
list[str] | None
|
Optional glob patterns to filter files
|
None
|
directories
|
bool
|
Whether to include directories
|
True
|
files
|
bool
|
|
True
|
show_hidden
|
bool
|
Whether to show hidden files
|
False
|
expanduser
|
bool
|
Whether to expand user directory (~)
|
True
|
base_path
|
JoinablePathLike | None
|
Optional base path to resolve relative paths against
|
None
|
Source code in src/slashed/completers.py
| def __init__(
self,
file_patterns: list[str] | None = None,
*,
directories: bool = True,
files: bool = True,
show_hidden: bool = False,
expanduser: bool = True,
base_path: JoinablePathLike | None = None,
) -> None:
"""Initialize path completer.
Args:
file_patterns: Optional glob patterns to filter files
directories: Whether to include directories
files: Whether to include files
show_hidden: Whether to show hidden files
expanduser: Whether to expand user directory (~)
base_path: Optional base path to resolve relative paths against
"""
self.file_patterns = file_patterns
self.directories = directories
self.files = files
self.show_hidden = show_hidden
self.expanduser = expanduser
self.base_path = UPath(base_path).resolve() if base_path else None
|
get_completions
async
Get path completions.
Source code in src/slashed/completers.py
| async def get_completions(
self,
context: CompletionContext[Any],
) -> AsyncIterator[CompletionItem]:
"""Get path completions."""
word = context.current_word or "."
try:
# Handle absolute paths
if UPath(word).is_absolute():
path = UPath(word)
# Handle user paths
elif self.expanduser and word.startswith("~"):
path = UPath(word).expanduser()
# Handle relative paths
else:
# If base_path is set, resolve relative to it
base = self.base_path or UPath.cwd()
path = (base / word).resolve()
# If path doesn't exist, use its parent for listing
if not path.exists():
completion_dir = path.parent
prefix = path.name
else:
completion_dir = path
prefix = ""
# List directory contents
for entry in completion_dir.iterdir():
# Skip hidden unless enabled
if not self.show_hidden and entry.name.startswith("."):
continue
# Skip if doesn't match prefix
if prefix and not entry.name.startswith(prefix):
continue
# Apply type filters
if entry.is_dir():
if not self.directories:
continue
elif entry.is_file():
if not self.files:
continue
if self.file_patterns and not any(
entry.match(pattern) for pattern in self.file_patterns
):
continue
# Get relative path if using base_path
if self.base_path:
try:
display = str(entry.relative_to(self.base_path)) # pyright: ignore[reportArgumentType]
except ValueError:
display = str(entry)
else:
display = str(entry)
# Create completion
name = str(entry)
if entry.is_dir():
name = f"{name}{os.sep}"
kind = "directory"
else:
kind = utils.get_file_kind(entry)
meta = utils.get_metadata(entry)
yield CompletionItem(text=name, display=display, kind=kind, metadata=meta)
except Exception as e: # noqa: BLE001
# Log error but don't raise
logger.debug("Path completion error: %s", e)
|
:fa-brands-github: Show source on GitHub