def handle_doc_range_format_request( self, request_context: RequestContext, params: DocumentRangeFormattingParams) -> None: """ Processes a formatting request by sending the entire documents text to sqlparse and returning a formatted document as a single TextEdit """ # Validate inputs and set up response response: List[TextEdit] = [] def do_send_default_empty_response(): request_context.send_response(response) if self.should_skip_formatting(params.text_document.uri): do_send_default_empty_response() return file: ScriptFile = self._workspace_service.workspace.get_file( params.text_document.uri) if file is None: do_send_default_empty_response() return # Process the text range and respond with the edit text_range = params.range sql: str = file.get_text_in_range(text_range) if sql is None or sql.strip() == '': do_send_default_empty_response() return edit: TextEdit = TextEdit.from_data(text_range, None) self._format_and_add_response(response, edit, sql, params) do_send_default_empty_response()
def _to_completion_item(cls, key: str, text_range: Range, lowercase: bool) -> CompletionItem: key = key.lower() if lowercase else key.upper() completion = CompletionItem() completion.label = key completion.detail = key + ' keyword' completion.insert_text = key completion.kind = CompletionItemKind.Keyword completion.text_edit = TextEdit.from_data(text_range, key) return completion
def to_completion_item(cls, completion: Completion, params: TextDocumentPosition) -> CompletionItem: key = completion.text start_position = LanguageService._get_start_position( params.position, completion.start_position) text_range = Range(start=start_position, end=params.position) kind = DISPLAY_META_MAP.get(completion.display_meta, CompletionItemKind.Unit) completion_item = CompletionItem() completion_item.label = key completion_item.detail = completion.display completion_item.insert_text = key completion_item.kind = kind completion_item.text_edit = TextEdit.from_data(text_range, key) # Add a sort text to put keywords after all other items completion_item.sort_text = f'~{key}' if completion_item.kind == CompletionItemKind.Keyword else key return completion_item
def _format_and_add_response(self, response: List[TextEdit], edit: TextEdit, text: str, params: DocumentFormattingParams) -> None: options = self._get_sqlparse_options(params.options) edit.new_text = sqlparse.format(text, **options) response.append(edit)
def _prepare_edit(self, file: ScriptFile) -> TextEdit: file_line_count: int = len(file.file_lines) last_char = len(file.file_lines[file_line_count - 1]) text_range = Range.from_data(0, 0, file_line_count - 1, last_char) return TextEdit.from_data(text_range, None)