Code Monkey home page Code Monkey logo

xml-language-server's Introduction

XML Language Server

I started this project when there was no XML Plugin available for VSCode. But now we have XML Plugin by RedHat available, so I recommend all to use that.
Do check em out: https://github.com/angelozerr/lsp4xml, https://github.com/redhat-developer/vscode-xml.
I will continue improving the current one, but not having the freedom to dedicate time on it may take a bit longer to finish it.

build-status Coverage Status

Language server for XML documents. It enables and LSP client (VScode, Sublime LSP, Eclipse Che) to leverage capabilities such as autocomplete, validation or documentation support for XML documents.

Refer to CONTRIBUTING.md for setting up development environment

Demo

demo

Features

  • XML Validations
    • XSD
    • DTD
  • Autocomplete
    • XSD
    • DTD
  • Documentation
    • XSD
    • DTD

What is language server?

Language server is the server part of Language Server Protocol (LSP).
As per official documentation,

The Language Server protocol is used between a tool (the client) and a language smartness provider (the server) to integrate features like auto complete, go to definition, find all references and alike into the tool.

Why XML?

We use XML documents, configs extensively for our day to day work, and simple config typo of elements or attributes breaks our head for hours. Intellij was already supporting features for XML such as validation, autocomplete, documentation (using annotations from xsd), but text editors like vscode or sublime doesn't have support for XML. It's an attempt to make editors smart for XML documents.

Why Java?

Currently the implementation of language server for XML is done in Java. I couldn't find a mature and apt library for XML manipulations in other languages. Becuase of the nature of Java previous to Java 9 the users of this server (language client) needs to have java installed in their system.

Build

  • Requirements:
    • Java 8
    • Maven 3.5+
mvn clean package # To generate jar file locally
mvn clean install # To install artifact in local repository

For Docker Fans

docker run -it --rm --name xml-language-server -v $(pwd):/usr/src/mymaven -w /usr/src/mymaven maven:3.6.0-jdk-8-alpine mvn clean install

Don't forget to replace $(pwd) with absolute path of the project.
Also if you are on windows make sure, you have give docker permission to access/mount volumes.

xml-language-server's People

Contributors

dmaroulidis avatar handofgod94 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

dmaroulidis scto

xml-language-server's Issues

Wrong documentation on hover

Following issues needs to be addressed regarding documentation:

  • Since documentation is searched with selectSingleNode it will display wrong documentation if the tag remains same.
  • namespace specific documentation. If elements belongs to different namespace, it should search documentation based on the namespace. Currently, it searches all the xsds files specified in schemaLocation.

Steps to reproduce:

  • Open any of the complex pom.xml, such as this projects itself, in any editor supporting LSP, preferably VSCode.
  • Hover over artifactId in dependency and parent pom itself, both displays same documentation.

org.dom4j.DocumentException on resolving schema

Describe the bug
If XML is not well formed, but has schemaLocation defined in the root tag, it fails with following error.
This is because, in order to resolve schemas, it first tries to parse it and then tries to show the error. This should be avoided
i.e. if XML is malformed, first display those error and then if its not then display schema validations.

