Skip to content

Commit

Permalink
Add ConfigItem class and auto collapse on search
Browse files Browse the repository at this point in the history
  • Loading branch information
fedecalendino committed Jul 20, 2022
1 parent f1b1992 commit 109c0d4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 94 deletions.
77 changes: 38 additions & 39 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ import * as vscode from 'vscode';
import { CONFIG_FILENAME, EXTENSION_NAME } from "./constants";


export class ConfigItem {

public label?: string;
public description?: string;
public environment?: string;
public environment_icon?: string;
public type?: string;
public type_icon?: string;

constructor(config: Config, data: JSON) {
if (data.hasOwnProperty("label"))
this.label = data["label"];

if (data.hasOwnProperty("description"))
this.description = data["description"];

if (data.hasOwnProperty("environment")) {
this.environment = data["environment"];
this.environment_icon = config.getEnvironmentIcon(this.environment);
}

if (data.hasOwnProperty("type")) {
this.type = data["type"];
this.type_icon = config.getTypeIcon(this.type);
}
}
}

export class Config {

private data: JSON;
Expand Down Expand Up @@ -37,53 +65,25 @@ export class Config {
return this.data["excluded"];
}

public getItem(name: string): JSON {
public getItem(name: string): ConfigItem {
if (!this.data["items"].hasOwnProperty(name))
return undefined;

return this.data["items"][name];
}

public getLabel(name: string): string {
const item = this.getItem(name);

if (!item || !item.hasOwnProperty("label"))
return "";

return item["label"];
return new ConfigItem(this, this.data["items"][name]);
}

public getDescription(name: string): string {
const item = this.getItem(name);

if (!item || !item.hasOwnProperty("description"))
return "";
public getEnvironmentIcon(environment: string): string {
if (!this.data.hasOwnProperty("environments"))
return undefined;

return item["description"];
return this.data["environments"][environment];
}

public getEnvironment(name: string): Array<string> {
const item = this.getItem(name);

if (!item || !item.hasOwnProperty("environment"))
return [undefined, undefined];

const environment = item["environment"];
const icon = this.data["environments"][environment];

return [environment, icon];
}

public getType(name: string): Array<string> {
const item = this.getItem(name);

if (!item || !item.hasOwnProperty("type"))
return [undefined, undefined];
public getTypeIcon(type: string): string {
if (!this.data.hasOwnProperty("types"))
return undefined;

const type = item["type"];
const icon = this.data["types"][type];

return [type, icon];
return this.data["types"][type];
}

static uri(): vscode.Uri {
Expand All @@ -95,5 +95,4 @@ export class Config {

return vscode.Uri.file(config_file_path);
}

}
35 changes: 17 additions & 18 deletions src/file_explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,25 @@ export class FSDocsFileExplorer {

this.treeView = vscode.window.createTreeView(
'fsdocs-file-explorer',
{ treeDataProvider }
{
treeDataProvider: treeDataProvider,
showCollapseAll: true
}
);

context.subscriptions.push(this.treeView);
}

private searchElement(context: vscode.ExtensionContext) {
private async searchElement(context: vscode.ExtensionContext) {
const options: vscode.InputBoxOptions = {
prompt: "Search in fsdocs labels and file/folder names",
placeHolder: "text to search"
prompt: "Search in fsdocs labels and descriptions",
placeHolder: "(found items will be highlighted with a 🔍)"
};

vscode.window.showInputBox(options).then(value => {
if (value) {
this.searchText = value.toLowerCase();
vscode.commands.executeCommand('workbench.actions.treeView.fsdocs-file-explorer.collapseAll');
this.revealFilesAndFolders(this.searchText);
} else {
this.searchText = undefined;
Expand All @@ -181,14 +185,11 @@ export class FSDocsFileExplorer {
}

private _revealFilesAndFolders(root: string, searchText: string) {
const name: string = root.split("/").at(-1);

if (this.config.excluded().includes(name)) {
return undefined;
}

readdirSync(root, {withFileTypes: true}).forEach(
(dirent) => {
if (this.config.excluded().includes(dirent.name))
return;

const path = `${root}/${dirent.name}`;

if (this._shouldReveal(dirent.name, searchText)) {
Expand All @@ -207,17 +208,15 @@ export class FSDocsFileExplorer {
}

private _shouldReveal(name: string, searchText: string): boolean {
if (name.includes(searchText))
return true;

const label = this.config.getLabel(name);
const item = this.config.getItem(name);

if (label && label.toLowerCase().includes(searchText))
return true;
if (!item)
return false;

const description = this.config.getDescription(name);
if (item.label !== undefined && item.label.toLowerCase().includes(searchText))
return true;

if (description && description.toLowerCase().includes(searchText))
if (item.description !== undefined && item.description.toLowerCase().includes(searchText))
return true;

return false;
Expand Down
76 changes: 39 additions & 37 deletions src/tree_data_provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-prototype-builtins */
import * as vscode from 'vscode';

import { Config } from './config';
import { Config, ConfigItem } from './config';
import { BaseFileSystemProvider as BaseTreeDataProvider } from "./tree_data_providers/base";
import { Entry } from "./tree_data_providers/entry";

Expand Down Expand Up @@ -48,67 +48,69 @@ export class MainTreeDataProvider extends BaseTreeDataProvider {
return treeItem;
}

const item = this.config.getItem(name);

if (!item)
return treeItem;

// eslint-disable-next-line no-prototype-builtins
treeItem.description = this.makeTreeItemDescription(name);
treeItem.tooltip = this.makeTreeItemTooltip(name);
treeItem.description = this.makeTreeItemDescription(item);
treeItem.tooltip = this.makeTreeItemTooltip(item);

return treeItem;
}

makeTreeItemDescription(name: string): string {
makeTreeItemDescription(item: ConfigItem): string {
let str = "";

const [environment, environment_icon] = this.config.getEnvironment(name);

if (environment) {
if (environment_icon)
str += `${environment_icon} `;
if (item.environment) {
if (item.environment_icon)
str += `${item.environment_icon} `;
else
str += `${environment} `;
str += `${item.environment} `;
}

const [type, type_icon] = this.config.getType(name);

if (type) {
if (type_icon)
str += `${type_icon} `;
if (item.type) {
if (item.type_icon)
str += `${item.type_icon} `;
else
str += `${type} `;
str += `${item.type} `;
}

const label = this.config.getLabel(name);

if (this.searchText && label.toLowerCase().includes(this.searchText))
str += `${label} 🔍`;
else
str += `${label}`;
str += `${item.label}`;

if (this.searchText) {
if (item.label && item.label.toLowerCase().includes(this.searchText))
str += ` 🔍`;
else if (item.description && item.description.toLowerCase().includes(this.searchText))
str += ` 🔍`;
}

return str;
}

makeTreeItemTooltip(name: string): vscode.MarkdownString {
makeTreeItemTooltip(item: ConfigItem): vscode.MarkdownString {
const md = new vscode.MarkdownString();

const label = this.config.getLabel(name);
const description = this.config.getDescription(name);

md.appendMarkdown(`**${label}**`);

const [environment, environment_icon] = this.config.getEnvironment(name);
md.appendMarkdown(`**${item.label}**`);

if (environment) {
md.appendMarkdown(` [${environment_icon} · ${environment}]`);
if (item.environment) {
if (item.environment_icon)
md.appendMarkdown(` [${item.environment_icon} · ${item.environment}]`);
else
md.appendMarkdown(` [${item.environment}]`);
}

const [type, type_icon] = this.config.getType(name);

if (type) {
md.appendMarkdown(` [${type_icon} · ${type}]`);
if (item.type) {
if (item.type_icon)
md.appendMarkdown(` [${item.type_icon} · ${item.type}]`);
else
md.appendMarkdown(` [${item.type}]`);
}

if (description) {
if (item.description) {
md.appendText("\n\n");
md.appendCodeblock(description);
md.appendCodeblock(item.description);
}

return md;
Expand Down

0 comments on commit 109c0d4

Please sign in to comment.