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

Provide encoding-related APIs for editor extensions #824

Open
sodatea opened this issue Nov 30, 2015 · 48 comments
Open

Provide encoding-related APIs for editor extensions #824

sodatea opened this issue Nov 30, 2015 · 48 comments
Labels
api feature-request Request for new features or functionality file-encoding File encoding type issues
Milestone

Comments

@sodatea
Copy link

sodatea commented Nov 30, 2015

Currently there are only two fields in the TextEditorOptions API, a few other TextEditor-related APIs, and none of them is able to deal with the text buffer's encoding.
Comparing to Atom's TextEdit API, that is far from enough.

Most importantly, the API limitation makes it (seems) impossible for vscode-editorconfig to implement features like charset support, which is a crucial need for many people.

@alexdima
Copy link
Member

👍 Also trimTrailingWhitespaces is missing

@alexdima alexdima added api feature-request Request for new features or functionality labels Nov 30, 2015
@kmpm
Copy link

kmpm commented Dec 1, 2015

#751 and #844 would probably be helped by this as well.
At least #751 would be solvable if working with BOM were better and my issues would completely go away if there was a way to, per editor extension, set the encoding and other stuff.
So 👍

@egamma egamma modified the milestone: Backlog Dec 10, 2015
@jednano

This comment was marked as resolved.

@jrieken
Copy link
Member

jrieken commented Apr 29, 2016

  • set/get encoding
  • set/get charset

@iHuahua

This comment was marked as spam.

@buzzzzer

This comment was marked as spam.

@vysker
Copy link

vysker commented Sep 8, 2016

I also need this for my extension, as I explain here. Did not realize this issue already existed.

Definitely want this feature!

@tomasiser
Copy link

I believe that full .editorconfig support is blocked (editorconfig/editorconfig-vscode#35) until the API for buffer encoding is added. Is there any chance this fact could speed this up?

@jednano

This comment was marked as spam.

@afucher

This comment was marked as spam.

3 similar comments
@irudoy

This comment was marked as spam.

@takekazuomi

This comment was marked as spam.

@pixelhuh

This comment was marked as spam.

@bizoo

This comment was marked as spam.

@chongchai

This comment was marked as spam.

@gingerbeardman

This comment was marked as spam.

@irudoy

This comment was marked as spam.

@lygstate

This comment was marked as spam.

@mozhuanzuojing

This comment was marked as spam.

@gingerbeardman

This comment was marked as spam.

@lygstate
Copy link

lygstate commented Jan 7, 2022

maybe we can create a pr for this?

@sophodoros
Copy link

sophodoros commented May 7, 2022

This feature was first requested back in 2015 when VSCode was just emerging from the primordial ooze. VSCode has come a long way since then, but still no encoding support in the extension API?

Perhaps a full-blown feature that allows the encoding to be modified is too far down the priority list. However, it seems like it shouldn't be too difficult to add a simple read-only encoding property to TextDocument. After all, the encoding is already displayed in VSCode's status bar alongside the language (TypeScript, etc), which IS supplied via the TextDocument.languageId property.

Even if the value shown in the status bar is a guess as seems to be suggested in earlier comments, it would be better than nothing. As an extension writer, I'd rather use VSCode's best guess at the encoding than try to figure it out on my own.

@gingerbeardman
Copy link

The most sane way to get features like this, or column select, is to use a different editor. IMHO

@lygstate
Copy link

ping for it

@Tarrowren
Copy link

I looked at the code and I thought the coding was tied to the document, but it's actually tied to the editor.

I spent some time exposing it to the TextEditor API

/**
 * Represents an editor that is attached to a {@link TextDocument document}.
 */
export interface TextEditor {
  ...

  getEncoding(): Promise<string | undefined>;

  setEncoding(encoding: string, mode: "encode" | "decode"): Promise<void>;
}

and then in the json extension using

export async function activate(context: ExtensionContext) {
  ...

  const editer = window.activeTextEditor;
  if (editer) {
    outputChannel.appendLine("ok");
    outputChannel.appendLine((await editer.getEncoding()) ?? "null");
    await editer.setEncoding("utf16be", "encode");
    outputChannel.appendLine((await editer.getEncoding()) ?? "null");
  }
}

It worked! When opening the file he changed the document from utf8 to utf16be.

1

@Tarrowren
Copy link

I have moved it to TextEdit and it now works like this

toDispose.push(
  workspace.onWillSaveTextDocument((e) => {
    e.waitUntil(Promise.resolve([TextEdit.setEncoding("utf8")]));
  })
);

// or

window.activeTextEditor.edit((textEdit) => {
  textEdit.setEncoding("utf8");
});

But encoding needs to be added to the UndoRedoSnapshot, and I haven't implemented it yet.

And I didn't follow the rules of eslint(local/code-import-patterns) and couldn't submit the code, so I had to return to the previous method.

If I want to provide encoding in the TextDocument, I tried it and it requires a lot of code changes, there may be other simpler ways to do it.

I've spent a lot of time on this and now I'm going back to work😥.

https://github.com/Tarrowren/vscode/tree/textedit-encoding

@lygstate
Copy link

@takekazuomi do you create a MR for this?

@helmut-hackl
Copy link

Add editor encoding api

@gingerbeardman
Copy link

image

🙄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api feature-request Request for new features or functionality file-encoding File encoding type issues
Projects
None yet
Development

No branches or pull requests