Skip to content

Commit

Permalink
Add xml.java.home preference
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <dakwon@redhat.com>
  • Loading branch information
dkwon17 authored and fbricon committed Jun 4, 2019
1 parent ec99ac5 commit ef91528
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 43 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Description

This VS Code extension provides support for creating and editing XML documents, based on the [LSP4XML language server](https://github.com/angelozerr/lsp4xml), running with Java.
This VS Code extension provides support for creating and editing XML documents, based on the [LSP4XML Language Server](https://github.com/angelozerr/lsp4xml), running with Java.

![Basic features](https://user-images.githubusercontent.com/148698/45977901-df208a80-c018-11e8-85ec-71c70ba8a5ca.gif)

Expand Down Expand Up @@ -38,6 +38,7 @@ See the [changelog](CHANGELOG.md) for the latest release. You might also find us

* Java JDK 8 or more recent
* Ensure Java path is set in either:
* `xml.java.home` in VSCode preferences
* `java.home` in VSCode preferences
* Environment variable `JAVA_HOME` or `JDK_HOME`
* **Note**: The path should end at the parent folder that contains the `bin` folder.
Expand All @@ -46,29 +47,30 @@ See the [changelog](CHANGELOG.md) for the latest release. You might also find us
## Supported VS Code settings

The following settings are supported:

* `xml.trace.server` : Trace the communication between VS Code and the XML language server in the Output view.
* `xml.trace.server` : Trace the communication between VS Code and the XML Language Server in the Output view.
* `xml.catalogs` : Register XML catalog files.
* `xml.logs.client` : Enable/disable logging to the Output view.
* `xml.fileAssociations` : Associate XML Schemas to XML file patterns.
* `xml.format.splitAttributes` : Set to `true` to split node attributes onto multiple lines during formatting. Defaults to `false`.
* `xml.format.joinCDATALines` : Set to `true` to join lines in CDATA content during formatting. Defaults to `false`.
* `xml.format.joinContentLines` : Set to `true` to join lines in node content during formatting. Defaults to `false`.
* `xml.format.joinCommentLines` : Set to `true` to join lines in comments during formatting. Defaults to `false`.
* `xml.format.preservedNewLines`: Set the maximum amount of newlines between elements. Defaults to `2`.
* `xml.format.preserveEmptyContent`: Set to `true` to preserve standalone whitespace content in an element. Defaults to `false`.
* `xml.format.spaceBeforeEmptyCloseTag`: Set to `true` to insert space before the end of a self closing tag. Defaults to `true`.
* `xml.format.quotations`: Set to `doubleQuotes` to format and only use `"`, or `singleQuotes` to format and only use `'`. Defaults to `doubleQuotes`.
* `xml.format.enabled` : Enable/disable formatting.
* `xml.autoCloseTags.enabled` : Enable/disable automatic tag closing.
**Note** 'editor.autoClosingBrackets' must be turned off to work.
* `xml.server.vmargs`: Extra VM arguments used to launch the XML Language Server. Requires VS Code restart.
* `xml.validation.enabled`: Set to `false` to disable all validation. Defaults to `true`.
* `xml.validation.schema`: Set to `false` to disable schema validation. Defaults to `true`.
* `xml.validation.noGrammar`: The message severity when a document has no associated grammar. Defaults to `hint`.
* `xml.format.spaceBeforeEmptyCloseTag`: Set to `true` to insert space before the end of a self closing tag. Defaults to `true`.
* `xml.format.quotations`: Set to `doubleQuotes` to format and only use `"`, or `singleQuotes` to format and only use `'`. Defaults to `doubleQuotes`.
* `xml.format.preserveEmptyContent`: Set to `true` to preserve standalone whitespace content in an element. Defaults to `false`.
* `xml.server.workDir`: Set an absolute path for all cached schemas to be stored. Defaults to `~/.lsp4xml`.

Since 0.6.0:
* `xml.format.preservedNewLines`: Set the maximum amount of newlines between elements. Defaults to `2`.
Since 0.7.0:
* `xml.java.home`: Set the Java path required to run the XML Language Server. If not set, falls back to either the `java.home` preference or the `JAVA_HOME` or `JDK_HOME` environment variables.

More detailed info in the [Wiki](https://github.com/redhat-developer/vscode-xml/wiki/Preferences).

Expand Down
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@
"description": "Enable/disable autoclosing of XML tags. \n\nIMPORTANT: Turn off editor.autoClosingTags for this to work",
"scope": "window"
},
"xml.java.home": {
"type": [
"string",
"null"
],
"default": null,
"description": "Specifies the folder path to the JDK (8 or more recent) used to launch the XML Language Server.\nOn Windows, backslashes must be escaped, i.e.\n\"xml.java.home\": \"C:\\\\Program Files\\\\Java\\\\jdk1.8.0_161\"",
"scope": "window"
},
"xml.server.vmargs": {
"type": [
"string",
Expand Down
95 changes: 60 additions & 35 deletions src/requirements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,52 +29,77 @@ interface ErrorData {
*
*/
export async function resolveRequirements(): Promise<RequirementsData> {
let java_home = await checkJavaRuntime();
let javaVersion = await checkJavaVersion(java_home);
return Promise.resolve({ 'java_home': java_home, 'java_version': javaVersion});
const javaHome = await checkJavaRuntime();
const javaVersion = await checkJavaVersion(javaHome);
return Promise.resolve({ 'java_home': javaHome, 'java_version': javaVersion});
}

function checkJavaRuntime(): Promise<string> {
return new Promise((resolve, reject) => {
let source : string;
let javaHome : string = readJavaConfig();
if (javaHome) {
source = 'The java.home variable defined in VS Code settings';
} else {
javaHome = process.env['JDK_HOME'];
if (javaHome) {
source = 'The JDK_HOME environment variable';
} else {
javaHome = process.env['JAVA_HOME'];
source = 'The JAVA_HOME environment variable';
}
}
if(javaHome ){
javaHome = expandHomeDir(javaHome);
if(!pathExists.sync(javaHome)){
openJDKDownload(reject, source+' points to a missing folder');
}
if(!pathExists.sync(path.resolve(javaHome, 'bin', JAVAC_FILENAME))){
openJDKDownload(reject, source+ ' does not point to a JDK.');
}
return resolve(javaHome);
}

checkXMLJavaHome(resolve, reject);
checkJavaHome(resolve, reject);
checkEnvVariable('JDK_HOME', resolve, reject);
checkEnvVariable('JAVA_HOME', resolve, reject);

//No settings, let's try to detect as last resort.
findJavaHome(function (err, home) {
if (err){
openJDKDownload(reject,'Java runtime could not be located');
}
else {
resolve(home);
}
});
if (err){
openJDKDownload(reject, 'Java runtime could not be located.');
}
else {
resolve(home);
}
});
});
}

function readJavaConfig() : string {
function checkXMLJavaHome(resolve, reject) {
const javaHome = readXMLJavaHomeConfig();
if (!javaHome) {
return;
}
const source = 'The xml.java.home variable defined in VS Code settings';
handleJavaPath(javaHome, source, resolve, reject);
}

function checkJavaHome(resolve, reject) {
const javaHome = readJavaHomeConfig();
if (!javaHome) {
return;
}
const source = 'The java.home variable defined in VS Code settings';
handleJavaPath(javaHome, source, resolve, reject);
}

function checkEnvVariable(name : string, resolve, reject) {
if (!process.env[name]) {
return;
}
const source = `The ${name} environment variable`;
handleJavaPath(process.env[name], source, resolve, reject);
}

function readXMLJavaHomeConfig() : string {
return workspace.getConfiguration('xml').java.home;
}

function readJavaHomeConfig() : string {
const config = workspace.getConfiguration();
return config.get<string>('java.home',null);
}

function handleJavaPath(javaHome : string, source : string, resolve, reject) {
const javaHomeExpanded = expandHomeDir(javaHome);

if (!pathExists.sync(javaHomeExpanded)) {
openJDKDownload(reject, source + ' points to a missing folder.');
}
if (!pathExists.sync(path.resolve(javaHomeExpanded, 'bin', JAVAC_FILENAME))) {
openJDKDownload(reject, source + ' does not point to a JDK.');
}
return resolve(javaHomeExpanded);
}

function checkJavaVersion(java_home: string): Promise<number> {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -112,7 +137,7 @@ export function parseMajorVersion(content:string):number {
return javaVersion;
}

function openJDKDownload(reject, cause) {
function openJDKDownload(reject, cause : string) {
let jdkUrl = 'https://developers.redhat.com/products/openjdk/download/?sc_cid=701f2000000RWTnAAO';
if (process.platform === 'darwin') {
jdkUrl = 'http://www.oracle.com/technetwork/java/javase/downloads/index.html';
Expand Down

0 comments on commit ef91528

Please sign in to comment.