From 109c0d4f9e6536f365537b636fdeec94bce9130e Mon Sep 17 00:00:00 2001 From: Fede Calendino Date: Wed, 20 Jul 2022 11:53:12 +0200 Subject: [PATCH] Add ConfigItem class and auto collapse on search --- src/config.ts | 77 +++++++++++++++++++-------------------- src/file_explorer.ts | 35 +++++++++--------- src/tree_data_provider.ts | 76 +++++++++++++++++++------------------- 3 files changed, 94 insertions(+), 94 deletions(-) diff --git a/src/config.ts b/src/config.ts index bf04963..1aa5d74 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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; @@ -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 { - 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 { - 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 { @@ -95,5 +95,4 @@ export class Config { return vscode.Uri.file(config_file_path); } - } \ No newline at end of file diff --git a/src/file_explorer.ts b/src/file_explorer.ts index cfb7ec8..a387ee8 100644 --- a/src/file_explorer.ts +++ b/src/file_explorer.ts @@ -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; @@ -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)) { @@ -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; diff --git a/src/tree_data_provider.ts b/src/tree_data_provider.ts index 43dd5e0..d508578 100644 --- a/src/tree_data_provider.ts +++ b/src/tree_data_provider.ts @@ -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"; @@ -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;