diff --git a/.vscode/launch.json b/.vscode/launch.json index f519778..a9df054 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,6 +13,7 @@ "args": [ "--extensionDevelopmentPath=${workspaceFolder}" ], + "sourceMaps": true, "outFiles": [ "${workspaceFolder}/out/**/*.js" ], diff --git a/CHANGELOG.md b/CHANGELOG.md index dd6226f..de8f7ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Change log +**0.25.0** + +- [#151](https://github.com/moshfeu/vscode-compare-folders/issues/151) - View as list like source control (Thanks [@michaelmesser](https://github.com/michaelmesser)) + **0.24.0** - [#2](https://github.com/moshfeu/vscode-compare-folders/issues/2) - Allow to remove file from the diff tree diff --git a/README.md b/README.md index d6e1fd6..890a043 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ The extension allows you to compare folders, show the diffs in a list and presen ![Screen record](https://user-images.githubusercontent.com/3723951/92312411-f1f3b600-efc8-11ea-93b8-e90a3e25e9cb.gif) -### Thanks +## Thanks The extension uses the great [`dir-compare`](https://github.com/gliviu/dir-compare) package made by [Liviu Grigorescu](https://github.com/gliviu). If you like this extension, please give a star to `dir-compare`! @@ -22,7 +22,7 @@ Please take this under consideration. There are several ways to choose folders to compare: -- Command Palette - +- Command Palette - `Compare a folder against workspace` choose a folder to compare against the workspace's folder (when workspace is not empty) - `Choose 2 folders and compare` - opens OS folder chooser twice - Compare Folders Panel (Click on the icon in the Activity Bar) @@ -34,7 +34,23 @@ There are several ways to choose folders to compare: - `Pick from recent compares` chose one of the pairs you compared in the past. - (The list can be reset by `Clear recent compares`) -`Refresh` - If there are changes in the compared folders' files, click on the `refresh` button will re-compare the folders. +## UI Features per Panel Section: + +### Differences +- Toolbar + - `Pick from History`: Choose one of the pairs you compared in the past. + - `Swap Sides`: Swap the sides of the compared folders. + - `Refresh`: Re-compare the folders if there are changes in the compared folders' files. + - `View as List / View as Tree`: Toggle between list and tree view modes. +- Files + - `Dismiss`: Remove the file from the list of compared items. + - `Take My File / Take Compared File`: Replace the file with the one from the other side. + +### Only in My Folder / Only in Compared Folder +- Files + - `Copy to My Folder / Copy to Compared Folder`: Copy the file to the other side. + - `Delete`: Permanently delete the file from the file system. + ## Options (under vscode settings) @@ -58,18 +74,18 @@ There are several ways to choose folders to compare: - `useDiffMerge` - boolean - Whether or not using [`Diff & Merge`](https://marketplace.visualstudio.com/items?itemName=moshfeu.diff-merge) extension as the Diff viewer. In order to use this option, you should install the extension - `folderLeft` + `folderRight` - strings - paths of 2 folders, compare them on the extension load. - `ignoreExtension` - Set groups of extensions so while comparing, pairs of files considered the same file if both of the files extension are in the same group. **Note:** each extension can show **only once** it this list -- `ignoreLineEnding` - boolean - ignore crlf/lf line ending differences -- `ignoreWhiteSpaces` - boolean - ignore white spaces at the beginning and ending of a line (similar to `diff -b`) -- `ignoreAllWhiteSpaces` - boolean - ignore all white space differences (similar to `diff -w`) -- `ignoreEmptyLines` - boolean - ignore differences caused by empty lines (similar to `diff -B`) -- `respectGitIgnore` - boolean - include / exclude files based on .gitignore - this option works together with `includeFilter` and `excludeFilter` options. ⚠️ The extension supports the main basic gitignore rules. For instance, it supports negation (`!`), but it doesn't support .gitignore files in subfolders. If there is an important use case that is not supported, please open an issue. - ***Example*** ```json "compareFolders.ignoreExtension": [ ["php", "cs"] ] ``` +- `ignoreLineEnding` - boolean - ignore crlf/lf line ending differences +- `ignoreWhiteSpaces` - boolean - ignore white spaces at the beginning and ending of a line (similar to `diff -b`) +- `ignoreAllWhiteSpaces` - boolean - ignore all white space differences (similar to `diff -w`) +- `ignoreEmptyLines` - boolean - ignore differences caused by empty lines (similar to `diff -B`) +- `respectGitIgnore` - boolean - include / exclude files based on .gitignore - this option works together with `includeFilter` and `excludeFilter` options. ⚠️ The extension supports the main basic gitignore rules. For instance, it supports negation (`!`), but it doesn't support .gitignore files in subfolders. If there is an important use case that is not supported, please open an issue. +- `defaultDiffViewMode` - One of the options: "tree" or "list". The default view mode when vscode is opened. Later it can be toggled by the "View as list / View as tree" button ## CLI @@ -79,10 +95,6 @@ By calling the following command from the terminal / command line, the extension COMPARE_FOLDERS=DIFF code path/to/folder1 path/to/folder2 ``` -### Read more about the development process - -You can find the series of the posts about how this extension developed step by step in [medium.com](https://medium.com/@moshfeu/comparefolders-visual-studio-code-extension-journey-intro-b540a0539629?source=friends_link&sk=db37e1889766ccd8fe553958a12a8f69). - ## Contributors @@ -95,6 +107,38 @@ You can find the series of the posts about how this extension developed step by + + + +
+ + md2perpe +
+ Per Persson +
+
+
+ + orellabac +
+ orellabac +
+
+
+ + Niccolò Mineo +
+ Niccolò Mineo +
+
+
+ + Sam Pellino +
+ Sam Pellino +
+
+
@@ -117,3 +161,7 @@ You can find it [here](/CHANGELOG.md) Activity bar's icon (and more) by [Stockio.com](https://www.stockio.com/free-icon/folders) Null icon made from Icon Fonts is licensed by CC BY 3.0 External link icon made by Dave Gandy from www.flaticon.com + +## Read more about the development process + +You can find the series of the posts about how this extension developed step by step in [medium.com](https://medium.com/@moshfeu/comparefolders-visual-studio-code-extension-journey-intro-b540a0539629?source=friends_link&sk=db37e1889766ccd8fe553958a12a8f69). diff --git a/package.json b/package.json index a56081a..9e86c56 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "compare-folders", "displayName": "Compare Folders", "description": "Compare folders by contents, present the files that have differences and display the diffs side by side", - "version": "0.24.3", + "version": "0.25.0", "repository": { "type": "git", "url": "https://github.com/moshfeu/vscode-compare-folders" @@ -148,6 +148,19 @@ "markdownDescription": "Specifies which file will be in the left and the right sides of the diff view", "scope": "window" }, + "compareFolders.defaultDiffViewMode": { + "type": "string", + "default": "tree", + "enum": [ + "tree", + "list" + ], + "enumDescriptions": [ + "Presents files and folders in a hierarchical tree structure, allowing for a more organized and nested view", + "Displays files and folders in a simple, flat list format" + ], + "scope": "window" + }, "compareFolders.showIdentical": { "type": "boolean", "default": false, @@ -212,6 +225,16 @@ } ], "view/title": [ + { + "command": "foldersCompare.viewAsList", + "group": "navigation@1", + "when": "view == foldersCompareAppService && foldersCompareContext.hasFolders && foldersCompare.diffViewMode == 'tree'" + }, + { + "command": "foldersCompare.viewAsTree", + "group": "navigation@1", + "when": "view == foldersCompareAppService && foldersCompareContext.hasFolders && foldersCompare.diffViewMode == 'list'" + }, { "command": "foldersCompare.refresh", "group": "navigation@2", @@ -219,12 +242,12 @@ }, { "command": "foldersCompare.swap", - "group": "navigation@1", + "group": "navigation@3", "when": "view == foldersCompareAppService && foldersCompareContext.hasFolders" }, { "command": "foldersCompare.pickFromRecentCompares", - "group": "navigation@3", + "group": "navigation@4", "when": "view == foldersCompareAppService" } ], @@ -268,12 +291,13 @@ }, "commands": [ { - "title": "Compare Folders: Pick from recent compares", + "title": "Pick from recent compares", "command": "foldersCompare.pickFromRecentCompares", "icon": { "dark": "resources/dark/history.svg", "light": "resources/light/history.svg" - } + }, + "category": "CompareFolders" }, { "title": "Compare Folders: Clear recent compares", @@ -322,7 +346,8 @@ "icon": { "light": "resources/light/refresh.svg", "dark": "resources/dark/refresh.svg" - } + }, + "category": "CompareFolders" }, { "title": "Swap Sides", @@ -330,7 +355,20 @@ "icon": { "light": "resources/light/swap.svg", "dark": "resources/dark/swap.svg" - } + }, + "category": "CompareFolders" + }, + { + "title": "View as list", + "command": "foldersCompare.viewAsList", + "icon": "$(list-selection)", + "category": "CompareFolders" + }, + { + "title": "View as tree", + "command": "foldersCompare.viewAsTree", + "icon": "$(list-tree)", + "category": "CompareFolders" }, { "command": "foldersCompare.compareSelectedFolders", diff --git a/src/constants/commands.ts b/src/constants/commands.ts index 3892d27..4d56ba3 100644 --- a/src/constants/commands.ts +++ b/src/constants/commands.ts @@ -5,6 +5,8 @@ export const COMPARE_FOLDERS_AGAINST_EACH_OTHER = 'foldersCompare.compareFolders export const COMPARE_SELECTED_FOLDERS = 'foldersCompare.compareSelectedFolders'; export const REFRESH = 'foldersCompare.refresh'; export const SWAP = 'foldersCompare.swap'; +export const VIEW_AS_LIST = 'foldersCompare.viewAsList'; +export const VIEW_AS_TREE = 'foldersCompare.viewAsTree'; export const COPY_TO_COMPARED = 'foldersCompare.copyToCompared'; export const COPY_TO_MY = 'foldersCompare.copyToMy'; export const TAKE_MY_FILE = 'foldersCompare.takeMyFile'; diff --git a/src/constants/contextKeys.ts b/src/constants/contextKeys.ts index 850f2a3..ea7d653 100644 --- a/src/constants/contextKeys.ts +++ b/src/constants/contextKeys.ts @@ -1 +1,2 @@ -export const HAS_FOLDERS = 'foldersCompareContext.hasFolders'; \ No newline at end of file +export const HAS_FOLDERS = 'foldersCompareContext.hasFolders' as const; +export const FILES_VIEW_MODE = 'foldersCompare.diffViewMode' as const; \ No newline at end of file diff --git a/src/context/global.ts b/src/context/global.ts index 3aecab2..36d43f7 100644 --- a/src/context/global.ts +++ b/src/context/global.ts @@ -1,5 +1,8 @@ import { commands } from 'vscode'; +import { HAS_FOLDERS, type FILES_VIEW_MODE } from '../constants/contextKeys'; -export const setContext = (key: string, value: string | boolean): void => { +type ContextKey = typeof HAS_FOLDERS | typeof FILES_VIEW_MODE; + +export const setContext = (key: ContextKey, value: string | boolean): void => { commands.executeCommand('setContext', key, value); }; \ No newline at end of file diff --git a/src/context/ui.ts b/src/context/ui.ts new file mode 100644 index 0000000..bff947e --- /dev/null +++ b/src/context/ui.ts @@ -0,0 +1,31 @@ +import { getConfiguration } from '../services/configuration'; +import { setContext } from './global'; + +export type DiffViewMode = 'tree' | 'list'; + +class UIContext { + private _diffViewMode?: DiffViewMode; + + init() { + this.updateFromConfiguration(); + } + + updateFromConfiguration() { + // don't update from configuration if diffViewMode is already set + if (this._diffViewMode) { + return; + } + this.diffViewMode = getConfiguration('defaultDiffViewMode'); + } + + set diffViewMode(mode: DiffViewMode) { + setContext('foldersCompare.diffViewMode', mode); + this._diffViewMode = mode; + } + + get diffViewMode(): DiffViewMode { + return this._diffViewMode || getConfiguration('defaultDiffViewMode'); + } +} + +export const uiContext = new UIContext(); \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index 37e614a..abede4c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,15 +1,17 @@ import { window, commands, ExtensionContext, workspace, Uri, version} from 'vscode'; import { CompareFoldersProvider } from './providers/foldersCompareProvider'; -import { COMPARE_FILES, CHOOSE_FOLDERS_AND_COMPARE, REFRESH, COMPARE_FOLDERS_AGAINST_EACH_OTHER, COMPARE_FOLDERS_AGAINST_WORKSPACE, COMPARE_SELECTED_FOLDERS, SWAP, COPY_TO_COMPARED, COPY_TO_MY, TAKE_MY_FILE, TAKE_COMPARED_FILE, DELETE_FILE, PICK_FROM_RECENT_COMPARES, CLEAR_RECENT_COMPARES, DISMISS_DIFFERENCE } from './constants/commands'; +import { COMPARE_FILES, CHOOSE_FOLDERS_AND_COMPARE, REFRESH, COMPARE_FOLDERS_AGAINST_EACH_OTHER, COMPARE_FOLDERS_AGAINST_WORKSPACE, COMPARE_SELECTED_FOLDERS, SWAP, COPY_TO_COMPARED, COPY_TO_MY, TAKE_MY_FILE, TAKE_COMPARED_FILE, DELETE_FILE, PICK_FROM_RECENT_COMPARES, CLEAR_RECENT_COMPARES, DISMISS_DIFFERENCE, VIEW_AS_LIST, VIEW_AS_TREE } from './constants/commands'; import { ViewOnlyProvider } from './providers/viewOnlyProvider'; import { globalState } from './services/globalState'; import { pickFromRecents } from './services/pickFromRecentCompares'; import { getConfiguration } from './services/configuration'; import { showDoneableInfo } from './utils/ui'; import { validate } from './services/ignoreExtensionTools'; +import { uiContext } from './context/ui'; export async function activate(context: ExtensionContext) { globalState.init(context); + uiContext.init(); const onlyInA = new ViewOnlyProvider(); const onlyInB = new ViewOnlyProvider(); const identicals = new ViewOnlyProvider(false); @@ -27,6 +29,8 @@ export async function activate(context: ExtensionContext) { commands.registerCommand(DISMISS_DIFFERENCE, foldersCompareProvider.dismissDifference), commands.registerCommand(REFRESH, foldersCompareProvider.refresh), commands.registerCommand(SWAP, foldersCompareProvider.swap), + commands.registerCommand(VIEW_AS_LIST, foldersCompareProvider.viewAs('list')), + commands.registerCommand(VIEW_AS_TREE, foldersCompareProvider.viewAs('tree')), commands.registerCommand(COPY_TO_COMPARED, foldersCompareProvider.copyToCompared), commands.registerCommand(COPY_TO_MY, foldersCompareProvider.copyToMy), commands.registerCommand(TAKE_MY_FILE, foldersCompareProvider.takeMyFile), diff --git a/src/models/file.ts b/src/models/file.ts index 76a6fff..2aafd47 100644 --- a/src/models/file.ts +++ b/src/models/file.ts @@ -16,12 +16,14 @@ export class File extends TreeItem { public readonly collapsibleState?: TreeItemCollapsibleState, public readonly command?: TreeItemCommand, public readonly children?: File[], - public resourceUri?: Uri + public resourceUri?: Uri, + public readonly description?: TreeItem['description'], + public readonly tooltip?: string, ) { super(label, collapsibleState); try { - this.tooltip = this.label; + this.tooltip ??= this.resourceUri?.fsPath || this.label; this.resourceUri = this.resourceUri || ( this.hasIcon ? undefined : diff --git a/src/providers/foldersCompareProvider.ts b/src/providers/foldersCompareProvider.ts index 7a77cab..95c789b 100644 --- a/src/providers/foldersCompareProvider.ts +++ b/src/providers/foldersCompareProvider.ts @@ -25,7 +25,7 @@ import { import { File } from '../models/file'; import { build } from '../services/treeBuilder'; import { pathContext } from '../context/path'; -import { getRelativePath } from '../utils/path'; +import { getLocalPath } from '../utils/path'; import { ViewOnlyProvider } from './viewOnlyProvider'; import { getConfiguration } from '../services/configuration'; import { setContext } from '../context/global'; @@ -33,6 +33,7 @@ import { HAS_FOLDERS } from '../constants/contextKeys'; import { log } from '../services/logger'; import { showErrorMessageWithMoreInfo, showInfoMessageWithTimeout } from '../utils/ui'; import { showUnaccessibleWarning } from '../services/validators'; +import { uiContext, type DiffViewMode } from '../context/ui'; export class CompareFoldersProvider implements TreeDataProvider { private _onDidChangeTreeData: EventEmitter = new EventEmitter(); @@ -195,14 +196,14 @@ export class CompareFoldersProvider implements TreeDataProvider { }); } - refresh = async (resetIgnoredFiles = true) => { + refresh = async (resetIgnoredFiles = true, shouldShowInfoMessage = true) => { if (resetIgnoredFiles) { this.ignoreDifferencesList.clear(); } try { this._diffs = (await compareFolders()); this.filterIgnoredFromDiffs(); - if (this._diffs.hasResult()) { + if (shouldShowInfoMessage && this._diffs.hasResult()) { showInfoMessageWithTimeout('Source Refreshed'); } this.updateUI(); @@ -216,6 +217,11 @@ export class CompareFoldersProvider implements TreeDataProvider { this.refresh(false); }; + viewAs = (mode: DiffViewMode) => () => { + uiContext.diffViewMode = mode; + this.refresh(false, false); + } + copyToCompared = (e: TreeItem) => { this.copyToFolder(e.resourceUri!, 'to-compared'); }; @@ -284,7 +290,7 @@ export class CompareFoldersProvider implements TreeDataProvider { getFolderName(filePath: string, basePath: string) { const base = basePath ? `${this.workspaceRoot}/${basePath}` : this.workspaceRoot; - return path.basename(path.dirname(getRelativePath(filePath, base))); + return path.basename(path.dirname(getLocalPath(filePath, base))); } getChildren(element?: File): File[] { diff --git a/src/providers/viewOnlyProvider.ts b/src/providers/viewOnlyProvider.ts index b08b685..1b91983 100644 --- a/src/providers/viewOnlyProvider.ts +++ b/src/providers/viewOnlyProvider.ts @@ -1,16 +1,17 @@ import { TreeDataProvider, TreeItem, EventEmitter, Event, Uri, TreeItemCollapsibleState } from 'vscode'; import { File } from '../models/file'; import { build } from '../services/treeBuilder'; +import type { ViewOnlyPaths } from '../types'; export class ViewOnlyProvider implements TreeDataProvider { private _onDidChangeTreeData: EventEmitter = new EventEmitter(); readonly onDidChangeTreeData: Event = this._onDidChangeTreeData.event; - private diffs: string[][] = []; + private diffs: ViewOnlyPaths = []; private rootPath: string = ''; constructor(private showPath = true) {} - update(diffs: string[][], rootPath: string) { + update(diffs: ViewOnlyPaths, rootPath: string) { this.diffs = diffs; this.rootPath = rootPath; this._onDidChangeTreeData.fire(null); diff --git a/src/services/comparer.ts b/src/services/comparer.ts index 82fbc99..cdedb99 100644 --- a/src/services/comparer.ts +++ b/src/services/comparer.ts @@ -5,7 +5,7 @@ import * as path from 'path'; import { DiffViewTitle, getConfiguration } from './configuration'; import { pathContext } from '../context/path'; import { compareIgnoredExtension, compareName, validate } from './ignoreExtensionTools'; -import { CompareOptions } from '../types'; +import { CompareOptions, type DiffPathss, type ViewOnlyPaths } from '../types'; import { log, printOptions, printResult } from './logger'; import { showErrorMessage } from '../utils/ui'; import { validatePermissions } from './validators'; @@ -133,20 +133,20 @@ export async function compareFolders(): Promise { const { diffSet = [] } = res; // diffSet contains all the files and filter only the not equals files and map them to pairs of Uris - const distinct = diffSet + const distinct: DiffPathss = diffSet .filter((diff) => diff.state === 'distinct') .map((diff) => [path.join(diff.path1!, diff.name1!), path.join(diff.path2!, diff.name2!)]); // readable 👍 performance 👎 - const left = diffSet + const left: ViewOnlyPaths = diffSet .filter((diff) => diff.state === 'left' && diff.type1 === 'file') .map((diff) => [buildPath(diff, '1')]); - const right = diffSet + const right: ViewOnlyPaths = diffSet .filter((diff) => diff.state === 'right' && diff.type2 === 'file') .map((diff) => [buildPath(diff, '2')]); - const identicals = showIdentical + const identicals: ViewOnlyPaths = showIdentical ? diffSet .filter((diff) => diff.state === 'equal' && diff.type1 === 'file') .map((diff) => [buildPath(diff, '1')]) @@ -183,10 +183,10 @@ function buildPath(diff: Difference, side: '1' | '2') { export class CompareResult { constructor( - public distinct: string[][], - public left: string[][], - public right: string[][], - public identicals: string[][], + public distinct: DiffPathss, + public left: ViewOnlyPaths, + public right: ViewOnlyPaths, + public identicals: ViewOnlyPaths, public unaccessibles: string[], public leftPath: string, public rightPath: string diff --git a/src/services/configuration.ts b/src/services/configuration.ts index 6271c44..8645eb9 100644 --- a/src/services/configuration.ts +++ b/src/services/configuration.ts @@ -1,4 +1,5 @@ import { workspace } from 'vscode'; +import type { DiffViewMode } from '../context/ui'; export type DiffViewTitle = 'name only' | 'compared path' | 'full path'; @@ -20,6 +21,7 @@ export interface IConfigurations { ignoreEmptyLines: boolean; ignoreLineEnding: boolean; respectGitIgnore: boolean; + defaultDiffViewMode: DiffViewMode; } type ConfigurationItem = keyof IConfigurations; diff --git a/src/services/treeBuilder.ts b/src/services/treeBuilder.ts index 5c2f5f7..e91bb12 100644 --- a/src/services/treeBuilder.ts +++ b/src/services/treeBuilder.ts @@ -4,14 +4,23 @@ import get from 'lodash/get'; import { COMPARE_FILES } from '../constants/commands'; import { File } from '../models/file'; import * as path from 'path'; -import { log } from 'util'; +import { uiContext } from '../context/ui'; +import type { DiffPaths, DiffPathss, ViewOnlyPaths } from '../types'; +import { log } from './logger'; type TreeNode = { path: string; [key: string]: TreeNode | [[string, string], string] | string; }; -export function build(paths: string[][], basePath: string) { +export function build(paths: DiffPathss | ViewOnlyPaths, basePath: string) { + if (uiContext.diffViewMode === 'list') { + return { + tree: {}, + treeItems: createList(paths, basePath), + } + } + const tree = {} as TreeNode; try { paths.forEach((filePath) => { @@ -39,6 +48,31 @@ export function build(paths: string[][], basePath: string) { } } +function createList(paths: DiffPathss | ViewOnlyPaths, basePath: string): File[] { + try { + return paths.map(([path1, path2]) => { + const relativePath = path.relative(basePath, path1); + const fileName = path.basename(path1); + return new File( + fileName, + 'file', + TreeItemCollapsibleState.None, + { + title: path1, + command: COMPARE_FILES, + arguments: [[path1, path2 || ''], relativePath], + }, + undefined, + Uri.file(path1), + true, + ) + }); + } catch (error) { + log(`can't create list`, error); + return []; + } +} + function createHierarchy(src: TreeNode): File[] { const children = (Object.entries(src) as Array<[string, TreeNode]>).reduce( (prev, [key, childrenOrFileData]) => { @@ -56,13 +90,20 @@ function createHierarchy(src: TreeNode): File[] { ) ); } else { - const [paths, relativePath] = (childrenOrFileData as unknown) as [[string, string], string]; + const [paths, relativePath] = (childrenOrFileData as unknown) as [DiffPaths, string]; prev.push( - new File(key, 'file', TreeItemCollapsibleState.None, { - title: key, - command: COMPARE_FILES, - arguments: [paths, relativePath], - }) + new File( + key, + 'file', + TreeItemCollapsibleState.None, + { + title: key, + command: COMPARE_FILES, + arguments: [paths, relativePath], + }, + undefined, + Uri.file(paths[0]) + ) ); } return prev; diff --git a/src/test/suite/treeBuilder.test.ts b/src/test/suite/treeBuilder.test.ts index 47219ee..0dbe508 100644 --- a/src/test/suite/treeBuilder.test.ts +++ b/src/test/suite/treeBuilder.test.ts @@ -4,10 +4,12 @@ import { File } from '../../models/file'; import { TreeItemCollapsibleState, Uri } from 'vscode'; import { COMPARE_FILES } from '../../constants/commands'; import * as path from 'path'; +import type { DiffPathss } from '../../types'; +import { uiContext } from '../../context/ui'; suite('Tree Builder', () => { test('Generate tree with one file', () => { - const paths = [['/base/path/to/rootFolder/index.html', '/base/path/to/rootFolder1/index.html']]; + const paths: DiffPathss = [['/base/path/to/rootFolder/index.html', '/base/path/to/rootFolder1/index.html']]; const basePath = '/base/path/to/rootFolder'; assert.deepStrictEqual( @@ -22,7 +24,7 @@ suite('Tree Builder', () => { }); test('Generate tree with deep hierarchy', () => { - const paths = [['/base/path/to/rootFolder/folder1/folder2/index.html', '/base/path/to/rootFolder1/folder1/folder2/index.html']]; + const paths: DiffPathss = [['/base/path/to/rootFolder/folder1/folder2/index.html', '/base/path/to/rootFolder1/folder1/folder2/index.html']]; const basePath = '/base/path/to/rootFolder'; assert.deepStrictEqual( @@ -43,7 +45,7 @@ suite('Tree Builder', () => { }); test('Generte list of TreeView\'s', () => { - const paths = [['/base/path/to/rootFolder/folder1/folder2/index.html', '/base/path/to/rootFolder1/folder1/folder2/index.html']]; + const paths: DiffPathss = [['/base/path/to/rootFolder/folder1/folder2/index.html', '/base/path/to/rootFolder1/folder1/folder2/index.html']]; const basePath = '/base/path/to/rootFolder'; const { treeItems } = build(paths, basePath); @@ -57,10 +59,39 @@ suite('Tree Builder', () => { title: 'index.html', command: COMPARE_FILES, arguments: [paths[0], path.join('folder1/folder2/index.html')] - }, undefined, undefined) + }, undefined, Uri.parse(paths[0][0])) ], Uri.parse(path.join(basePath, 'folder1', 'folder2'))) ], Uri.parse(path.join(basePath, 'folder1'))) ] ); + }); + + test('Generte diffs as list', () => { + const paths: DiffPathss = [['/base/path/to/rootFolder/folder1/subfolder/index.html', '/base/path/to/rootFolder/folder2/subfolder/index.html']]; + const [path1, path2] = paths[0]; + const basePath = '/base/path/to/rootFolder'; + + uiContext.diffViewMode = 'list'; + const {tree, treeItems} = build(paths, basePath); + + assert.deepStrictEqual(tree, {}); + assert.deepStrictEqual( + treeItems, + [ + new File( + 'index.html', + 'file', + TreeItemCollapsibleState.None, + { + title: path1, + command: COMPARE_FILES, + arguments: [[path1, path2], path.join('folder1', 'subfolder', 'index.html')], + }, + undefined, + Uri.parse(path1), + true + ), + ] + ); }); }); diff --git a/src/types.ts b/src/types.ts index 896bb74..2263aaa 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,9 @@ import type { Options } from 'dir-compare'; +export type DiffPaths = [path1: string, path2: string]; +export type DiffPathss = DiffPaths[]; +export type ViewOnlyPaths = [path: string][]; + export type CompareOptions = Options & { ignoreExtension?: [[string, string]]; }; diff --git a/src/utils/path.ts b/src/utils/path.ts index d689291..f96916a 100644 --- a/src/utils/path.ts +++ b/src/utils/path.ts @@ -1,3 +1,3 @@ -export function getRelativePath(path: string, basePath: string): string { +export function getLocalPath(path: string, basePath: string): string { return path.replace(basePath, ''); } \ No newline at end of file