@@ -6,8 +6,10 @@ import { Context, Effect, Layer, Option, Schema } from "effect" |
6 | 6 | import { EventV2 } from "./event" |
7 | 7 | import { FSUtil } from "./fs-util" |
8 | 8 | import { Location } from "./location" |
9 | | -import { NonNegativeInt, PositiveInt, RelativePath } from "./schema" |
10 | | -import { Search } from "./filesystem/search" |
| 9 | +import { PositiveInt, RelativePath } from "./schema" |
| 10 | +import { FileSystemSearch } from "./filesystem/search" |
| 11 | +import { Entry, Match } from "./filesystem/schema" |
| 12 | +export { Entry, Match, Submatch } from "./filesystem/schema" |
11 | 13 |
|
12 | 14 | export const ReadInput = Schema.Struct({ |
13 | 15 | path: RelativePath, |
@@ -28,39 +30,23 @@ export const ListInput = Schema.Struct({ |
28 | 30 | }) |
29 | 31 | export type ListInput = typeof ListInput.Type |
30 | 32 |
|
31 | | -export class Entry extends Schema.Class<Entry>("FileSystem.Entry")({ |
32 | | - path: RelativePath, |
33 | | - uri: Schema.String, |
34 | | - type: Schema.Literals(["file", "directory"]), |
35 | | - mime: Schema.String, |
36 | | -}) {} |
37 | | - |
38 | | -export const FindInput = Schema.Struct({ |
| 33 | +export class FindInput extends Schema.Class<FindInput>("FileSystem.FindInput")({ |
39 | 34 | query: Schema.String, |
40 | 35 | type: Schema.Literals(["file", "directory"]).pipe(Schema.optional), |
41 | 36 | limit: PositiveInt.pipe(Schema.optional), |
42 | | -}) |
43 | | -export type FindInput = typeof FindInput.Type |
| 37 | +}) {} |
44 | 38 |
|
45 | | -export const GrepInput = Schema.Struct({ |
| 39 | +export class GlobInput extends Schema.Class<GlobInput>("FileSystem.GlobInput")({ |
46 | 40 | pattern: Schema.String, |
47 | | - include: Schema.String.pipe(Schema.optional), |
| 41 | + path: RelativePath.pipe(Schema.optional), |
48 | 42 | limit: PositiveInt.pipe(Schema.optional), |
49 | | -}) |
50 | | -export type GrepInput = typeof GrepInput.Type |
| 43 | +}) {} |
51 | 44 |
|
52 | | -export class GrepMatch extends Schema.Class<GrepMatch>("LocationFileSystem.GrepMatch")({ |
53 | | - path: RelativePath, |
54 | | - lines: Schema.String, |
55 | | - line: PositiveInt, |
56 | | - offset: NonNegativeInt, |
57 | | - submatches: Schema.Array( |
58 | | - Schema.Struct({ |
59 | | - text: Schema.String, |
60 | | - start: NonNegativeInt, |
61 | | - end: NonNegativeInt, |
62 | | - }), |
63 | | - ), |
| 45 | +export class GrepInput extends Schema.Class<GrepInput>("FileSystem.GrepInput")({ |
| 46 | + pattern: Schema.String, |
| 47 | + path: RelativePath.pipe(Schema.optional), |
| 48 | + include: Schema.String.pipe(Schema.optional), |
| 49 | + limit: PositiveInt.pipe(Schema.optional), |
64 | 50 | }) {} |
65 | 51 |
|
66 | 52 | export const Event = { |
@@ -76,17 +62,18 @@ export interface Interface { |
76 | 62 | readonly read: (input: ReadInput) => Effect.Effect<Content> |
77 | 63 | readonly list: (input?: ListInput) => Effect.Effect<Entry[]> |
78 | 64 | readonly find: (input: FindInput) => Effect.Effect<Entry[]> |
79 | | - readonly grep: (input: GrepInput) => Effect.Effect<GrepMatch[]> |
| 65 | + readonly glob: (input: GlobInput) => Effect.Effect<readonly Entry[]> |
| 66 | + readonly grep: (input: GrepInput) => Effect.Effect<readonly Match[]> |
80 | 67 | } |
81 | 68 |
|
82 | 69 | export class Service extends Context.Service<Service, Interface>()("@opencode/v2/FileSystem") {} |
83 | 70 |
|
84 | | -export const layer = Layer.effect( |
| 71 | +const baseLayer = Layer.effect( |
85 | 72 | Service, |
86 | 73 | Effect.gen(function* () { |
87 | 74 | const fs = yield* FSUtil.Service |
88 | 75 | const location = yield* Location.Service |
89 | | - const search = yield* Search.Service |
| 76 | + const search = yield* FileSystemSearch.Service |
90 | 77 | const root = yield* fs.realPath(location.directory).pipe(Effect.orDie) |
91 | 78 | const resolve = Effect.fnUntraced(function* (input?: RelativePath) { |
92 | 79 | const absolute = path.resolve(location.directory, input ?? ".") |
@@ -96,22 +83,10 @@ export const layer = Layer.effect( |
96 | 83 | if (!FSUtil.contains(root, real)) return yield* Effect.die(new Error("Path escapes the location")) |
97 | 84 | return { absolute, real, directory: location.directory, root } |
98 | 85 | }) |
99 | | - const entry = Effect.fnUntraced(function* (absolute: string, selected = { directory: location.directory, root }) { |
100 | | - const real = yield* fs.realPath(absolute).pipe(Effect.catch(() => Effect.void)) |
101 | | - if (!real) return |
102 | | - if (!FSUtil.contains(selected.root, real)) return |
103 | | - const info = yield* fs.stat(real).pipe(Effect.catch(() => Effect.void)) |
104 | | - const type = info?.type === "Directory" ? "directory" : info?.type === "File" ? "file" : undefined |
105 | | - if (!type) return |
106 | | - return new Entry({ |
107 | | - path: RelativePath.make(path.relative(selected.directory, absolute)), |
108 | | - uri: pathToFileURL(real).href, |
109 | | - type, |
110 | | - mime: type === "directory" ? "application/x-directory" : FSUtil.mimeType(real), |
111 | | - }) |
112 | | - }) |
113 | | - |
114 | 86 | return Service.of({ |
| 87 | + find: search.find, |
| 88 | + glob: search.glob, |
| 89 | + grep: search.grep, |
115 | 90 | read: Effect.fn("FileSystem.read")(function* (input) { |
116 | 91 | const target = yield* resolve(input.path) |
117 | 92 | const info = yield* fs.stat(target.real).pipe(Effect.orDie) |
@@ -145,62 +120,28 @@ export const layer = Layer.effect( |
145 | 120 | if (info.type !== "Directory") return yield* Effect.die(new Error("Path is not a directory")) |
146 | 121 | return yield* fs.readDirectoryEntries(target.real).pipe( |
147 | 122 | Effect.orDie, |
148 | | - Effect.flatMap((items) => |
149 | | - Effect.forEach(items, (item) => entry(path.join(target.absolute, item.name), target), { |
150 | | - concurrency: "unbounded", |
151 | | - }), |
152 | | - ), |
153 | 123 | Effect.map((items) => |
154 | 124 | items |
155 | | - .filter((item): item is Entry => item !== undefined) |
| 125 | + .flatMap((item) => { |
| 126 | + if (item.type !== "file" && item.type !== "directory") return [] |
| 127 | + const absolute = path.join(target.absolute, item.name) |
| 128 | + const relative = path.relative(target.directory, absolute) |
| 129 | + return [ |
| 130 | + new Entry({ |
| 131 | + path: RelativePath.make(relative + (item.type === "directory" ? path.sep : "")), |
| 132 | + type: item.type, |
| 133 | + mime: item.type === "directory" ? "application/x-directory" : FSUtil.mimeType(absolute), |
| 134 | + }), |
| 135 | + ] |
| 136 | + }) |
156 | 137 | .sort((a, b) => (a.type === b.type ? a.path.localeCompare(b.path) : a.type === "directory" ? -1 : 1)), |
157 | 138 | ), |
158 | 139 | ) |
159 | 140 | }), |
160 | | - find: Effect.fn("FileSystem.find")(function* (input) { |
161 | | - const found = yield* search |
162 | | - .file({ |
163 | | - cwd: location.directory, |
164 | | - query: input.query, |
165 | | - limit: input.limit, |
166 | | - kind: input.type ?? "all", |
167 | | - }) |
168 | | - .pipe(Effect.orDie) |
169 | | - return found.map( |
170 | | - (item) => |
171 | | - new Entry({ |
172 | | - path: RelativePath.make(item.path), |
173 | | - uri: pathToFileURL(path.join(location.directory, item.path)).href, |
174 | | - type: item.type, |
175 | | - mime: item.type === "directory" ? "application/x-directory" : FSUtil.mimeType(item.path), |
176 | | - }), |
177 | | - ) |
178 | | - }), |
179 | | - grep: Effect.fn("FileSystem.grep")(function* (input) { |
180 | | - return (yield* search |
181 | | - .search({ |
182 | | - cwd: location.directory, |
183 | | - pattern: input.pattern, |
184 | | - glob: input.include ? [input.include] : undefined, |
185 | | - limit: input.limit, |
186 | | - }) |
187 | | - .pipe(Effect.orDie)).items.map( |
188 | | - (item) => |
189 | | - new GrepMatch({ |
190 | | - path: RelativePath.make(item.path.text), |
191 | | - lines: item.lines.text, |
192 | | - line: item.line_number, |
193 | | - offset: item.absolute_offset, |
194 | | - submatches: item.submatches.map((submatch) => ({ |
195 | | - text: submatch.match.text, |
196 | | - start: submatch.start, |
197 | | - end: submatch.end, |
198 | | - })), |
199 | | - }), |
200 | | - ) |
201 | | - }), |
202 | 141 | }) |
203 | 142 | }), |
204 | 143 | ) |
205 | 144 |
|
| 145 | +export const layer = baseLayer.pipe(Layer.provide(FileSystemSearch.defaultLayer), Layer.provide(FSUtil.defaultLayer)) |
| 146 | + |
206 | 147 | export const locationLayer = layer |