I am writing a VSCode extension and want to use the executeDocumentSymbolProvider API to get the list of functions of a file.
I mainly have 2 problems with how to use executeDocumentSymbolProvider:
How to correctly use executeDocumentSymbolProvider
How to resolve the returned Promise
How to correctly use executeDocumentSymbolProvider
Below are the links I referenced to call executeDocumentSymbolProvider:
https://code.visualstudio.com/api/references/commands
https://code.visualstudio.com/api/extension-guides/command
https://www.reddit.com/r/vscode/comments/oxnhna/get_all_symbols_of_the_current_document_in_vscode/
How can I implement my own code outline layout in vscode?
And this is how I call the API:
var active = vscode.window.activeTextEditor;
let symbols = await vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider',active.document.uri);
Although this does return a Promise, I can't help but wonder if this is the correct way to invoke it because from the VSCode API doc it looks like I should invoke it like let symbols = await vscode.commands.executeCommand<vscode.DocumentSymbol[]>('vscode.executeDocumentSymbolProvider',active.document.uri). However I keep getting an error that says there should be an argument in the [].
How to resolve the returned Promise
The following picture is the returned Promise.
I have tried the following methods to resolve it but I still get Promise instead of symbols array as the return value.
async function(){
var active = vscode.window.activeTextEditor;
let symbols = await vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider',active.document.uri);
}
var active = vscode.window.activeTextEditor;
vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider',active.document.uri).then((symbols) => {
if(symbols !== undefined){
console.log(symbols);
}
});
async function getFunctionList(textEditor){
var active = vscode.window.activeTextEditor;
return await new Promise((resolve, reject) => {
setTimeout(function(){
resolve(vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider',active.document.uri));
}, 1000);
});
}
Please let me know if you have used executeDocumentSymbolProvider before. Thank you!
Be sure to install the C/C++ Extension Pack by Microsoft on VSCode for getting symbols from a c file. Otherwise, VSCode won't be able to find the symbols.
Related
I've noticed that, when a Promise is used inside an async method, a lot of properties which should be in the IntelliSense are actually there.
Take this simple code for example (i'm using REACT and I'm inside a Hook component, just for give you a context).
const notAsyncMethod = () => {
let a : Promise<string> = new Promise(null);
}
const asyncMethod = async () => {
let a : Promise<string> = new Promise(null);
}
Now, in the first case, this is the IntelliSense suggestions:
Which is exactly what I expect.
Then, in the second case, this is what IntelliSense suggests me:
Why is this happening? Is it normal, or there might be some problem with my code? And, if this is how it should work.. Why is this happening, and from what those properties are coming?
I'm trying to have a string be used inside a puppeteer string, it won't work for some reason.
Specifically with this code
await page.waitForSelector('div[class = "sh-dlr__list-result"')
When i try to parse in a variable
let identified1 = 'div[class = "sh-dlr__list-result"'
so making
await page.waitForSelector(identified1)
It won't work. This is really limiting, is there a way around this issue?
This is the expanded code
https://jsfiddle.net/hewlbern/6p7kdozt/10/
Run it in your computer, jsfiddle unsure if I can run it from there.
I believe it is creating a cors error now - very weird! Why would using a variable create a cors error : /
Thanks!
The reason is because you're declaring identified inside the page.evaluate(). So, when you do the following it's already out of scope.
if (currentPage < pagesToScrape) {
console.log(identified1);
await Promise.all([
await page.click(buttonSelector),
await page.waitForSelector(identified),
]);
}
You did log the identified1 but you're using identified for the selector.
You'll have to pass the identifier2 to the pageFunction like so:
let newProducts = await page.evaluate(({ identifier2 }) => {
// ...
},{ identifier2 });
See here some examples:
I have a trouble. I receive many objects where I mapping them and I make a external consult using axios and save the return, let's the code:
let savedClients = Object.entries(documents).map(personDocument => {
let [person, document] = personDocument
documentFormated = document
documentNumbers = document.replace(/\D/g, '')
return ConsultDocuments.getResponse(documentNumbers).then(resultScore => { // Calling the axios
const info = { ...resultScore }
return Save.saveClient(info)
})
})
Promise.all(savedClients).then(results => {
console.log(results) // Come only one document, repeted with the total documents passed in map
})
The problem is when it realized the all map first and then make the consults with only the last result many time (the total of documents passed)
This code is legacy and use async/await don't work (serious, if i don't stay here)
I'am tried N ways to make this, and with the libary Q(), it's make the map in correcty order but it's doesn't wait the axios, and all results come with "pending"
Thanks!
I'm trying to use redux saga to query a number of rest endpoints to get a human readable name associated with each discovered network, to fill a dropdown list with selectable networks.
I'm having trouble doing this, my forking is not working. I keep getting the error message:
TypeError: __webpack_require__.i(..) is not a function(...)
Every example using all() I've found online use call and know ahead of time every request being made. However, judging from the API I tried something like this:
const pendingQueries = [];
for(networkId in discoveredNetworks) {
pendingQueries.push(fork(getApplicationName, networkId);
}
const queryResults = yield all(pendingQueries);
This failed. I've tried a number of other permutations since. From testing I'm able to verify that I can do this:
const results = [];
for(networkId in discoveredNetworks) {
results.push(yield fork(getApplicationName, networkId));
}
and if There is a long enough delay the method will run and complete, though this approach obviously doesn't gaurentee that the forked methods will complete before I use result as I want. Still it seems to confirm the problem is in my use of all.
What is wrong with my all command?
Why don’t you wrap each request in a promise and call them like so:
var promises = []
for(networkId in discoveredNetworks) {
promises.push(new Promise((res, rej) => {
// code from getApplicationName goes here
// call res(result) on success and rej(error) on failure
}));
}
const results = yield call(Promise.all(promises))
I got this to work by giving up on the all() method, which I never got to work as advertised but wasn't really the right method for the job.
For forks I should have been using join() instead. so something along the lines of this:
const pendingQueries = [];
for(networkId in discoveredNetworks) {
pendingQueries.push(yield fork(getApplicationName, networkId);
}
const results = yield join(...pendingQueries);
results.forEach((result) => {
// my logic for handling the response and generating action
}
I'm working with a Lightswitch 2015 application for tracking customer satisfaction interviews. I'm trying to create a function that will create a new interview, populate a few items, then save it to the database, and open it for editing in a new window. I'm struggling with understanding the behavior of promises in this context.
I have these three blocs of code:
1)
myapp.Interviews_Management.AddInterview_Tap_execute = function (screen)
{
var NewInterview = screen.Interviews.addNew();
NewInterview.c_Date = screen.InterviewDate;
NewInterview.Participant = screen.Interviewee;
NewInterview.Location = screen.InterviewFocusLocation;
screen.closePopup()
.then(() =>
{
myapp.applyChanges()
.then(() =>{ myapp.showInterview_AddEdit(NewInterview); });
}, (error) =>{ msls.showMessageBox(error.toString()); });
};
2)
myapp.Interviews_Management.AddInterview_Tap_execute = function (screen)
{
var NewInterview = screen.Interviews.addNew();
NewInterview.c_Date = screen.InterviewDate;
NewInterview.Participant = screen.Interviewee;
NewInterview.Location = screen.InterviewFocusLocation;
screen.closePopup()
.then(myapp.applyChanges()
.then(myapp.showInterview_AddEdit(NewInterview),
(error) =>{ msls.showMessageBox(error.toString()); })
);
};
3)
myapp.Interviews_Management.AddInterview_Tap_execute = function (screen)
{
var NewInterview = screen.Interviews.addNew();
NewInterview.c_Date = screen.InterviewDate;
NewInterview.Participant = screen.Interviewee;
NewInterview.Location = screen.InterviewFocusLocation;
screen.closePopup()
.then(myapp.applyChanges())
.then(myapp.showInterview_AddEdit(NewInterview),
(error) =>{ msls.showMessageBox(error.toString()); });
};
1 works as expected; that is, creates a new interview, populates the fields, and opens an edit window. However, I'm concerned that errors thrown from inside the lambda function(s) won't be properly passed to the external context.
2 and 3 both create a new interview and populate the fields, but then throws an error saying, "This action cannot be taken while screen is navigating.", which seems to suggest it is not waiting for each promise to fulfill before attempting to execute the next one. 3 also seems like perhaps it is wrapping promises in other promises, which may again lead to not properly passing any thrown errors outward (and, I hear, is a pretty common anti-pattern for people struggling with understanding promises?).
Can someone help me understand what exactly is going on here? I've been struggling generally with proper best practice in nesting promises or using a series of promises; any help with proper way of handling this would be much appreciated!
You always need to pass a callback function to then, not the result of calling it immediately.
You want
screen.closePopup()
.then(myapp.applyChanges) // or () => myapp.applyChanges()
.then(() => myapp.showInterview_AddEdit(NewInterview)),
.catch(error => { msls.showMessageBox(error.toString()); });
For why that catch is necessary (better than using the second then parameter), see When is .then(success, fail) considered an antipattern for promises?.