Skip to content

Commit

Permalink
Merge pull request #207 from pieces-app/fix-bugs
Browse files Browse the repository at this point in the history
Fix bugs
  • Loading branch information
bishoy-at-pieces authored Oct 11, 2024
2 parents 373361e + c942fa5 commit e5df42f
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def associate_asset(self, asset: "BasicAsset"):
Args:
- asset (BasicAsset): The asset to be associated.
"""
self.pieces_client.tag_api.tag_associate_asset(asset.id,self.tag.id)
self.pieces_client.tag_api.tag_associate_asset(asset._id,self.tag.id)

def disassociate_asset(self, asset: "BasicAsset"):
"""
Expand Down
2 changes: 1 addition & 1 deletion _version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.3.4"
__version__ = "1.4.0"
14 changes: 8 additions & 6 deletions assets/create_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from .._pieces_lib.pieces_os_client import ClassificationSpecificEnum,FragmentMetadata
import sublime_plugin
import sublime
from . ext_map import file_map
from .ext_map import file_map

class PiecesCreateAssetCommand(sublime_plugin.TextCommand):
@check_pieces_os()
def run(self,edit,data=None,metadata=None, add_metadata=True, open_on_save = True, tags = []):
def run(self,edit,data=None,metadata=None, add_metadata=True, open_on_save = True, tags = [], run_async = True):
if not data:
# Get the all the selected text
data = "\n".join([self.view.substr(selection) for selection in self.view.sel()])
Expand All @@ -25,15 +25,17 @@ def run_create_async():
self.view.set_status('Pieces Creating', 'Creating an asset')
created_asset_id = PiecesSettings.api_client.create_asset(data,metadata)
if open_on_save:
self.view.window().run_command("pieces_list_assets",{"pieces_asset_id":created_asset_id})
sublime.active_window().run_command("pieces_list_assets",{"pieces_asset_id":created_asset_id})
self.view.erase_status('Pieces Creating')
asset = BasicAsset(created_asset_id)

for tag in tags:
BasicTag.from_raw_content(PiecesSettings.api_client,tag).associate_asset(asset)

# Creating the new asset using the assets API
sublime.set_timeout_async(run_create_async ,0)
if run_async:
# Creating the new asset using the assets API
sublime.set_timeout_async(run_create_async ,0)
else:
run_create_async()



Expand Down
14 changes: 8 additions & 6 deletions assets/export_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{content}
]]></content>
<tabTrigger>{trigger}</tabTrigger>
<description>{description}</description>
<description>{asset_description}</description>
<scope>{scope}</scope>
</snippet>"""

Expand Down Expand Up @@ -74,11 +74,13 @@ def run(self,pieces_asset_id):

if syntax:
self.dummy_view.assign_syntax(syntax = syntax)
self.scope = self.dummy_view.syntax().scope # obtain the scope using the syntax
else:
self.dummy_view.run_command("append",{"characters":self.content})

self.dummy_view.run_command("append",{"characters":self.content})
self.scope = self.dummy_view.scope_name(
10 # ignoring the content at the begining
).strip().replace(' ', ', ')
self.scope = self.dummy_view.scope_name(
10 # ignoring the content at the begining
).strip().replace(' ', ', ')

self.sheet = mdpopups.new_html_sheet(self.window,asset_wrapper.name,contents="")
self.update_sheet()
Expand Down Expand Up @@ -146,7 +148,7 @@ def run(self,field:str,sheet_id):
"""
self.field = field
self.instance = PiecesExportAssetToSublimeCommand.get_instance(sheet_id)
self.window.show_input_panel(f"{field.title()}:", getattr(self.instance,field), self.on_done, None, None)
self.window.show_input_panel(f"{field.replace('_',' ').title()}:", getattr(self.instance,field), self.on_done, None, None)

def on_done(self, user_response):
setattr(self.instance,self.field,user_response)
Expand Down
30 changes: 24 additions & 6 deletions assets/import_command.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
import sublime
import sublime_plugin
import xml.etree.ElementTree as ET
from .create_asset import PiecesCreateAssetCommand
from .ext_map import file_map

from ..startup_utils import check_pieces_os
from ..progress_bar import ProgressBar
from .._pieces_lib.pieces_os_client import FragmentMetadata



class PiecesImportAssetCommand(sublime_plugin.WindowCommand):
@check_pieces_os()
def run(self,sublime_snippets):
def save(snippet, open_on_save):
def save(snippet, open_on_save, run_async = True):
dummy_view = self.window.create_output_panel("pieces_dummy_view",unlisted=True)
dummy_view.run_command("insert_snippet", {"name": snippet})
content = dummy_view.substr(sublime.Region(0, dummy_view.size()))
snippet_xml = sublime.load_resource(snippet)
tree = ET.fromstring(snippet_xml)
scope = tree.find('scope')
metadata = None
if scope:
ext = file_map.reverse.get(sublime.find_syntax_by_name(scope.text))
metadata = FragmentMetadata(ext = ext) if ext else None

PiecesCreateAssetCommand(
dummy_view
).run(
"",
content,
add_metadata=False,
add_metadata=False, # automatically extracting the metadata should be false
metadata = metadata,
open_on_save=open_on_save,
run_async = run_async,
tags = ["Sublime", "Sublime Snippet", ".sublime_snippet"]
)

Expand All @@ -27,10 +43,12 @@ def save(snippet, open_on_save):
def run_async():
view = self.window.active_view()
snippets_len = len(snippets)
progress_bar = ProgressBar("Pieces Importing Snippets:",total=snippets_len)
progress_bar.start()
for idx,snippet in enumerate(snippets,start=1):
if view:
view.set_status("import_snippet", f"Pieces Importing Snippets: {idx}/{snippets_len}")
save(snippet, open_on_save = False)
save(snippet, open_on_save = False, run_async=False)
progress_bar.update_progress(idx)
progress_bar.stop()

view.erase_status("import_snippet") if view else None

Expand All @@ -48,7 +66,7 @@ class SublimeSnippetsInputHandler(sublime_plugin.ListInputHandler):
def list_items(self):
snippets = sublime.find_resources("*.sublime-snippet")
return [
("Import All", "all"),
(f"Import All ({len(snippets)} Snippets)", "all"),
*[(snippet,snippet) for snippet in snippets]
]

1 change: 1 addition & 0 deletions assets/list_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def update_sheet(cls,sheet,asset_id,buttons_kwargs={}):
try:
markdown_text = asset.markdown
except:
sheet.close()
return sublime.error_message("Asset Not Found")
if (not buttons_kwargs.get("share")) and (asset.shares):
buttons_kwargs["share"] = {
Expand Down
8 changes: 6 additions & 2 deletions progress_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def update_progress(self, progress):
self.current = progress
if self.current >= self.total:
self.stop()
self._update()

def _update(self, status=0):
"""Update the progress bar status."""
Expand All @@ -47,15 +48,18 @@ def _update(self, status=0):
before = int((self.current / self.total) * self.width)
after = self.width - before
progress_bar = f"[{'█' * before}{'░' * after}] {percentage}%"
sublime.status_message(f"{self.label} {progress_bar}")
return

else:
# Unmeasured progress bar
status = status % (2 * self.width)
before = min(status, (2 * self.width) - status)
after = self.width - before
progress_bar = f"[{'█' * before}{'░' * after}]"

# Update the status message
sublime.status_message(f"{self.label} {progress_bar}")
# Update the status message
sublime.status_message(f"{self.label} {progress_bar}")

# Schedule the next update
if not self._done:
Expand Down

0 comments on commit e5df42f

Please sign in to comment.