def _get_test_suite_from_document( self, xml_document: XmlDocument) -> Optional[TestSuiteInfoResult]: tool = GalaxyToolXmlDocument.from_xml_document(xml_document) tool_id = tool.get_tool_id() tests_range = tool.get_tests_range() if tool_id and tests_range: tests = tool.get_tests() test_cases: List[TestInfoResult] = [] id = 1 for test in tests: range = xml_document.get_full_range(test) if range: test_cases.append( TestInfoResult( tool_id=tool_id, test_id=str(id), uri=xml_document.document.uri, range=range, ), ) id += 1 return TestSuiteInfoResult( tool_id=tool_id, uri=xml_document.document.uri, range=tests_range, children=test_cases, ) return None
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 sort_document_param_attributes( self, xml_document: XmlDocument) -> List[ReplaceTextRangeResult]: params = xml_document.find_all_elements_with_name(PARAM) rval: List[ReplaceTextRangeResult] = [] for param in params: result = self.sort_param_attributes(param, xml_document) if result: rval.append(result) 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 go_to_definition(self, xml_document: XmlDocument, position: Position) -> Optional[List[Location]]: macro_definitions = self.macro_definitions_provider.load_macro_definitions( xml_document) word = xml_document.document.word_at_position(position) token_definition = macro_definitions.get_token_definition(word) if token_definition: return [token_definition.location] macro_definition = macro_definitions.get_macro_definition(word) if macro_definition: return [macro_definition.location] offset = xml_document.document.offset_at_position(position) node = xml_document.find_node_at(offset) if isinstance(node, XmlContent) and node.parent.name == "import": content_node = node start, end = content_node.get_content_offsets() import_filename = xml_document.get_text_between_offsets(start, end) return macro_definitions.go_to_import_definition(import_filename) return None
def _adapt_format(self, xml_document: XmlDocument, insert_range: Range, xml_text: str, insert_in_new_line: bool = True) -> str: """Adapts a chunk of XML text to the current formatting of the document given the insertion position.""" formatted_macro = self.format_service.format_content(xml_text).rstrip() reference_line = insert_range.start.line if not insert_in_new_line: reference_line -= 1 indent = xml_document.get_line_indentation(reference_line) final_macro_text = self._apply_indent(formatted_macro, indent) if insert_in_new_line: return f"\n{final_macro_text}" return final_macro_text
def get_available_refactoring_actions( self, xml_document: XmlDocument, params: CodeActionParams) -> List[CodeAction]: """Gets a collection of possible refactoring code actions on a selected chunk of the document.""" code_actions = [] text_in_range = xml_document.get_text_in_range(params.range) target_element_tag = self._get_valid_full_element_tag(text_in_range) if target_element_tag is not None: macro = MacroData(name=target_element_tag, content=text_in_range.strip()) macro_definitions = self.macros.definitions_provider.load_macro_definitions( xml_document) tool = GalaxyToolXmlDocument.from_xml_document(xml_document) code_actions.extend( self.macros.create_extract_to_macros_file_actions( tool, macro_definitions, macro, params)) code_actions.extend( self.macros.create_extract_to_local_macro_actions( tool, macro, params)) return code_actions
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 get_xml_context(self, xml_document: XmlDocument, position: Position) -> XmlContext: """Gets the XML context at a given position inside the document. Args: document (Document): The current document. position (Position): The position inside de document. Returns: XmlContext: The resulting context with the current node definition and other information. If the context can not be determined, the default context with no information is returned. """ offset = xml_document.document.offset_at_position(position) if xml_document.is_empty: return XmlContext(xml_document, self.xsd_tree.root, node=None) node = xml_document.get_node_at(offset) xsd_node = self.find_matching_xsd_element(node, self.xsd_tree) line_text = xml_document.document.lines[position.line] context = XmlContext(xml_document, xsd_node, node, line_text, position, offset) return context