Skip to content

Commit c251da5

Browse files
committed
Merge branch 'codex/references-peek-lite'
2 parents a03d59f + 4cb95e4 commit c251da5

7 files changed

Lines changed: 1004 additions & 8 deletions

File tree

anycode-base/src/editor.ts

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ import { vesper } from './theme';
33
import { Renderer } from './renderer/Renderer';
44
import { getPosFromMouse } from './mouse';
55
import { Selection, hasDiagnosticSelection } from "./selection";
6-
import { Completion, CompletionRequest, DefinitionRequest, DefinitionResponse, HoverRequest } from "./lsp";
6+
import {
7+
Completion,
8+
CompletionRequest,
9+
DefinitionRequest,
10+
DefinitionResponse,
11+
HoverRequest,
12+
ReferencesRequest,
13+
} from "./lsp";
714
import {
815
Action, ActionContext, ActionResult,
916
executeAction, handlePasteText,
@@ -71,6 +78,7 @@ export class AnycodeEditor {
7178
private completionProvider: ((request: CompletionRequest) => Promise<Completion[]>) | null = null;
7279
private hoverProvider: ((request: HoverRequest) => Promise<string | null>) | null = null;
7380
private goToDefinitionProvider: ((request: DefinitionRequest) => Promise<DefinitionResponse>) | null = null;
81+
private referencesPeekProvider: ((request: ReferencesRequest) => Promise<void>) | null = null;
7482
private onCursorChangeCallback: ((newCursor: Position, oldCursor: Position) => void) | null = null;
7583
private hoverDebounceTimer: number | null = null;
7684
private hoverRequestToken = 0;
@@ -238,9 +246,16 @@ export class AnycodeEditor {
238246

239247
public async init() {
240248
await this.code.init();
241-
if (!this.readOnly) {
242-
this.setupEventListeners();
249+
if (this.readOnly) {
250+
this.setupReadOnlyEventListeners();
251+
return;
243252
}
253+
this.setupEventListeners();
254+
}
255+
256+
private setupReadOnlyEventListeners() {
257+
this.handleScroll = this.handleScroll.bind(this);
258+
this.container.addEventListener("scroll", this.handleScroll);
244259
}
245260

246261
public getContainer(): HTMLDivElement {
@@ -257,12 +272,46 @@ export class AnycodeEditor {
257272
this.renderer.renderCursor(line, column);
258273
}
259274

275+
public setSelectionRange(
276+
startLine: number,
277+
startColumn: number,
278+
endLine: number,
279+
endColumn: number,
280+
center: boolean = false,
281+
): void {
282+
const startOffset = this.code.getOffset(startLine, startColumn);
283+
const endOffset = this.code.getOffset(endLine, endColumn);
284+
this.selection = new Selection(startOffset, endOffset);
285+
this.offset = endOffset;
286+
287+
if (center) {
288+
this.renderer.focusCenter(this.getEditorState());
289+
} else {
290+
this.renderer.focus(this.getEditorState());
291+
}
292+
293+
const applySelection = () => {
294+
if (!this.selection || this.selection.isEmpty()) return;
295+
this.renderer.renderSelection(this.code, this.selection);
296+
};
297+
298+
// First pass immediately, then reinforce after possible virtualized re-render.
299+
applySelection();
300+
requestAnimationFrame(() => {
301+
applySelection();
302+
requestAnimationFrame(() => {
303+
applySelection();
304+
});
305+
});
306+
}
307+
260308
public requestFocus(line: number, column: number, center: boolean = false): void {
261-
if (this.readOnly) return;
262309
this.needFocus = true;
263310
const offset = this.code.getOffset(line, column);
264311
this.offset = offset;
265-
this.codeContent.focus();
312+
if (!this.readOnly) {
313+
this.codeContent.focus();
314+
}
266315

267316
if (center) this.renderer.focusCenter(this.getEditorState());
268317
else this.renderer.focus(this.getEditorState());
@@ -308,6 +357,12 @@ export class AnycodeEditor {
308357
this.goToDefinitionProvider = goToDefinitionProvider;
309358
}
310359

360+
public setReferencesPeekProvider(
361+
referencesPeekProvider: (request: ReferencesRequest) => Promise<void>
362+
) {
363+
this.referencesPeekProvider = referencesPeekProvider;
364+
}
365+
311366
public setOnCursorChange(callback: (newState: Position, oldState: Position) => void) {
312367
this.onCursorChangeCallback = callback;
313368
}
@@ -462,6 +517,11 @@ export class AnycodeEditor {
462517
this.isCompletionOpen = false;
463518
}
464519

520+
if (e.altKey) {
521+
this.openReferencesPeek(pos.row, pos.col).catch(console.error);
522+
return;
523+
}
524+
465525
if (e.metaKey || e.ctrlKey) {
466526
this.goToDefinition(pos.row, pos.col).catch(console.error);
467527
}
@@ -486,6 +546,25 @@ export class AnycodeEditor {
486546
}
487547
}
488548

549+
private async openReferencesPeek(row: number, col: number): Promise<void> {
550+
if (!this.referencesPeekProvider) {
551+
console.warn('References peek provider not set');
552+
return;
553+
}
554+
555+
try {
556+
const referencesRequest: ReferencesRequest = {
557+
file: this.code.filename,
558+
row: row,
559+
column: col
560+
};
561+
562+
await this.referencesPeekProvider(referencesRequest);
563+
} catch (error) {
564+
console.error('Failed to get references:', error);
565+
}
566+
}
567+
489568
private handleMouseUp(e: MouseEvent) {
490569
// console.log('handleMouseUp ', this.selection);
491570
this.isMouseSelecting = false;
@@ -1349,5 +1428,4 @@ export class AnycodeEditor {
13491428

13501429
this.renderer.verifyDiffs(this.diffs);
13511430
}
1352-
13531431
}

anycode/App.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ const App: React.FC = () => {
167167

168168
useEffect(() => {
169169
const handleKeyDown = (e: KeyboardEvent) => {
170+
const activePaneId = editors.activeEditorPaneId;
171+
if (activePaneId && editors.handleReferencesPeekKeyDown(activePaneId, e)) {
172+
return;
173+
}
174+
175+
if (e.shiftKey && e.key === 'F12') {
176+
e.preventDefault();
177+
editors.openReferencesPeekForActiveCursor();
178+
return;
179+
}
180+
170181
if (e.metaKey && e.key === 'f') {
171182
e.preventDefault();
172183
}
@@ -191,7 +202,15 @@ const App: React.FC = () => {
191202
return () => {
192203
document.removeEventListener('keydown', handleKeyDown);
193204
};
194-
}, [editors.activeFileId, editors.saveFile, editors.undoCursor, editors.redoCursor]);
205+
}, [
206+
editors.activeEditorPaneId,
207+
editors.activeFileId,
208+
editors.handleReferencesPeekKeyDown,
209+
editors.openReferencesPeekForActiveCursor,
210+
editors.redoCursor,
211+
editors.saveFile,
212+
editors.undoCursor,
213+
]);
195214

196215
const handleSearch = ({ pattern }: { id: string; pattern: string }) => {
197216
search.startSearch(pattern);

anycode/features/editor/EditorPanel.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { AnycodeEditorReact } from 'anycode-react';
2+
import type { ReferencesPeekState } from '../../types';
3+
import { ReferencesPeek } from './ReferencesPeek';
24

35
type EditorPanelProps = {
46
panelKey: string;
@@ -7,13 +9,18 @@ type EditorPanelProps = {
79
editorStates: ReadonlyMap<string, unknown>;
810
getActiveFileIdForPane: (paneId: string) => string | null;
911
setActiveEditorPaneId: (paneId: string) => void;
12+
getReferencesPeekForPane: (paneId: string) => ReferencesPeekState | null;
13+
closeReferencesPeek: (paneId?: string) => void;
14+
setSelectedReferenceInPeek: (paneId: string, nextIndex: number) => void;
15+
openReferenceFromPeek: (paneId: string, itemIndex?: number) => void;
1016
};
1117
};
1218

1319
export const EditorPanel = ({ panelKey, editors }: EditorPanelProps) => {
1420
const paneFileId = editors.getActiveFileIdForPane(panelKey);
1521
const paneFile = paneFileId ? editors.files.find((file) => file.id === paneFileId) : null;
1622
const editorState = paneFile ? editors.editorStates.get(paneFile.id) : null;
23+
const referencesPeek = editors.getReferencesPeekForPane(panelKey);
1724

1825
return (
1926
<div
@@ -29,6 +36,14 @@ export const EditorPanel = ({ panelKey, editors }: EditorPanelProps) => {
2936
) : (
3037
<div className="no-editor"></div>
3138
)}
39+
{referencesPeek ? (
40+
<ReferencesPeek
41+
state={referencesPeek}
42+
onClose={() => editors.closeReferencesPeek(panelKey)}
43+
onSelectItem={(index) => editors.setSelectedReferenceInPeek(panelKey, index)}
44+
onOpenItem={(index) => editors.openReferenceFromPeek(panelKey, index)}
45+
/>
46+
) : null}
3247
</div>
3348
);
3449
};

0 commit comments

Comments
 (0)