From fd424dc72950dcc50868d6fcfcb10c897c39b0e6 Mon Sep 17 00:00:00 2001 From: Maximilian Wittmer Date: Tue, 4 Jul 2023 13:43:18 +0200 Subject: [PATCH] Refactor CharsetManager#setCharsetFor #525 split function into private subfunctions --- .../internal/resources/CharsetManager.java | 83 +++++++++++-------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/CharsetManager.java b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/CharsetManager.java index 7f16808b5ac..7d645423cc9 100644 --- a/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/CharsetManager.java +++ b/resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/CharsetManager.java @@ -440,45 +440,60 @@ public void projectPreferencesChanged(IProject project) { } public void setCharsetFor(IPath resourcePath, String newCharset) throws CoreException { - // for the workspace root we just set a preference in the instance scope - if (resourcePath.segmentCount() == 0) { - IEclipsePreferences resourcesPreferences = InstanceScope.INSTANCE.getNode(ResourcesPlugin.PI_RESOURCES); - if (newCharset != null) - resourcesPreferences.put(ResourcesPlugin.PREF_ENCODING, newCharset); - else - resourcesPreferences.remove(ResourcesPlugin.PREF_ENCODING); - try { - resourcesPreferences.flush(); - } catch (BackingStoreException e) { - IProject project = workspace.getRoot().getProject(resourcePath.segment(0)); - String message = Messages.resources_savingEncoding; - throw new ResourceException(IResourceStatus.FAILED_SETTING_CHARSET, project.getFullPath(), message, e); - } - return; + if (IPath.ROOT.equals(resourcePath)) { + setCharsetForRoot(newCharset); + } else if (workspace.getRoot().findMember(resourcePath) instanceof Resource resource) { + setCharsetForResource(resourcePath, newCharset, resource); } - // for all other cases, we set a property in the corresponding project - IResource resource = workspace.getRoot().findMember(resourcePath); - if (resource != null) { - try { - // disable the listener so we don't react to changes made by ourselves - Preferences encodingSettings = getPreferences(resource.getProject(), true, resource.isDerived(IResource.CHECK_ANCESTORS)); - if (newCharset == null || newCharset.trim().length() == 0) - encodingSettings.remove(getKeyFor(resourcePath)); - else - encodingSettings.put(getKeyFor(resourcePath), newCharset); - flushPreferences(encodingSettings, true); - if (resource instanceof IProject) { - IProject project = (IProject) resource; - ValidateProjectEncoding.scheduleProjectValidation(workspace, project); - } - } catch (BackingStoreException e) { - IProject project = workspace.getRoot().getProject(resourcePath.segment(0)); - String message = Messages.resources_savingEncoding; - throw new ResourceException(IResourceStatus.FAILED_SETTING_CHARSET, project.getFullPath(), message, e); + } + + private void setCharsetForRoot(String newCharset) throws ResourceException { + IEclipsePreferences resourcesPreferences = InstanceScope.INSTANCE.getNode(ResourcesPlugin.PI_RESOURCES); + if (newCharset != null) { + resourcesPreferences.put(ResourcesPlugin.PREF_ENCODING, newCharset); + } else { + resourcesPreferences.remove(ResourcesPlugin.PREF_ENCODING); + } + + try { + resourcesPreferences.flush(); + } catch (BackingStoreException e) { + setCharsetForHasFailed(IPath.ROOT, e); + } + } + + private void setCharsetForResource(IPath resourcePath, String newCharset, IResource resource) + throws ResourceException { + try { + setResourceEncodingSettings(resourcePath, newCharset, resource); + if (resource instanceof Project project) { + ValidateProjectEncoding.scheduleProjectValidation(workspace, project); } + } catch (BackingStoreException e) { + setCharsetForHasFailed(resourcePath, e); } } + private void setResourceEncodingSettings(IPath resourcePath, String newCharset, IResource resource) + throws BackingStoreException { + // disable the listener so we don't react to changes made by ourselves + Preferences encodingSettings = getPreferences(resource.getProject(), true, + resource.isDerived(IResource.CHECK_ANCESTORS)); + + if (newCharset == null || newCharset.isBlank()) { + encodingSettings.remove(getKeyFor(resourcePath)); + } else { + encodingSettings.put(getKeyFor(resourcePath), newCharset); + } + flushPreferences(encodingSettings, true); + } + + private void setCharsetForHasFailed(IPath resourcePath, BackingStoreException e) throws ResourceException { + IProject project = workspace.getRoot().getProject(resourcePath.segment(0)); + String message = Messages.resources_savingEncoding; + throw new ResourceException(IResourceStatus.FAILED_SETTING_CHARSET, project.getFullPath(), message, e); + } + @Override public void shutdown(IProgressMonitor monitor) { InstanceScope.INSTANCE.getNode(ResourcesPlugin.PI_RESOURCES)