def f( params: DeclarationParams ) -> Optional[Union[Location, List[Location], List[LocationLink]]]: location = Location( uri='uri', range=Range( start=Position(line=0, character=0), end=Position(line=1, character=1), ), ) location_link = LocationLink( target_uri='uri', target_range=Range( start=Position(line=0, character=0), end=Position(line=1, character=1), ), target_selection_range=Range( start=Position(line=0, character=0), end=Position(line=2, character=2), ), origin_selection_range=Range( start=Position(line=0, character=0), end=Position(line=3, character=3), ), ) return { # type: ignore 'file://return.location': location, 'file://return.location_list': [location], 'file://return.location_link_list': [location_link], }.get(params.text_document.uri, None)
def _build_diagnostics_for_expanded_macros( self, tool: GalaxyToolXmlDocument, invalid_document_error) -> List[Diagnostic]: virtual_uri = tool.xml_document.document.uri.replace( "file", "gls-expand") diagnostics = [ Diagnostic( range=tool.get_macros_range(), message=error.message, source=self.server_name, code=DiagnosticCodes.INVALID_EXPANDED_TOOL, related_information=[ DiagnosticRelatedInformation( message= ("The validation error ocurred on the expanded version of " "the document, i.e. after replacing macros. " "Click here to preview the expanded document."), location=Location( uri=f"{virtual_uri}{EXPAND_DOCUMENT_URI_SUFFIX}", range=Range( start=Position(line=error.line - 1, character=error.column), end=Position(line=error.line - 1, character=error.column), ), ), ) ], ) for error in invalid_document_error.error_log.filter_from_errors() ] return diagnostics
def lsp_location(name: Name) -> Optional[Location]: """Get LSP location from Jedi definition.""" module_path = name.module_path if module_path is None: return None return Location(uri=module_path.as_uri(), range=lsp_range(name))
def declaration(ls: DiplomatLanguageServer, params: DeclarationParams) -> Location: if not ls.indexed: reindex_all(ls) uri_source = unquote(params.text_document.uri) ref_range = Range(start=params.position, end=params.position) ret = ls.svindexer.get_definition_from_location( Location(uri=uri_source, range=ref_range)) return ret
def references(ls: DiplomatLanguageServer, params: ReferenceParams) -> T.List[Location]: """Returns completion items.""" if not ls.indexed: reindex_all(ls) uri_source = unquote(params.text_document.uri) ref_range = Range(start=params.position, end=params.position) ret = ls.svindexer.get_refs_from_location( Location(uri=uri_source, range=ref_range)) return ret
def test_location(): assert Location( uri="file:///document.txt", range=Range(start=Position(line=1, character=2), end=Position(line=3, character=4)), ) == Location( uri="file:///document.txt", range=Range(start=Position(line=1, character=2), end=Position(line=3, character=4)), ) assert Location( uri="file:///document.txt", range=Range(start=Position(line=1, character=2), end=Position(line=3, character=4)), ) != Location( uri="file:///another.txt", range=Range(start=Position(line=1, character=2), end=Position(line=3, character=4)), ) assert (Location( uri="file:///document.txt", range=Range(start=Position(line=1, character=2), end=Position(line=3, character=4)), ) != "something else") assert "file:///document.txt:1:2-3:4" == repr( Location( uri="file:///document.txt", range=Range(start=Position(line=1, character=2), end=Position(line=3, character=4)), ))
def go_to_import_definition(self, file_name: str) -> Optional[List[Location]]: imported_macros = self.imported_macros.get(file_name) if imported_macros and imported_macros.document and imported_macros.document.root: macros_file_uri = imported_macros.file_uri content_range = imported_macros.document.get_full_range( imported_macros.document.root) if content_range: return [Location( uri=macros_file_uri, range=content_range, )] return None
def f(params: ReferenceParams) -> Optional[List[Location]]: if params.text_document.uri == 'file://return.list': return [ Location( uri='uri', range=Range( start=Position(line=0, character=0), end=Position(line=1, character=1), ), ), ] else: return None
def f( params: DocumentSymbolParams ) -> Union[List[SymbolInformation], List[DocumentSymbol]]: symbol_info = SymbolInformation( name='symbol', kind=SymbolKind.Namespace, location=Location( uri='uri', range=Range( start=Position(line=0, character=0), end=Position(line=1, character=1), ), ), container_name='container', deprecated=False, ) document_symbol_inner = DocumentSymbol( name='inner_symbol', kind=SymbolKind.Number, range=Range( start=Position(line=0, character=0), end=Position(line=1, character=1), ), selection_range=Range( start=Position(line=0, character=0), end=Position(line=1, character=1), ), ) document_symbol = DocumentSymbol( name='symbol', kind=SymbolKind.Object, range=Range( start=Position(line=0, character=0), end=Position(line=10, character=10), ), selection_range=Range( start=Position(line=0, character=0), end=Position(line=10, character=10), ), detail='detail', children=[document_symbol_inner], deprecated=True, ) return { # type: ignore 'file://return.symbol_information_list': [symbol_info], 'file://return.document_symbol_list': [document_symbol], }.get(params.text_document.uri, None)
def doc_definition(self, doc: Document, label: str) -> List[Location]: """Goto definition implementation for ``:doc:`` targets""" uri = self.resolve_doc(doc, label) if not uri: return [] return [ Location( uri=uri, range=Range( start=Position(line=0, character=0), end=Position(line=1, character=0), ), ) ]
def _kythe_to_lsp_location(self, kloc : KytheLocation) -> Location: uri_base = kloc.path start_line = kloc.start_line start_character = kloc.start_char end_line = kloc.end_line end_character = kloc.end_char loc = Location(uri=uri_base, range=Range( start=Position(line=start_line, character=start_character), end=Position(line=end_line, character=end_character)) ) return loc
def ref_definition(self, label: str) -> List[Location]: """Goto definition implementation for ``:ref:`` targets""" if not self.rst.app or not self.rst.app.env: return [] types = set(self.rst.get_role_target_types("ref")) std = self.rst.get_domain("std") if std is None: return [] docname = self.find_docname_for_label(label, std, types) if docname is None: return [] path = self.rst.app.env.doc2path(docname) uri = Uri.from_fs_path(path) doctree = self.rst.get_initial_doctree(uri) if doctree is None: return [] uri = None line = None for node in doctree.traverse(condition=nodes.target): if "refid" not in node: continue if doctree.nameids.get(label, "") == node["refid"]: uri = Uri.from_fs_path(node.source) line = node.line break if uri is None or line is None: return [] return [ Location( uri=uri, range=Range( start=Position(line=line - 1, character=0), end=Position(line=line, character=0), ), ) ]
def _get_macro_definitions( self, macros_xml: XmlDocument) -> Dict[str, MacroDefinition]: macro_elements = macros_xml.find_all_elements_with_name(MACRO) xml_elements = macros_xml.find_all_elements_with_name(XML) macro_elements += xml_elements rval = {} for element in macro_elements: name = element.get_attribute(NAME) macro_def = MacroDefinition( name=name, location=Location( uri=macros_xml.document.uri, range=macros_xml.get_full_range(element), ), token_params=self.get_token_params(macros_xml, element), ) rval[macro_def.name] = macro_def return rval
def _get_token_definitions( self, macros_xml: XmlDocument) -> Dict[str, TokenDefinition]: token_elements = macros_xml.find_all_elements_with_name(TOKEN) rval = {} for element in token_elements: token = element.get_attribute(NAME) if token is None: continue name = token.replace("@", "") value = element.get_content(macros_xml.document.source) token_def = TokenDefinition( name=name, location=Location( uri=macros_xml.document.uri, range=macros_xml.get_full_range(element), ), value=value, ) rval[token_def.name] = token_def return rval
def get_token_params(self, macros_xml: XmlDocument, element: XmlElement) -> Dict[str, TokenParam]: token_params = {} for attr_name, attr in element.attributes.items(): if attr_name.startswith("token_"): param_name = attr_name.replace("token_", "") token_name = param_name.upper() default_value = attr.get_value() token_param = TokenParam( name=token_name, param_name=param_name, default_value=default_value, value= f"**Token parameter**\n- Default value: `{default_value}`", location=Location( uri=macros_xml.document.uri, range=macros_xml.get_full_range(attr), ), ) token_params[token_name] = token_param return token_params
def _build_diagnostics_for_macros_file_syntax_error( self, tool: GalaxyToolXmlDocument, syntax_error) -> List[Diagnostic]: result = Diagnostic( range=tool.get_import_macro_file_range(syntax_error.filename), message=syntax_error.msg, source=self.server_name, related_information=[ DiagnosticRelatedInformation( message="Syntax error found on imported file.", location=Location( uri=Path(syntax_error.filename).as_uri(), range=Range( start=Position(line=syntax_error.lineno - 1, character=syntax_error.offset), end=Position(line=syntax_error.lineno - 1, character=syntax_error.offset), ), ), ) ], ) return [result]
def get_references(doc: Dict[str, str], symbol: str) -> List[Location]: """Given a representation of a document and a symbol string, return the Location of every use of that symbol within the document.""" current_line = 0 output = [] symbol_len = len(symbol) uri = from_fs_path(doc["path"]) for line in doc["source"].split("\n"): for index in [ i for i in range(len(line)) if line.startswith(symbol, i) ]: output.append( Location( uri=uri, range=Range( start=Position(line=current_line, character=index), end=Position(line=current_line, character=index + symbol_len), ), )) current_line += 1 return output
def find_definitions( self, context: DefinitionContext, directive: str, domain: Optional[str], argument: str, ) -> List[Location]: if domain or directive not in {"literalinclude", "include"}: return [] uri = self.resolve_path(context.doc, argument) if not uri: return [] return [ Location( uri=uri, range=Range( start=Position(line=0, character=0), end=Position(line=1, character=0), ), ) ]
def lsp_location(name: Name) -> Location: """Get LSP location from Jedi definition.""" return Location(uri=name.module_path.as_uri(), range=lsp_range(name))
results = await completion_request(client, test_uri, text) items = {item.label for item in results.items} expected = expected or set() unexpected = unexpected or set() assert expected == items & expected assert set() == items & unexpected check.completion_items(client, results.items) WELCOME_LABEL = Location( uri="index.rst", range=Range( start=Position(line=5, character=0), end=Position(line=6, character=0), ), ) @pytest.mark.asyncio @pytest.mark.parametrize( "path,position,expected", [ ("definitions.rst", Position(line=3, character=33), None), ("definitions.rst", Position(line=5, character=13), None), ("definitions.rst", Position(line=7, character=33), None), ("definitions.rst", Position(line=9, character=42), None), ("definitions.rst", Position(line=5, character=33), WELCOME_LABEL), ("definitions.rst", Position(line=9, character=35), WELCOME_LABEL),
"theorems/pythagoras.rst", Position(line=8, character=4), None, ), ( "definitions.rst", Position(line=17, character=20), None, ), ( "theorems/pythagoras.rst", Position(line=8, character=18), Location( uri="math.rst", range=Range( start=Position(line=0, character=0), end=Position(line=1, character=0), ), ), ), ( "theorems/pythagoras.rst", Position(line=10, character=22), Location( uri="math.rst", range=Range( start=Position(line=0, character=0), end=Position(line=1, character=0), ), ), ),