17:00:50.377 [pool-2-thread-1] ERROR io.github.handofgod94.schema.resolve.XsdSchemaResolver - Unable to parse or load schema
org.dom4j.DocumentException: Error on line 18 of document  : The element type "choice" must be terminated by the matching end-tag "</choice>".
	at org.dom4j.io.SAXReader.read(SAXReader.java:462) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.dom4j.io.SAXReader.read(SAXReader.java:345) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at io.github.handofgod94.schema.resolve.XsdSchemaResolver.resolve(XsdSchemaResolver.java:44) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at io.github.handofgod94.main.XmlDocumentService.didSave(XmlDocumentService.java:145) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_191]
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_191]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_191]
	at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_191]
	at org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint.lambda$null$0(GenericEndpoint.java:65) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint.notify(GenericEndpoint.java:152) [xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.handleNotification(RemoteEndpoint.java:220) [xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.eclipse.lsp4j.jsonrpc.RemoteEndpoint.consume(RemoteEndpoint.java:187) [xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.handleMessage(StreamMessageProducer.java:192) [xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.eclipse.lsp4j.jsonrpc.json.StreamMessageProducer.listen(StreamMessageProducer.java:94) [xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.eclipse.lsp4j.jsonrpc.json.ConcurrentMessageProcessor.run(ConcurrentMessageProcessor.java:99) [xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_191]
	at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_191]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_191]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_191]
	at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
Caused by: org.xml.sax.SAXParseException: The element type "choice" must be terminated by the matching end-tag "</choice>".
	at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	at org.dom4j.io.SAXReader.read(SAXReader.java:445) ~[xml-language-server-1.0-SNAPSHOT-jar-with-dependencies.jar:?]
	... 19 more

To Reproduce

Steps to reproduce the behavior:

  1. Open any well formed XML File with valid schemaLocation
  2. Add an extra element but just the starting tag and not the ending tag.
  3. Save the file.

Expected behavior
It should first display malformed XML error in diagnostic report.

Desktop (please complete the following information):

  • OS : Windows
  • Editor : VSCode
  • Editor Version : 1.30.1

Local cache repository for Remote XSD

Implement a cache repository, preferably using h2, to store XSD fetched from Remote
Future release should also handle DTD files.
The lookup for xsd will first take place in this cache and then will try to fetch it from the remote.
It should handle multiple versions of xsd too.

Enable CI/CD using travis

Add required configs for travis CI to run.
A separate dev branch on which travis builds will be configured and a daily build needs to be configured on master branch.

Avoid displaying attribute already present in autocomplete popup

When autocomplete requested for an element, it shows attribute already present in the element.
An xml can't have 2 same attributes for same element.
It should not display elements which are already present

Currently it displays, an error if the attributes are repeated for the current element.

Need to reload if schemaLocation changes

Describe the bug
Currently, if user adds, removes or updates a schemaLocation for an XML, the file needs to be reopened
to load new XML Schema models from the XSD.

To Reproduce
Steps to reproduce the behavior:

  1. Open any valid XML File
  2. Modify the schemaLocation
  3. Save it.

Expected behavior
It should load the metadata from new XSD location, and should provide hover information and
element/attribute completion accordingly.

Desktop (please complete the following information):

  • OS : Windows
  • Editor : VSCode
  • Editor Version : 1.30.1

Integration Tests

Need to implement integration test and ci integration with travis.
It should consider maximum possible xmls and xsd that users can have and the server
should not crash, unknowingly.

Refactor Utility Classes

Utility classes currently are written as java 8 Function lambdas. This decreases readability and documentations. It needs to be changed to static final methods.

Generic validation for elements and attributes

All the features of lsp for elements and attributes should only be activated in specific contexts.

For e.g.

  <foo fizz="buzz">bar</foo>
  • The hover should not be activated for innerText
  • The hover should not be activated for attributeValue
  • The hover should not be activated for comments
  • The completion invocation should not give suggestions inside comments..... and many more.

Certain unit tests written as part PR(#25) are failing because it checks these kind of scenarios.

Sequence constraint not honored for autocompletion

Describe the bug
The completion suggestion is listing all the suggestions for a give parent, and is not honoring the
sequence in the elements should occur. The users gets notification after they modify the documet

To Reproduce
Steps to reproduce the behavior:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://camel.apache.org/schema/spring 
       http://camel.apache.org/schema/spring/camel-spring.xsd">

  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <route>
      <from uri="seda:foo"/>
      <to uri="mock:results"/>
    </route>
  </camelContext>
</beans>
  1. Open VSCode with attached XML
  2. List down element suggestion after line 17 by typing <.
  3. Select export element and add uri parameter to it since its required.
  4. It throws an cvc error.

Expected behavior
It should not list export if it's after route element as per XSD, since it has to honor xsd:sequence.

Screenshots

  • Wrong suggestions in autocomplete
    image

Desktop (please complete the following information):

  • OS : Windows
  • Editor : VSCode
  • Editor Version : 1.28.2

Documentation on hover

XSD files have xs:annotation which may contain documentation info for an element.
Support needed for providing documentation on hover of elements.

API Implementation using Google Guice

Google Guice is a nice Java library for dependency Injection.
Integrate it for better and more modular codebase for Language server.
Also document it that developer needs to follow.

Unable to resolve local xsd schemaLocation

Describe the bug
If XML has a schemaLocation path relative to the current XML, then it's unable to resolve it

To Reproduce
Steps to reproduce the behavior:

  1. Open any valid XML file, pointing to local XSD.
  2. Displays an error, saying unable to load.

Expected behavior
If relative path is correct, then it should load it.

Desktop (please complete the following information):

  • OS : Windows
  • Editor : VSCode
  • Editor Version : 1.30.1

Auto completion by reading XSD documents

Support for autocompletion of xml element tags and attributes.
For alpha release, focus will be on basic XSD schemas, DTD Support will be added later.
Also initial plan is to make it work for documents having namespace in the root element i.e. not prefix in the element tags.
Support for elements having namespace will be done at later stage.

Autocompletion suggestion outside xml elements

Displays auto completion list out side xml elements.

To Reproduce
Steps to reproduce the behavior:

  1. Open any XML document.
  2. Go to any position which is out side of an element but in the same line.
  3. Invoke suggestions by hitting Ctrl+Space.

Expected behavior
It should not display the list of attributes for that element.

Screenshot:
image

Desktop (please complete the following information):

  • OS : Windows
  • Editor :VSCode
  • Editor Version :1.28.2

Upgrade Java

Currently it's written in Java 8.
Certain features, needs to evaluated first:

  • new module system capabilities of java 9+. All the third party libs should have module support.
  • Change the annotation to JSR-330 for dependency injections.
  • Evaluate the size of heavy fat run time that will be created if we use jmod and jlink as packaging instead of jars.
  • Update maven version
  • Update travis build scripts

Add licence

Preferably MIT or The Unlicense for the repo.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.