Skip to content
This repository was archived by the owner on May 11, 2022. It is now read-only.

Commit 109f9db

Browse files
committed
FUSETOOLS2-1165 - set focus only when creating through UI
it avoids that the focus is set on the extension every time external extension are contributing tutorials (such as VS Code Camel K) Signed-off-by: Aurélien Pupier <[email protected]>
1 parent ea309de commit 109f9db

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const DEFAULT_TUTORIAL_CATEGORY = "Didact";
3636
export const DEFAULT_TUTORIAL_NAME = "Didact Demo";
3737

3838
export const didactTutorialsProvider = new DidactNodeProvider();
39-
let didactTreeView : vscode.TreeView<SimpleNode>;
39+
export let didactTreeView : vscode.TreeView<SimpleNode>;
4040
let didactTelemetry: DidactTelemetry;
4141

4242
export async function activate(context: vscode.ExtensionContext): Promise<any> {

src/test/suite/extension.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ suite('Extension Test Suite', () => {
4545
}
4646
}
4747
}
48-
expect(match).to.be.true;
48+
expect(match, `Tutorial ${tutorialName} not found in registry. It contains ${existingRegistry}`).to.be.true;
4949
}
5050
});
5151

src/test/suite/nodeProvider.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
before('set up the registry tests', async () => {
3434
// make sure that the HelloWorld tutorial is registered if it's been cleared
35-
await registerTutorialWithCategory(tutorialName, tutorialUri.fsPath, tutorialCategory);
35+
await registerTutorialWithCategory(tutorialName, tutorialUri.fsPath, tutorialCategory, true);
3636
});
3737

