Skip to content

Commit c0b77ca

Browse files
authored
feat(console): open error links using the active code editor (#4084)
Use [launch-editor](https://www.npmjs.com/package/launch-editor) to open the wing file in the currently active code editor. https://github.com/winglang/wing/assets/5547636/cda55017-ded4-4b85-80f0-875465d7227a resolves #4067 *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
1 parent 13082be commit c0b77ca

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

apps/wing-console/console/server/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@winglang/sdk": "workspace:^",
2323
"codespan-wasm": "^0.4.0",
2424
"debug": "^4.3.4",
25+
"launch-editor": "^2.6.0",
2526
"ms": "^2.1.3"
2627
},
2728
"devDependencies": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module "launch-editor" {
2+
export default function launchEditor(file: string): void;
3+
}

apps/wing-console/console/server/src/router/app.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -601,12 +601,23 @@ export const createAppRouter = () => {
601601
}),
602602
)
603603
.mutation(async ({ ctx, input }) => {
604-
ctx.emitter.emit("openFileInEditor", {
605-
path: input.path,
606-
line: input.line,
607-
column: input.column,
608-
});
604+
if (ctx.emitter.listenerCount("openFileInEditor") > 0) {
605+
ctx.emitter.emit("openFileInEditor", {
606+
path: input.path,
607+
line: input.line,
608+
column: input.column,
609+
});
610+
return;
611+
}
612+
const { default: launch } = await import("launch-editor");
613+
launch(`${input.path}:${input.line}:${input.column}`);
609614
}),
615+
616+
/**
617+
* Warning! Subscribing to this procedure will override the default behavior of opening files in the editor
618+
* provided by the "app.openFileInEditor" procedure.
619+
* Needed to manage file opening within the vs-code extension.
620+
*/
610621
"app.openFileInEditorSubscription": createProcedure.subscription(
611622
({ ctx }) => {
612623
return observable<FileLink>((emit) => {

apps/wing-console/console/ui/src/features/console-logs.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import classNames from "classnames";
99
import throttle from "lodash.throttle";
1010
import { Fragment, useEffect, useRef, useState } from "react";
1111

12-
import {
13-
OpenFileInEditorButton,
14-
useFileLink,
15-
} from "../shared/use-file-link.js";
12+
import { OpenFileInEditorButton } from "../shared/use-file-link.js";
1613
import { createHtmlLink } from "../shared/use-file-link.js";
1714

1815
const dateTimeFormat = new Intl.DateTimeFormat(undefined, {

apps/wing-console/console/ui/src/shared/use-file-link.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ export const createHtmlLink = (
1111
.replaceAll(
1212
/\B((?:[a-z]:)?[/\\]\S+):(\d+):(\d+)/gi,
1313
(match, path, line, column) => {
14-
const link = `vscode://file/${path}:${line}:${column}`;
15-
return `<a class="${className}" href="${link}" path="${path}" line="${line}" column="${column}" >${match}</a>`;
14+
return `<a class="${className}" path="${path}" line="${line}" column="${column}" >${match}</a>`;
1615
},
1716
)
1817
.replaceAll(/(\r\n|\n|\r)/gm, expanded ? "<br />" : "\n");
@@ -25,6 +24,7 @@ export const OpenFileInEditorButton = ({ children }: PropsWithChildren) => {
2524
<button
2625
className="appearance-none text-left"
2726
onClick={(event) => {
27+
event.preventDefault();
2828
const target = event.target as HTMLElement;
2929
if (target.tagName === "A") {
3030
const path = target.getAttribute("path")!;

pnpm-lock.yaml

+13-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)