Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide java project setting files by default. #776

Merged
merged 5 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ The following settings are supported:
* `java.import.gradle.enabled` : Enable/disable the Gradle importer.
* `java.import.maven.enabled` : Enable/disable the Maven importer.
* `java.autobuild.enabled` : Enable/disable the 'auto build'.
* `java.maxConcurrentBuilds`: Set max simultaneous project builds.
* `java.completion.favoriteStaticMembers` : Defines a list of static members or types with static members.
* `java.completion.importOrder` : Defines the sorting order of import statements.
* `java.progressReports.enabled` : [Experimental] Enable/disable progress reports from background processes on the server.
Expand All @@ -91,8 +92,8 @@ The following settings are supported:
* `java.completion.guessMethodArguments` : When set to true, method arguments are guessed when a method is selected from as list of code assist proposals.
* `java.completion.enabled` : Enable/disable code completion support.

*New in 0.36.0:*
* `java.maxConcurrentBuilds`: Set max simultaneous project builds.
*New in 0.38.0:*
* `java.configuration.excludeProjectSettingsFiles`: Exclude the extension generated project setting files(.classpath, .project, .settings, .factorypath) from the file explorer. When set to false, it will not revert the changes made on the setting.json file(s).


Troubleshooting
Expand Down
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@
"description": "Specifies the severity of the message when the classpath is incomplete for a Java file",
"scope": "window"
},
"java.configuration.excludeProjectSettingsFiles": {
"type": "boolean",
"default": true,
"description": "Exclude the extension generated project setting files from the file explorer. When set false, it will not revert the changes made on the setting.json file(s).",
"scope": "window"
},
"java.configuration.updateBuildConfiguration": {
"type": [
"string"
Expand Down
42 changes: 8 additions & 34 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ SourceAttachmentRequest, SourceAttachmentResult, SourceAttachmentAttribute } fro
import { ExtensionAPI } from './extension.api';
import * as buildpath from './buildpath';
import * as net from 'net';
import { getJavaConfiguration } from './utils';
import { onConfigurationChange, excludeProjectSettingsFiles } from './settings';

let oldConfig;
let lastStatus;
let languageClient: LanguageClient;
let jdtEventEmitter = new EventEmitter<Uri>();
Expand Down Expand Up @@ -82,7 +83,6 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
item.command = Commands.OPEN_OUTPUT;
let progressBar = window.createStatusBarItem(StatusBarAlignment.Left, Number.MIN_VALUE+1);

oldConfig = getJavaConfiguration();
let serverOptions;
let port = process.env['SERVER_PORT'];
if (!port) {
Expand Down Expand Up @@ -301,6 +301,11 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
}
};
workspace.registerTextDocumentContentProvider('jdt', provider);
if (workspace.workspaceFolders) {
workspace.workspaceFolders.map((folder) => {
excludeProjectSettingsFiles(folder.uri);
});
}
});

let cleanWorkspaceExists = fs.existsSync( path.join(workspacePath, cleanWorkspaceFileName));
Expand Down Expand Up @@ -435,34 +440,6 @@ function isJavaConfigFile(path: String) {
return path.endsWith('pom.xml') || path.endsWith('.gradle');
}

function onConfigurationChange() {
return workspace.onDidChangeConfiguration(params => {
let newConfig = getJavaConfiguration();
if (hasJavaConfigChanged(oldConfig, newConfig)) {
let msg = 'Java Language Server configuration changed, please restart VS Code.';
let action = 'Restart Now';
let restartId = Commands.RELOAD_WINDOW;
oldConfig = newConfig;
window.showWarningMessage(msg, action).then((selection) => {
if (action === selection) {
commands.executeCommand(restartId);
}
});
}
});
}

function hasJavaConfigChanged(oldConfig, newConfig) {
return hasConfigKeyChanged('home', oldConfig, newConfig)
|| hasConfigKeyChanged('jdt.ls.vmargs', oldConfig, newConfig)
|| hasConfigKeyChanged('progressReports.enabled', oldConfig, newConfig);
}

function hasConfigKeyChanged(key, oldConfig, newConfig) {
return oldConfig.get(key) !== newConfig.get(key);
}


