@peaske7/readit 0.1.7 → 0.2.0

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.
Files changed (118) hide show
  1. package/README.md +0 -3
  2. package/biome.json +1 -1
  3. package/bun.lock +43 -185
  4. package/docs/perf-baseline.md +75 -0
  5. package/docs/superpowers/plans/2026-03-26-surgical-pruning.md +1176 -0
  6. package/e2e/perf/add-comment.spec.ts +118 -0
  7. package/e2e/perf/fixtures/generate.ts +331 -0
  8. package/e2e/perf/initial-load.spec.ts +49 -0
  9. package/e2e/perf/perf.setup.ts +23 -0
  10. package/e2e/perf/perf.teardown.ts +9 -0
  11. package/e2e/perf/scroll.spec.ts +39 -0
  12. package/e2e/perf/tab-switch.spec.ts +69 -0
  13. package/e2e/perf/text-selection.spec.ts +119 -0
  14. package/e2e/perf/utils/metrics.ts +286 -0
  15. package/e2e/perf/utils/perf-cli.ts +86 -0
  16. package/package.json +9 -18
  17. package/playwright.config.ts +12 -0
  18. package/src/App.tsx +133 -178
  19. package/src/{cli/index.ts → cli.ts} +211 -107
  20. package/src/components/ActionsMenu.tsx +6 -27
  21. package/src/components/DocumentViewer/DocumentViewer.tsx +78 -105
  22. package/src/components/DocumentViewer/MermaidDiagram.tsx +6 -7
  23. package/src/components/Header.tsx +9 -20
  24. package/src/components/InlineEditor.tsx +5 -5
  25. package/src/components/MarginNote.tsx +71 -93
  26. package/src/components/MarginNotes.tsx +7 -34
  27. package/src/components/RawModal.tsx +9 -8
  28. package/src/components/ReanchorConfirm.tsx +2 -2
  29. package/src/components/SettingsModal.tsx +11 -89
  30. package/src/components/TabBar.tsx +4 -4
  31. package/src/components/TableOfContents.tsx +5 -5
  32. package/src/components/comments/CommentInput.tsx +7 -35
  33. package/src/components/comments/CommentListItem.tsx +9 -11
  34. package/src/components/comments/CommentManager.tsx +53 -37
  35. package/src/components/comments/CommentNav.tsx +14 -14
  36. package/src/components/ui/ActionLink.tsx +14 -18
  37. package/src/components/ui/Button.tsx +42 -43
  38. package/src/components/ui/Dialog.tsx +73 -113
  39. package/src/components/ui/DropdownMenu.tsx +113 -69
  40. package/src/components/ui/Text.tsx +30 -37
  41. package/src/contexts/CommentContext.tsx +75 -106
  42. package/src/contexts/LocaleContext.tsx +45 -4
  43. package/src/contexts/PositionsContext.tsx +16 -0
  44. package/src/contexts/SettingsContext.tsx +133 -0
  45. package/src/hooks/useClickOutside.ts +0 -4
  46. package/src/hooks/useCommentNavigation.ts +6 -29
  47. package/src/hooks/useComments.ts +6 -18
  48. package/src/hooks/useDocument.ts +35 -34
  49. package/src/hooks/useHeadings.test.ts +8 -50
  50. package/src/hooks/useHeadings.ts +5 -88
  51. package/src/hooks/useScrollSpy.ts +10 -14
  52. package/src/hooks/useTextSelection.ts +1 -38
  53. package/src/lib/__fixtures__/bench-data.ts +1 -41
  54. package/src/lib/anchor.bench.ts +57 -67
  55. package/src/lib/anchor.test.ts +5 -1
  56. package/src/lib/anchor.ts +13 -93
  57. package/src/lib/comment-storage.test.ts +4 -4
  58. package/src/lib/comment-storage.ts +2 -46
  59. package/src/lib/export.ts +7 -13
  60. package/src/lib/highlight/core.test.ts +1 -1
  61. package/src/lib/highlight/dom.ts +5 -68
  62. package/src/lib/highlight/highlighter.ts +102 -262
  63. package/src/lib/highlight/resolver.ts +112 -0
  64. package/src/lib/highlight/types.ts +0 -35
  65. package/src/lib/highlight/worker.ts +45 -0
  66. package/src/lib/i18n/en.ts +1 -50
  67. package/src/lib/i18n/ja.ts +1 -50
  68. package/src/lib/i18n/types.ts +1 -49
  69. package/src/lib/margin-layout.ts +5 -27
  70. package/src/lib/positions.ts +150 -0
  71. package/src/lib/utils.ts +2 -19
  72. package/src/schema.ts +81 -0
  73. package/src/{server/index.ts → server.ts} +111 -81
  74. package/src/{store/index.ts → store.ts} +14 -46
  75. package/vite.config.ts +8 -0
  76. package/src/components/DocumentViewer/IframeContainer.tsx +0 -251
  77. package/src/components/DocumentViewer/InlineCode.tsx +0 -60
  78. package/src/components/DocumentViewer/index.ts +0 -1
  79. package/src/components/FloatingTOC.tsx +0 -61
  80. package/src/components/ShortcutCapture.tsx +0 -48
  81. package/src/components/ShortcutList.tsx +0 -198
  82. package/src/components/comments/CommentMinimap.tsx +0 -62
  83. package/src/components/ui/ActionBar.tsx +0 -16
  84. package/src/components/ui/SeparatorDot.tsx +0 -9
  85. package/src/contexts/LayoutContext.tsx +0 -88
  86. package/src/hooks/useClipboard.ts +0 -82
  87. package/src/hooks/useEditorScheme.ts +0 -51
  88. package/src/hooks/useFontPreference.ts +0 -59
  89. package/src/hooks/useKeybindings.ts +0 -108
  90. package/src/hooks/useKeyboardShortcuts.ts +0 -63
  91. package/src/hooks/useLayoutMode.ts +0 -44
  92. package/src/hooks/useLocalePreference.ts +0 -42
  93. package/src/hooks/useReanchorMode.ts +0 -33
  94. package/src/hooks/useScrollMetrics.ts +0 -56
  95. package/src/hooks/useThemePreference.ts +0 -66
  96. package/src/lib/comment-storage.bench.ts +0 -63
  97. package/src/lib/context.bench.ts +0 -41
  98. package/src/lib/context.test.ts +0 -224
  99. package/src/lib/context.ts +0 -193
  100. package/src/lib/editor-links.ts +0 -59
  101. package/src/lib/export.bench.ts +0 -35
  102. package/src/lib/highlight/colors.ts +0 -37
  103. package/src/lib/highlight/core.ts +0 -54
  104. package/src/lib/highlight/index.ts +0 -23
  105. package/src/lib/highlight/script-builder.ts +0 -485
  106. package/src/lib/html-processor.test.tsx +0 -170
  107. package/src/lib/html-processor.tsx +0 -95
  108. package/src/lib/i18n/completeness.test.ts +0 -51
  109. package/src/lib/i18n/translations.test.ts +0 -39
  110. package/src/lib/layout-constants.ts +0 -12
  111. package/src/lib/margin-layout.bench.ts +0 -28
  112. package/src/lib/scroll.test.ts +0 -118
  113. package/src/lib/scroll.ts +0 -47
  114. package/src/lib/shortcut-registry.test.ts +0 -173
  115. package/src/lib/shortcut-registry.ts +0 -209
  116. package/src/lib/utils.test.ts +0 -110
  117. package/src/store/index.test.ts +0 -242
  118. package/src/types/index.ts +0 -127
