Skip to content

Commit

Permalink
treePath can be string[]
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Jun 18, 2023
1 parent cd1e4c3 commit 2a8f388
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 33 deletions.
66 changes: 44 additions & 22 deletions denops/ddu/ddu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
Previewer,
SourceInfo,
SourceOptions,
TreePath,
UiOptions,
UserOptions,
UserSource,
Expand Down Expand Up @@ -96,8 +97,8 @@ export class Ddu {
private cancelToRefresh = false;
private redrawLock = new Lock(0);
private startTime = 0;
private expandedPaths = new Set<string>();
private searchPath = "";
private expandedPaths = new Set<string[]>();
private searchPath: TreePath = "";

constructor(loader: Loader) {
this.loader = loader;
Expand Down Expand Up @@ -410,7 +411,7 @@ export class Ddu {
] as string
: item.word;
if (item.isExpanded && item.treePath) {
this.expandedPaths.add(item.treePath);
this.expandedPaths.add(convertTreePath(item.treePath));
}
return {
...item,
Expand All @@ -421,7 +422,7 @@ export class Ddu {
__level: (level ?? 0) + (item.level ?? 0),
__expanded: Boolean(
item.treePath &&
this.isExpanded(item.treePath),
this.isExpanded(convertTreePath(item.treePath)),
),
};
}
Expand Down Expand Up @@ -517,7 +518,7 @@ export class Ddu {
if (restoreItemState) {
items.forEach((item) => {
if (item.treePath) {
item.__expanded = this.isExpanded(item.treePath);
item.__expanded = this.isExpanded(convertTreePath(item.treePath));
}
});
}
Expand Down Expand Up @@ -566,7 +567,10 @@ export class Ddu {
}
if (
!searchTargetItem && item.treePath &&
isParentPath(item.treePath, searchPath)
isParentPath(
convertTreePath(item.treePath),
convertTreePath(searchPath),
)
) {
searchTargetItem = await this.expandItem(
denops,
Expand Down Expand Up @@ -953,7 +957,7 @@ export class Ddu {
}

let flags = ActionFlags.None;
let searchPath = "";
let searchPath: TreePath = "";
if (typeof (ret) === "object") {
flags = ret.flags;
searchPath = ret.searchPath;
Expand Down Expand Up @@ -1060,7 +1064,7 @@ export class Ddu {
preventRedraw?: boolean;
} | {
// Expand recursively to find the `search` path
search: string;
search: TreePath;
maxLevel: number;
preventRedraw?: boolean;
},
Expand All @@ -1077,7 +1081,7 @@ export class Ddu {
source,
);

this.setExpanded(parent.treePath);
this.setExpanded(convertTreePath(parent.treePath));
parent.__expanded = true;

// Set path
Expand Down Expand Up @@ -1146,14 +1150,17 @@ export class Ddu {
// Expand recursively to find the `search` path
child.__expanded ||
child.isTree && child.treePath &&
isParentPath(child.treePath, options.search)
isParentPath(
convertTreePath(child.treePath),
convertTreePath(options.search),
)
)
: children.filter((child) =>
// Expand recursively to the maxLevel
child.__expanded ||
child.isTree && child.treePath &&
// NOTE: Skip hidden directory
!basename(child.treePath).startsWith(".")
!basename(treePath2Filename(child.treePath)).startsWith(".")
);

if (expandTargetChildren.length > 0) {
Expand Down Expand Up @@ -1187,7 +1194,10 @@ export class Ddu {
if (
"search" in options &&
!searchedItem && parent.treePath &&
isParentPath(parent.treePath, options.search)
isParentPath(
convertTreePath(parent.treePath),
convertTreePath(options.search),
)
) {
searchedItem = children.find((item) =>
options.search === item.treePath ?? item.word
Expand Down Expand Up @@ -1241,7 +1251,7 @@ export class Ddu {
continue;
}

this.setUnexpanded(item.treePath);
this.setUnexpanded(convertTreePath(item.treePath));
item.__expanded = false;
await this.callColumns(denops, sourceOptions.columns, [item]);

Expand Down Expand Up @@ -1716,19 +1726,19 @@ export class Ddu {
}

private isExpanded(
itemTreePath: string,
itemTreePath: string[],
): boolean {
return Boolean(
this.expandedPaths.has(itemTreePath),
);
}
private setExpanded(
itemTreePath: string,
itemTreePath: string[],
): void {
this.expandedPaths.add(itemTreePath);
}
private setUnexpanded(
itemTreePath: string,
itemTreePath: string[],
): void {
[...this.expandedPaths].forEach((v) => {
if (v === itemTreePath || isParentPath(itemTreePath, v)) {
Expand Down Expand Up @@ -2064,8 +2074,17 @@ async function globpath(
return paths;
}

function isParentPath(checkPath: string, searchPath: string) {
return checkPath !== searchPath && searchPath.startsWith(checkPath + pathsep);
function convertTreePath(treePath: TreePath) {
return typeof treePath === "string" ? treePath.split(pathsep) : treePath;
}

function treePath2Filename(treePath: TreePath) {
return typeof treePath === "string" ? treePath : treePath.join(pathsep);
}

function isParentPath(checkPath: string[], searchPath: string[]) {
return checkPath !== searchPath &&
searchPath.join(pathsep).startsWith(checkPath.join(pathsep) + pathsep);
}

Deno.test("sourceArgs", () => {
Expand Down Expand Up @@ -2126,13 +2145,16 @@ Deno.test("sourceArgs", () => {
});

Deno.test("isParentPath", () => {
assertEquals(true, isParentPath("/home", "/home/string"));
assertEquals(
true,
isParentPath("/home".split("/"), "/home/string".split("/")),
);
assertEquals(
true,
isParentPath(
"/home/shougo/work/ddu.vim",
"/home/shougo/work/ddu.vim/denops/ddu/deps.ts",
"/home/shougo/work/ddu.vim".split("/"),
"/home/shougo/work/ddu.vim/denops/ddu/deps.ts".split("/"),
),
);
assertEquals(false, isParentPath("hoge", "/home"));
assertEquals(false, isParentPath("hoge".split("/"), "/home".split("/")));
});
18 changes: 10 additions & 8 deletions denops/ddu/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ export type DduEvent = "close" | "cancel";

export type SourceName = string;

export type TreePath = string | string[];

export type Context = {
bufName: string;
bufNr: number;
done: boolean;
input: string;
maxItems: number;
mode: string;
path: string;
pathHistories: string[];
path: TreePath;
pathHistories: TreePath[];
winId: number;
};

Expand All @@ -54,7 +56,7 @@ export type UserSource = {
export type SourceInfo = {
name: string;
index: number;
path: string;
path: TreePath;
kind: string;
};

Expand All @@ -74,7 +76,7 @@ export type DduOptions = {
push: boolean;
refresh: boolean;
resume: boolean;
searchPath: string;
searchPath: TreePath;
sourceOptions: Record<SourceName, Partial<SourceOptions>>;
sourceParams: Record<SourceName, Partial<BaseSourceParams>>;
sources: UserSource[];
Expand Down Expand Up @@ -112,7 +114,7 @@ export type SourceOptions = {
matcherKey: string;
matchers: string[];
maxItems: number;
path: string;
path: TreePath;
sorters: string[];
volatile: boolean;
};
Expand Down Expand Up @@ -166,7 +168,7 @@ export type Item<
status?: ItemStatus;
kind?: string;
level?: number;
treePath?: string;
treePath?: TreePath;
isExpanded?: boolean;
isTree?: boolean;
};
Expand All @@ -187,7 +189,7 @@ export type DduItem =
export type ExpandItem = {
item: DduItem;
maxLevel?: number;
search?: string;
search?: TreePath;
};

export type ActionArguments<Params extends BaseActionParams> = {
Expand Down Expand Up @@ -219,7 +221,7 @@ export enum ActionFlags {

export type ActionResult = {
flags: ActionFlags;
searchPath: string;
searchPath: TreePath;
};

/**
Expand Down
6 changes: 3 additions & 3 deletions doc/ddu.txt
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ resume (boolean)
Default: v:false

*ddu-option-searchPath*
searchPath (string)
searchPath (string | string[])
Search the path. This only works when specified |ddu#start()|
or |ddu#redraw()|.
NOTE: The path must be absolute path.
Expand Down Expand Up @@ -674,7 +674,7 @@ maxItems (number)
Default: 10000

*ddu-source-option-path*
path (string)
path (string | string[])
Specify an initial narrowing path.
NOTE: It must be full path.

Expand Down Expand Up @@ -1033,7 +1033,7 @@ status (object) (Optional)
The item time.

*ddu-item-attribute-treePath*
treePath (string) (Optional)
treePath (string | string[]) (Optional)
The item tree path.
NOTE: The attribute must be set to support tree feature.

Expand Down

0 comments on commit 2a8f388

Please sign in to comment.