Skip to content

Commit

Permalink
Start adding goto functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Dec 12, 2024
1 parent c76828b commit 31b779f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
2 changes: 2 additions & 0 deletions demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
tsSyncWorker,
tsFacet,
tsFacetWorker,
tsGotoWorker,
} from "../src/index.js";
import * as Comlink from "comlink";
import { WorkerShape } from "../src/worker.js";
Expand Down Expand Up @@ -124,6 +125,7 @@ increment('not a number');`,
],
}),
tsHoverWorker(),
tsGotoWorker(),
],
parent: document.querySelector("#editor-worker")!,
});
Expand Down
50 changes: 50 additions & 0 deletions src/goto/tsGotoWorker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { EditorView } from "@codemirror/view";
import { tsFacetWorker } from "../index.js";

/**
* Supports 'going to' a variable definition by meta or
* ctrl-clicking on it.
*/
export function tsGotoWorker() {
return EditorView.domEventHandlers({
click: (event, view) => {
const config = view.state.facet(tsFacetWorker);
if (!config?.worker) return false;

// TODO: maybe this should be _just_ meta?
// I think ctrl should probably be preserved.
// Need to check what VS Code does
if (!(event.metaKey || event.ctrlKey)) return false;

const pos = view.posAtCoords({
x: event.clientX,
y: event.clientY,
});

if (pos === null) return;

config.worker
.getHover({
path: config.path,
pos,
})
.then((hoverData) => {
const definition = hoverData?.typeDef?.at(0);

if (definition) {
if (config.path === definition.fileName) {
const tr = view.state.update({
selection: {
anchor: definition.textSpan.start,
head: definition.textSpan.start + definition.textSpan.length,
},
});
view.dispatch(tr);
}
}
});

return true;
},
});
}
28 changes: 14 additions & 14 deletions src/hover/renderTooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type ts from "typescript";
import type { HoverInfo } from "./getHover.js";

export type TooltipRenderer = (
arg0: HoverInfo,
editorView: EditorView,
arg0: HoverInfo,
editorView: EditorView,
) => TooltipView;

/**
Expand All @@ -14,19 +14,19 @@ export type TooltipRenderer = (
* classes.
*/
export const defaultRenderer: TooltipRenderer = (info: HoverInfo) => {
const div = document.createElement("div");
if (info.quickInfo?.displayParts) {
div.appendChild(renderDisplayParts(info.quickInfo.displayParts));
}
return { dom: div };
const div = document.createElement("div");
if (info.quickInfo?.displayParts) {
div.appendChild(renderDisplayParts(info.quickInfo.displayParts));
}
return { dom: div };
};

export const renderDisplayParts = (displayParts: ts.SymbolDisplayPart[]) => {
const div = document.createElement("div");
for (const part of displayParts) {
const span = div.appendChild(document.createElement("span"));
span.className = `quick-info-${part.kind}`;
span.innerText = part.text;
}
return div;
const div = document.createElement("div");
for (const part of displayParts) {
const span = div.appendChild(document.createElement("span"));
span.className = `quick-info-${part.kind}`;
span.innerText = part.text;
}
return div;
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export * from "./hover/renderTooltip.js";

export * from "./facet/tsFacet.js";
export * from "./facet/tsFacetWorker.js";

export * from "./goto/tsGotoWorker.js";

0 comments on commit 31b779f

Please sign in to comment.