From ce6a11d26bdcd1247f75a0993cefef2daaba732d Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Fri, 13 May 2022 03:46:52 +0200 Subject: [PATCH] Should explicitly report the fatal settings error Signed-off-by: Snjezana Peco --- package.json | 4 ++-- src/extension.ts | 10 +++++----- src/settings.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 8598d2d23..5340d12b4 100644 --- a/package.json +++ b/package.json @@ -543,7 +543,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. \n\nFor example: \n\n`\"java.format.settings.url\":\"file://home/myuser/format.xml\"`\n\n `\"java.format.settings.url\": \"https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml\"`\n\n`\"java.format.settings.url\":\".vscode/format.xml\"`\n\n `\"java.format.settings.url\":\".vscode\\\\format.xml\" (Windows)` \n\n`\"java.format.settings.url\":\"file:///C:/Users/MyUser/format.xml\" (Windows)` \n\n`\"java.format.settings.url\":\"C:\\\\Users\\\\MyUser\\\\format.xml\" (Windows)` \n\nSee [Eclipse formatter xml settings](https://github.com/redhat-developer/vscode-java/wiki/Formatter-settings).", "default": null, "scope": "window" }, @@ -839,7 +839,7 @@ }, "java.settings.url": { "type": "string", - "markdownDescription": "Specifies the url or file path to the workspace Java settings. See [Setting Global Preferences](https://github.com/redhat-developer/vscode-java/wiki/Settings-Global-Preferences)", + "markdownDescription": "Specifies the url or file path to the workspace Java settings. \n\nFor example: \n\n`\"java.settings.url\":\"file://home/myuser/settings.prefs\"`\n\n`\"java.settings.url\":\".vscode/settings.prefs\"`\n\n `\"java.settings.url\":\".vscode\\\\settings.prefs\" (Windows)` \n\n`\"java.settings.url\":\"file:///C:/Users/MyUser/settings.prefs\" (Windows)` \n\n`\"java.settings.url\":\"C:\\\\Users\\\\MyUser\\\\settings.prefs\" (Windows)` \n\nSee [Setting Global Preferences](https://github.com/redhat-developer/vscode-java/wiki/Settings-Global-Preferences)", "default": null, "scope": "window" }, diff --git a/src/extension.ts b/src/extension.ts index 21e9c3cbf..afe19f135 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -14,7 +14,7 @@ import { initialize as initializeRecommendation } from './recommendation'; import { Commands } from './commands'; import { ExtensionAPI, ClientStatus } from './extension.api'; import { getJavaConfiguration, deleteDirectory, getBuildFilePatterns, getInclusionPatternsFromNegatedExclusion, convertToGlob, getExclusionBlob, ensureExists } from './utils'; -import { onConfigurationChange, getJavaServerMode, ServerMode, ACTIVE_BUILD_TOOL_STATE, handleTextBlockClosing } from './settings'; +import { onConfigurationChange, getJavaServerMode, ServerMode, ACTIVE_BUILD_TOOL_STATE, handleTextBlockClosing, checkJavaPreferences, checkSettings, isRemote } from './settings'; import { logger, initializeLogFile } from './log'; import glob = require('glob'); import { SyntaxLanguageClient } from './syntaxLanguageClient'; @@ -185,6 +185,10 @@ export function activate(context: ExtensionContext): Promise { cleanJavaWorkspaceStorage(); + checkSettings('format.settings.url'); + + checkSettings('settings.url'); + serverStatusBarProvider.initialize(); return requirements.resolveRequirements(context).catch(error => { @@ -856,10 +860,6 @@ function openDocument(extensionPath, formatterUrl, defaultFormatter, relativePat }); } -function isRemote(f) { - return f !== null && f.startsWith('http:/') || f.startsWith('https:/') || f.startsWith('file:/'); -} - async function addFormatter(extensionPath, formatterUrl, defaultFormatter, relativePath) { const options: InputBoxOptions = { value: (relativePath ? relativePath : formatterUrl), diff --git a/src/settings.ts b/src/settings.ts index 05c95ac36..900d2439c 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -2,6 +2,7 @@ import * as fs from 'fs'; import * as path from 'path'; +import * as url from 'url'; import { window, Uri, workspace, WorkspaceConfiguration, commands, ConfigurationTarget, env, ExtensionContext, TextEditor, Range, Disposable, WorkspaceFolder, TextDocument, Position, SnippetString, TextLine } from 'vscode'; import { Commands } from './commands'; import { cleanWorkspaceFileName } from './extension'; @@ -55,6 +56,8 @@ export function onConfigurationChange(workspacePath: string) { } // update old config oldConfig = newConfig; + checkSettings('format.settings.url'); + checkSettings('settings.url'); }); } @@ -319,3 +322,42 @@ export function handleTextBlockClosing(document: TextDocument, changes: readonly } } } + +export function checkSettings(preferenceName) { + const preference: string = getJavaConfiguration().get(preferenceName); + let show = false; + if (isRemote(preference)) { + try { + const fileUrl = new url.URL(preference); + const protocol = fileUrl.protocol; + if (protocol === 'file:') { + show = !fs.existsSync(fileUrl); + if (!show) { + show = !preference.startsWith("file:///") && (preference.startsWith("file://") || !preference.startsWith("file:/")); + } + } + } catch (err) { + show = true; + } + } else if (preference !== null && !fs.existsSync(preference)) { + show = true; + } + if (show) { + const msg = `The 'java.${preferenceName}=${preference}' preference is not valid.`; + const action = 'Open settings'; + const restartId = Commands.OPEN_JSON_SETTINGS; + window.showWarningMessage(msg, action).then((selection) => { + if (action === selection) { + commands.executeCommand(restartId, `java.${preferenceName}`); + } + }); + } +} + +export function isRemote(f) { + if (f !== null) { + const protocol = url.parse(f).protocol; + return protocol !== null && (protocol === 'file:' || protocol === 'http:' || protocol === 'https:'); + } + return false; +} \ No newline at end of file