From 3a7673612eff483c48c4559dcc9027f6057a176a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vetle=20Andersen=20Kopperg=C3=A5rd?= <89847536+vetlek@users.noreply.github.com> Date: Wed, 30 Aug 2023 10:48:24 +0200 Subject: [PATCH] Use correct encoding reading from fs --- package-lock.json | 2 +- server/src/documentProvider.ts | 25 +++++++++++++++++++------ server/src/server.ts | 6 +++++- server/src/settings.ts | 12 ++++++++---- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5a2ed1a1..8b7c17ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,7 +27,7 @@ "typescript": "^5.1.6" }, "engines": { - "vscode": "^1.75.0" + "vscode": "^1.81.0" } }, "node_modules/@aashutoshrathi/word-wrap": { diff --git a/server/src/documentProvider.ts b/server/src/documentProvider.ts index 2dd0afa8..3780d868 100644 --- a/server/src/documentProvider.ts +++ b/server/src/documentProvider.ts @@ -21,6 +21,7 @@ import { } from "vscode-languageserver-textdocument"; import * as path from "path"; import { TextDecoder } from "util"; +import { SettingsManager } from "./settings"; class Document implements ITextDocument { private inMemoryDoc?: ITextDocument; @@ -101,7 +102,8 @@ export class DocumentProvider { private readonly cache = new ResourceMap(); private readonly documents: TextDocuments; private readonly connection: Connection; - private decoder = new TextDecoder("utf-8"); + private decoderUTF8 = new TextDecoder("utf-8"); + private decoderWindows1252 = new TextDecoder("windows-1252"); readonly _onDidChangeDoc = new Emitter(); @@ -119,12 +121,16 @@ export class DocumentProvider { readonly onDidLoadDoc = this._onDidLoadDoc.event; + readonly settingsManager: SettingsManager; + constructor( connection: Connection, - documents: TextDocuments + documents: TextDocuments, + settingsManager: SettingsManager ) { this.connection = connection; this.documents = documents; + this.settingsManager = settingsManager; this.documents.onDidChangeContent((e) => { if (!this.isRelevantFile(e.document.uri)) { @@ -235,16 +241,23 @@ export class DocumentProvider { uri: string ): Promise { try { - const content = await this.connection.sendRequest( + const encodedContent = await this.connection.sendRequest( protocol.fsReadFile, { uri: uri, } ); - const utf8bytes = new Uint8Array(content); - const contentString = this.decoder.decode(utf8bytes); + const bytes = new Uint8Array(encodedContent); + const settings = await this.settingsManager.getSettings(); + const encoding = settings?.encoding ?? "windows1252"; + let content: string; + if (encoding === "utf8") { + content = this.decoderUTF8.decode(bytes); + } else { + content = this.decoderWindows1252.decode(bytes); + } const doc = new Document(uri, { - onDiskDoc: TextDocument.create(uri, "septic", 0, contentString), + onDiskDoc: TextDocument.create(uri, "septic", 0, content), }); this.cache.set(uri, doc); return doc; diff --git a/server/src/server.ts b/server/src/server.ts index 154fca43..a4a5b8d7 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -37,7 +37,11 @@ const documents: TextDocuments = new TextDocuments(TextDocument); const settingsManager = new SettingsManager(connection); -const documentProvider = new DocumentProvider(connection, documents); +const documentProvider = new DocumentProvider( + connection, + documents, + settingsManager +); const langService: ILanguageService = createLanguageService( settingsManager, diff --git a/server/src/settings.ts b/server/src/settings.ts index d7e5d790..ab010fe5 100644 --- a/server/src/settings.ts +++ b/server/src/settings.ts @@ -21,6 +21,7 @@ export interface Settings { readonly diagnostics: DiagnosticsSettings; readonly folding: FoldingSettings; readonly formatting: FormattingSettings; + readonly encoding: "utf8" | "windows1252"; } export class SettingsManager { @@ -46,10 +47,13 @@ export class SettingsManager { } private async updateSettings(): Promise { - const settings = await this.connection.workspace.getConfiguration( - "septic" - ); - this.settings = settings; + const settings = await this.connection.workspace.getConfiguration(); + this.settings = { + diagnostics: settings["septic"].diagnostics, + folding: settings["septic"].folding, + formatting: settings["septic"].formatting, + encoding: settings["[septic]"]["files.encoding"], + }; this.updateMetaInfo(); }