From 44efe2680a629e843f65f6853c65d8adc134b5f1 Mon Sep 17 00:00:00 2001 From: Matthias Behr Date: Mon, 4 Jan 2021 20:18:29 +0100 Subject: [PATCH] fix(restquery): prefer the docs that are visible If the get/docs/ id is not the document path but a number (e.g. 0) the id'th one from the tree view - so the visible dlt docs - is used and not from the known/still in mem documents. Fallback to prev method if e.g. no document is visble. Added a deprecated warning for the case where more than 1 document is know. This fallback should be removed in the future. --- src/dltDocumentProvider.ts | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/dltDocumentProvider.ts b/src/dltDocumentProvider.ts index f61d49ec..c837ea07 100644 --- a/src/dltDocumentProvider.ts +++ b/src/dltDocumentProvider.ts @@ -750,12 +750,12 @@ export class DltDocumentProvider implements vscode.TreeDataProvider.*?)\/(?.*?)($|\?(?.+)$)/; const regRes = re.exec(query); if (regRes?.length && regRes.groups) { - console.log(`got regRes.length=${regRes.length}`); - regRes.forEach(regR => console.log(JSON.stringify(regR))); + //console.log(`got regRes.length=${regRes.length}`); + //regRes.forEach(regR => console.log(JSON.stringify(regR))); const cmd = regRes.groups['cmd']; const path = regRes.groups['path']; const options = regRes.groups['options']; - console.log(` cmd='${cmd}' path='${path}' options='${options}'`); + console.log(` restQuery cmd='${cmd}' path='${path}' options='${options}'`); switch (cmd) { case 'get': { @@ -809,11 +809,23 @@ export class DltDocumentProvider implements vscode.TreeDataProvider= 0 && docIdx < this._documents.size) { - const iter = this._documents.entries(); - for (let i = 0; i <= docIdx; ++i) { - const [, aDoc] = iter.next().value; - if (i === docIdx) { doc = aDoc; } + + // take the docIdx th. dlt doc that is visible: + if (this._treeRootNodes.length > docIdx) { + const childNode = this._treeRootNodes[docIdx]; + // now find the document for that: + this._documents.forEach(aDoc => { + if (aDoc.treeNode === childNode) { doc = aDoc; } + }); + } + if (!doc) { // fallback to prev. method. which is ok for one doc, but not for mult.... + if (this._documents.size > 1) { console.warn(`DltDocumentProvider.restQuery: you're using a deprecated method to access documents! Please only refer to visible documents!`); } + if (docIdx >= 0 && docIdx < this._documents.size) { + const iter = this._documents.entries(); + for (let i = 0; i <= docIdx; ++i) { + const [, aDoc] = iter.next().value; + if (i === docIdx) { doc = aDoc; } + } } } }