13
13
import com .welovecoding .nbeditorconfig .processor .operation .TrimTrailingWhiteSpaceOperation ;
14
14
import com .welovecoding .nbeditorconfig .processor .operation .tobedone .CharsetOperation ;
15
15
import java .io .IOException ;
16
+ import java .nio .charset .Charset ;
16
17
import java .nio .charset .StandardCharsets ;
17
18
import java .util .logging .Level ;
18
19
import java .util .logging .Logger ;
19
20
import java .util .prefs .BackingStoreException ;
20
21
import java .util .prefs .Preferences ;
21
22
import org .netbeans .api .project .Project ;
23
+ import org .netbeans .api .queries .FileEncodingQuery ;
22
24
import org .netbeans .modules .editor .indent .spi .CodeStylePreferences ;
23
25
import org .openide .cookies .EditorCookie ;
24
26
import org .openide .filesystems .FileObject ;
@@ -117,8 +119,11 @@ protected FileInfo excuteOperations(DataObject dataObject, MappedEditorConfig co
117
119
FileObject primaryFile = dataObject .getPrimaryFile ();
118
120
StringBuilder content ;
119
121
122
+ MappedCharset mappedCharset = config .getCharset ();
123
+ Charset defaultCharset = FileEncodingQuery .getEncoding (primaryFile );
120
124
try {
121
- content = new StringBuilder (primaryFile .asText ());
125
+ String charset = mappedCharset != null ? mappedCharset .getCharset ().name () : defaultCharset .name ();
126
+ content = new StringBuilder (primaryFile .asText (charset ));
122
127
} catch (IOException ex ) {
123
128
LOG .log (Level .WARNING , "Failed to get the text of the file" );
124
129
content = new StringBuilder ("" );
@@ -136,15 +141,13 @@ protected FileInfo excuteOperations(DataObject dataObject, MappedEditorConfig co
136
141
info .setCookie (cookie );
137
142
138
143
// 1. "charset"
139
- MappedCharset mappedCharset = config .getCharset ();
140
-
141
144
if (mappedCharset != null ) {
142
145
logOperation (EditorConfigConstant .CHARSET , mappedCharset .getName ());
143
146
boolean changedCharset = new CharsetOperation ().run (dataObject , mappedCharset );
144
147
fileChangeNeeded = fileChangeNeeded || changedCharset ;
145
148
info .setCharset (mappedCharset .getCharset ());
146
149
} else {
147
- info .setCharset (StandardCharsets . UTF_8 );
150
+ info .setCharset (defaultCharset );
148
151
}
149
152
150
153
// 2. "end_of_line"
@@ -191,11 +194,6 @@ protected FileInfo excuteOperations(DataObject dataObject, MappedEditorConfig co
191
194
fileChangeNeeded = fileChangeNeeded || trimmedWhiteSpaces ;
192
195
}
193
196
194
- if (mappedCharset != null ) {
195
- } else {
196
- info .setCharset (StandardCharsets .UTF_8 );
197
- }
198
-
199
197
info .setFileChangeNeeded (fileChangeNeeded );
200
198
info .setStyleFlushNeeded (styleFlushNeeded );
201
199
0 commit comments