admin管理员组

文章数量:1355542

In VS Code, I can use the editor.action.toggleWordWrap command (View: Toggle Word Wrap, bound to alt+z by default) to toggle word wrap for a document. Can I detect this state and/or toggling programmatically in an extension? That is, given a vscode.TextDocument, can I either get a boolean saying whether word wrap is enabled, or register a listener to notify me when that state changes?

In VS Code, I can use the editor.action.toggleWordWrap command (View: Toggle Word Wrap, bound to alt+z by default) to toggle word wrap for a document. Can I detect this state and/or toggling programmatically in an extension? That is, given a vscode.TextDocument, can I either get a boolean saying whether word wrap is enabled, or register a listener to notify me when that state changes?

Share Improve this question asked Mar 30 at 21:14 Sam EstepSam Estep 13.4k3 gold badges39 silver badges81 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

So it turns out that the approach below will not work.

Most of the toggle menu items like View > Show Minimap or others affect all editors and all files. They are implemented by writing to user settings and that is why they become "persistent". Basically, those menu items open settings.json, toggle the global e.g. editor.minimap.enabled and then the UI reacts to the settings change and show/hide the minimap based on the settings value.

But for View > Word Wrap, we have found that many people don't want that they word wrap all files, but only the current focused file. This has to do with how you sometimes open a file that has really long lines, or maybe view a diff and you want to "quickly toggle word wrap for this file to see something". So View > Word Wrap is somewhat different than the other menu items, because it affects only the current file.

This is not persistent because our settings.json cannot yet express resource based settings (it supports either global or per language settings, but not per individual file).

from GitHub: Remember the setting for wordwrap

Thank you for clarifying. Currently Alt+Z is only toggling viewport wrapping on or off for the current file and does this without writing to user settings. We added this command to the core of VS Code in order to cover some use-cases where people want to turn on viewport wrapping "on-the-fly", and only for a single file.

from GitHub: Allow Alt-Z to toggle to wordWrapColumn when word wrap is defaulted off.

Most toggle- commands do change the User Settings but Toggle Word Wrap does not. Instead that new view mode is stored somewhere on a file-by-file basis.

It may be stored in the C:\Users\xxxxx\AppData\Roaming\Code\User directory (possibly globalStorage\state.vscdb) although that isn't obvious from my research. And the location of that file would be different on other operating systems and for vscode Insiders.

Depending on your use case, you may be able to take advantage of the context key editorWordWrap which does "appear" to track whether word wrap is on for a particular file. But you can only use that context key in a keybinding, you cannot get it from within an extension - getContext is a much-requested feature that seems unlikely to ever be implemented (see https://github/microsoft/vscode/issues/10471).


Try

const document = vscode.window.activeTextEditor.document;

const wordWrap = vscode.workspace.getConfiguration("editor", document).get("wordWrap");

which should give you the current state of the editor.wordWrap setting for that TextDocument. You can listen to vscode.onDidChangeConfiguration() to see if that changes.

vscode.workspace.onDidChangeConfiguration((event) => {
  if (event.affectsConfiguration("editor.wordWrap", document)) {
     //  it changed
  }
});

Look at the ConfigurationScope parameters of those two functions I mentioned above. The different options can be used to catch different settings scopes (like language-specific or workspaceFolder).

  • a Uri representing a resource
  • a TextDocument representing an open text document
  • a WorkspaceFolder representing a workspace folder
  • an object containing:
    uri: an optional Uri of a text document
    languageId: the language identifier of a text document

本文标签: Can I detect word wrap programmatically in a VS Code extensionStack Overflow