Skip to content
This repository was archived by the owner on Apr 10, 2021. It is now read-only.

Commit abf409c

Browse files
committed
Use FileEncodingQuery.getEncoding() instead of StandardCharsets.UTF_8
1 parent 0025db5 commit abf409c

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/main/java/com/welovecoding/nbeditorconfig/processor/EditorConfigProcessor.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
import com.welovecoding.nbeditorconfig.processor.operation.TrimTrailingWhiteSpaceOperation;
1414
import com.welovecoding.nbeditorconfig.processor.operation.tobedone.CharsetOperation;
1515
import java.io.IOException;
16+
import java.nio.charset.Charset;
1617
import java.nio.charset.StandardCharsets;
1718
import java.util.logging.Level;
1819
import java.util.logging.Logger;
1920
import java.util.prefs.BackingStoreException;
2021
import java.util.prefs.Preferences;
2122
import org.netbeans.api.project.Project;
23+
import org.netbeans.api.queries.FileEncodingQuery;
2224
import org.netbeans.modules.editor.indent.spi.CodeStylePreferences;
2325
import org.openide.cookies.EditorCookie;
2426
import org.openide.filesystems.FileObject;
@@ -117,9 +119,10 @@ protected FileInfo excuteOperations(DataObject dataObject, MappedEditorConfig co
117119
FileObject primaryFile = dataObject.getPrimaryFile();
118120
StringBuilder content;
119121

122+
MappedCharset mappedCharset = config.getCharset();
123+
Charset defaultCharset = FileEncodingQuery.getEncoding(primaryFile);
120124
try {
121-
MappedCharset mc = config.getCharset();
122-
String charset = mc != null ? mc.getCharset().name() : StandardCharsets.UTF_8.name();
125+
String charset = mappedCharset != null ? mappedCharset.getCharset().name() : defaultCharset.name();
123126
content = new StringBuilder(primaryFile.asText(charset));
124127
} catch (IOException ex) {
125128
LOG.log(Level.WARNING, "Failed to get the text of the file");
@@ -138,15 +141,13 @@ protected FileInfo excuteOperations(DataObject dataObject, MappedEditorConfig co
138141
info.setCookie(cookie);
139142

140143
// 1. "charset"
141-
MappedCharset mappedCharset = config.getCharset();
142-
143144
if (mappedCharset != null) {
144145
logOperation(EditorConfigConstant.CHARSET, mappedCharset.getName());
145146
boolean changedCharset = new CharsetOperation().run(dataObject, mappedCharset);
146147
fileChangeNeeded = fileChangeNeeded || changedCharset;
147148
info.setCharset(mappedCharset.getCharset());
148149
} else {
149-
info.setCharset(StandardCharsets.UTF_8);
150+
info.setCharset(defaultCharset);
150151
}
151152

152153
// 2. "end_of_line"

0 commit comments

Comments
 (0)