@opencode/util 0.0.0-dev-20770 → 0.0.0-dev-20774

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/path.d.ts CHANGED
@@ -8,3 +8,11 @@ export declare function comparablePath(path: string): string;
8
8
  export declare function containsDirectory(parent: string, child: string): boolean;
9
9
  export declare function sameDirectory(a: string, b: string): boolean;
10
10
  export declare function encodeFilePath(filepath: string): string;
11
+ export declare function isLineRangeHash(hash: string): boolean;
12
+ export declare function parsePathLineSuffix(input: string): {
13
+ path: string;
14
+ selection: {
15
+ start: number;
16
+ end: number;
17
+ } | undefined;
18
+ };
package/dist/path.js CHANGED
@@ -85,3 +85,45 @@ export function encodeFilePath(filepath) {
85
85
  })
86
86
  .join("/");
87
87
  }
88
+ const MAX_LINE_NUMBER = 1_000_000;
89
+ const lineHashPattern = /^#L(\d+)(?:C\d+)?(?:-L?(\d+)(?:C\d+)?)?$/i;
90
+ const lineHashSuffixPattern = /^(.*?)#L(\d+)(?:C\d+)?(?:-L?(\d+)(?:C\d+)?)?$/i;
91
+ const lineColonSuffixPattern = /^(.*?):(\d+)(?::\d+)?(?:-(\d+)(?::\d+)?)?:?$/;
92
+ function toLineRange(first, second) {
93
+ if (!Number.isSafeInteger(first) ||
94
+ !Number.isSafeInteger(second) ||
95
+ first < 1 ||
96
+ second < 1 ||
97
+ first > MAX_LINE_NUMBER ||
98
+ second > MAX_LINE_NUMBER) {
99
+ return undefined;
100
+ }
101
+ return {
102
+ start: Math.min(first, second),
103
+ end: Math.max(first, second),
104
+ };
105
+ }
106
+ export function isLineRangeHash(hash) {
107
+ return lineHashPattern.test(hash);
108
+ }
109
+ export function parsePathLineSuffix(input) {
110
+ const hashMatch = input.match(lineHashSuffixPattern);
111
+ if (hashMatch) {
112
+ const first = Number(hashMatch[2]);
113
+ const second = hashMatch[3] ? Number(hashMatch[3]) : first;
114
+ return {
115
+ path: hashMatch[1] ?? "",
116
+ selection: toLineRange(first, second),
117
+ };
118
+ }
119
+ const colonMatch = input.match(lineColonSuffixPattern);
120
+ if (colonMatch && !/^[a-z]:?$/i.test(colonMatch[1] ?? "")) {
121
+ const first = Number(colonMatch[2]);
122
+ const second = colonMatch[3] ? Number(colonMatch[3]) : first;
123
+ return {
124
+ path: colonMatch[1] ?? "",
125
+ selection: toLineRange(first, second),
126
+ };
127
+ }
128
+ return { path: input, selection: undefined };
129
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@opencode/util",
4
- "version": "0.0.0-dev-20770",
4
+ "version": "0.0.0-dev-20774",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "repository": {