def sendSelectionUpdate(self, selectedRegions): """ Send currently selected regions to the peer. @param selectedRegions: C{sublime.RegionSet} of all selected regions in the current view. """ status_bar.heartbeat_message('sharing with %s' % self.str()) self.sendMessage(interface.SELECTION, payload=str(selectedRegions))
def sendViewPositionUpdate(self, centerOnRegion): """ Send a window view position update to the peer so they know what we are looking at. @param centerOnRegion: C{sublime.Region} of the central-most line of the current visible portion of the view to send to the peer. """ status_bar.heartbeat_message('sharing with %s' % self.str()) self.sendMessage(interface.POSITION, payload=str(centerOnRegion))
def connectionLost(self, reason): if self.peerType == interface.CLIENT: # ignore this, clientConnectionLost() below will also be called return self.state = interface.STATE_DISCONNECTED if error.ConnectionDone == reason.type: self.disconnect() else: status_bar.heartbeat_message('lost share session with %s' % self.str()) # may want to reconnect, but for now lets print why logger.error('Connection lost: %s - %s' % (reason.type, reason.value))
def connectionLost(self, reason): registry.removeSession(self) if self.peerType == base.CLIENT: # ignore this, clientConnectionLost() below will also be called return self.state = base.STATE_DISCONNECTED if error.ConnectionDone == reason.type: self.disconnect() else: status_bar.heartbeat_message('lost share session with %s' % self.str()) # may want to reconnect, but for now lets print why self.logger.error('Connection lost: %s - %s' % (reason.type, reason.value))
def sendEdit(self, editType, content=None): """ Send an edit event to the peer. @param editType: C{str} edit type (see above) @param content: C{Array} contents of the edit (None-able) """ status_bar.heartbeat_message('sharing with %s' % self.str()) logger.debug('sending edit: %s %s' %(interface.numeric_to_symbolic[editType], content)) if (editType == interface.EDIT_TYPE_INSERT) \ or (editType == interface.EDIT_TYPE_INSERT_SNIPPET) \ or (editType == interface.EDIT_TYPE_PASTE): self.sendMessage(interface.EDIT, editType, payload=content) else: self.sendMessage(interface.EDIT, editType)
def sendEdit(self, editType, content=None): """ Send an edit event to the peer. @param editType: C{str} edit type (see above) @param content: C{Array} contents of the edit (None-able) """ status_bar.heartbeat_message('sharing with %s' % self.str()) self.logger.debug('sending edit: %s %s' %(base.numeric_to_symbolic[editType], content)) if (editType == base.EDIT_TYPE_INSERT) \ or (editType == base.EDIT_TYPE_INSERT_SNIPPET) \ or (editType == base.EDIT_TYPE_PASTE): self.sendMessage(base.EDIT, editType, payload=content) else: self.sendMessage(base.EDIT, editType)
def handleViewChanges(self): """ Runs on the main UI event loop. Goes through the list of events queued up to modify the shared view and applies them to the associated view. """ self.toDoToViewQueueLock.acquire() while len(self.toDoToViewQueue) > 0: toDo = self.toDoToViewQueue.pop(0) if len(toDo) == 2: logger.debug('Handling view change %s with size %d payload' % (interface.numeric_to_symbolic[toDo[0]], len(toDo[1]))) if (toDo[0] == interface.SHARE_VIEW) or (toDo[0] == interface.RESHARE_VIEW): self.totalNewViewSize = 0 if toDo[0] == interface.SHARE_VIEW: self.view = sublime.active_window().new_file() payloadBits = toDo[1].split('|') if payloadBits[0] == 'NONAME': self.view.set_name('SHARING-WITH-%s' % self.sharingWithUser) else: self.view.set_name(payloadBits[0]) self.totalNewViewSize = int(payloadBits[1]) else: # resync event, purge the old view in preparation for the fresh content logger.debug('resyncing view') self.lastResyncdPosition = 0 self.totalNewViewSize = int(toDo[1]) self.view.set_read_only(True) self.view.set_scratch(True) status_bar.progress_message("receiving view from %s" % self.sharingWithUser, self.view.size(), self.totalNewViewSize) elif toDo[0] == interface.VIEW_CHUNK: self.view.set_read_only(False) self.viewPopulateEdit = self.view.begin_edit() # if we are a resync chunk... if hasattr(self, 'lastResyncdPosition'): self.view.replace(self.viewPopulateEdit, \ sublime.Region(self.lastResyncdPosition, self.lastResyncdPosition + len(toDo[1])), \ toDo[1]) self.lastResyncdPosition += len(toDo[1]) else: self.view.insert(self.viewPopulateEdit, self.view.size(), toDo[1]) self.view.end_edit(self.viewPopulateEdit) self.viewPopulateEdit = None self.view.set_read_only(True) status_bar.progress_message("receiving view from %s" % self.sharingWithUser, self.view.size(), self.totalNewViewSize) elif toDo[0] == interface.END_OF_VIEW: self.view.set_syntax_file(toDo[1]) if hasattr(self, 'lastResyncdPosition'): del self.lastResyncdPosition status_bar.progress_message("receiving view from %s" % self.sharingWithUser, self.view.size(), self.totalNewViewSize) # view is populated and configured, lets share! self.onStartCollab() elif toDo[0] == interface.SELECTION: status_bar.heartbeat_message('sharing with %s' % self.str()) regions = [] for regionMatch in REGION_PATTERN.finditer(toDo[1]): regions.append(sublime.Region(int(regionMatch.group(1)), int(regionMatch.group(2)))) self.recvSelectionUpdate(regions) elif toDo[0] == interface.POSITION: status_bar.heartbeat_message('sharing with %s' % self.str()) regionMatch = REGION_PATTERN.search(toDo[1]) if regionMatch: self.recvViewPositionUpdate(sublime.Region(int(regionMatch.group(1)), int(regionMatch.group(2)))) elif len(toDo) == 3: status_bar.heartbeat_message('sharing with %s' % self.str()) # edit event assert toDo[0] == interface.EDIT # make the shared selection the ACTUAL selection self.view.sel().clear() for region in self.view.get_regions(self.sharingWithUser): self.view.sel().add(region) self.view.erase_regions(self.sharingWithUser) self.recvEdit(toDo[1], toDo[2]) self.toDoToViewQueueLock.release()
def handleViewChanges(self): """ Runs on the main UI event loop. Goes through the list of events queued up to modify the shared view and applies them to the associated view. """ self.toDoToViewQueueLock.acquire() while len(self.toDoToViewQueue) > 0: toDo = self.toDoToViewQueue.pop(0) if len(toDo) == 2: logger.debug( 'Handling view change %s with size %d payload' % (interface.numeric_to_symbolic[toDo[0]], len(toDo[1]))) if (toDo[0] == interface.SHARE_VIEW) or ( toDo[0] == interface.RESHARE_VIEW): self.totalNewViewSize = 0 if toDo[0] == interface.SHARE_VIEW: self.view = sublime.active_window().new_file() payloadBits = toDo[1].split('|') if payloadBits[0] == 'NONAME': self.view.set_name('SHARING-WITH-%s' % self.sharingWithUser) else: self.view.set_name(payloadBits[0]) self.totalNewViewSize = int(payloadBits[1]) else: # resync event, purge the old view in preparation for the fresh content logger.debug('resyncing view') self.lastResyncdPosition = 0 self.totalNewViewSize = int(toDo[1]) self.view.set_read_only(True) self.view.set_scratch(True) status_bar.progress_message( "receiving view from %s" % self.sharingWithUser, self.view.size(), self.totalNewViewSize) elif toDo[0] == interface.VIEW_CHUNK: self.view.set_read_only(False) self.viewPopulateEdit = self.view.begin_edit() # if we are a resync chunk... if hasattr(self, 'lastResyncdPosition'): self.view.replace(self.viewPopulateEdit, \ sublime.Region(self.lastResyncdPosition, self.lastResyncdPosition + len(toDo[1])), \ toDo[1]) self.lastResyncdPosition += len(toDo[1]) else: self.view.insert(self.viewPopulateEdit, self.view.size(), toDo[1]) self.view.end_edit(self.viewPopulateEdit) self.viewPopulateEdit = None self.view.set_read_only(True) status_bar.progress_message( "receiving view from %s" % self.sharingWithUser, self.view.size(), self.totalNewViewSize) elif toDo[0] == interface.END_OF_VIEW: self.view.set_syntax_file(toDo[1]) if hasattr(self, 'lastResyncdPosition'): del self.lastResyncdPosition status_bar.progress_message( "receiving view from %s" % self.sharingWithUser, self.view.size(), self.totalNewViewSize) # view is populated and configured, lets share! self.onStartCollab() elif toDo[0] == interface.SELECTION: status_bar.heartbeat_message('sharing with %s' % self.str()) regions = [] for regionMatch in REGION_PATTERN.finditer(toDo[1]): regions.append( sublime.Region(int(regionMatch.group(1)), int(regionMatch.group(2)))) self.recvSelectionUpdate(regions) elif toDo[0] == interface.POSITION: status_bar.heartbeat_message('sharing with %s' % self.str()) regionMatch = REGION_PATTERN.search(toDo[1]) if regionMatch: self.recvViewPositionUpdate( sublime.Region(int(regionMatch.group(1)), int(regionMatch.group(2)))) elif len(toDo) == 3: status_bar.heartbeat_message('sharing with %s' % self.str()) # edit event assert toDo[0] == interface.EDIT # make the shared selection the ACTUAL selection self.view.sel().clear() for region in self.view.get_regions(self.sharingWithUser): self.view.sel().add(region) self.view.erase_regions(self.sharingWithUser) self.recvEdit(toDo[1], toDo[2]) self.toDoToViewQueueLock.release()