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,9 +119,10 @@ 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
- 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 ();
123
126
content = new StringBuilder (primaryFile .asText (charset ));
124
127
} catch (IOException ex ) {
125
128
LOG .log (Level .WARNING , "Failed to get the text of the file" );
@@ -138,15 +141,13 @@ protected FileInfo excuteOperations(DataObject dataObject, MappedEditorConfig co
138
141
info .setCookie (cookie );
139
142
140
143
// 1. "charset"
141
- MappedCharset mappedCharset = config .getCharset ();
142
-
143
144
if (mappedCharset != null ) {
144
145
logOperation (EditorConfigConstant .CHARSET , mappedCharset .getName ());
145
146
boolean changedCharset = new CharsetOperation ().run (dataObject , mappedCharset );
146
147
fileChangeNeeded = fileChangeNeeded || changedCharset ;
147
148
info .setCharset (mappedCharset .getCharset ());
148
149
} else {
149
- info .setCharset (StandardCharsets . UTF_8 );
150
+ info .setCharset (defaultCharset );
150
151
}
151
152
152
153
// 2. "end_of_line"
0 commit comments