@tduyng/pi-outline 0.0.1 → 0.0.2
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/package.json +1 -1
- package/src/declarations.ts +10 -2
- package/src/outline.ts +15 -7
package/package.json
CHANGED
package/src/declarations.ts
CHANGED
|
@@ -63,6 +63,7 @@ interface Mark {
|
|
|
63
63
|
|
|
64
64
|
function markLines(lines: string[], language: Language): Mark[] {
|
|
65
65
|
let inFence = false
|
|
66
|
+
let inTemplate = false
|
|
66
67
|
return lines.map((line) => {
|
|
67
68
|
const blank = line.trim() === ''
|
|
68
69
|
if (language === 'markdown') {
|
|
@@ -74,9 +75,16 @@ function markLines(lines: string[], language: Language): Mark[] {
|
|
|
74
75
|
const level = heading ? heading[1]!.length : 0
|
|
75
76
|
return { blank, measure: level, declares: heading !== null, boundary: heading !== null }
|
|
76
77
|
}
|
|
78
|
+
const startedInsideTemplate = inTemplate
|
|
79
|
+
if ((line.match(/(?<!\\)`/g) ?? []).length % 2 === 1) inTemplate = !inTemplate
|
|
77
80
|
const measure = /^[ \t]*/.exec(line)![0].length
|
|
78
|
-
const declares = PATTERNS[language].some((pattern) => pattern.test(line))
|
|
79
|
-
return {
|
|
81
|
+
const declares = !startedInsideTemplate && PATTERNS[language].some((pattern) => pattern.test(line))
|
|
82
|
+
return {
|
|
83
|
+
blank,
|
|
84
|
+
measure,
|
|
85
|
+
declares,
|
|
86
|
+
boundary: !startedInsideTemplate && !blank && !CLOSER.test(line)
|
|
87
|
+
}
|
|
80
88
|
})
|
|
81
89
|
}
|
|
82
90
|
|
package/src/outline.ts
CHANGED
|
@@ -33,15 +33,23 @@ export const outlineTool = {
|
|
|
33
33
|
const text = `No outline for ${params.path}: no declaration rules for this file type. Use grep, or read with offset and limit.`
|
|
34
34
|
return { content: [{ type: 'text', text }], details: undefined }
|
|
35
35
|
}
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
try {
|
|
37
|
+
if (statSync(absolute).size > MAX_BYTES) {
|
|
38
|
+
const text = `${params.path} is over 2MB. Use grep for a name, or read with offset and limit.`
|
|
39
|
+
return { content: [{ type: 'text', text }], details: undefined }
|
|
40
|
+
}
|
|
41
|
+
const content = readFileSync(absolute, 'utf8')
|
|
42
|
+
const declarations = findDeclarations(content, language)
|
|
43
|
+
const lineCount = content.split('\n').length
|
|
44
|
+
const text = renderOutline(params.path, language, lineCount, declarations, params.depth ?? 1)
|
|
38
45
|
return { content: [{ type: 'text', text }], details: undefined }
|
|
46
|
+
} catch (error) {
|
|
47
|
+
const reason = error instanceof Error ? error.message : String(error)
|
|
48
|
+
return {
|
|
49
|
+
content: [{ type: 'text', text: `Could not outline ${params.path}: ${reason}` }],
|
|
50
|
+
details: undefined
|
|
51
|
+
}
|
|
39
52
|
}
|
|
40
|
-
const content = readFileSync(absolute, 'utf8')
|
|
41
|
-
const declarations = findDeclarations(content, language)
|
|
42
|
-
const lineCount = content.split('\n').length
|
|
43
|
-
const text = renderOutline(params.path, language, lineCount, declarations, params.depth ?? 1)
|
|
44
|
-
return { content: [{ type: 'text', text }], details: undefined }
|
|
45
53
|
}
|
|
46
54
|
} satisfies ToolDefinition<typeof parameters>
|
|
47
55
|
|