Skip to content

Commit

Permalink
[vscode_projects:1.3] Use new Matcher introduced in interface version…
Browse files Browse the repository at this point in the history
… 2.3
  • Loading branch information
Sharsie committed Jun 29, 2024
1 parent d0f4044 commit 2e8d6c0
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions vscode_projects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
from dataclasses import dataclass
from albert import *

md_iid = "2.2"
md_version = "1.2"
md_iid = "2.3"
md_version = "1.3"
md_name = "VSCode projects"
md_description = "Open VSCode projects"
md_url = "https:/albertlauncher/python/tree/master/vscode_projects"
md_license = "MIT"
md_bin_dependencies = ["code"]
md_authors = ["@Sharsie"]


@dataclass
class Project:
displayName: str
Expand Down Expand Up @@ -104,10 +103,11 @@ def projectManagerEnabled(self, value):
if found == False:
warning(
"Project Manager search was enabled, but configuration file was not found")
self.notification = Notification(
title=f"{md_name}",
body=f"Configuration file was not found for the Project Manager extension. Please make sure the extension is installed."
notif = Notification(
title=f"{self.name}",
text=f"Configuration file was not found for the Project Manager extension. Please make sure the extension is installed."
)
notif.send()

# Priority settings for project manager results using name search
@property
Expand Down Expand Up @@ -160,19 +160,19 @@ def terminalCommand(self, value):
self.writeConfig("terminalCommand", value)

def __init__(self):
self.iconUrls = [f"file:{Path(__file__).parent}/icon.svg"]

PluginInstance.__init__(self)

TriggerQueryHandler.__init__(
self,
id=md_id,
name=md_name,
description=md_description,
id=self.id,
name=self.name,
description=self.description,
defaultTrigger="code ",
synopsis="project name or path"
)

PluginInstance.__init__(self, extensions=[self])
self.iconUrls = [f"file:{Path(__file__).parent}/icon.svg"]
self.notification = None

configFound = False

for p in self._configStoragePaths:
Expand Down Expand Up @@ -312,19 +312,18 @@ def handleTriggerQuery(self, query):
if not query.isValid:
return

# Normalize user query
normalizedQuery = self._normalizeString(query.string.strip())

if normalizedQuery == "":
if query.string == "":
return

matcher = Matcher(query.string)

results: dict[str, SearchResult] = {}

if self.recentEnabled:
results = self._searchInRecentFiles(normalizedQuery, results)
results = self._searchInRecentFiles(matcher, results)

if self.projectManagerEnabled:
results = self._searchInProjectManager(normalizedQuery, results)
results = self._searchInProjectManager(matcher, results)

sortedItems = sorted(results.values(), key=lambda item: "%s_%s_%s" % (
'{:03d}'.format(item.priority), '{:03d}'.format(item.sortIndex), item.project.name), reverse=False)
Expand All @@ -333,7 +332,7 @@ def handleTriggerQuery(self, query):
query.add(self._createItem(i.project, query))

# Creates an item for the query based on the project and plugin settings
def _createItem(self, project: Project, query: TriggerQuery) -> StandardItem:
def _createItem(self, project: Project, query: Query) -> StandardItem:
actions: list[Action] = []

if self.terminalCommand != "":
Expand Down Expand Up @@ -362,18 +361,18 @@ def _createItem(self, project: Project, query: TriggerQuery) -> StandardItem:
id=project.path,
text=project.displayName,
subtext=project.path,
iconUrls=[f"file:{Path(__file__).parent}/icon.svg"],
iconUrls=[f"file://{Path(__file__).parent}/icon.svg"],
inputActionText=f"{query.trigger} {project.displayName}",
actions=actions,
)

def _searchInRecentFiles(self, search: str, results: dict[str, SearchResult]) -> dict[str, SearchResult]:
def _searchInRecentFiles(self, matcher: Matcher, results: dict[str, SearchResult]) -> dict[str, SearchResult]:
sortIndex = 1

for path in self._configStoragePaths:
c = self._getStorageConfig(path)
for proj in c.projects:
if proj.name.find(search) != -1 or proj.path.find(search) != -1:
if matcher.match(proj.name) or matcher.match(proj.path):
results[proj.path] = self._getHigherPriorityResult(
SearchResult(
project=proj,
Expand All @@ -388,21 +387,21 @@ def _searchInRecentFiles(self, search: str, results: dict[str, SearchResult]) ->

return results

def _searchInProjectManager(self, search: str, results: dict[str, SearchResult]) -> dict[str, SearchResult]:
def _searchInProjectManager(self, matcher: Matcher, results: dict[str, SearchResult]) -> dict[str, SearchResult]:
for path in self._configProjectManagerPaths:
c = self._getProjectManagerConfig(path)
for proj in c.projects:
if proj.name.find(search) != -1:
if matcher.match(proj.name):
results[proj.path] = self._getHigherPriorityResult(
SearchResult(
project=proj,
priority=self.priorityPMName,
sortIndex=0 if proj.name == search else 1
sortIndex=0 if matcher.match(proj.name).isExactMatch() else 1
),
results.get(proj.path),
)

if proj.path.find(search) != -1:
if matcher.match(proj.path):
results[proj.path] = self._getHigherPriorityResult(
SearchResult(
project=proj,
Expand All @@ -413,7 +412,7 @@ def _searchInProjectManager(self, search: str, results: dict[str, SearchResult])
)

for tag in proj.tags:
if tag.find(search) != -1:
if matcher.match(tag):
results[proj.path] = self._getHigherPriorityResult(
SearchResult(
project=proj,
Expand Down

0 comments on commit 2e8d6c0

Please sign in to comment.