Skip to content

Commit 21b995f

Browse files
kirjsalxhub
authored andcommitted
feat(docs-infra): allow navigating to editor error locations
This shows up in a little pop-up, and is clickable now, and leads to exact line
1 parent 87e05e9 commit 21b995f

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

adev/src/app/editor/code-editor/code-editor.component.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,16 @@
128128
</button>
129129
<ul>
130130
@for (error of errors(); track error) {
131-
<li>(line: {{ error.lineNumber }}:{{ error.characterPosition }}) {{ error.message }}</li>
131+
<li>
132+
<a
133+
class="adev-error-location-link"
134+
href="#"
135+
(click)="$event.preventDefault(); openFileAtLocation(error)"
136+
>
137+
(line: {{ error.lineNumber }}:{{ error.characterPosition }})
138+
</a>
139+
{{ error.message }}
140+
</li>
132141
}
133142
</ul>
134143
</div>

adev/src/app/editor/code-editor/code-editor.component.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ export class CodeEditor {
150150
this.displayErrorsBox.set(false);
151151
}
152152

153+
protected openFileAtLocation(error: DiagnosticWithLocation): void {
154+
// Scroll the editor to the error location
155+
// The error is always in the current file since diagnostics are file-specific
156+
const lineNumber = error.lineNumber;
157+
const characterPosition = error.characterPosition;
158+
159+
// Calculate the position in the document
160+
// CodeMirror uses 0-based line numbers, but our error uses 1-based
161+
const line = Math.max(0, lineNumber - 1);
162+
163+
// Request the editor to scroll to this line
164+
// We'll need to add a method to CodeMirrorEditor service to handle this
165+
this.codeMirrorEditor.scrollToLine(line, characterPosition);
166+
}
167+
168+
protected closeRenameFile(): void {
169+
this.isRenamingFile.set(false);
170+
}
171+
153172
protected canRenameFile = (filename: string) => this.canDeleteFile(filename);
154173

155174
protected canDeleteFile(filename: string) {

adev/src/app/editor/code-editor/code-mirror-editor.service.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,28 @@ export class CodeMirrorEditor {
183183
this._editorView.setState(editorState);
184184
}
185185

186+
scrollToLine(line: number, character: number = 0): void {
187+
if (!this._editorView) return;
188+
189+
const state = this._editorView.state;
190+
const doc = state.doc;
191+
192+
if (line < 0 || line >= doc.lines) {
193+
console.warn(`Line ${line} is out of bounds (0-${doc.lines - 1})`);
194+
return;
195+
}
196+
197+
const lineObj = doc.line(line + 1);
198+
const pos = lineObj.from + Math.min(character, lineObj.length);
199+
200+
this._editorView.dispatch({
201+
selection: {anchor: pos, head: pos},
202+
scrollIntoView: true,
203+
});
204+
205+
this._editorView.focus();
206+
}
207+
186208
private initTypescriptVfsWorker(): void {
187209
if (this.tsVfsWorker || !this.tsVfsWorkerFactory) {
188210
return;

0 commit comments

Comments
 (0)