Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use correct encoding reading from fs #289

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
Loading