Skip to content

Commit

Permalink
Refactor quick panel item creation
Browse files Browse the repository at this point in the history
1. display folder name for windows with folder(s) open
2. shorten home paths (on all OS)
  • Loading branch information
deathaxe committed Jan 10, 2023
1 parent 15f74a0 commit 1107682
Showing 1 changed file with 62 additions and 56 deletions.
118 changes: 62 additions & 56 deletions plugin.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,89 @@
import os
from pathlib import Path

import sublime
import sublime_plugin


class WindowInputHandler(sublime_plugin.ListInputHandler):

def name(self):
return "window_id"

def placeholder(self):
return "Choose a window"

def list_items(self):
items = []

kind_project = [sublime.KIND_ID_NAMESPACE, "P", "Project"]
kind_folder = [sublime.KIND_ID_NAMESPACE, "F", "Folder"]
kind_file = [sublime.KIND_ID_NAVIGATION, "f", "File"]
kind_scratch = [sublime.KIND_ID_AMBIGUOUS, "S", "Scratch"]
def active_file(window):
view = window.active_view()
if not view:
return (None, None)
try:
file = Path(view.file_name())
return (file.parent, file.name)
except:
return (None, view.name())

def active_folder(folders, active_file_path):
if active_file_path:
for folder in folders:
try:
folder = Path(folder)
active_file_path.relative_to(folder)
return folder
except ValueError:
continue
return Path(folders[0])

def transform_folder(folder):
try:
home = "USERPROFILE" if os.name == "nt" else "HOME"
return f"~{os.sep}{folder.relative_to(os.getenv(home, ''))}"
except ValueError:
return folder

def create_item(window):
active_file_path, active_file_name = active_file(window)
folders = window.folders()
workspace = window.workspace_file_name()

if workspace:
title = Path(workspace).stem
kind = [sublime.KIND_ID_NAMESPACE, "P", "Project"]
if folders:
second_line = transform_folder(
active_folder(folders, active_file_path)
)
else:
second_line = "No folders in project yet!"

active_window = sublime.active_window()
for window in sublime.windows():
if window == active_window:
continue
elif folders:
folder = active_folder(folders, active_file_path)
title = folder.name
kind = [sublime.KIND_ID_NAVIGATION, "F", "Folder"]
second_line = transform_folder(folder)

active_file_location = None
active_file_name = "untitled"
view = window.active_view()
if view:
file_name = view.file_name()
if file_name:
active_file_location, active_file_name = os.path.split(file_name)
elif view.name():
active_file_name = view.name()

project_file_name = window.workspace_file_name()
if project_file_name:
title = os.path.splitext(os.path.basename(project_file_name))[0]
kind = kind_project
second_line = f"Active File: {active_file_name}"
elif active_file_path:
title = active_file_name
kind = [sublime.KIND_ID_NAVIGATION, "f", "File"]
second_line = transform_folder(active_file_path)

else:
title = active_file_name

folders = window.folders()
if folders:
kind = kind_folder
for folder in window.folders():
if active_file_name.startswith(folder):
second_line = f"Folder: {folder}"
break
else:
second_line = f"Folder: {folders[0]}"
elif active_file_location:
kind = kind_file
second_line = f"Location: {active_file_location}"
else:
kind = kind_scratch
second_line = "Scratch Window"

items.append(
sublime.ListInputItem(
text=title,
value=window.id(),
annotation=f"Window {window.id()}",
kind=kind,
details=[second_line]
)
kind = [sublime.KIND_ID_AMBIGUOUS, "S", "Scratch"]
second_line = "Scratch Window"

return sublime.ListInputItem(
text=title or "untitled",
value=window.id(),
annotation=f"Window {window.id()}",
kind=kind,
details=[f"<i>{second_line}</i>"],
)

return items
return [create_item(window) for window in sublime.windows()]


class SwitchWindowCommand(sublime_plugin.ApplicationCommand):

def input_description(self):
return "Switch Window"

Expand All @@ -88,6 +97,3 @@ def run(self, window_id=None):
if window.id() == window_id:
window.bring_to_front()
break

def is_enabled(self):
return len(sublime.windows()) > 1

0 comments on commit 1107682

Please sign in to comment.