Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vscode API - provideTextDocument not executing more then once

I am writing a Vscode FTP extension following this example.

My problem is that it seems that provideTextDocumentContent only gets called once per file.. opening the file again does not recall the function. Instead it just shows you the content already loaded.

Refer to code below:

public provideTextDocumentContent(uri: vscode.Uri, token: vscode.CancellationToken): vscode.ProviderResult<string> {
    console.log('uri.path');
    return this.model.getContent(uri).then(content => content);
}

and these are my results when clicking items on my View

log=> '/index.cfml'   // Clicked on index.cfml
log=> '/login.cfml'   // Clicked on login.cfml
log=>                 // Clicked on index.cfml
log=>                 // Clicked on login.cfml
log=> '/signup.cfml'  // Clicked on signup.cfml

Expected results would be:

log=> '/index.cfml'   // Clicked on index.cfml
log=> '/login.cfml'   // Clicked on login.cfml
log=> '/index.cfml'   // Clicked on index.cfml
log=> '/login.cfml'   // Clicked on login.cfml
log=> '/signup.cfml'  // Clicked on signup.cfml

As you can see provideTextDocumentContent gets called the first time I click on an item but the second time I click on the same item it does not get called. Any idea why this is happening or how I could force vscode to call it for every click?

EDIT: I currently have an onDidChange event at the top of my data provider but the implementation may be wrong. Below is a rundown of what my data provider looks like.

export class FtpTreeDataProvider implements vscode.TreeDataProvider<FtpNode>, vscode.TextDocumentContentProvider {

private _onDidChangeTreeData: vscode.EventEmitter<any> = new vscode.EventEmitter<any>();
readonly onDidChangeTreeData: vscode.Event<any> = this._onDidChangeTreeData.event;

constructor(private readonly model: FtpModel){ }

public refresh(): any {
    this._onDidChangeTreeData.fire();
}

public getTreeItem(element) {/*some code*/}
public getChildren(element) {/*some code*/}
public getParent(element) {/*some code*/}

public provideTextDocumentContent(uri: vscode.Uri, token: vscode.CancellationToken): vscode.ProviderResult<string> {
    console.log(uri.path);
    return this.model.getContent(uri);
}
like image 422
KR34T1V Avatar asked May 14 '19 10:05

KR34T1V


2 Answers

VS Code will reuse the provided content as long as the document is open. Switching away from a document closes its editor but does close the document itself. If you fully close the document (in all tabs), you should see the provider invoked when you open a new document.

If you wish to update an existing document's content, implement the onDidChange event on TextDocumentContentProvider and use it to signal that VS Code should call provideTextDocumentContent

like image 54
Matt Bierner Avatar answered Oct 13 '22 13:10

Matt Bierner


As @Matt Bierner said:

"If you wish to update an existing document's content, implement the onDidChange event on TextDocumentContentProvider and use it to signal that VS Code should call provideTextDocumentContent"

My problem was that I had to fire() _onDidChange with an uri. I ended up adding this line to my getTreeItem method:

this._onDidChange.fire(element.resource);

now each item is freshly downloaded on opening.

like image 41
KR34T1V Avatar answered Oct 13 '22 15:10

KR34T1V