Skip to content

Commit 27d80ab

Browse files
committed
Refactor global into its own store
1 parent 1c66eaa commit 27d80ab

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

frontend/src/App.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<script setup lang="ts">
22
import callstack_view from "./stacktrace-frame.vue"
33
import { editor, languages, Position } from "monaco-editor";
4-
import { stacktraceMap } from "./main";
54
import { useMonacoGlobalInit, type MonacoRefType } from "./monaco";
65
import type { Checkbox } from "@vscode/webview-ui-toolkit";
76
import { generateHoverContent } from "./hover";
8-
import { useGlobalSettingsStore, useVsEvents } from "./stores";
7+
import { useGlobalSettingsStore, useVsEvents, useStacktraceMapStore } from "./stores";
98
import { useBackendApi } from "./backend-api";
109
1110
const store = useVsEvents();
1211
const fn = useBackendApi();
1312
const settings = useGlobalSettingsStore();
13+
const stacktraceMapStore = useStacktraceMapStore();
1414
1515
function initGlobalMonaco(monacoRef: MonacoRefType) {
1616
monacoRef?.languages.registerHoverProvider('*', {
1717
provideHover: async (model: editor.ITextModel, position: Position, _token: /* CancellationToken */ any, _context?: languages.HoverContext<languages.Hover> | undefined): Promise<languages.Hover> => {
18-
const callLocationInfo = stacktraceMap.get(model.id);
18+
const callLocationInfo = stacktraceMapStore.getCallLocation(model.id);
1919
if (!callLocationInfo) {
2020
return { contents: [] };
2121
}

frontend/src/composables/useStacktraceManagement.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import type { CallLocation } from 'shared/src';
2-
import { stacktraceMap } from '../main';
2+
import { useStacktraceMapStore } from '../stores';
3+
import type { editor } from 'monaco-editor';
34

45
export function useStacktraceManagement() {
6+
const stacktraceMapStore = useStacktraceMapStore();
7+
58
function getRealLineNumber(trace: CallLocation, lineNumber: number) {
69
const lineOffset = trace.fileLocationOffset.startLine;
710
return lineOffset + lineNumber; // WARNING: this is 1-based
811
}
912

10-
function registerStacktraceEditor(editor: any, traceFrame: CallLocation) {
13+
function registerStacktraceEditor(editor: editor.IStandaloneCodeEditor, traceFrame: CallLocation) {
1114
const currentId = editor.getModel()?.id;
1215
if (!currentId) {
1316
throw new Error("Model does not have a id!");
1417
}
15-
stacktraceMap.set(currentId, traceFrame);
16-
editor.onDidDispose(() => stacktraceMap.delete(currentId));
18+
stacktraceMapStore.registerEditor(currentId, traceFrame);
19+
editor.onDidDispose(() => stacktraceMapStore.unregisterEditor(currentId));
1720
}
1821

1922
function processCodeForDenseMode(code: string, startLine: number, denseMode: boolean) {

frontend/src/main.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ import App from './App.vue'
44
import type { } from "vscode-webview"; // this defines globals (acquireVsCodeApi)
55
import { install as VueMonacoEditorPlugin } from '@guolao/vue-monaco-editor'
66
import { allComponents, provideVSCodeDesignSystem } from "@vscode/webview-ui-toolkit";
7-
import type { CallLocation } from 'shared/src';
87
import { getComlinkChannel } from './messaging';
98

109
provideVSCodeDesignSystem().register(allComponents);
11-
const vscode = acquireVsCodeApi<{ denseMode: boolean }>();
12-
export const stacktraceMap = new Map</* editor.ITextModel.id */string, CallLocation>();
1310

14-
// Initialize Comlink channels early
11+
12+
const vscode = acquireVsCodeApi<{ denseMode: boolean }>();
1513
const comlinkChannels = getComlinkChannel(vscode);
1614

1715
const pinia = createPinia();
@@ -21,5 +19,5 @@ app.use(pinia);
2119
app.provide('vscode', vscode);
2220
app.provide('comlinkChannels', comlinkChannels);
2321

24-
app.use(VueMonacoEditorPlugin, { paths: { vs: 'https://cdn.jsdelivr.net/npm/monaco-editor@0.50.0/min/vs' }, });
22+
app.use(VueMonacoEditorPlugin, { paths: { vs: 'https://cdn.jsdelivr.net/npm/monaco-editor@0.52.0/min/vs' }, });
2523
app.mount('#app');

frontend/src/stores.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { defineStore } from "pinia"
22
import { inject, shallowRef, ref } from "vue"
33
import * as Comlink from "comlink/dist/esm/comlink";
4-
import type { ComlinkFrontendApi, MonacoTheme, StackTraceInfo } from "shared/src";
5-
import type { WebviewApi } from "vscode-webview";
4+
import type { ComlinkFrontendApi, MonacoTheme, StackTraceInfo, CallLocation } from "shared/src";
65
import type { Endpoint } from "comlink";
76

87

@@ -72,3 +71,32 @@ export const useGlobalSettingsStore = defineStore('vscode-global-settings', () =
7271
}
7372
};
7473
});
74+
75+
export const useStacktraceMapStore = defineStore('stacktrace-map', () => {
76+
// Map from Monaco editor model ID to CallLocation
77+
const stacktraceMap = ref(new Map<string, CallLocation>());
78+
79+
const registerEditor = (editorId: string, callLocation: CallLocation) => {
80+
stacktraceMap.value.set(editorId, callLocation);
81+
};
82+
83+
const unregisterEditor = (editorId: string) => {
84+
stacktraceMap.value.delete(editorId);
85+
};
86+
87+
const getCallLocation = (editorId: string): CallLocation | undefined => {
88+
return stacktraceMap.value.get(editorId);
89+
};
90+
91+
const clear = () => {
92+
stacktraceMap.value.clear();
93+
};
94+
95+
return {
96+
stacktraceMap: stacktraceMap,
97+
registerEditor,
98+
unregisterEditor,
99+
getCallLocation,
100+
clear
101+
};
102+
});

0 commit comments

Comments
 (0)