Skip to content

Commit 3e3e12a

Browse files
authored
Merge pull request #1230 from github/aeisenberg/astviewer-uri
Fix invalid file comparison for changing ast viewer location
2 parents 3d21b20 + 421f5d2 commit 3e3e12a

6 files changed

Lines changed: 29 additions & 19 deletions

File tree

extensions/ql-vscode/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [UNRELEASED]
44

5+
- Fix a bug where the AST viewer was not synchronizing its selected node when the editor selection changes. [#1230](https://github.com/github/vscode-codeql/pull/1230)
6+
57
## 1.6.1 - 17 March 2022
68

79
No user facing changes.

extensions/ql-vscode/src/astViewer.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
TextEditorSelectionChangeEvent,
1111
TextEditorSelectionChangeKind,
1212
Location,
13-
Range
13+
Range,
14+
Uri
1415
} from 'vscode';
1516
import * as path from 'path';
1617

@@ -104,7 +105,7 @@ class AstViewerDataProvider extends DisposableObject implements TreeDataProvider
104105
export class AstViewer extends DisposableObject {
105106
private treeView: TreeView<AstItem>;
106107
private treeDataProvider: AstViewerDataProvider;
107-
private currentFile: string | undefined;
108+
private currentFileUri: Uri | undefined;
108109

109110
constructor() {
110111
super();
@@ -125,12 +126,12 @@ export class AstViewer extends DisposableObject {
125126
this.push(window.onDidChangeTextEditorSelection(this.updateTreeSelection, this));
126127
}
127128

128-
updateRoots(roots: AstItem[], db: DatabaseItem, fileName: string) {
129+
updateRoots(roots: AstItem[], db: DatabaseItem, fileUri: Uri) {
129130
this.treeDataProvider.roots = roots;
130131
this.treeDataProvider.db = db;
131132
this.treeDataProvider.refresh();
132-
this.treeView.message = `AST for ${path.basename(fileName)}`;
133-
this.currentFile = fileName;
133+
this.treeView.message = `AST for ${path.basename(fileUri.fsPath)}`;
134+
this.currentFileUri = fileUri;
134135
// Handle error on reveal. This could happen if
135136
// the tree view is disposed during the reveal.
136137
this.treeView.reveal(roots[0], { focus: false })?.then(
@@ -174,7 +175,7 @@ export class AstViewer extends DisposableObject {
174175

175176
if (
176177
this.treeView.visible &&
177-
e.textEditor.document.uri.fsPath === this.currentFile &&
178+
e.textEditor.document.uri.fsPath === this.currentFileUri?.fsPath &&
178179
e.selections.length === 1
179180
) {
180181
const selection = e.selections[0];
@@ -199,6 +200,6 @@ export class AstViewer extends DisposableObject {
199200
this.treeDataProvider.db = undefined;
200201
this.treeDataProvider.refresh();
201202
this.treeView.message = undefined;
202-
this.currentFile = undefined;
203+
this.currentFileUri = undefined;
203204
}
204205
}

extensions/ql-vscode/src/contextual/astBuilder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DecodedBqrsChunk, BqrsId, EntityValue } from '../pure/bqrs-cli-types';
44
import { DatabaseItem } from '../databases';
55
import { ChildAstItem, AstItem } from '../astViewer';
66
import fileRangeFromURI from './fileRangeFromURI';
7+
import { Uri } from 'vscode';
78

89
/**
910
* A class that wraps a tree of QL results from a query that
@@ -17,7 +18,7 @@ export default class AstBuilder {
1718
queryResults: QueryWithResults,
1819
private cli: CodeQLCliServer,
1920
public db: DatabaseItem,
20-
public fileName: string
21+
public fileName: Uri
2122
) {
2223
this.bqrsPath = queryResults.query.resultsPaths.resultsPath;
2324
}

extensions/ql-vscode/src/contextual/templateProvider.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
TextDocument,
1111
Uri
1212
} from 'vscode';
13-
import * as path from 'path';
1413

1514
import { decodeSourceArchiveUri, encodeArchiveBasePath, zipArchiveScheme } from '../archive-filesystem-provider';
1615
import { CodeQLCliServer } from '../cli';
@@ -160,7 +159,7 @@ export class TemplatePrintAstProvider {
160159
return new AstBuilder(
161160
query, this.cli,
162161
this.dbm.findDatabaseItem(dbUri)!,
163-
path.basename(fileUri.fsPath),
162+
fileUri,
164163
);
165164
}
166165

extensions/ql-vscode/src/vscode-tests/no-workspace/astViewer.test.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as sinon from 'sinon';
55
import * as yaml from 'js-yaml';
66

77
import { AstViewer, AstItem } from '../../astViewer';
8-
import { commands, Range } from 'vscode';
8+
import { commands, Range, Uri } from 'vscode';
99
import { DatabaseItem } from '../../databases';
1010
import { testDisposeHandler } from '../test-dispose-handler';
1111

@@ -40,7 +40,7 @@ describe('AstViewer', () => {
4040
it('should update the viewer roots', () => {
4141
const item = {} as DatabaseItem;
4242
viewer = new AstViewer();
43-
viewer.updateRoots(astRoots, item, 'def/abc');
43+
viewer.updateRoots(astRoots, item, Uri.file('def/abc'));
4444

4545
expect((viewer as any).treeDataProvider.roots).to.eq(astRoots);
4646
expect((viewer as any).treeDataProvider.db).to.eq(item);
@@ -59,25 +59,31 @@ describe('AstViewer', () => {
5959
doSelectionTest(expr, expr.fileLocation?.range);
6060
});
6161

62-
it('should select nothing', () => {
62+
it('should select nothing because of no overlap in range', () => {
6363
doSelectionTest(undefined, new Range(2, 3, 4, 5));
6464
});
6565

66+
it('should select nothing because of different file', () => {
67+
doSelectionTest(undefined, astRoots[0].fileLocation?.range, Uri.file('def'));
68+
});
69+
70+
const defaultUri = Uri.file('def/abc');
71+
6672
function doSelectionTest(
6773
expectedSelection: any,
6874
selectionRange: Range | undefined,
69-
fsPath = 'def/abc',
75+
fileUri = defaultUri
7076
) {
7177
const item = {} as DatabaseItem;
7278
viewer = new AstViewer();
73-
viewer.updateRoots(astRoots, item, fsPath);
79+
viewer.updateRoots(astRoots, item, defaultUri);
7480
const spy = sandbox.spy();
7581
(viewer as any).treeView.reveal = spy;
7682
Object.defineProperty((viewer as any).treeView, 'visible', {
7783
value: true
7884
});
7985

80-
const mockEvent = createMockEvent(selectionRange, fsPath);
86+
const mockEvent = createMockEvent(selectionRange, fileUri);
8187
(viewer as any).updateTreeSelection(mockEvent);
8288
if (expectedSelection) {
8389
expect(spy).to.have.been.calledWith(expectedSelection);
@@ -88,7 +94,7 @@ describe('AstViewer', () => {
8894

8995
function createMockEvent(
9096
selectionRange: Range | undefined,
91-
fsPath: string,
97+
uri: Uri,
9298
) {
9399
return {
94100
selections: [{
@@ -98,7 +104,7 @@ describe('AstViewer', () => {
98104
textEditor: {
99105
document: {
100106
uri: {
101-
fsPath
107+
fsPath: uri.fsPath
102108
}
103109
}
104110
}

extensions/ql-vscode/src/vscode-tests/no-workspace/contextual/astBuilder.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import AstBuilder from '../../../contextual/astBuilder';
77
import { QueryWithResults } from '../../../run-queries';
88
import { CodeQLCliServer } from '../../../cli';
99
import { DatabaseItem } from '../../../databases';
10+
import { Uri } from 'vscode';
1011

1112
chai.use(chaiAsPromised);
1213
const expect = chai.expect;
@@ -145,7 +146,7 @@ describe('AstBuilder', () => {
145146
resultsPath: '/a/b/c'
146147
}
147148
}
148-
} as QueryWithResults, mockCli, {} as DatabaseItem, '');
149+
} as QueryWithResults, mockCli, {} as DatabaseItem, Uri.file(''));
149150
}
150151

151152
function mockDecode(resultSet: 'nodes' | 'edges' | 'graphProperties') {

0 commit comments

Comments
 (0)