Skip to content

Commit 9b4d082

Browse files
committed
feat: addd the "share current buffer" command
1 parent e7a3f7f commit 9b4d082

File tree

2 files changed

+91
-6
lines changed

2 files changed

+91
-6
lines changed

Codemp.sublime-commands

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@
9494
// 'buffer_id': 'test'
9595
}
9696
},
97+
{
98+
"caption": "Codemp: Share Current Buffer...",
99+
"command": "codemp_share_local_buffer",
100+
"args": {
101+
// 'workspace_id': 'asd'
102+
}
103+
},
97104
// {
98105
// "caption": "Codemp: Create Buffer",
99106
// "command": "codemp_create_buffer",

plugin/commands/workspace.py

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,91 @@ def run(self, buffer_id): # pyright: ignore[reportIncompatibleMethodOverride]
9191
# The call must happen separately, otherwise it causes sublime to crash...
9292
# no idea why...
9393
sublime.set_timeout(lambda: buffers.remove(buffer_id), 10)
94+
95+
96+
class CodempShareLocalBufferCommand(sublime_plugin.WindowCommand):
97+
def is_enabled(self) -> bool:
98+
return workspaces.hasactive()
99+
100+
def input_description(self) -> str:
101+
return "Share to workspace:"
102+
103+
def input(self, args):
104+
if "workspace_id" not in args:
105+
return SimpleListInput(
106+
("workspace_id", session.client.active_workspaces())
107+
)
108+
109+
def run(self, workspace_id: str):
110+
# get the current active window
111+
# compute the buffer name:
112+
# just the name is alone,
113+
# the relative path if in a project
114+
# check existance:
115+
# if existing, ask for overwrite, and beam stuff up
116+
# if not, create and then beam stuff up.
117+
view = self.window.active_view()
118+
if view:
119+
self.wid = workspace_id
120+
self.make_buffer_id(view)
121+
122+
def make_buffer_id(self, view: sublime.View):
123+
# if file_name is nothing, then the buffer is not saved to disk,
124+
# and only has a name.
125+
126+
isephimeral = view.file_name() is None
127+
128+
if isephimeral:
129+
tmpbid = view.name()
130+
# ask for confirmation of the buffer name.
131+
self.window.show_input_panel("Share with name:",
132+
tmpbid,
133+
lambda str: self.check_validity_and_share(str, view),
134+
view.set_name,
135+
lambda: view.set_name(tmpbid))
136+
return
137+
138+
windowhasproject = self.window.project_data() is not None
139+
tmpbid = str(view.file_name())
140+
if not windowhasproject:
141+
self.check_validity_and_share(os.path.basename(tmpbid), view)
142+
return
143+
144+
projectfolders = self.window.project_data().get("folders") #pyright: ignore
145+
if not projectfolders:
146+
self.check_validity_and_share(os.path.basename(tmpbid), view)
147+
return
148+
149+
projpaths = [f['path'] for f in projectfolders]
150+
for projpath in projpaths:
151+
if os.path.commonpath([projpath]) == os.path.commonpath([projpath, tmpbid]):
152+
bid = os.path.relpath(tmpbid, projpath)
153+
self.check_validity_and_share(bid, view)
154+
return
155+
156+
def check_validity_and_share(self, bid, view):
157+
vws = workspaces.lookupId(self.wid)
158+
if bid in buffers:
159+
# we are already attached and the buffer exists. Simply send
160+
# the current contents to the remote.
161+
bfm = buffers.lookupId(bid)
162+
bfm.overwrite(TEXT_LISTENER)
163+
return
164+
165+
allbuffers = vws.handle.fetch_buffers().wait()
166+
if bid not in allbuffers:
167+
# in the future make this creation ephimeral.
168+
self.window.run_command("codemp_create_buffer", {
169+
"workspace_id": self.wid,
170+
"buffer_id": bid
171+
})
172+
94173
def _():
95-
buffers.remove(buffer_id)
96-
if not vws.handle.detach_buffer(buffer_id):
97-
logger.error(f"could not leave the buffer {buffer_id}.")
98-
else:
99-
logger.debug(f"successfully detached from {buffer_id}.")
100-
sublime.set_timeout(_, 10)
174+
vbuff = some(buffers.register(bid, vws, localview=view))
175+
vbuff.overwrite(TEXT_LISTENER)
176+
177+
sublime.set_timeout_async(_)
178+
101179

102180
# Leave Buffer Comand
103181
class CodempCreateBufferCommand(sublime_plugin.WindowCommand):

0 commit comments

Comments
 (0)