Skip to content

Commit ef91528

Browse files
dkwon17fbricon
authored andcommitted
Add xml.java.home preference
Signed-off-by: David Kwon <dakwon@redhat.com>
1 parent ec99ac5 commit ef91528

File tree

3 files changed

+79
-43
lines changed

3 files changed

+79
-43
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Description
66

7-
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.
7+
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.
88

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

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

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

4849
The following settings are supported:
49-
50-
* `xml.trace.server` : Trace the communication between VS Code and the XML language server in the Output view.
50+
51+
* `xml.trace.server` : Trace the communication between VS Code and the XML Language Server in the Output view.
5152
* `xml.catalogs` : Register XML catalog files.
5253
* `xml.logs.client` : Enable/disable logging to the Output view.
5354
* `xml.fileAssociations` : Associate XML Schemas to XML file patterns.
5455
* `xml.format.splitAttributes` : Set to `true` to split node attributes onto multiple lines during formatting. Defaults to `false`.
5556
* `xml.format.joinCDATALines` : Set to `true` to join lines in CDATA content during formatting. Defaults to `false`.
5657
* `xml.format.joinContentLines` : Set to `true` to join lines in node content during formatting. Defaults to `false`.
5758
* `xml.format.joinCommentLines` : Set to `true` to join lines in comments during formatting. Defaults to `false`.
59+
* `xml.format.preservedNewLines`: Set the maximum amount of newlines between elements. Defaults to `2`.
60+
* `xml.format.preserveEmptyContent`: Set to `true` to preserve standalone whitespace content in an element. Defaults to `false`.
61+
* `xml.format.spaceBeforeEmptyCloseTag`: Set to `true` to insert space before the end of a self closing tag. Defaults to `true`.
62+
* `xml.format.quotations`: Set to `doubleQuotes` to format and only use `"`, or `singleQuotes` to format and only use `'`. Defaults to `doubleQuotes`.
5863
* `xml.format.enabled` : Enable/disable formatting.
5964
* `xml.autoCloseTags.enabled` : Enable/disable automatic tag closing.
6065
**Note** 'editor.autoClosingBrackets' must be turned off to work.
6166
* `xml.server.vmargs`: Extra VM arguments used to launch the XML Language Server. Requires VS Code restart.
6267
* `xml.validation.enabled`: Set to `false` to disable all validation. Defaults to `true`.
6368
* `xml.validation.schema`: Set to `false` to disable schema validation. Defaults to `true`.
6469
* `xml.validation.noGrammar`: The message severity when a document has no associated grammar. Defaults to `hint`.
65-
* `xml.format.spaceBeforeEmptyCloseTag`: Set to `true` to insert space before the end of a self closing tag. Defaults to `true`.
66-
* `xml.format.quotations`: Set to `doubleQuotes` to format and only use `"`, or `singleQuotes` to format and only use `'`. Defaults to `doubleQuotes`.
67-
* `xml.format.preserveEmptyContent`: Set to `true` to preserve standalone whitespace content in an element. Defaults to `false`.
6870
* `xml.server.workDir`: Set an absolute path for all cached schemas to be stored. Defaults to `~/.lsp4xml`.
6971

