def hide_tag_preview(view: sublime.View): "Hides tag preview in given view" buffer_id = view.buffer_id() if buffer_id in phantoms_by_buffer: del phantoms_by_buffer[buffer_id] view.erase_phantoms(phantom_key)
def select_item(view: sublime.View, sel: sublime.Region, is_css=False, is_previous=False): "Selects next/previous item for CSS source" buffer_id = view.buffer_id() pos = sel.begin() # Check if we are still in calculated model model = models_for_buffer.get(buffer_id) if model: region = find_region(sel, model.ranges, is_previous) if region: select(view, region) return # Out of available selection range, move to next tag pos = model.start if is_previous else model.end # Calculate new model from current editor content content = utils.get_content(view) model = emmet.select_item(content, pos, is_css, is_previous) if model: models_for_buffer[buffer_id] = model region = find_region(sel, model.ranges, is_previous) if region: select(view, region) return
def handle_selection_change(view: sublime.View): caret = get_caret(view) syntax_name = syntax.from_pos(view, caret) buffer_id = view.buffer_id() if syntax.is_html(syntax_name) and not syntax.is_jsx(syntax_name): ctx = emmet.get_tag_context(view, caret, syntax.is_xml(syntax_name)) if ctx and 'close' in ctx and \ ctx['attributes'] and \ ctx['close'].contains(caret) and \ not view.visible_region().contains(ctx['open']): pos = ctx['close'].b # Do not display preview if user forcibly hides it with Esc key # for current location if buffer_id in previews_by_buffer: pt = previews_by_buffer[buffer_id][0] if pt == pos: return preview = create_tag_preview(ctx) if len(preview) > max_preview_len: preview = '%s...' % preview[0:max_preview_len] show_tag_preview(view, pos, preview, ctx['open'].a) previews_by_buffer[buffer_id] = (pos, True) track_preview() return hide_tag_preview(view) if buffer_id in previews_by_buffer: del previews_by_buffer[buffer_id]
def on_selection_modified_async(self, view: sublime.View): caret = utils.get_caret(view) syntax_name = syntax.from_pos(view, caret) buffer_id = view.buffer_id() if syntax.is_html(syntax_name): ctx = emmet.get_tag_context(view, caret, syntax.is_xml(syntax_name)) if ctx and 'close' in ctx and ctx['close'].contains(caret) and \ not view.visible_region().contains(ctx['open']): pos = ctx['close'].b # Do not display preview if user forcibly hides it with Esc key # for current location if buffer_id in previews_by_buffer: pt = previews_by_buffer[buffer_id][0] if pt == pos: return preview = view.substr(ctx['open']) if len(preview) > max_preview_len: preview = '%s...' % preview[0:max_preview_len] show_tag_preview(view, pos, preview, ctx['open'].a) previews_by_buffer[buffer_id] = (pos, True) return hide_tag_preview(view) if buffer_id in previews_by_buffer: del previews_by_buffer[buffer_id]
def _request_async( self, view: sublime.View, region: sublime.Region, session_buffer_diagnostics: Sequence[Tuple[SessionBufferProtocol, Sequence[Diagnostic]]], only_with_diagnostics: bool, actions_handler: Callable[[CodeActionsByConfigName], None], on_save_actions: Optional[Dict[str, bool]] = None) -> None: use_cache = on_save_actions is None if use_cache: location_cache_key = "{}#{}:{}:{}".format(view.buffer_id(), view.change_count(), region, only_with_diagnostics) if self._response_cache: cache_key, cache_collector = self._response_cache if location_cache_key == cache_key: sublime.set_timeout( lambda: actions_handler(cache_collector.get_actions())) return else: self._response_cache = None collector = CodeActionsCollector(actions_handler) with collector: listener = windows.listener_for_view(view) if listener: for session in listener.sessions_async('codeActionProvider'): if on_save_actions: supported_kinds = session.get_capability( 'codeActionProvider.codeActionKinds') matching_kinds = get_matching_kinds( on_save_actions, supported_kinds or []) if matching_kinds: params = text_document_code_action_params( view, region, [], matching_kinds) request = Request.codeAction(params, view) session.send_request_async( request, *filtering_collector(session.config.name, matching_kinds, collector)) else: diagnostics = [] # type: Sequence[Diagnostic] for sb, diags in session_buffer_diagnostics: if sb.session == session: diagnostics = diags break if only_with_diagnostics and not diagnostics: continue params = text_document_code_action_params( view, region, diagnostics) request = Request.codeAction(params, view) session.send_request_async( request, collector.create_collector(session.config.name)) if use_cache: self._response_cache = (location_cache_key, collector)
def notify_did_change(view: sublime.View): file_name = view.file_name() if file_name: if view.buffer_id() in pending_buffer_changes: del pending_buffer_changes[view.buffer_id()] # config = config_for_scope(view) client = client_for_view(view) if client: document_state = get_document_state(file_name) uri = filename_to_uri(file_name) params = { "textDocument": { "uri": uri, # "languageId": config.languageId, clangd does not like this field, but no server uses it? "version": document_state.inc_version(), }, "contentChanges": [{ "text": view.substr(sublime.Region(0, view.size())) }] } client.send_notification(Notification.didChange(params))
def show_tag_preview(view: sublime.View, pt: int, text: str, dest: int): "Displays given tag preview at `pt` location" buffer_id = view.buffer_id() if buffer_id not in phantoms_by_buffer: phantom_set = sublime.PhantomSet(view, phantom_key) phantoms_by_buffer[buffer_id] = phantom_set else: phantom_set = phantoms_by_buffer[buffer_id] r = sublime.Region(pt, pt) nav = lambda href: go_to_pos(view, int(href)) phantoms = [sublime.Phantom(r, phantom_content(text, dest), sublime.LAYOUT_INLINE, on_navigate=nav)] phantom_set.update(phantoms)
def notify_did_change(view: sublime.View): file_name = view.file_name() window = view.window() if window and file_name: if view.buffer_id() in pending_buffer_changes: del pending_buffer_changes[view.buffer_id()] config = config_for_scope(view) client = client_for_view(view) if client and config: uri = filename_to_uri(file_name) languageId = config.get_language_id(view) ds = get_document_state(window, file_name) if ds.languageId == languageId: params = { "textDocument": { "uri": uri, "version": ds.inc_version(), }, "contentChanges": [{ "text": view.substr(sublime.Region(0, view.size())) }] } client.send_notification(Notification.didChange(params)) else: # The languageId has changed, reopen file ds.languageId = languageId params = {"textDocument": {"uri": uri}} client.send_notification(Notification.didClose(params)) params = { "textDocument": { "uri": uri, "languageId": ds.languageId, "text": view.substr(sublime.Region(0, view.size())), "version": ds.inc_version(), } } client.send_notification(Notification.didOpen(params))
def queue_did_change(view: sublime.View): buffer_id = view.buffer_id() buffer_version = 1 pending_buffer = None if buffer_id in pending_buffer_changes: pending_buffer = pending_buffer_changes[buffer_id] buffer_version = pending_buffer["version"] + 1 pending_buffer["version"] = buffer_version else: pending_buffer_changes[buffer_id] = { "view": view, "version": buffer_version } sublime.set_timeout_async( lambda: purge_did_change(buffer_id, buffer_version), 500)
def update_diagnostics_phantoms(view: sublime.View, diagnostics: 'List[Diagnostic]'): global phantom_sets_by_buffer buffer_id = view.buffer_id() if not show_diagnostics_phantoms or view.is_dirty(): phantoms = None else: phantoms = list( create_phantom(view, diagnostic) for diagnostic in diagnostics) if phantoms: phantom_set = phantom_sets_by_buffer.get(buffer_id) if not phantom_set: phantom_set = sublime.PhantomSet(view, "lsp_diagnostics") phantom_sets_by_buffer[buffer_id] = phantom_set phantom_set.update(phantoms) else: phantom_sets_by_buffer.pop(buffer_id, None)
def update_diagnostics_phantoms(view: sublime.View, diagnostics: 'List[Diagnostic]'): global phantom_sets_by_buffer buffer_id = view.buffer_id() if not settings.show_diagnostics_phantoms or view.is_dirty(): phantoms = None else: phantoms = list( create_phantom(view, diagnostic) for diagnostic in diagnostics) if phantoms: phantom_set = phantom_sets_by_buffer.get(buffer_id) if not phantom_set: phantom_set = sublime.PhantomSet(view, "code_intel_diagnostics") phantom_sets_by_buffer[buffer_id] = phantom_set phantom_set.update(phantoms) else: phantom_sets_by_buffer.pop(buffer_id, None)
def reset_model(view: sublime.View): "Resets stores model for given view" buffer_id = view.buffer_id() if buffer_id in models_for_buffer: del models_for_buffer[buffer_id]
def has_preview(view: sublime.View): buffer_id = view.buffer_id() if buffer_id in previews_by_buffer: return previews_by_buffer[buffer_id][1] return False
def reset_tag_preview(view: sublime.View): buffer_id = view.buffer_id() if buffer_id in previews_by_buffer: pt, visible = previews_by_buffer[buffer_id] if visible: previews_by_buffer[buffer_id] = (pt, False)
def on_query_context(self, view: sublime.View, key: str, *args): if key == 'emmet_tag_preview': buffer_id = view.buffer_id() if buffer_id in previews_by_buffer: return previews_by_buffer[buffer_id][1] return None
def view_to_uri(view: sublime.View) -> str: file_name = view.file_name() if not file_name: return "buffer://sublime/{}".format(view.buffer_id()) return filename_to_uri(file_name)