microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
163.72k stars 29.09k forks source link

Error creating tree view `Cannot set property 'dataProvider' of undefined` #46526

Closed DanTup closed 6 years ago

DanTup commented 6 years ago

@sandy081 Not sure if I'm using this correctly as the FTP example seems slightly different (it's creating a FileSystemProvider?).

I tried calling createTreeView around my tree provider (since this seemed to be the only way to get a TreeView which is where the reveal method is?):

if (config.previewFlutterOutline && analyzer.capabilities.supportsFlutterOutline) {
    const flutterOutlineProvider = new FlutterOutlineProvider(analyzer);
    const tree = vs.window.createTreeView("flutter_outline_tree", { treeDataProvider: flutterOutlineProvider });

    context.subscriptions.push(vs.window.registerTreeDataProvider("dartFlutterOutline", flutterOutlineProvider));
    context.subscriptions.push(tree);
    context.subscriptions.push(flutterOutlineProvider);
}

However I get this error (using latest Insiders):

  ERR Cannot set property 'dataProvider' of undefined: TypeError: Cannot set property 'dataProvider' of undefined
    at t.$registerTreeViewDataProvider (file:///Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:2863:886)
    at e._doInvokeHandler (file:///Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:2869:87)
    at e._invokeHandler (file:///Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:2868:808)
    at e._receiveRequest (file:///Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:2867:931)
    at e._receiveOneMessage (file:///Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:2867:700)
    at file:///Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:2866:754
    at file:///Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:2869:621
    at e.fire (file:///Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:166:729)
    at Socket.<anonymous> (file:///Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:291:388)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:191:7)
    at readableAddChunk (_stream_readable.js:178:18)
    at Socket.Readable.push (_stream_readable.js:136:10)
    at Pipe.onread (net.js:560:20)

Let me know if I'm doing something wrong or if there's another sample I can use as a reference.

vscodebot[bot] commented 6 years ago

(Experimental duplicate detection) Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:

DanTup commented 6 years ago

Can I tell @vscodebot when it's response is bad so it can learn? :-)

sandy081 commented 6 years ago

@DanTup May I know if you contributed the view in package.json?

DanTup commented 6 years ago

@sandy081 Yep, I did (as dartFlutterOutline, which is the ID in the registerTreeDataProvider registration, not the createTreeView one) - it's an existing tree I already have that I was trying to add Reveal support for. I wasn't really sure if I was using it correctly; what's the expected way to convert code that already works with registerTreeDataProvider to get access to reveal?

sandy081 commented 6 years ago

what's the expected way to convert code that already works with registerTreeDataProvider to get access to reveal

You just move away from registerTreeDataProvider to createTreeView. In any case, you need to register the view in the package.json.

DanTup commented 6 years ago

@sandy081 Aha, got it.

How can I tell the difference between when a user clicks on the tree and when I selected an item programmatically? (to avoid the tree trying to navigate my editor when it selected an item as a result of the editor position changing)

sandy081 commented 6 years ago

@DanTup Currently there is no differentiation. Selecting by user or programatically triggers the command. Please file an issue to not to execute command when selection is done programatically.

sandy081 commented 6 years ago

Closing this issue as there is nothing to be do done here

DanTup commented 6 years ago

@sandy081 Done! https://github.com/Microsoft/vscode/issues/46857

Btw, is this shipping in the next release? (You removed the milestone and I'm not sure what that means)

sandy081 commented 6 years ago

There is no fix attached to this issue and hence closed this and removed the milestone. So there is nothing to ship for this issue.

DanTup commented 6 years ago

@sandy081 Whoops, sorry, I confused this with the other case. Makes perfect sense, thanks/sorry! :)

pltrant commented 6 years ago

I ran into the same issue as @DanTup. Unless I am missing something, using createTreeView in place of registerTreeViewDataProvider isn't documented anywhere outside of this issue and the sample code. The 1.21 Release Notes even shows using registerTreeViewDataProvider and the 1.22 Release Notes just refers to the former. I also had to explicitly assign an ID to the TreeItem nodes before reveal would work. The 1.22 Release Notes should probably should state this information for extension authors to make use of it.

sandy081 commented 6 years ago

@pltrant Can you please confirm if you have done the following

And you see the issue mentioned in the description even after following above?

Unless I am missing something, using createTreeView in place of registerTreeViewDataProvider isn't documented anywhere outside of this issue and the sample code

The 1.21 Release Notes even shows using registerTreeViewDataProvider and the 1.22 Release Notes just refers to the former

In 1.21, I exposed TreeView in the proposed api and can be accessed while calling registerTreeViewDataProvider. But after further discussions, we thought it is not good idea to return a view while registering a data provider. Hence I provided a new api to get access to TreeView. Note: Proposed apis are subjected to change.

I also had to explicitly assign an ID to the TreeItem nodes before reveal would work. The 1.22 Release Notes should probably should state this information for extension authors to make use of it.

Not sure if I get it completely. Why do you have to assign an ID for reveal to work?

pltrant commented 6 years ago

And you see the issue mentioned in the description even after following above?

Sorry, if I wasn't clear. I did get it to finally work. I was just commenting on the difficulty in doing so since createTreeView wasn't documented in any issue or release notes. Being a proposed API, it's understandable that it can change.

Not sure if I get it completely. Why do you have to assign an ID for reveal to work?

I don't know. The following javascript code is how I make the call:

myView.reveal({label: stringVariable});

And in my getTreeItem code, I have:

getTreeItem(element) {
        element.id = element.label;

Until I added the line to explicitly set the ID, reveal would not work. Without, it would result in the following error:

Error: Cannot resolve tree item for element 0/0:

sandy081 commented 6 years ago

createTreeView will be available in the stable API from 1.22 and is self documented. It is also documented in the release notes and documentation.

ID is not mandatory for the reveal to work. I could reproduce the bug with the sample FTP Explorer here. Opened a new issue for this - #47147