function getTempWorkspace() {
return path.resolve(os.tmpdir(), 'vscodesws_' + makeRandomHexString(5));
}
Expand All @@ -477,9 +454,6 @@ function makeRandomHexString(length) {
return result;
}

function getJavaConfiguration(): WorkspaceConfiguration {
return workspace.getConfiguration('java');
}

async function cleanWorkspace(workspacePath) {
const doIt = 'Restart and delete';
Expand Down Expand Up @@ -746,4 +720,4 @@ function isPrefix(parentPath: string, childPath: string): boolean {
}
const relative = path.relative(parentPath, childPath);
return !!relative && !relative.startsWith('..') && !path.isAbsolute(relative);
}
}
69 changes: 69 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

import { window, Uri, workspace, WorkspaceConfiguration, commands, ConfigurationTarget } from 'vscode';
import { LanguageClient } from 'vscode-languageclient';
import { Commands } from './commands';
import { getJavaConfiguration } from './utils';


const DEFAULT_HIDDEN_FILES: string[] = ['**/.classpath', '**/.project', '**/.settings', '**/.factorypath'];

let oldConfig: WorkspaceConfiguration = getJavaConfiguration();

export function onConfigurationChange() {
return workspace.onDidChangeConfiguration(params => {
if (!params.affectsConfiguration('java')) {
return;
}
let newConfig = getJavaConfiguration();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want to check that if configuration.excludeProjectSettingsFiles is true, but it was false before, then excludeProjectSettingsFiles should be called.

Be aware that oldConfig is only changed if the "java configuration" changed, so it makes things a bit trickier.

if (hasJavaConfigChanged(oldConfig, newConfig)) {
let msg = 'Java Language Server configuration changed, please restart VS Code.';
let action = 'Restart Now';
let restartId = Commands.RELOAD_WINDOW;
oldConfig = newConfig;
window.showWarningMessage(msg, action).then((selection) => {
if (action === selection) {
commands.executeCommand(restartId);
}
});
}
});
}

export function excludeProjectSettingsFiles(workspaceUri: Uri) {
const excudedConfig = getJavaConfiguration().get('configuration.excludeProjectSettingsFiles');
if (excudedConfig) {
const config = workspace.getConfiguration('files', workspaceUri);
const excludedValue: Object = config.get('exclude');
const needExcludeFiles: Object = {};

let needUpdate = false;
for (const hiddenFiles of DEFAULT_HIDDEN_FILES) {
if (!excludedValue.hasOwnProperty(hiddenFiles)) {
needExcludeFiles[hiddenFiles] = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this will always store a subset of the files, overriding existing settings.
We do want to merge excludedValue + DEFAULT_HIDDEN_FILES in config.get('exclude')

needUpdate = true;
}
}
if (needUpdate) {
window.showInformationMessage('Do you want to exclude the VSCode Java project settings files(.classpath, .project. .settings, .factorypath) from the file explorer.', 'Always', 'Workspace', 'Never').then((result) => {
if (result === 'Always') {
config.update('exclude', needExcludeFiles, ConfigurationTarget.Global);
} if (result === 'Workspace') {
config.update('exclude', needExcludeFiles, ConfigurationTarget.Workspace);
} else if (result === 'Never') {
getJavaConfiguration().update('configuration.excludeProjectSettingsFiles', false, ConfigurationTarget.Global);
}
});
}
}
}

function hasJavaConfigChanged(oldConfig: WorkspaceConfiguration, newConfig: WorkspaceConfiguration) {
return hasConfigKeyChanged('home', oldConfig, newConfig)
|| hasConfigKeyChanged('jdt.ls.vmargs', oldConfig, newConfig)
|| hasConfigKeyChanged('progressReports.enabled', oldConfig, newConfig);
}

function hasConfigKeyChanged(key, oldConfig, newConfig) {
return oldConfig.get(key) !== newConfig.get(key);
}
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

import { workspace, WorkspaceConfiguration } from 'vscode';

export function getJavaConfiguration(): WorkspaceConfiguration {
return workspace.getConfiguration('java');
}