Skip to content

Commit

Permalink
fix(schemas): Do not include stubs as part of template suggestions wh…
Browse files Browse the repository at this point in the history
…en applying a template (#2357)
  • Loading branch information
tma66 committed Feb 3, 2022
1 parent 110916f commit a746e9c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/plugin-core/src/WSUtilsV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ export class WSUtilsV2 implements IWSUtilsV2 {
async findNoteFromMultiVaultAsync(opts: {
fname: string;
quickpickTitle: string;
nonStubOnly?: boolean;
vault?: DVault;
}): Promise<RespV3<NoteProps | undefined>> {
const { fname, quickpickTitle, vault } = opts;
const { fname, quickpickTitle, nonStubOnly = false, vault } = opts;
let existingNote: NoteProps | undefined;
const engine = ExtensionProvider.getEngine();
const maybeNotes = NoteUtils.getNotesByFnameFromEngine({
Expand All @@ -115,12 +116,16 @@ export class WSUtilsV2 implements IWSUtilsV2 {
vault,
});

if (maybeNotes.length === 1) {
const filteredNotes = nonStubOnly
? maybeNotes.filter((note) => !note.stub)
: maybeNotes;

if (filteredNotes.length === 1) {
// Only one match so use that as note
existingNote = maybeNotes[0];
} else if (maybeNotes.length > 1) {
existingNote = filteredNotes[0];
} else if (filteredNotes.length > 1) {
// If there are multiple notes with this fname, prompt user to select which vault
const vaults = maybeNotes.map((noteProps) => {
const vaults = filteredNotes.map((noteProps) => {
return {
vault: noteProps.vault,
label: `${fname} from ${VaultUtils.getName(noteProps.vault)}`,
Expand All @@ -138,7 +143,7 @@ export class WSUtilsV2 implements IWSUtilsV2 {
});

if (!_.isUndefined(resp)) {
existingNote = _.find(maybeNotes, { vault: resp.vault });
existingNote = _.find(filteredNotes, { vault: resp.vault });
} else {
// If user escaped out of quickpick, then do not return error. Return undefined note instead
return {
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-core/src/WSUtilsV2Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ export interface IWSUtilsV2 {
*
* @param fname: name of note to look for
* @param quickpickTitle: title of quickpick to display if multiple matches are found
* @param nonStubOnly?: if provided, boolean to determine whether to return non-stub notes only. Default behavior is to return all
* @param vault?: if provided, vault to search note from
*/
findNoteFromMultiVaultAsync(opts: {
fname: string;
quickpickTitle: string;
nonStubOnly?: boolean;
vault?: DVault;
}): Promise<RespV3<NoteProps | undefined>>;
}
1 change: 1 addition & 0 deletions packages/plugin-core/src/commands/NoteLookupCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ export class NoteLookupCommand
fname: ref,
quickpickTitle:
"Select which template to apply or press [ESC] to not apply a template",
nonStubOnly: true,
vault: maybeVault,
});
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,64 @@ suite("NoteLookupCommand", function () {
}
);

describeMultiWS(
"WHEN schema template references to a template note and there exists a stub with the same name",
{
ctx,
preSetupHook: ENGINE_HOOKS_MULTI.setupBasicMulti,
postSetupHook: async ({ wsRoot, vaults }) => {
// Schema is in vault1
const vault = vaults[0];
// Template is in vault1
await NoteTestUtilsV4.createNote({
wsRoot,
body: "food ch2 template",
fname: "template.ch2",
vault,
});
// template.ch2 is now a stub in vault2
await NoteTestUtilsV4.createNote({
wsRoot,
body: "food ch2 child note",
fname: "template.ch2.child",
vault: vaults[1],
});
const template: SchemaTemplate = {
id: "template.ch2",
type: "note",
};
await setupSchemaCrossVault({ wsRoot, vault, template });
},
},
() => {
let showQuickPick: sinon.SinonStub;

beforeEach(() => {
showQuickPick = sinon.stub(vscode.window, "showQuickPick");
});
afterEach(() => {
showQuickPick.restore();
});

test("THEN user does not get prompted with stub suggesstion and template note body gets applied to new note", async () => {
const cmd = new NoteLookupCommand();
await cmd.run({
initialValue: "food.ch2",
noConfirm: true,
});
const { engine, vaults } = ExtensionProvider.getDWorkspace();

const newNote = NoteUtils.getNoteByFnameFromEngine({
fname: "food.ch2",
engine,
vault: vaults[0],
});
expect(showQuickPick.calledOnce).toBeFalsy();
expect(_.trim(newNote?.body)).toEqual("food ch2 template");
});
}
);

describeMultiWS(
"WHEN schema template references to a template note that lies in multiple vaults without cross vault notation",
{
Expand Down

0 comments on commit a746e9c

Please sign in to comment.