diff --git a/src/GscFileParser.ts b/src/GscFileParser.ts index b303586..8c876aa 100644 --- a/src/GscFileParser.ts +++ b/src/GscFileParser.ts @@ -2623,6 +2623,20 @@ export class GscGroup { } + /** + * Find parent group of current group that contains given range. + */ + public getParentWithinRange(range: vscode.Range): GscGroup | undefined { + var group = this as GscGroup; + while (group.parent !== undefined) { + if (group.parent.getRange().contains(range)) { + return group.parent; + } + group = group.parent; + } + return undefined; + } + /** * Select variable tokens like 'level.aaa.bbb' from document diff --git a/src/GscFiles.ts b/src/GscFiles.ts index c53dfae..579c343 100644 --- a/src/GscFiles.ts +++ b/src/GscFiles.ts @@ -623,8 +623,6 @@ export class GscFiles { private static getWebviewContent(): string { const editor = vscode.window.activeTextEditor; - const cursor = editor?.selection.active; - const uri = editor?.document.uri; var html = ` @@ -632,11 +630,13 @@ export class GscFiles {
Cursor position: ${cursor?.line}, ${cursor?.character}
`; - } + if (editor) { + const cursor = editor.selection.active; + const cursorRange = new vscode.Range(editor.selection.start, editor.selection.end); + const uri = editor.document.uri; + + html += `Cursor position: ${cursor.line}, ${cursor.character} start: ${cursorRange.start.line}, ${cursorRange.start?.character} end: ${cursorRange.end?.line}, ${cursorRange.end?.character}
`; - if (uri && cursor) { const gscFile = GscFiles.getCachedFile(uri); @@ -644,21 +644,30 @@ export class GscFiles { html += "File is not part of workspace: " + uri.fsPath + "
"; } else { - - // Get group before cursor - var groupAtCursor = gscFile.data.root.findGroupOnLeftAtPosition(cursor); + var groupAtCursorStart = gscFile.data.root.findGroupOnLeftAtPosition(cursorRange.start.translate(0, 1)); - if (groupAtCursor === undefined) { + + if (groupAtCursorStart === undefined) { html += "No item found at position.
"; } else { - html += "" + GscFileParser.debugGroupAsString(gscFile.data.root.tokensAll, undefined, groupAtCursor, 0, true, groupAtCursor) + ""; - html += "
----------------------------------------------------------------------------------------"; - html += "
" + GscFileParser.debugAsString(gscFile.data.root.tokensAll, groupAtCursor.findParentOfType(GroupType.Root)!, true, groupAtCursor) + ""; + if (cursorRange.isEmpty) { + html += "
" + GscFileParser.debugGroupAsString(gscFile.data.root.tokensAll, undefined, groupAtCursorStart, 0, true, groupAtCursorStart) + ""; + html += "
----------------------------------------------------------------------------------------"; + html += "
" + GscFileParser.debugAsString(gscFile.data.root.tokensAll, groupAtCursorStart.findParentOfType(GroupType.Root)!, true, groupAtCursorStart) + ""; + } else { + const parentInRange = groupAtCursorStart.getParentWithinRange(cursorRange); + if (parentInRange === undefined) { + html += "
No group found in selected range"; + } else { + html += "
" + GscFileParser.debugGroupAsString(gscFile.data.root.tokensAll, undefined, parentInRange, 0, true) + ""; + } + } + html += "
"; html += "Function:\n";