VSCode extension - how to alter file's text

后端 未结 4 1719
天涯浪人
天涯浪人 2021-02-20 12:56

I have an extension that grabs the open file\'s text and alters it. Once the text is altered, how do I put it back into the file that is displayed in VSCode?



        
4条回答
  •  名媛妹妹
    2021-02-20 13:23

    The API you can use here is TextEditor.edit, whose definition is

    edit(callback: (editBuilder: TextEditorEdit) => void, options?: {   undoStopBefore: boolean; undoStopAfter: boolean; }): Thenable;
    

    It asks for a callback as the first parameter and in the callback, you can make edits to the document by visiting editBuilder.

    I put a sample extension in https://github.com/Microsoft/vscode-extension-samples/tree/master/document-editing-sample which reverses the content in current selection, which is basically a simple use TextEditor.edit.

提交回复
热议问题