A typical use-case scenario for our code edit control is editing XML text. Our code editor was already capable of editing XML and providing basic capabilities such as syntax highlighting, XML formatting, and simple syntax diagnostics. However, it lacked some essential features that the most popular XML formatters and validators have: XSD-based schema validation and auto-completion.
XML editing is usually solved with our Code Editor by linking text edit control to the XML parser, which performs syntax analysis driving all these features. We have been working on different aspects of code analysis for a long time, from in-house developed parsers to ones that use industry-grade API; the XML parser is one of the parsers that we developed in the early days. In the latest update, we have implemented these missing features: schema-based validation and code completion.
Parsing of XML code is implemented using the same approach we utilize for other advanced parsers, which is parsing text into an abstract syntax tree (AST). In the case of XML, it’s relatively trivial, as it only has a few syntax elements: tags, tag parameters, processing instructions, and the body. As a result of the syntax analysis, we have enough information to drive features like code outlining, XML formatting, and error diagnostic of the incorrect syntax in the XML.
Often, XML is bound to the XML schema that formally describes the elements in the XML document. An XML schema defines constraints related to the content of the XML document and validates the XML syntax against this schema; it can also provide guidance about possible input values, making code editing much more efficient.
In previous releases, we added a Language Server protocol-based XML parser with all these features. However, it comes with a large footprint - an LSP server for this parser is implemented using Java, which requires users to install Java on a target computer.
A new XML parser (XmlParserWithSchema) uses System.Xml.Schema.XmlSchema class, which only relies on .NET Framework and does not have any extra dependencies.
XML parser tries to load XmlSchema from the XML document if explicitly declared there; otherwise, developers can set schema file or URL directly via XML parser properties. We use XmlSchema to validate XML document content and display validation errors in the text editor. Likewise, we implement auto-completion functionality by locating the XmlSchema type corresponding to the current position within the text and providing possible input values for its elements or attributes.
Moreover, we made this parser do its job in a separate thread, which improves performance when editing large documents.
We currently support code completion for complex types, choice, and sequence elements, as well as enumerations for parameter values.
It still does not cover the full XSD syntax and complex schema definition, so we are very interested in the feedback related to this new feature.