Skip to content

Commit

Permalink
refactor: tidy codes
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed Mar 1, 2022
1 parent c84f878 commit 6c90a00
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 30 deletions.
4 changes: 4 additions & 0 deletions menus/Context.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
{
"command": "copy_uri_from_context_menu"
},
{
"caption": "Copy All URIs",
"command": "copy_uri_from_view"
},
{
"caption": "-",
"id": "selection"
Expand Down
8 changes: 8 additions & 0 deletions menus/Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
"caption": "OpenUri: Open URIs from the Current View",
"command": "open_uri_from_view"
},
{
"caption": "OpenUri: Copy URIs from Cursors",
"command": "copy_uri_from_cursors"
},
{
"caption": "OpenUri: Copy URIs from the Current View",
"command": "copy_uri_from_view"
},
{
"caption": "OpenUri: Select URIs from Cursors",
"command": "select_uri_from_cursors"
Expand Down
11 changes: 11 additions & 0 deletions menus/Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
{
"caption": "-"
},
{
"caption": "Copy URIs from Cursors",
"command": "copy_uri_from_cursors"
},
{
"caption": "Copy URIs from the Current View",
"command": "copy_uri_from_view"
},
{
"caption": "-"
},
{
"caption": "Select URIs from Cursors",
"command": "select_uri_from_cursors"
Expand Down
2 changes: 1 addition & 1 deletion plugin/OpenUri.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def on_hover(self, point: int, hover_zone: int) -> None:
if hover_zone != sublime.HOVER_TEXT:
uri_regions: List[sublime.Region] = []
else:
uri_regions = find_uri_regions_by_region(self.view, point, get_setting("uri_search_radius"))
uri_regions = find_uri_regions_by_region(self.view, point)

if uri_regions and get_setting_show_open_button(self.view) == "hover":
show_popup(self.view, uri_regions[0], point)
Expand Down
108 changes: 81 additions & 27 deletions plugin/OpenUriCommands.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,124 @@
from .helpers.functions import find_uri_regions_by_regions
from .helpers.functions import open_uri_with_browser
from .helpers.settings import get_setting
from .helpers.shared import is_plugin_ready
from .helpers.types import EventDict, RegionLike, UriSource
from abc import ABCMeta
from typing import Iterable, List, Optional
import sublime
import sublime_plugin


def get_uri_regions(
view: sublime.View,
source: UriSource,
event: Optional[EventDict] = None,
) -> List[sublime.Region]:
def get_uri_regions(view: sublime.View, source: UriSource, event: Optional[EventDict] = None) -> List[sublime.Region]:
regions: Iterable[RegionLike]

if source == UriSource.CONTEXT_MENU:
if source == UriSource.NONE:
regions = tuple()
elif source == UriSource.CONTEXT_MENU:
regions = (view.window_to_text((event["x"], event["y"])),) * 2 if event else tuple()
elif source == UriSource.CURSORS:
regions = view.sel()
elif source == UriSource.FILE:
regions = ((0, view.size()),)
else:
raise RuntimeError
return find_uri_regions_by_regions(view, regions, get_setting("uri_search_radius"))
return find_uri_regions_by_regions(view, regions)


class CopyUriFromContextMenuCommand(sublime_plugin.TextCommand):
def description(self, event: EventDict) -> str: # type: ignore
class AbstractUriCommand(sublime_plugin.TextCommand, metaclass=ABCMeta):
source = UriSource.NONE

def is_enabled(self) -> bool:
return is_plugin_ready()

def is_visible(self) -> bool:
return is_plugin_ready()

def get_uri_regions(self, event: Optional[EventDict] = None) -> List[sublime.Region]:
return get_uri_regions(self.view, self.source, event)


# -------- #
# copy URI #
# -------- #


class AbstractCopyUriCommand(AbstractUriCommand, metaclass=ABCMeta):
def run(
self,
edit: sublime.Edit,
event: Optional[EventDict] = None,
unique: bool = True,
sort: bool = True,
) -> None:
if uri_regions := self.get_uri_regions(event):
uris: Iterable[str] = map(self.view.substr, uri_regions)
if unique:
uris = set(uris)
if sort:
uris = sorted(uris)
sublime.set_clipboard("\n".join(uris))


class CopyUriFromViewCommand(AbstractCopyUriCommand):
source = UriSource.FILE


class CopyUriFromCursorsCommand(AbstractCopyUriCommand):
source = UriSource.CURSORS


class CopyUriFromContextMenuCommand(AbstractCopyUriCommand):
source = UriSource.CONTEXT_MENU

def description(self, event: Optional[EventDict] = None) -> str: # type: ignore
return f"Copy {self._find_url(event)}"

def is_visible(self, event: EventDict) -> bool: # type: ignore
return bool(self._find_url(event))
def is_visible(self, event: Optional[EventDict] = None) -> bool: # type: ignore
return super().is_visible() and bool(self.get_uri_regions(event))

def want_event(self) -> bool:
return True

def run(self, edit: sublime.Edit, event: EventDict) -> None:
if url := self._find_url(event):
sublime.set_clipboard(url)

def _find_url(self, event: EventDict) -> str:
uri_region = next(iter(get_uri_regions(self.view, UriSource.CONTEXT_MENU, event)), None)
def _find_url(self, event: Optional[EventDict] = None) -> str:
uri_region = next(iter(self.get_uri_regions(event)), None)
return self.view.substr(uri_region) if uri_region else ""


class OpenUriFromCursorsCommand(sublime_plugin.TextCommand):
source = UriSource.CURSORS
# -------- #
# open URI #
# -------- #


def run(self, edit: sublime.Edit, browser: str = "") -> None:
for uri in set(map(self.view.substr, get_uri_regions(self.view, self.source))):
class AbstractOpenUriCommand(AbstractUriCommand, metaclass=ABCMeta):
def run(self, edit: sublime.Edit, event: Optional[EventDict] = None, browser: str = "") -> None:
for uri in set(map(self.view.substr, self.get_uri_regions(event))):
open_uri_with_browser(uri, browser)


class OpenUriFromViewCommand(OpenUriFromCursorsCommand):
class OpenUriFromViewCommand(AbstractOpenUriCommand):
source = UriSource.FILE


class SelectUriFromCursorsCommand(sublime_plugin.TextCommand):
class OpenUriFromCursorsCommand(AbstractOpenUriCommand):
source = UriSource.CURSORS

def run(self, edit: sublime.Edit) -> None:
if uri_regions := get_uri_regions(self.view, self.source):

# ---------- #
# select URI #
# ---------- #


class AbstractSelectUriCommand(AbstractUriCommand, metaclass=ABCMeta):
def run(self, edit: sublime.Edit, event: Optional[EventDict] = None) -> None:
if uri_regions := self.get_uri_regions(event):
sel = self.view.sel()
sel.clear()
sel.add_all(uri_regions)


class SelectUriFromViewCommand(SelectUriFromCursorsCommand):
class SelectUriFromViewCommand(AbstractSelectUriCommand):
source = UriSource.FILE


class SelectUriFromCursorsCommand(AbstractSelectUriCommand):
source = UriSource.CURSORS
5 changes: 3 additions & 2 deletions plugin/helpers/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def compile_uri_regex() -> Tuple[Optional[Pattern[str]], Tuple[str, ...]]:
def find_uri_regions_by_region(
view: sublime.View,
region: RegionLike,
search_radius: int = 200,
search_radius: Optional[int] = None,
) -> List[sublime.Region]:
"""
@brief Found intersected URI regions from view by the region
Expand All @@ -119,7 +119,7 @@ def find_uri_regions_by_region(
def find_uri_regions_by_regions(
view: sublime.View,
regions: Iterable[RegionLike],
search_radius: int = 200,
search_radius: Optional[int] = None,
) -> List[sublime.Region]:
"""
@brief Found intersected URI regions from view by regions
Expand All @@ -130,6 +130,7 @@ def find_uri_regions_by_regions(
@return Found URI regions
"""

search_radius = int(search_radius or get_setting("uri_search_radius"))
st_regions = sorted(map(region_into_st_region_form, regions))

search_regions = simplify_intersected_regions(
Expand Down
4 changes: 4 additions & 0 deletions plugin/helpers/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class G:
}


def is_plugin_ready() -> bool:
return bool(G.settings and G.uri_regex_obj)


def global_get(dotted: str, default: Optional[Any] = None) -> Any:
return dotted_get(G, dotted, default)

Expand Down
1 change: 1 addition & 0 deletions plugin/helpers/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ class UriSource(enum.Enum):
CONTEXT_MENU = enum.auto()
CURSORS = enum.auto()
FILE = enum.auto()
NONE = enum.auto()

0 comments on commit 6c90a00

Please sign in to comment.