70-
Since 0.6.0:
71-
* `xml.format.preservedNewLines`: Set the maximum amount of newlines between elements. Defaults to `2`.
72+
Since 0.7.0:
73+
* `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.
7274

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

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@
188188
"description": "Enable/disable autoclosing of XML tags. \n\nIMPORTANT: Turn off editor.autoClosingTags for this to work",
189189
"scope": "window"
190190
},
191+
"xml.java.home": {
192+
"type": [
193+
"string",
194+
"null"
195+
],
196+
"default": null,
197+
"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\"",
198+
"scope": "window"
199+
},
191200
"xml.server.vmargs": {
192201
"type": [
193202
"string",

src/requirements.ts

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,52 +29,77 @@ interface ErrorData {
2929
*
3030
*/
3131
export async function resolveRequirements(): Promise<RequirementsData> {
32-
let java_home = await checkJavaRuntime();
33-
let javaVersion = await checkJavaVersion(java_home);
34-
return Promise.resolve({ 'java_home': java_home, 'java_version': javaVersion});
32+
const javaHome = await checkJavaRuntime();
33+
const javaVersion = await checkJavaVersion(javaHome);
34+
return Promise.resolve({ 'java_home': javaHome, 'java_version': javaVersion});
3535
}
3636

3737
function checkJavaRuntime(): Promise<string> {
3838
return new Promise((resolve, reject) => {
39-
let source : string;
40-
let javaHome : string = readJavaConfig();
41-
if (javaHome) {
42-
source = 'The java.home variable defined in VS Code settings';
43-
} else {
44-
javaHome = process.env['JDK_HOME'];
45-
if (javaHome) {
46-
source = 'The JDK_HOME environment variable';
47-
} else {
48-
javaHome = process.env['JAVA_HOME'];
49-
source = 'The JAVA_HOME environment variable';
50-
}
51-
}
52-
if(javaHome ){
53-
javaHome = expandHomeDir(javaHome);
54-
if(!pathExists.sync(javaHome)){
55-
openJDKDownload(reject, source+' points to a missing folder');
56-
}
57-
if(!pathExists.sync(path.resolve(javaHome, 'bin', JAVAC_FILENAME))){
58-
openJDKDownload(reject, source+ ' does not point to a JDK.');
59-
}
60-
return resolve(javaHome);
61-
}
39+
40+
checkXMLJavaHome(resolve, reject);
41+
checkJavaHome(resolve, reject);
42+
checkEnvVariable('JDK_HOME', resolve, reject);
43+
checkEnvVariable('JAVA_HOME', resolve, reject);
44+
6245
//No settings, let's try to detect as last resort.
6346
findJavaHome(function (err, home) {
64-
if (err){
65-
openJDKDownload(reject,'Java runtime could not be located');
66-
}
67-
else {
68-
resolve(home);
69-
}
70-
});
47+
if (err){
48+
openJDKDownload(reject, 'Java runtime could not be located.');
49+
}
50+
else {
51+
resolve(home);
52+
}
53+
});
7154
});
7255
}
7356

74-
function readJavaConfig() : string {
57+
function checkXMLJavaHome(resolve, reject) {
58+
const javaHome = readXMLJavaHomeConfig();
59+
if (!javaHome) {
60+
return;
61+
}
62+
const source = 'The xml.java.home variable defined in VS Code settings';
63+
handleJavaPath(javaHome, source, resolve, reject);
64+
}
65+
66+
function checkJavaHome(resolve, reject) {
67+
const javaHome = readJavaHomeConfig();
68+
if (!javaHome) {
69+
return;
70+
}
71+
const source = 'The java.home variable defined in VS Code settings';
72+
handleJavaPath(javaHome, source, resolve, reject);
73+
}
74+
75+
function checkEnvVariable(name : string, resolve, reject) {
76+
if (!process.env[name]) {
77+
return;
78+
}
79+
const source = `The ${name} environment variable`;
80+
handleJavaPath(process.env[name], source, resolve, reject);
81+
}
82+
83+
function readXMLJavaHomeConfig() : string {
84+
return workspace.getConfiguration('xml').java.home;
85+
}
86+
87+
function readJavaHomeConfig() : string {
7588
const config = workspace.getConfiguration();
7689
return config.get<string>('java.home',null);
7790
}
91+
92+
function handleJavaPath(javaHome : string, source : string, resolve, reject) {
93+
const javaHomeExpanded = expandHomeDir(javaHome);
94+
95+
if (!pathExists.sync(javaHomeExpanded)) {
96+
openJDKDownload(reject, source + ' points to a missing folder.');
97+
}
98+
if (!pathExists.sync(path.resolve(javaHomeExpanded, 'bin', JAVAC_FILENAME))) {
99+
openJDKDownload(reject, source + ' does not point to a JDK.');
100+
}
101+
return resolve(javaHomeExpanded);
102+
}
78103

79104
function checkJavaVersion(java_home: string): Promise<number> {
80105
return new Promise((resolve, reject) => {
@@ -112,7 +137,7 @@ export function parseMajorVersion(content:string):number {
112137
return javaVersion;
113138
}
114139

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

0 commit comments

Comments
 (0)