@@ -1,66 +0,0 @@
1
- import { useCallback, useEffect, useState } from "react";
2
- import { type ThemeMode, ThemeModes } from "../types";
3
-
4
- const STORAGE_KEY = "readit:theme";
5
-
6
- const DARK_MQ = "(prefers-color-scheme: dark)";
7
-
8
- function getStoredTheme(): ThemeMode {
9
- try {
10
- const stored = localStorage.getItem(STORAGE_KEY);
11
- if (
12
- stored === ThemeModes.LIGHT ||
13
- stored === ThemeModes.DARK ||
14
- stored === ThemeModes.SYSTEM
15
- ) {
16
- return stored;
17
- }
18
- } catch {
19
- // localStorage may be unavailable
20
- }
21
- return ThemeModes.SYSTEM;
22
- }
23
-
24
- function applyTheme(mode: ThemeMode): void {
25
- const isDark =
26
- mode === ThemeModes.DARK ||
27
- (mode === ThemeModes.SYSTEM && window.matchMedia(DARK_MQ).matches);
28
-
29
- document.documentElement.classList.toggle("dark", isDark);
30
- }
31
-
32
- interface UseThemePreferenceResult {
33
- themeMode: ThemeMode;
34
- setThemeMode: (mode: ThemeMode) => void;
35
- }
36
-
37
- export function useThemePreference(): UseThemePreferenceResult {
38
- const [themeMode, setThemeModeState] = useState<ThemeMode>(getStoredTheme);
39
-
40
- // Apply theme class whenever mode changes
41
- useEffect(() => {
42
- applyTheme(themeMode);
43
- }, [themeMode]);
44
-
45
- // Listen for system preference changes when in "system" mode
46
- useEffect(() => {
47
- if (themeMode !== ThemeModes.SYSTEM) return;
48
-
49
- const mq = window.matchMedia(DARK_MQ);
50
- const handler = () => applyTheme(ThemeModes.SYSTEM);
51
-
52
- mq.addEventListener("change", handler);
53
- return () => mq.removeEventListener("change", handler);
54
- }, [themeMode]);
55
-
56
- const setThemeMode = useCallback((mode: ThemeMode) => {
57
- setThemeModeState(mode);
58
- try {
59
- localStorage.setItem(STORAGE_KEY, mode);
60
- } catch {
61
- // localStorage may be unavailable
62
- }
63
- }, []);
64
-
65
- return { themeMode, setThemeMode };
66
- }
@@ -1,63 +0,0 @@
1
- import { bench, describe } from "vitest";
2
- import {
3
- COMMENT_FILE_LARGE,
4
- COMMENT_FILE_MEDIUM,
5
- COMMENT_FILE_OBJ_LARGE,
6
- COMMENT_FILE_OBJ_MEDIUM,
7
- COMMENT_FILE_SMALL,
8
- LARGE_DOC,
9
- } from "./__fixtures__/bench-data";
10
- import {
11
- computeHash,
12
- createComment,
13
- parseCommentFile,
14
- serializeComments,
15
- } from "./comment-storage";
16
-
17
- describe("parseCommentFile", () => {
18
- bench("1 comment", () => {
19
- parseCommentFile(COMMENT_FILE_SMALL);
20
- });
21
-
22
- bench("10 comments", () => {
23
- parseCommentFile(COMMENT_FILE_MEDIUM);
24
- });
25
-
26
- bench("50 comments", () => {
27
- parseCommentFile(COMMENT_FILE_LARGE);
28
- });
29
- });
30
-
31
- describe("serializeComments", () => {
32
- bench("10 comments", () => {
33
- serializeComments(COMMENT_FILE_OBJ_MEDIUM);
34
- });
35
-
36
- bench("50 comments", () => {
37
- serializeComments(COMMENT_FILE_OBJ_LARGE);
38
- });
39
- });
40
-
41
- describe("computeHash", () => {
42
- const shortString = "x".repeat(100);
43
-
44
- bench("short string (100 chars)", () => {
45
- computeHash(shortString);
46
- });
47
-
48
- bench("large doc (~10k chars)", () => {
49
- computeHash(LARGE_DOC);
50
- });
51
- });
52
-
53
- describe("createComment", () => {
54
- bench("short selection", () => {
55
- createComment("selected text here", "my comment", 100, 118, LARGE_DOC);
56
- });
57
-
58
- const longSelection = "a".repeat(2000);
59
-
60
- bench("long selection (triggers truncation)", () => {
61
- createComment(longSelection, "my comment", 0, 2000, LARGE_DOC);
62
- });
63
- });
@@ -1,41 +0,0 @@
1
- import { bench, describe } from "vitest";
2
- import { HTML_DOC, LARGE_DOC } from "./__fixtures__/bench-data";
3
- import { extractContext, formatForLLM, stripHtmlTags } from "./context";
4
-
5
- describe("stripHtmlTags", () => {
6
- bench("200-line HTML document", () => {
7
- stripHtmlTags(HTML_DOC);
8
- });
9
-
10
- bench("short HTML string", () => {
11
- stripHtmlTags(
12
- "<p>Hello &amp; <strong>world</strong> with &#60;entities&#62;</p>",
13
- );
14
- });
15
- });
16
-
17
- describe("extractContext", () => {
18
- bench("single-line selection, markdown", () => {
19
- extractContext({ content: LARGE_DOC, startOffset: 500, endOffset: 530 });
20
- });
21
-
22
- bench("multi-line selection, markdown", () => {
23
- extractContext({ content: LARGE_DOC, startOffset: 500, endOffset: 800 });
24
- });
25
-
26
- bench("HTML content (triggers stripHtmlTags)", () => {
27
- extractContext({ content: HTML_DOC, startOffset: 100, endOffset: 200 });
28
- });
29
- });
30
-
31
- describe("formatForLLM", () => {
32
- const ctx = extractContext({
33
- content: LARGE_DOC,
34
- startOffset: 500,
35
- endOffset: 530,
36
- });
37
-
38
- bench("format with comment", () => {
39
- formatForLLM({ context: ctx, fileName: "doc.md", comment: "Needs review" });
40
- });
41
- });
@@ -1,224 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { extractContext, formatForLLM, stripHtmlTags } from "./context";
3
-
4
- describe("stripHtmlTags", () => {
5
- it("removes simple HTML tags", () => {
6
- expect(stripHtmlTags("<p>Hello</p>")).toBe("Hello");
7
- expect(stripHtmlTags("<div><span>Nested</span></div>")).toBe("Nested");
8
- });
9
-
10
- it("removes script and style content entirely", () => {
11
- expect(stripHtmlTags('<script>alert("xss")</script>text')).toBe("text");
12
- expect(stripHtmlTags("<style>.foo { color: red }</style>text")).toBe(
13
- "text",
14
- );
15
- });
16
-
17
- it("decodes common named entities", () => {
18
- expect(stripHtmlTags("&lt;tag&gt;")).toBe("<tag>");
19
- expect(stripHtmlTags("&amp;&nbsp;&quot;&#39;")).toBe("& \"'");
20
- });
21
-
22
- it("decodes numeric entities (decimal)", () => {
23
- expect(stripHtmlTags("&#65;&#66;&#67;")).toBe("ABC");
24
- });
25
-
26
- it("decodes numeric entities (hex)", () => {
27
- expect(stripHtmlTags("&#x41;&#x42;&#x43;")).toBe("ABC");
28
- expect(stripHtmlTags("&#X41;&#X42;&#X43;")).toBe("ABC"); // case-insensitive
29
- });
30
-
31
- it("handles mixed content", () => {
32
- const html = "<p>Hello &amp; <strong>World</strong>!</p>";
33
- expect(stripHtmlTags(html)).toBe("Hello & World!");
34
- });
35
-
36
- it("preserves plain text", () => {
37
- expect(stripHtmlTags("plain text")).toBe("plain text");
38
- });
39
- });
40
-
41
- describe("extractContext", () => {
42
- it("extracts single-line selection with markers", () => {
43
- const content = "line1\nline2\nline3\nline4\nline5";
44
- // "line2" starts at offset 6, ends at 11
45
- const result = extractContext({ content, startOffset: 6, endOffset: 11 });
46
-
47
- expect(result.startLine).toBe(2);
48
- expect(result.endLine).toBe(2);
49
- // Should have >>> and <<< markers around "line2"
50
- expect(result.lines.some((l) => l.includes(">>> line2 <<<"))).toBe(true);
51
- });
52
-
53
- it("includes context lines before and after", () => {
54
- const content = "line1\nline2\nline3\nline4\nline5";
55
- // Select "line3" at offset 12
56
- const result = extractContext({
57
- content,
58
- startOffset: 12,
59
- endOffset: 17,
60
- contextLines: 2,
61
- });
62
-
63
- expect(result.startLine).toBe(3);
64
- expect(result.endLine).toBe(3);
65
- // Should include 2 lines before (line1, line2) and 2 after (line4, line5)
66
- expect(result.lines.length).toBe(5);
67
- });
68
-
69
- it("handles multi-line selection", () => {
70
- const content = "line1\nline2\nline3\nline4\nline5";
71
- // Select from "line2" to "line3" (offset 6 to 17)
72
- const result = extractContext({ content, startOffset: 6, endOffset: 17 });
73
-
74
- expect(result.startLine).toBe(2);
75
- expect(result.endLine).toBe(3);
76
- // Start line should have >>> marker
77
- expect(result.lines.some((l) => l.includes(">>>"))).toBe(true);
78
- // End line should have <<< marker
79
- expect(result.lines.some((l) => l.includes("<<<"))).toBe(true);
80
- });
81
-
82
- it("handles selection at start of document", () => {
83
- const content = "line1\nline2\nline3";
84
- // Select "line1" at start
85
- const result = extractContext({ content, startOffset: 0, endOffset: 5 });
86
-
87
- expect(result.startLine).toBe(1);
88
- expect(result.endLine).toBe(1);
89
- expect(result.lines[0]).toContain(">>> line1 <<<");
90
- });
91
-
92
- it("handles selection at end of document", () => {
93
- const content = "line1\nline2\nline3";
94
- // Select "line3" (offset 12 to 17)
95
- const result = extractContext({ content, startOffset: 12, endOffset: 17 });
96
-
97
- expect(result.startLine).toBe(3);
98
- expect(result.endLine).toBe(3);
99
- expect(result.lines.some((l) => l.includes(">>> line3 <<<"))).toBe(true);
100
- });
101
-
102
- it("truncates very long lines", () => {
103
- const longLine = "a".repeat(250);
104
- const content = `short\n${longLine}\nshort`;
105
- // Select part of the long line
106
- const result = extractContext({ content, startOffset: 6, endOffset: 256 });
107
-
108
- // Long line should be truncated with ...
109
- const longLineOutput = result.lines.find((l) => l.length > 100);
110
- expect(longLineOutput?.endsWith("...")).toBe(true);
111
- });
112
-
113
- it("handles HTML content by stripping tags", () => {
114
- const html = "<p>paragraph</p>\n<div>div content</div>";
115
- // After stripping: "paragraph\ndiv content"
116
- // Select "paragraph" at offset 0-9
117
- const result = extractContext({
118
- content: html,
119
- startOffset: 0,
120
- endOffset: 9,
121
- });
122
-
123
- expect(result.lines.some((l) => l.includes(">>> paragraph <<<"))).toBe(
124
- true,
125
- );
126
- });
127
-
128
- it("normalizes CRLF to LF", () => {
129
- const content = "line1\r\nline2\r\nline3";
130
- // After normalization: "line1\nline2\nline3"
131
- // Select "line2" at offset 6
132
- const result = extractContext({ content, startOffset: 6, endOffset: 11 });
133
-
134
- expect(result.startLine).toBe(2);
135
- expect(result.lines.some((l) => l.includes(">>> line2 <<<"))).toBe(true);
136
- });
137
-
138
- it("limits context lines to document bounds", () => {
139
- const content = "only\ntwo\nlines";
140
- // Select middle line with 5 context lines requested
141
- const result = extractContext({
142
- content,
143
- startOffset: 5,
144
- endOffset: 8,
145
- contextLines: 5,
146
- });
147
-
148
- // Should not go beyond document bounds
149
- expect(result.lines.length).toBeLessThanOrEqual(3);
150
- });
151
-
152
- it("truncates very long selections with ellipsis", () => {
153
- // Create content with more than MAX_SELECTION_LINES (10) lines
154
- const lines = Array.from({ length: 15 }, (_, i) => `line${i + 1}`);
155
- const content = lines.join("\n");
156
- // Select all content
157
- const result = extractContext({
158
- content,
159
- startOffset: 0,
160
- endOffset: content.length,
161
- contextLines: 0,
162
- });
163
-
164
- // Should include ... for truncated middle
165
- expect(result.lines.some((l) => l === "...")).toBe(true);
166
- });
167
- });
168
-
169
- describe("formatForLLM", () => {
170
- it("formats context with header and line range", () => {
171
- const context = {
172
- lines: ["before", ">>> selected <<<", "after"],
173
- startLine: 5,
174
- endLine: 5,
175
- };
176
-
177
- const result = formatForLLM({ context, fileName: "test.md" });
178
-
179
- expect(result).toContain("# From: test.md");
180
- expect(result).toContain("Lines 5-5:");
181
- expect(result).toContain("---");
182
- expect(result).toContain(">>> selected <<<");
183
- });
184
-
185
- it("includes optional comment", () => {
186
- const context = {
187
- lines: ["text"],
188
- startLine: 1,
189
- endLine: 1,
190
- };
191
-
192
- const result = formatForLLM({
193
- context,
194
- fileName: "test.md",
195
- comment: "This needs review",
196
- });
197
-
198
- expect(result).toContain("Comment: This needs review");
199
- });
200
-
201
- it("omits comment section when not provided", () => {
202
- const context = {
203
- lines: ["text"],
204
- startLine: 1,
205
- endLine: 1,
206
- };
207
-
208
- const result = formatForLLM({ context, fileName: "test.md" });
209
-
210
- expect(result).not.toContain("Comment:");
211
- });
212
-
213
- it("formats multi-line range correctly", () => {
214
- const context = {
215
- lines: [">>> start", "middle", "end <<<"],
216
- startLine: 10,
217
- endLine: 12,
218
- };
219
-
220
- const result = formatForLLM({ context, fileName: "doc.html" });
221
-
222
- expect(result).toContain("Lines 10-12:");
223
- });
224
- });
@@ -1,193 +0,0 @@
1
- /**
2
- * Context extraction and formatting utilities for LLM clipboard copy.
3
- */
4
-
5
- interface ContextResult {
6
- lines: string[]; // Lines with >>> markers inserted
7
- startLine: number; // 1-based line number
8
- endLine: number; // 1-based line number
9
- }
10
-
11
- interface FormatOptions {
12
- context: ContextResult;
13
- fileName: string;
14
- comment?: string;
15
- }
16
-
17
- /**
18
- * Parameters for context extraction.
19
- * Using object destructuring per style guide §3.5 for clarity.
20
- */
21
- export interface ExtractContextParams {
22
- content: string;
23
- startOffset: number;
24
- endOffset: number;
25
- contextLines?: number;
26
- }
27
-
28
- const DEFAULT_CONTEXT_LINES = 2;
29
- const MAX_SELECTION_LINES = 10;
30
- const MAX_LINE_LENGTH = 200;
31
-
32
- /**
33
- * Strip HTML tags to get plain text matching TreeWalker offset calculation.
34
- */
35
- export function stripHtmlTags(html: string): string {
36
- // Remove script/style content entirely
37
- let text = html.replace(/<(script|style)[^>]*>[\s\S]*?<\/\1>/gi, "");
38
- // Remove all HTML tags
39
- text = text.replace(/<[^>]+>/g, "");
40
- // Decode common named entities
41
- text = text.replace(/&nbsp;/g, " ");
42
- text = text.replace(/&lt;/g, "<");
43
- text = text.replace(/&gt;/g, ">");
44
- text = text.replace(/&amp;/g, "&");
45
- text = text.replace(/&quot;/g, '"');
46
- text = text.replace(/&#39;/g, "'");
47
- // Decode numeric entities (decimal and hex)
48
- text = text.replace(/&#(\d+);/g, (_, code) =>
49
- String.fromCharCode(Number.parseInt(code, 10)),
50
- );
51
- text = text.replace(/&#x([0-9a-f]+);/gi, (_, code) =>
52
- String.fromCharCode(Number.parseInt(code, 16)),
53
- );
54
- return text;
55
- }
56
-
57
- /**
58
- * Detect if content is HTML based on presence of HTML tags.
59
- */
60
- function isHtml(content: string): boolean {
61
- return /<[a-z][\s\S]*>/i.test(content);
62
- }
63
-
64
- /**
65
- * Truncate a line if it exceeds max length.
66
- */
67
- function truncateLine(
68
- line: string,
69
- maxLength: number = MAX_LINE_LENGTH,
70
- ): string {
71
- if (line.length <= maxLength) return line;
72
- return `${line.slice(0, maxLength - 3)}...`;
73
- }
74
-
75
- /**
76
- * Extract context around a selection using character offsets.
77
- * Returns lines with >>> and <<< markers inserted at selection boundaries.
78
- */
79
- export function extractContext({
80
- content,
81
- startOffset,
82
- endOffset,
83
- contextLines = DEFAULT_CONTEXT_LINES,
84
- }: ExtractContextParams): ContextResult {
85
- // For HTML, strip tags to match offset calculation
86
- const textContent = isHtml(content) ? stripHtmlTags(content) : content;
87
- // Normalize CRLF to LF for consistent offset calculation
88
- const normalizedContent = textContent.replace(/\r\n/g, "\n");
89
-
90
- const lines = normalizedContent.split("\n");
91
- let currentOffset = 0;
92
- let startLineIndex = -1;
93
- let endLineIndex = -1;
94
- let startCharInLine = 0;
95
- let endCharInLine = 0;
96
-
97
- // Find lines containing the selection
98
- for (let i = 0; i < lines.length; i++) {
99
- const lineLength = lines[i].length;
100
- const lineEnd = currentOffset + lineLength;
101
-
102
- if (startLineIndex === -1 && lineEnd >= startOffset) {
103
- startLineIndex = i;
104
- startCharInLine = startOffset - currentOffset;
105
- }
106
-
107
- if (lineEnd >= endOffset) {
108
- endLineIndex = i;
109
- endCharInLine = endOffset - currentOffset;
110
- break;
111
- }
112
-
113
- currentOffset += lineLength + 1; // +1 for newline
114
- }
115
-
116
- // Handle edge case: couldn't find lines
117
- if (startLineIndex === -1) startLineIndex = 0;
118
- if (endLineIndex === -1) endLineIndex = lines.length - 1;
119
-
120
- // Calculate context range
121
- const contextStart = Math.max(0, startLineIndex - contextLines);
122
- const contextEnd = Math.min(lines.length - 1, endLineIndex + contextLines);
123
-
124
- // Build output lines with markers
125
- const outputLines: string[] = [];
126
- const selectionSpan = endLineIndex - startLineIndex + 1;
127
- const shouldTruncateMiddle = selectionSpan > MAX_SELECTION_LINES;
128
-
129
- for (let i = contextStart; i <= contextEnd; i++) {
130
- let line = lines[i];
131
-
132
- // Handle truncation for very long selections
133
- if (shouldTruncateMiddle) {
134
- const showStart = startLineIndex + 2;
135
- const showEnd = endLineIndex - 2;
136
-
137
- if (i > showStart && i < showEnd) {
138
- if (i === showStart + 1) {
139
- outputLines.push("...");
140
- }
141
- continue;
142
- }
143
- }
144
-
145
- // Insert markers for selection boundaries
146
- if (i === startLineIndex && i === endLineIndex) {
147
- // Single line selection
148
- const before = line.slice(0, startCharInLine);
149
- const selected = line.slice(startCharInLine, endCharInLine);
150
- const after = line.slice(endCharInLine);
151
- line = `${before}>>> ${selected} <<<${after}`;
152
- } else if (i === startLineIndex) {
153
- // Start of multi-line selection
154
- const before = line.slice(0, startCharInLine);
155
- const selected = line.slice(startCharInLine);
156
- line = `${before}>>> ${selected}`;
157
- } else if (i === endLineIndex) {
158
- // End of multi-line selection
159
- const selected = line.slice(0, endCharInLine);
160
- const after = line.slice(endCharInLine);
161
- line = `${selected} <<<${after}`;
162
- }
163
-
164
- outputLines.push(truncateLine(line));
165
- }
166
-
167
- return {
168
- lines: outputLines,
169
- startLine: startLineIndex + 1, // 1-based
170
- endLine: endLineIndex + 1, // 1-based
171
- };
172
- }
173
-
174
- /**
175
- * Format extracted context for LLM clipboard copy.
176
- */
177
- export function formatForLLM({
178
- context,
179
- fileName,
180
- comment,
181
- }: FormatOptions): string {
182
- const header = `# From: ${fileName}`;
183
- const lineRange = `Lines ${context.startLine}-${context.endLine}:`;
184
- const body = ["---", ...context.lines, "---"].join("\n");
185
-
186
- const parts = [header, "", lineRange, body];
187
-
188
- if (comment) {
189
- parts.push("", `Comment: ${comment}`);
190
- }
191
-
192
- return parts.join("\n");
193
- }
@@ -1,59 +0,0 @@
1
- import { type EditorScheme, EditorSchemes } from "../types";
2
-
3
- // Known source file extensions — kept broad to cover common languages
4
- const EXTENSIONS =
5
- "ts|tsx|js|jsx|mjs|cjs|json|md|mdx|css|scss|html|htm|py|rs|go|rb|java|kt|yml|yaml|toml|sh|bash|zsh|sql|graphql|gql|vue|svelte|astro|c|cpp|h|hpp|cs|swift|zig|lua|ex|exs|erl|hrl|elm|clj|cljs|ml|mli|fs|fsx|r|jl|dart|tf|hcl|proto|xml|svg";
6
-
7
- // Match file paths with known extensions and optional :line[:col] suffix
8
- // Requires at least one `/` to avoid matching bare filenames like "README.md"
9
- const FILE_PATH_RE = new RegExp(
10
- `^(?:\\.\\.\\/|\\.\\/)?((?:[\\w@.-]+\\/)+[\\w.-]+\\.(?:${EXTENSIONS}))(?::(\\d+)(?::(\\d+))?)?$`,
11
- );
12
-
13
- export interface FilePathMatch {
14
- path: string;
15
- line?: number;
16
- col?: number;
17
- }
18
-
19
- export function parseFilePath(text: string): FilePathMatch | undefined {
20
- const match = FILE_PATH_RE.exec(text.trim());
21
- if (!match) return undefined;
22
-
23
- return {
24
- path: match[1],
25
- line: match[2] ? Number.parseInt(match[2], 10) : undefined,
26
- col: match[3] ? Number.parseInt(match[3], 10) : undefined,
27
- };
28
- }
29
-
30
- export function buildEditorUri(
31
- scheme: EditorScheme,
32
- absolutePath: string,
33
- line?: number,
34
- col?: number,
35
- ): string {
36
- if (scheme === EditorSchemes.NONE) return "";
37
-
38
- // vscode://file/path:line:col
39
- let uri = `${scheme}://file/${absolutePath}`;
40
- if (line !== undefined) {
41
- uri += `:${line}`;
42
- if (col !== undefined) {
43
- uri += `:${col}`;
44
- }
45
- }
46
- return uri;
47
- }
48
-
49
- export function resolveAbsolutePath(
50
- relativePath: string,
51
- workingDirectory: string,
52
- ): string {
53
- if (relativePath.startsWith("/")) return relativePath;
54
-
55
- const base = workingDirectory.endsWith("/")
56
- ? workingDirectory
57
- : `${workingDirectory}/`;
58
- return `${base}${relativePath}`;
59
- }
@@ -1,35 +0,0 @@
1
- import { bench, describe } from "vitest";
2
- import {
3
- COMMENTS_1,
4
- COMMENTS_10,
5
- COMMENTS_50,
6
- } from "./__fixtures__/bench-data";
7
- import { generatePrompt, generateRawText } from "./export";
8
-
9
- describe("generatePrompt", () => {
10
- bench("1 comment", () => {
11
- generatePrompt(COMMENTS_1, "test.md");
12
- });
13
-
14
- bench("10 comments", () => {
15
- generatePrompt(COMMENTS_10, "test.md");
16
- });
17
-
18
- bench("50 comments", () => {
19
- generatePrompt(COMMENTS_50, "test.md");
20
- });
21
- });
22
-
23
- describe("generateRawText", () => {
24
- bench("1 comment", () => {
25
- generateRawText(COMMENTS_1);
26
- });
27
-
28
- bench("10 comments", () => {
29
- generateRawText(COMMENTS_10);
30
- });
31
-
32
- bench("50 comments", () => {
33
- generateRawText(COMMENTS_50);
34
- });
35
- });