3838
test('Verify that we get null as the parent of the category', async () => {

src/test/suite/registry.test.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,64 @@ import {getRegisteredTutorials, getDidactCategories, getTutorialsForCategory, ge
33
import {before} from 'mocha';
44
import * as vscode from 'vscode';
55
import { ADD_TUTORIAL_TO_REGISTRY, getContext, REGISTER_TUTORIAL } from '../../extensionFunctions';
6-
import { DEFAULT_TUTORIAL_CATEGORY, DEFAULT_TUTORIAL_NAME } from '../../extension';
6+
import { DEFAULT_TUTORIAL_CATEGORY, DEFAULT_TUTORIAL_NAME, didactTreeView, didactTutorialsProvider } from '../../extension';
7+
import { waitUntil } from 'async-wait-until';
78

89
const name = 'new-tutorial';
910
const category = 'some-category';
1011
const source = 'my-uri';
1112
const name2 = 'new-tutorial-2';
1213
const category2 = 'some-category-2';
1314
const source2 = 'my-uri-2';
15+
const name3 = 'new-tutorial-3';
16+
const category3 = 'some-category-3';
17+
const source3 = 'my-uri-3';
1418

1519
suite('Didact registry test suite', () => {
1620

17-
before('set up the registry tests', async () => {
21+
before('Clear the registry tests', async () => {
1822
await clearRegisteredTutorials(false);
1923
});
2024

2125
test('assert that clearing the registry made it empty', async () => {
2226
const registry = getRegisteredTutorials();
23-
assert.strictEqual(registry, undefined);
27+
assert.strictEqual(registry, undefined, `Registry was expected undefined but got ${registry}`);
2428

2529
// clean up and add one demo tutorial back in
2630
const tutorialUri = vscode.Uri.file(getContext().asAbsolutePath('./demos/markdown/didact-demo.didact.md'));
27-
await registerTutorialWithCategory(DEFAULT_TUTORIAL_NAME, tutorialUri.fsPath, DEFAULT_TUTORIAL_CATEGORY);
31+
await registerTutorialWithCategory(DEFAULT_TUTORIAL_NAME, tutorialUri.fsPath, DEFAULT_TUTORIAL_CATEGORY, true);
2832

2933
const addRegistry = getRegisteredTutorials();
30-
assert.notStrictEqual(addRegistry, undefined);
34+
assert.notStrictEqual(addRegistry, undefined, `Registry was expected not to be undefined`);
3135
});
3236

37+
test('Add to registry without setting focus', async() => {
38+
await clearRegisteredTutorials(false);
39+
const selectedTutorialsBeforeTest = didactTreeView.selection;
40+
assert.strictEqual(selectedTutorialsBeforeTest.length, 0, `There are unexpected selected Tutorials before test: ${selectedTutorialsBeforeTest}`);
41+
42+
try {
43+
await registerTutorialWithArgs(name3, source3, category3);
44+
} catch(error) {
45+
assert.fail('We failed to create the new didact registry entry');
46+
}
47+
48+
const selectedTutorials = didactTreeView.selection;
49+
assert.strictEqual(selectedTutorials.length, 0, `There are unexpected selected Tutorials: ${selectedTutorials}`);
50+
});
51+
3352
test('add to registry', async () => {
3453
try {
35-
await registerTutorialWithArgs(name, source, category).then( () => {
54+
await registerTutorialWithArgs(name, source, category, true).then( () => {
3655
assert.ok('No errors thrown while creating new didact registry entry');
3756
});
3857
} catch (error) {
3958
assert.fail('We failed to create the new didact registry entry');
4059
}
60+
61+
const selectedTutorials = didactTreeView.selection;
62+
assert.strictEqual(selectedTutorials.length, 1, `There are unexpected selected Tutorials: ${selectedTutorials}`);
63+
assert.strictEqual(selectedTutorials[0].label, name);
4164

4265
try {
4366
await registerTutorialWithCategory(name, source, category);

src/utils.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export async function registerTutorialWithJSON( jsonObject: any) {
140140
return registerTutorialWithClass(newTutorial);
141141
}
142142

143-
export async function registerTutorialWithClass(newDidact: Tutorial): Promise<void> {
143+
export async function registerTutorialWithClass(newDidact: Tutorial, setFocusInTreeView = false): Promise<void> {
144144
const newDidactAsString = JSON.stringify(newDidact);
145145
let existingRegistry : string[] | undefined = getRegisteredTutorials();
146146
if(!existingRegistry) {
@@ -165,6 +165,12 @@ export async function registerTutorialWithClass(newDidact: Tutorial): Promise<vo
165165
extensionFunctions.sendTextToOutputChannel(`Didact tutorial with name ${newDidact.name} and category ${newDidact.category} already exists`);
166166
}
167167
}
168+
if (setFocusInTreeView) {
169+
await focusInTreeView(existingRegistry, newDidact);
170+
}
171+
}
172+
173+
async function focusInTreeView(existingRegistry: string[], newDidact: Tutorial) {
168174
await commands.executeCommand('didact.tutorials.focus'); // open the tutorials view
169175
await extensionFunctions.getContext().workspaceState.update(DIDACT_REGISTERED_SETTING, existingRegistry);
170176
refreshTreeview();
@@ -175,13 +181,13 @@ export async function registerTutorialWithClass(newDidact: Tutorial): Promise<vo
175181
}
176182
}
177183

178-
export async function registerTutorialWithArgs(name : string, sourceUri : string, category : string ): Promise<void> {
184+
export async function registerTutorialWithArgs(name : string, sourceUri : string, category : string, setFocusInTreeView = false ): Promise<void> {
179185
const newTutorial = new Tutorial(name, sourceUri, category);
180-
return registerTutorialWithClass(newTutorial);
186+
return registerTutorialWithClass(newTutorial, setFocusInTreeView);
181187
}
182188

183-
export async function registerTutorialWithCategory(name : string, sourceUri : string, category : string ): Promise<void> {
184-
return registerTutorialWithArgs(name, sourceUri, category);
189+
export async function registerTutorialWithCategory(name : string, sourceUri : string, category : string, setFocusInTreeView = false ): Promise<void> {
190+
return registerTutorialWithArgs(name, sourceUri, category, setFocusInTreeView);
185191
}
186192

187193
export async function registerEmbeddedTutorials(context: ExtensionContext, name: string, pathInExtension: string): Promise<void> {
@@ -351,9 +357,9 @@ export async function addNewTutorialWithNameAndCategoryForDidactUri(uri: Uri, na
351357
}
352358
if (tutorialName && tutorialCategory) {
353359
if (uri.scheme.toLowerCase() === 'file') {
354-
await registerTutorialWithArgs(tutorialName, uri.fsPath, tutorialCategory);
360+
await registerTutorialWithArgs(tutorialName, uri.fsPath, tutorialCategory, true);
355361
} else {
356-
await registerTutorialWithArgs(tutorialName, uri.toString(), tutorialCategory);
362+
await registerTutorialWithArgs(tutorialName, uri.toString(), tutorialCategory, true);
357363
}
358364
}
359365
}

0 commit comments

Comments
 (0)