diff --git a/formatters/eclipse-formatter.xml b/formatters/eclipse-formatter.xml
deleted file mode 100644
index 8b078b85a..000000000
--- a/formatters/eclipse-formatter.xml
+++ /dev/null
@@ -1,405 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/package.json b/package.json
index db1d4fbb1..ea374becc 100644
--- a/package.json
+++ b/package.json
@@ -1053,7 +1053,7 @@
},
"java.format.settings.url": {
"type": "string",
- "markdownDescription": "Specifies the url or file path to the [Eclipse formatter xml settings](https://github.com/redhat-developer/vscode-java/wiki/Formatter-settings).",
+ "markdownDescription": "Specifies the url or file path to the [Eclipse formatter xml settings](https://github.com/redhat-developer/vscode-java/wiki/Formatter-settings). Don't have one yet? [Generate a starter file](command:java.open.formatter.settings) seeded from the current formatter settings.",
"default": null,
"scope": "window",
"order": 20
diff --git a/src/commands.ts b/src/commands.ts
index 533372c73..fd607089e 100644
--- a/src/commands.ts
+++ b/src/commands.ts
@@ -124,6 +124,12 @@ export namespace Commands {
*/
export const OPEN_FORMATTER = 'java.open.formatter.settings';
+ /**
+ * Server-side command: generate a starter java.format.settings.url XML file, seeded from the
+ * current effective formatter settings.
+ */
+ export const GENERATE_FORMATTER_SETTINGS = 'java.project.generateFormatterSettings';
+
/**
* Open a file given the URI
*/
diff --git a/src/extension.ts b/src/extension.ts
index 67d9e0207..4c9af7669 100644
--- a/src/extension.ts
+++ b/src/extension.ts
@@ -1090,7 +1090,6 @@ function openLogFile(logFile, openingFailureWarning: string, column: ViewColumn
}
async function openFormatter(extensionPath) {
- const defaultFormatter = path.join(extensionPath, 'formatters', 'eclipse-formatter.xml');
const formatterUrl: string = getJavaConfiguration().get('format.settings.url');
if (formatterUrl && formatterUrl.length > 0) {
if (isRemote(formatterUrl)) {
@@ -1098,7 +1097,7 @@ async function openFormatter(extensionPath) {
} else {
const document = getPath(formatterUrl);
if (document && fs.existsSync(document)) {
- return openDocument(extensionPath, document, defaultFormatter, null);
+ return openDocument(document);
}
}
}
@@ -1115,13 +1114,13 @@ async function openFormatter(extensionPath) {
file = path.join(root, fileName);
}
if (!fs.existsSync(file)) {
- addFormatter(extensionPath, file, defaultFormatter, relativePath);
+ await promptAndGenerateFormatterSettings(extensionPath, file, relativePath);
} else {
if (formatterUrl) {
getJavaConfiguration().update('format.settings.url', (relativePath !== null ? relativePath : file), global);
- openDocument(extensionPath, file, file, defaultFormatter);
+ openDocument(file);
} else {
- addFormatter(extensionPath, file, defaultFormatter, relativePath);
+ await promptAndGenerateFormatterSettings(extensionPath, file, relativePath);
}
}
}
@@ -1140,12 +1139,9 @@ function getPath(f) {
return null;
}
-function openDocument(extensionPath, formatterUrl, defaultFormatter, relativePath) {
+function openDocument(formatterUrl) {
return workspace.openTextDocument(formatterUrl)
.then(doc => {
- if (!doc) {
- addFormatter(extensionPath, formatterUrl, defaultFormatter, relativePath);
- }
return window.showTextDocument(doc, window.activeTextEditor ?
window.activeTextEditor.viewColumn : undefined)
.then(editor => !!editor);
@@ -1153,10 +1149,8 @@ function openDocument(extensionPath, formatterUrl, defaultFormatter, relativePat
.then(didOpen => {
if (!didOpen) {
window.showWarningMessage('Could not open Formatter Settings file');
- addFormatter(extensionPath, formatterUrl, defaultFormatter, relativePath);
- } else {
- return didOpen;
}
+ return didOpen;
});
}
@@ -1164,55 +1158,69 @@ function isRemote(f) {
return f !== null && f.startsWith('http:/') || f.startsWith('https:/') || f.startsWith('file:/');
}
-async function addFormatter(extensionPath, formatterUrl, defaultFormatter, relativePath) {
+/**
+ * Prompts for a settings file path (or a remote URL), then creates it - seeded with a
+ * java.format.settings.url template generated live from the server's current effective formatter
+ * settings (java.project.generateFormatterSettings), not a static bundled file, so it stays
+ * accurate as the server's defaults evolve and reflects any overrides already in effect.
+ * java.format.settings.url is only updated once the file is actually written, not before, so a
+ * failed write never leaves the setting pointing at a file that doesn't exist.
+ */
+async function promptAndGenerateFormatterSettings(extensionPath, formatterUrl, relativePath) {
const options: InputBoxOptions = {
value: (relativePath ? relativePath : formatterUrl),
prompt: 'please enter URL or Path:',
ignoreFocusOut: true
};
- await window.showInputBox(options).then(f => {
- if (f) {
- const global = workspace.workspaceFolders === undefined;
- if (isRemote(f)) {
- commands.executeCommand(Commands.OPEN_BROWSER, Uri.parse(f));
- getJavaConfiguration().update('format.settings.url', f, global);
- } else {
- if (!path.isAbsolute(f)) {
- const fileName = f;
- if (!global) {
- f = path.join(workspace.workspaceFolders[0].uri.fsPath, fileName);
- relativePath = fileName;
- } else {
- const root = path.join(extensionPath, '..', 'redhat.java');
- ensureExists(root);
- f = path.join(root, fileName);
- }
- } else {
- relativePath = null;
- }
- getJavaConfiguration().update('format.settings.url', (relativePath !== null ? relativePath : f), global);
- if (!fs.existsSync(f)) {
- const name = relativePath !== null ? relativePath : f;
- const msg = `' ${name} ' does not exist. Do you want to create it?`;
- const action = 'Yes';
- window.showWarningMessage(msg, action, 'No').then((selection) => {
- if (action === selection) {
- try {
- ensureExists(path.dirname(f));
- fs.createReadStream(defaultFormatter)
- .pipe(fs.createWriteStream(f))
- .on('finish', () => openDocument(extensionPath, f, defaultFormatter, relativePath));
- } catch (error) {
- window.showErrorMessage(`Failed to create ${f}: ${error}`);
- }
- }
- });
- } else {
- openDocument(extensionPath, f, defaultFormatter, relativePath);
- }
- }
+ const f = await window.showInputBox(options);
+ if (!f) {
+ return;
+ }
+ const global = workspace.workspaceFolders === undefined;
+ if (isRemote(f)) {
+ commands.executeCommand(Commands.OPEN_BROWSER, Uri.parse(f));
+ getJavaConfiguration().update('format.settings.url', f, global);
+ return;
+ }
+ let target = f;
+ let targetRelativePath = relativePath;
+ if (!path.isAbsolute(f)) {
+ const fileName = f;
+ if (!global) {
+ target = path.join(workspace.workspaceFolders[0].uri.fsPath, fileName);
+ targetRelativePath = fileName;
+ } else {
+ const root = path.join(extensionPath, '..', 'redhat.java');
+ ensureExists(root);
+ target = path.join(root, fileName);
}
- });
+ } else {
+ targetRelativePath = null;
+ }
+ if (fs.existsSync(target)) {
+ openDocument(target);
+ return;
+ }
+ let content: string;
+ try {
+ content = await commands.executeCommand(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.GENERATE_FORMATTER_SETTINGS);
+ } catch (error) {
+ window.showErrorMessage(`Failed to generate formatter settings: ${error}`);
+ return;
+ }
+ if (!content) {
+ window.showErrorMessage('Failed to generate formatter settings: the server returned no content.');
+ return;
+ }
+ try {
+ ensureExists(path.dirname(target));
+ await fs.promises.writeFile(target, content, 'utf8');
+ } catch (error) {
+ window.showErrorMessage(`Failed to create ${target}: ${error}`);
+ return;
+ }
+ getJavaConfiguration().update('format.settings.url', (targetRelativePath !== null ? targetRelativePath : target), global);
+ openDocument(target);
}
async function getTriggerFiles(): Promise {