Skip to content

Commit ead138e

Browse files
Merge pull request #2480 from github/robertbrignull/app-environment
Add EnvironmentContext so common code can do localisation
2 parents 9672a0b + b722fb4 commit ead138e

File tree

8 files changed

+57
-14
lines changed

8 files changed

+57
-14
lines changed

extensions/ql-vscode/src/common/app.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ export interface App {
2323
readonly onDidChangeWorkspaceFolders: Event<WorkspaceFoldersChangeEvent>;
2424
readonly credentials: Credentials;
2525
readonly commands: AppCommandManager;
26+
readonly environment: EnvironmentContext;
2627
}
2728

2829
export enum AppMode {
2930
Production = 1,
3031
Development = 2,
3132
Test = 3,
3233
}
34+
35+
export interface EnvironmentContext {
36+
language: string;
37+
}

extensions/ql-vscode/src/common/file-tree-nodes.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { basename, dirname, join } from "path";
2-
import { env } from "vscode";
2+
import { EnvironmentContext } from "./app";
33

44
/**
55
* A node in the tree of files. This will be either a `FileTreeDirectory` or a `FileTreeLeaf`.
@@ -35,6 +35,7 @@ export class FileTreeDirectory<T = undefined> extends FileTreeNode<T> {
3535
constructor(
3636
_path: string,
3737
_name: string,
38+
protected readonly env: EnvironmentContext,
3839
private _children: Array<FileTreeNode<T>> = [],
3940
) {
4041
super(_path, _name);
@@ -66,7 +67,9 @@ export class FileTreeDirectory<T = undefined> extends FileTreeNode<T> {
6667
this._children.filter(
6768
(child) => child instanceof FileTreeLeaf || child.children.length > 0,
6869
);
69-
this._children.sort((a, b) => a.name.localeCompare(b.name, env.language));
70+
this._children.sort((a, b) =>
71+
a.name.localeCompare(b.name, this.env.language),
72+
);
7073
this._children.forEach((child, i) => {
7174
child.finish();
7275
if (
@@ -77,6 +80,7 @@ export class FileTreeDirectory<T = undefined> extends FileTreeNode<T> {
7780
const replacement = new FileTreeDirectory<T>(
7881
child.children[0].path,
7982
`${child.name} / ${child.children[0].name}`,
83+
this.env,
8084
Array.from(child.children[0].children),
8185
);
8286
this._children[i] = replacement;
@@ -89,7 +93,11 @@ export class FileTreeDirectory<T = undefined> extends FileTreeNode<T> {
8993
if (existingChild !== undefined) {
9094
return existingChild as FileTreeDirectory<T>;
9195
} else {
92-
const newChild = new FileTreeDirectory<T>(join(this.path, name), name);
96+
const newChild = new FileTreeDirectory<T>(
97+
join(this.path, name),
98+
name,
99+
this.env,
100+
);
93101
this.addChild(newChild);
94102
return newChild;
95103
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { env } from "vscode";
2+
import { EnvironmentContext } from "../app";
3+
4+
export class AppEnvironmentContext implements EnvironmentContext {
5+
public get language(): string {
6+
return env.language;
7+
}
8+
}

extensions/ql-vscode/src/common/vscode/vscode-app.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import * as vscode from "vscode";
22
import { VSCodeCredentials } from "./authentication";
33
import { Disposable } from "../../pure/disposable-object";
4-
import { App, AppMode } from "../app";
4+
import { App, AppMode, EnvironmentContext } from "../app";
55
import { AppEventEmitter } from "../events";
66
import { extLogger, Logger, queryServerLogger } from "../logging";
77
import { Memento } from "../memento";
88
import { VSCodeAppEventEmitter } from "./events";
99
import { AppCommandManager, QueryServerCommandManager } from "../commands";
1010
import { createVSCodeCommandManager } from "./commands";
11+
import { AppEnvironmentContext } from "./environment-context";
1112

1213
export class ExtensionApp implements App {
1314
public readonly credentials: VSCodeCredentials;
@@ -69,4 +70,8 @@ export class ExtensionApp implements App {
6970
public createEventEmitter<T>(): AppEventEmitter<T> {
7071
return new VSCodeAppEventEmitter<T>();
7172
}
73+
74+
public get environment(): EnvironmentContext {
75+
return new AppEnvironmentContext();
76+
}
7277
}

extensions/ql-vscode/src/queries-panel/query-discovery.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ export class QueryDiscovery
4141
new MultiFileSystemWatcher(),
4242
);
4343

44-
constructor(app: App, private readonly cliServer: CodeQLCliServer) {
44+
constructor(
45+
private readonly app: App,
46+
private readonly cliServer: CodeQLCliServer,
47+
) {
4548
super("Query Discovery", extLogger);
4649

4750
this.onDidChangeQueriesEmitter = this.push(app.createEventEmitter<void>());
@@ -124,7 +127,11 @@ export class QueryDiscovery
124127
return undefined;
125128
}
126129

127-
const rootDirectory = new FileTreeDirectory<string>(fullPath, name);
130+
const rootDirectory = new FileTreeDirectory<string>(
131+
fullPath,
132+
name,
133+
this.app.environment,
134+
);
128135
for (const queryPath of resolvedQueries) {
129136
const relativePath = normalize(relative(fullPath, queryPath));
130137
const dirName = dirname(relativePath);

extensions/ql-vscode/src/query-testing/qltest-discovery.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Uri,
77
RelativePattern,
88
WorkspaceFolder,
9+
env,
910
} from "vscode";
1011
import { MultiFileSystemWatcher } from "../common/vscode/multi-file-system-watcher";
1112
import { CodeQLCliServer } from "../codeql-cli/cli";
@@ -97,7 +98,7 @@ export class QLTestDiscovery extends Discovery<QLTestDiscoveryResults> {
9798
private async discoverTests(): Promise<FileTreeDirectory> {
9899
const fullPath = this.workspaceFolder.uri.fsPath;
99100
const name = this.workspaceFolder.name;
100-
const rootDirectory = new FileTreeDirectory(fullPath, name);
101+
const rootDirectory = new FileTreeDirectory(fullPath, name, env);
101102

102103
// Don't try discovery on workspace folders that don't exist on the filesystem
103104
if (await pathExists(fullPath)) {

extensions/ql-vscode/test/__mocks__/appMock.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { App, AppMode } from "../../src/common/app";
1+
import { App, AppMode, EnvironmentContext } from "../../src/common/app";
22
import { AppEvent, AppEventEmitter } from "../../src/common/events";
33
import { Memento } from "../../src/common/memento";
44
import { Disposable } from "../../src/pure/disposable-object";
@@ -24,6 +24,7 @@ export function createMockApp({
2424
onDidChangeWorkspaceFolders = jest.fn(),
2525
credentials = testCredentialsWithStub(),
2626
commands = createMockCommandManager(),
27+
environment = createMockEnvironmentContext(),
2728
}: {
2829
extensionPath?: string;
2930
workspaceStoragePath?: string;
@@ -34,6 +35,7 @@ export function createMockApp({
3435
onDidChangeWorkspaceFolders?: Event<WorkspaceFoldersChangeEvent>;
3536
credentials?: Credentials;
3637
commands?: AppCommandManager;
38+
environment?: EnvironmentContext;
3739
}): App {
3840
return {
3941
mode: AppMode.Test,
@@ -48,6 +50,7 @@ export function createMockApp({
4850
createEventEmitter,
4951
credentials,
5052
commands,
53+
environment,
5154
};
5255
}
5356

@@ -68,3 +71,9 @@ export class MockAppEventEmitter<T> implements AppEventEmitter<T> {
6871
// no-op
6972
}
7073
}
74+
75+
export function createMockEnvironmentContext(): EnvironmentContext {
76+
return {
77+
language: "en-US",
78+
};
79+
}

extensions/ql-vscode/test/vscode-tests/minimal-workspace/queries-panel/query-tree-data-provider.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EventEmitter } from "vscode";
1+
import { EventEmitter, env } from "vscode";
22
import {
33
FileTreeDirectory,
44
FileTreeLeaf,
@@ -31,8 +31,8 @@ describe("QueryTreeDataProvider", () => {
3131
it("converts FileTreeNode to QueryTreeViewItem", async () => {
3232
const dataProvider = new QueryTreeDataProvider({
3333
queries: [
34-
new FileTreeDirectory<string>("dir1", "dir1", [
35-
new FileTreeDirectory<string>("dir1/dir2", "dir2", [
34+
new FileTreeDirectory<string>("dir1", "dir1", env, [
35+
new FileTreeDirectory<string>("dir1/dir2", "dir2", env, [
3636
new FileTreeLeaf<string>(
3737
"dir1/dir2/file1",
3838
"file1",
@@ -45,7 +45,7 @@ describe("QueryTreeDataProvider", () => {
4545
),
4646
]),
4747
]),
48-
new FileTreeDirectory<string>("dir3", "dir3", [
48+
new FileTreeDirectory<string>("dir3", "dir3", env, [
4949
new FileTreeLeaf<string>("dir3/file3", "file3", "javascript"),
5050
]),
5151
],
@@ -78,7 +78,7 @@ describe("QueryTreeDataProvider", () => {
7878
const onDidChangeQueriesEmitter = new EventEmitter<void>();
7979
const queryDiscoverer: QueryDiscoverer = {
8080
queries: [
81-
new FileTreeDirectory<string>("dir1", "dir1", [
81+
new FileTreeDirectory<string>("dir1", "dir1", env, [
8282
new FileTreeLeaf<string>("dir1/file1", "file1", "javascript"),
8383
]),
8484
],
@@ -89,7 +89,7 @@ describe("QueryTreeDataProvider", () => {
8989
expect(dataProvider.getChildren().length).toEqual(1);
9090

9191
queryDiscoverer.queries?.push(
92-
new FileTreeDirectory<string>("dir2", "dir2", [
92+
new FileTreeDirectory<string>("dir2", "dir2", env, [
9393
new FileTreeLeaf<string>("dir2/file2", "file2", "javascript"),
9494
]),
9595
);

0 commit comments

Comments
 (0)