Skip to content

Commit

Permalink
#22: Add memo.collapseBacklinksPanelItems config prop
Browse files Browse the repository at this point in the history
  • Loading branch information
svsool committed Jul 28, 2020
1 parent 3b8e6a1 commit 897d8d7
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 20 deletions.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
"scope": "resource",
"description": "The maximum height of the image preview",
"type": "number"
},
"memo.collapseBacklinksPanelItems": {
"default": false,
"scope": "resource",
"description": "Collapse backlinks panel items by default.",
"type": "boolean"
}
}
},
Expand All @@ -96,6 +102,12 @@
}
]
},
"viewsWelcome": [
{
"view": "memoBacklinksExplorer",
"contents": "No information to show"
}
],
"markdown.previewStyles": [
"./media/fontello/css/fontello.css",
"./media/markdown.css"
Expand Down
6 changes: 2 additions & 4 deletions src/extension.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as vscode from 'vscode';

import { closeAllEditors } from './test/testUtils';
import { closeEditorsAndCleanWorkspace } from './test/testUtils';

const MEMO_EXTENSION_ID = 'svsool.markdown-memo';

describe('extension', () => {
beforeEach(async () => {
await closeAllEditors();
});
beforeEach(closeEditorsAndCleanWorkspace);

it('should find extension in extensions list', () => {
expect(vscode.extensions.all.some((extension) => extension.id === MEMO_EXTENSION_ID)).toBe(
Expand Down
38 changes: 38 additions & 0 deletions src/extensions/BacklinksTreeDataProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
closeEditorsAndCleanWorkspace,
getWorkspaceFolder,
toPlainObject,
updateMemoConfigProperty,
getConfigProperty,
} from '../test/testUtils';

const getChildren = async () => {
Expand Down Expand Up @@ -298,4 +300,40 @@ describe('BacklinksTreeDataProvider()', () => {
},
]);
});

it('should collapse parent items according to configuration', async () => {
const link = rndName();
const name0 = rndName();
const name1 = rndName();

await createFile(`${link}.md`);
await createFile(`a-${name0}.md`, `First note with backlink [[${link}]]`);
await createFile(`b-${name1}.md`, `Second note with backlink [[${link}]]`);

const doc = await openTextDocument(`${link}.md`);

await window.showTextDocument(doc);

await updateMemoConfigProperty('collapseBacklinksPanelItems', true);

expect((await getChildren()).every((child) => child.collapsibleState === 1)).toBe(true);
});

it('should expand parent items according to config', async () => {
const link = rndName();
const name0 = rndName();
const name1 = rndName();

await createFile(`${link}.md`);
await createFile(`a-${name0}.md`, `First note with backlink [[${link}]]`);
await createFile(`b-${name1}.md`, `Second note with backlink [[${link}]]`);

const doc = await openTextDocument(`${link}.md`);

await window.showTextDocument(doc);

expect(getConfigProperty('collapseBacklinksPanelItems', null)).toBe(false);

expect((await getChildren()).every((child) => child.collapsibleState === 2)).toBe(true);
});
});
15 changes: 8 additions & 7 deletions src/extensions/BacklinksTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getWorkspaceFolder,
trimSlashes,
sortPaths,
getConfigProperty,
} from '../utils';
import { FoundRefT } from '../types';

Expand Down Expand Up @@ -40,12 +41,8 @@ export default class BacklinksTreeDataProvider implements vscode.TreeDataProvide
if (!element) {
const fsPath = vscode.window.activeTextEditor?.document.uri.fsPath;

const noInfoToShow = [
new Backlink('No information to show', undefined, vscode.TreeItemCollapsibleState.None),
];

if (!fsPath || (fsPath && !containsMarkdownExt(fsPath))) {
return noInfoToShow;
return [];
}

const refFromFilename = path.parse(fsPath).name;
Expand All @@ -58,14 +55,18 @@ export default class BacklinksTreeDataProvider implements vscode.TreeDataProvide
const pathsSorted = sortPaths(Object.keys(referencesByPath), { shallowFirst: true });

if (!pathsSorted.length) {
return noInfoToShow;
return [];
}

const collapsibleState = getConfigProperty('collapseBacklinksPanelItems', false)
? vscode.TreeItemCollapsibleState.Collapsed
: vscode.TreeItemCollapsibleState.Expanded;

return pathsSorted.map((pathParam) => {
const backlink = new Backlink(
path.basename(pathParam),
referencesByPath[pathParam],
vscode.TreeItemCollapsibleState.Expanded,
collapsibleState,
);
backlink.description = `(${referencesByPath[pathParam].length}) ${trimSlashes(
pathParam
Expand Down
8 changes: 1 addition & 7 deletions src/extensions/completionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import {
import path from 'path';
import groupBy from 'lodash.groupby';

import {
getWorkspaceCache,
fsPathToRef,
containsImageExt,
containsOtherKnownExts,
getConfigProperty,
} from '../utils';
import { getWorkspaceCache, fsPathToRef, containsImageExt, containsOtherKnownExts } from '../utils';

const padWithZero = (n: number): string => (n < 10 ? '0' + n : String(n));

Expand Down
32 changes: 30 additions & 2 deletions src/test/testUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import rimraf from 'rimraf';
import path from 'path';
import { workspace, Uri, commands } from 'vscode';
import { workspace, Uri, commands, ConfigurationTarget } from 'vscode';
export { default as waitForExpect } from 'wait-for-expect';

import * as utils from '../utils';
Expand All @@ -11,9 +11,11 @@ const {
getImgUrlForMarkdownPreview,
getFileUrlForMarkdownPreview,
escapeForRegExp,
getConfigProperty,
} = utils;

export {
getConfigProperty,
getWorkspaceFolder,
getImgUrlForMarkdownPreview,
getFileUrlForMarkdownPreview,
Expand Down Expand Up @@ -87,7 +89,33 @@ export const closeAllEditors = async () => {
await delay(100);
};

const getDefaultConfigProperties = (): {
default?: any;
scope?: string;
description?: string;
type?: string;
}[] => {
return require('../../package.json').contributes.configuration.properties;
};

export const updateConfigProperty = async (property: string, value: unknown) => {
await workspace.getConfiguration().update(property, value, ConfigurationTarget.Workspace);
};

export const updateMemoConfigProperty = async (property: string, value: unknown) =>
await updateConfigProperty(`memo.${property}`, value);

const resetMemoConfigProps = async () =>
await Promise.all(
Object.entries(getDefaultConfigProperties()).map(([propName, propConfig]) =>
propConfig.default !== undefined
? updateConfigProperty(propName, propConfig.default)
: undefined,
),
);

export const closeEditorsAndCleanWorkspace = async () => {
await resetMemoConfigProps();
await closeAllEditors();
cleanWorkspace();
await cleanWorkspaceCache();
Expand All @@ -96,4 +124,4 @@ export const closeEditorsAndCleanWorkspace = async () => {
export const getWorkspaceCache = async (): Promise<WorkspaceCache> =>
(await commands.executeCommand('_memo.getWorkspaceCache')) as WorkspaceCache;

export const toPlainObject = (value: unknown) => JSON.parse(JSON.stringify(value));
export const toPlainObject = <R>(value: unknown): R => JSON.parse(JSON.stringify(value));

0 comments on commit 897d8d7

Please sign in to comment.