Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vscode API: read clipboard text content

I'm currently trying to write an extension for Visual Studio Code, and I can't manage to understand how to read the clipboard content.

The VSCode API specifies this method:

readText ():Thenable<String>

Following what I read about Thenable, I should be able to get the clipboard's text like that:

var clipboard_content = vscode.env.clipboard.readText().then((text)=>text);

But all I manage to get is a Promise { pending } object.

What I would like to get is the clipboard content as a string

like image 602
olinox14 Avatar asked Feb 11 '19 14:02

olinox14


People also ask

How do I allow VS Code to access my clipboard for reading?

Click on Site Settings. Click View permissions and data stored across sites. Select the entry for the site that is associated with your VS Code server. Scroll down until you find a Clipboard permission, and select Allow from the provided dropdown menu to the right of the permission.

What is CodeLens VS Code?

CodeLens lets you stay focused on your work while you find out what happened to your code–without leaving the editor. You can find references to a piece of code, changes to your code, linked bugs, work items, code reviews, and unit tests.

What is Uri VS Code?

@:jsRequire("vscode","Uri") A universal resource identifier representing either a file on disk or another resource, like untitled resources.


1 Answers

Basics mistake.

Because you use promises (async) and want async/await (linear) functionality.

It should be (promises, async code):

vscode.env.clipboard.readText().then((text)=>{
    clipboard_content = text; 
    /* code */
});

or (sequential code)

let clipboard_content = await vscode.env.clipboard.readText(); 
/* code */

PS.: In JS, you should use camelCase instead of snake_case when naming variables and functions. This is one of the recommendations of JavaScript Standard Style

like image 72
bato3 Avatar answered Sep 22 '22 21:09

bato3