Skip to content

Commit

Permalink
fix: use correct encoding reading from fs (#289)
Browse files Browse the repository at this point in the history
Use correct encoding reading from fs
  • Loading branch information
vetlek authored Sep 1, 2023
1 parent 5162405 commit 9edabb2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
25 changes: 19 additions & 6 deletions server/src/documentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,7 +102,8 @@ export class DocumentProvider {
private readonly cache = new ResourceMap<Document>();
private readonly documents: TextDocuments<TextDocument>;
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<URI>();

Expand All @@ -119,12 +121,16 @@ export class DocumentProvider {

readonly onDidLoadDoc = this._onDidLoadDoc.event;

readonly settingsManager: SettingsManager;

constructor(
connection: Connection,
documents: TextDocuments<TextDocument>
documents: TextDocuments<TextDocument>,
settingsManager: SettingsManager
) {
this.connection = connection;
this.documents = documents;
this.settingsManager = settingsManager;

this.documents.onDidChangeContent((e) => {
if (!this.isRelevantFile(e.document.uri)) {
Expand Down Expand Up @@ -235,16 +241,23 @@ export class DocumentProvider {
uri: string
): Promise<Document | undefined> {
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;
Expand Down
6 changes: 5 additions & 1 deletion server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ const documents: TextDocuments<TextDocument> = 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,
Expand Down
12 changes: 8 additions & 4 deletions server/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Settings {
readonly diagnostics: DiagnosticsSettings;
readonly folding: FoldingSettings;
readonly formatting: FormattingSettings;
readonly encoding: "utf8" | "windows1252";
}

export class SettingsManager {
Expand All @@ -46,10 +47,13 @@ export class SettingsManager {
}

private async updateSettings(): Promise<void> {
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();
}

Expand Down

0 comments on commit 9edabb2

Please sign in to comment.