|
""" |
|
Sublime Text wildcard search plugin. |
|
|
|
Copyright (c) 2020 - 2022 Isaac Muse <[email protected]> |
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
|
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
|
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
|
persons to whom the Software is furnished to do so, subject to the following conditions: |
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
|
Software. |
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
|
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
|
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
""" |
|
import sublime |
|
import sublime_plugin |
|
import os |
|
import re |
|
from wcmatch import glob |
|
import platform |
|
|
|
RE_WIN_DRIVE = re.compile(r"(^[A-Za-z]{1}:(?:\\|/))") |
|
|
|
|
|
class WildcardNavCommand(sublime_plugin.WindowCommand): |
|
"""Wildcard Navigation command.""" |
|
|
|
def run(self): |
|
"""Run command.""" |
|
|
|
self.window.show_input_panel('glob', '', self.glob, None, None) |
|
|
|
def get_folder_path(self, folder, proj): |
|
"""Get folder path.""" |
|
|
|
if ( |
|
(sublime.platform() == "windows" and RE_WIN_DRIVE.match(folder["path"]) is None and proj is not None) or |
|
(sublime.platform() != "windows" and not folder["path"].startswith("/") and proj is not None) |
|
): |
|
return path.abspath(path.join(path.dirname(proj_file), folder["path"])) |
|
else: |
|
return folder['path'] |
|
|
|
def glob(self, value): |
|
"""Perform a filesystem glob.""" |
|
|
|
flags = ( |
|
glob.GLOBSTAR | glob.NEGATE | glob.SPLIT | glob.MATCHBASE | glob.NEGATEALL | glob.O | |
|
glob.EXTGLOB | glob.BRACE |
|
) |
|
folders = [] |
|
proj = self.window.project_file_name() |
|
self.files = [] |
|
|
|
for folder in self.window.project_data().get('folders', []): |
|
folder = self.get_folder_path(folder, proj) |
|
self.files.extend([os.path.join(folder, f) for f in glob.glob(value, flags=flags, root_dir=folder)]) |
|
|
|
if self.files: |
|
self.window.show_quick_panel( |
|
[[os.path.basename(x), x] for x in self.files], |
|
self.check_selection, |
|
0, |
|
0, |
|
self.preview_selection |
|
) |
|
|
|
def preview_selection(self, value): |
|
"""Check selection.""" |
|
|
|
if value > -1: |
|
self.window.open_file(self.files[value], sublime.TRANSIENT) |
|
|
|
def check_selection(self, value): |
|
"""Check selection.""" |
|
|
|
if value > -1: |
|
self.window.open_file(self.files[value]) |