@@ -310,7 +310,7 @@ void flushPreferences(Preferences projectPrefs, boolean shouldDisableCharsetDelt
310
310
*
311
311
* @param resourcePath the path for the resource
312
312
* @param recurse whether the parent should be queried
313
- * @return the charset setting for the given resource
313
+ * @return the charset setting for the given resource or <code>null</code>.
314
314
*/
315
315
public String getCharsetFor (IPath resourcePath , boolean recurse ) {
316
316
Assert .isLegal (resourcePath .segmentCount () >= 1 );
@@ -319,10 +319,11 @@ public String getCharsetFor(IPath resourcePath, boolean recurse) {
319
319
Preferences prefs = getPreferences (project , false , false );
320
320
Preferences derivedPrefs = getPreferences (project , false , true );
321
321
322
- if (prefs == null && derivedPrefs == null )
322
+ if (prefs == null && derivedPrefs == null ) {
323
323
// no preferences found - for performance reasons, short-circuit
324
324
// lookup by falling back to workspace's default setting
325
325
return recurse ? ResourcesPlugin .getEncoding () : null ;
326
+ }
326
327
327
328
return internalGetCharsetFor (prefs , derivedPrefs , resourcePath , recurse );
328
329
}
@@ -366,32 +367,36 @@ Preferences getPreferences(IProject project, boolean create, boolean isDerived,
366
367
}
367
368
368
369
private String internalGetCharsetFor (Preferences prefs , Preferences derivedPrefs , IPath resourcePath , boolean recurse ) {
369
- String charset = null ;
370
-
371
- // try to find the encoding in regular and then derived preferences
372
- if (prefs != null )
373
- charset = prefs .get (getKeyFor (resourcePath ), null );
374
- // derivedPrefs may be not null, only if #isDerivedEncodingStoredSeparately returns true
375
- // so the explicit check against #isDerivedEncodingStoredSeparately is not required
376
- if (charset == null && derivedPrefs != null )
377
- charset = derivedPrefs .get (getKeyFor (resourcePath ), null );
378
-
379
- if (!recurse )
370
+ String charset = getCharsetFromPreferences (prefs , derivedPrefs , resourcePath );
371
+ if (!recurse ) {
380
372
return charset ;
373
+ }
381
374
382
375
while (charset == null && resourcePath .segmentCount () > 1 ) {
383
376
resourcePath = resourcePath .removeLastSegments (1 );
384
- // try to find the encoding in regular and then derived preferences
385
- if (prefs != null )
386
- charset = prefs .get (getKeyFor (resourcePath ), null );
387
- if (charset == null && derivedPrefs != null )
388
- charset = derivedPrefs .get (getKeyFor (resourcePath ), null );
377
+ charset = getCharsetFromPreferences (prefs , derivedPrefs , resourcePath );
389
378
}
390
379
391
380
// ensure we default to the workspace encoding if none is found
392
381
return charset == null ? ResourcesPlugin .getEncoding () : charset ;
393
382
}
394
383
384
+ private String getCharsetFromPreferences (Preferences prefs , Preferences derivedPrefs , IPath resourcePath ) {
385
+ String charset = null ;
386
+ String key = getKeyFor (resourcePath );
387
+
388
+ // try to find the encoding in regular and then derived preferences
389
+ if (prefs != null ) {
390
+ charset = prefs .get (key , null );
391
+ }
392
+ // derivedPrefs may be not null, only if #isDerivedEncodingStoredSeparately returns true
393
+ // so the explicit check against #isDerivedEncodingStoredSeparately is not required
394
+ if (charset == null && derivedPrefs != null ) {
395
+ charset = derivedPrefs .get (key , null );
396
+ }
397
+ return charset ;
398
+ }
399
+
395
400
private boolean isDerivedEncodingStoredSeparately (IProject project ) {
396
401
// be careful looking up for our node so not to create any nodes as side effect
397
402
Preferences node = Platform .getPreferencesService ().getRootNode ().node (ProjectScope .SCOPE );
0 commit comments