def test_get_node_at_returns_expected_type(self, document: Document, position: Position, expected: NodeType) -> None:
        parser = XmlDocumentParser()
        xml_document = parser.parse(document)
        offset = document.offset_at_position(position)

        actual = xml_document.get_node_at(offset)

        assert actual.node_type == expected
Exemple #2
0
    def text_to_position(self, doc: Document, position: Position) -> str:
        """Return the contents of the document up until the given position.

        Parameters
        ----------
        doc:
           The document associated with the given position
        position:
           The position representing the point at which to stop gathering text.
        """
        idx = doc.offset_at_position(position)
        return doc.source[:idx]
    def get_xml_context(self, document: Document,
                        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 = document.offset_at_position(position)

        parser = XmlDocumentParser()
        xml_document = parser.parse(document)
        if xml_document.is_empty:
            return XmlContext(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 = document.lines[position.line]
        context = XmlContext(xsd_node, node, line_text, position, offset)
        return context