Skip to content

Commit 3d71811

Browse files
Merge branch 'main' into dependabot/npm_and_yarn/npm_and_yarn-e04d5d616f
2 parents 871be76 + 164e532 commit 3d71811

12 files changed

Lines changed: 149 additions & 27 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.18.0
2+
3+
- **Feat:** support `charset`
4+
15
## 0.17.4
26

37
- **Fix:** indentation shifting on focus

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ for more information on the multiple ways of installing VSCode extensions.
4343
- `end_of_line` (on save)
4444
- `insert_final_newline` (on save)
4545
- `trim_trailing_whitespace` (on save)
46-
47-
## On the backlog
48-
49-
- `charset`
46+
- `charset` (on open/after save)
5047

5148
## How it works
5249

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"displayName": "EditorConfig",
44
"description": "EditorConfig Support for Visual Studio Code",
55
"publisher": "EditorConfig",
6-
"version": "0.17.4",
6+
"version": "0.18.0",
77
"icon": "EditorConfig_icon.png",
88
"engines": {
9-
"vscode": "^1.98.0"
9+
"vscode": "^1.100.0"
1010
},
1111
"author": "EditorConfig Team",
1212
"license": "MIT",
@@ -114,7 +114,7 @@
114114
"devDependencies": {
115115
"@types/mocha": "^10.0.10",
116116
"@types/node": "^20.19.0",
117-
"@types/vscode": "~1.98.0",
117+
"@types/vscode": "~1.100.0",
118118
"@typescript-eslint/eslint-plugin": "^8.34.0",
119119
"@typescript-eslint/parser": "^8.34.0",
120120
"@vscode/test-electron": "^2.5.2",

src/DocumentWatcher.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,31 @@ import {
88
window,
99
workspace,
1010
} from 'vscode'
11+
import { KnownProps } from 'editorconfig'
12+
1113
import {
1214
InsertFinalNewline,
1315
PreSaveTransformation,
1416
SetEndOfLine,
1517
TrimTrailingWhitespace,
1618
} from './transformations'
17-
1819
import {
1920
applyTextEditorOptions,
2021
resolveCoreConfig,
2122
resolveFile,
2223
resolveTextEditorOptions,
2324
} from './api'
2425

26+
type Charset = Exclude<KnownProps['charset'], undefined | 'unset'>
27+
type EncodingMap = Record<Charset, TextDocument['encoding']>
28+
const encodingMap = {
29+
'utf-8': 'utf8',
30+
'utf-8-bom': 'utf8bom',
31+
'utf-16le': 'utf16le',
32+
'utf-16be': 'utf16be',
33+
latin1: 'iso88591',
34+
} as const satisfies EncodingMap
35+
2536
export default class DocumentWatcher {
2637
private disposable: Disposable
2738
private preSaveTransformations: PreSaveTransformation[] = [
@@ -65,6 +76,8 @@ export default class DocumentWatcher {
6576
if (path.basename(doc.fileName) === '.editorconfig') {
6677
this.log('.editorconfig file saved.')
6778
}
79+
// in case document was dirty on open/text editor change
80+
this.handleDocumentEncoding(doc)
6881
}),
6982
)
7083

@@ -78,6 +91,10 @@ export default class DocumentWatcher {
7891
}),
7992
)
8093

94+
subscriptions.push(
95+
workspace.onDidOpenTextDocument(this.handleDocumentEncoding),
96+
)
97+
8198
this.disposable = Disposable.from.apply(this, subscriptions)
8299
this.log('Document watcher initialized')
83100
}
@@ -156,6 +173,39 @@ export default class DocumentWatcher {
156173
onNoActiveTextEditor: this.onNoActiveTextEditor,
157174
onSuccess: this.onSuccess,
158175
})
176+
this.handleDocumentEncoding(editor.document)
159177
}
160178
}
179+
180+
private async handleDocumentEncoding(document: TextDocument) {
181+
const relativePath = workspace.asRelativePath(document.fileName)
182+
const editorconfigSettings = await resolveCoreConfig(document, {
183+
onBeforeResolve: this.onBeforeResolve,
184+
})
185+
186+
const { charset } = editorconfigSettings
187+
this.log(`${relativePath}: Target charset is`, charset ?? 'not set')
188+
if (!charset) {
189+
return
190+
}
191+
if (!(charset in encodingMap)) {
192+
this.log(`${relativePath}: Unsupported charset`)
193+
return
194+
}
195+
196+
const targetEncoding = encodingMap[charset as keyof typeof encodingMap]
197+
if (targetEncoding === document.encoding) {
198+
return
199+
}
200+
201+
if (document.isDirty) {
202+
this.log(`${relativePath}: Cannot change encoding, document is dirty`)
203+
return
204+
}
205+
206+
this.log(`${relativePath}: Re-opening document with ${targetEncoding} encoding...`)
207+
await workspace.openTextDocument(document.uri, {
208+
encoding: targetEncoding,
209+
})
210+
}
161211
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
root = true
2+
3+
[*]
4+
charset = latin1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
root = true
2+
3+
[*]
4+
charset = unset
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-16be
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-16le
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8-bom

0 commit comments

Comments
 (0)