Skip to content

Commit

Permalink
fix: lsp=false, locale
Browse files Browse the repository at this point in the history
  • Loading branch information
guyutongxue committed May 17, 2024
1 parent 666c476 commit 8e535ef
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 48 deletions.
9 changes: 1 addition & 8 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,7 @@
</div>
</div>
</div>
<script type="module">
// Use for example
// http://localhost:5173/?locale=zh-hans or
// http://localhost:5173/?locale=fr
// for a localised version
import { initLocalLoader } from 'monaco-editor-wrapper/vscode/locale';
await initLocalLoader();
import './src/main.ts';
<script type="module" src="src/preload.ts">
</script>
</body>
</html>
5 changes: 4 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const editorValue = {
set: (value: string) => {},
};

export function setEditorValueSource(getter: () => string, setter: (value: string) => void) {
export function setEditorValueSource(
getter: () => string,
setter: (value: string) => void
) {
editorValue.get = getter;
editorValue.set = setter;
}
Expand Down
95 changes: 60 additions & 35 deletions src/editor.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import getConfigurationServiceOverride, { getUserConfiguration } from "@codingame/monaco-vscode-configuration-service-override";
import getConfigurationServiceOverride from "@codingame/monaco-vscode-configuration-service-override";
// these three imports are actually not required here,
// but the dynamic imports in monaco-editor-wrapper are otherwise blocking in a production build
// maybe this can ne overcome by using other config options
import getTextmateServiceOverride from "@codingame/monaco-vscode-textmate-service-override";
import getThemeServiceOverride from "@codingame/monaco-vscode-theme-service-override";
import '@codingame/monaco-vscode-theme-defaults-default-extension';
import "@codingame/monaco-vscode-theme-defaults-default-extension";
// this is required syntax highlighting
import "@codingame/monaco-vscode-cpp-default-extension";
import { Uri } from "vscode";
import { BrowserMessageReader, BrowserMessageWriter } from "vscode-languageclient/browser";
import { LanguageClientConfig, MonacoEditorLanguageClientWrapper, UserConfig } from "monaco-editor-wrapper";
import {
BrowserMessageReader,
BrowserMessageWriter,
} from "vscode-languageclient/browser";
import {
LanguageClientConfig,
MonacoEditorLanguageClientWrapper,
UserConfig,
} from "monaco-editor-wrapper";
import EditorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker";
import { setClangdStatus } from "./ui";
import {
Expand All @@ -27,12 +34,19 @@ let retry = 0;
let succeeded = false;
const wrapper = new MonacoEditorLanguageClientWrapper();

export const createUserConfig = async (code: string, serverWorker: Worker, enableLsp: boolean): Promise<UserConfig> => {
export const createUserConfig = async (
code: string,
serverWorkerPromise: Promise<Worker>,
enableLsp: boolean
): Promise<UserConfig> => {
let languageClientConfig: LanguageClientConfig | undefined;
if (enableLsp) {
const serverWorker = await serverWorkerPromise;
const recreateLsp = async () => {
console.log("reloading lsp...");
wrapper.getLanguageClientWrapper()?.restartLanguageClient(serverWorker, false);
wrapper
.getLanguageClientWrapper()
?.restartLanguageClient(serverWorker, false);
};

const restart = async () => {
Expand All @@ -42,7 +56,9 @@ export const createUserConfig = async (code: string, serverWorker: Worker, enabl
setClangdStatus("indeterminate");
readerOnError.dispose();
readerOnClose.dispose();
wrapper.getLanguageClientWrapper()?.restartLanguageClient(serverWorker, false);
wrapper
.getLanguageClientWrapper()
?.restartLanguageClient(serverWorker, false);
} finally {
retry++;
if (retry > 5 && !succeeded) {
Expand All @@ -67,22 +83,22 @@ export const createUserConfig = async (code: string, serverWorker: Worker, enabl

languageClientConfig = {
languageId: LANGUAGE_ID,
name: 'Clangd WASM Language Server',
name: "Clangd WASM Language Server",
options: {
$type: 'WorkerDirect',
worker: serverWorker
$type: "WorkerDirect",
worker: serverWorker,
},
clientOptions: {
documentSelector: [LANGUAGE_ID],
workspaceFolder: {
index: 0,
name: 'workspace',
name: "workspace",
uri: Uri.file(WORKSPACE_PATH),
},
},
connectionProvider: {
get: async () => ({ reader, writer }),
}
},
};
}

Expand All @@ -98,43 +114,41 @@ export const createUserConfig = async (code: string, serverWorker: Worker, enabl
},
async open() {
return false;
}
}
},
},
},
userServices: {
...getConfigurationServiceOverride(),
...getTextmateServiceOverride(),
...getThemeServiceOverride()
...getThemeServiceOverride(),
},
debugLogging: true
debugLogging: true,
},
editorAppConfig: {
$type: 'extended',
$type: "extended",
codeResources: {
main: {
text: code,
uri: FILE_PATH
}
uri: FILE_PATH,
},
},
userConfiguration: {
json: JSON.stringify({
'workbench.colorTheme': getCurrentTheme(),
'editor.wordBasedSuggestions': 'off',
'editor.inlayHints.enabled': 'offUnlessPressed',
'editor.quickSuggestionsDelay': 200
})
json: getUserConfigurationJson(),
},
useDiffEditor: false
}
useDiffEditor: false,
},
},
loggerConfig: {
enabled: true,
debugEnabled: true
}
debugEnabled: true,
},
};
};

export const createEditor = async (element: HTMLElement, userConfig: UserConfig) => {
export const createEditor = async (
element: HTMLElement,
userConfig: UserConfig
) => {
element.innerHTML = "";
await wrapper.initAndStart(userConfig, element!);
const editorInstance = wrapper.getEditor()!;
Expand All @@ -144,16 +158,27 @@ export const createEditor = async (element: HTMLElement, userConfig: UserConfig)
(value) => editorInstance.setValue(value)
);
return editorInstance;
};

function getUserConfigurationJson(): string {
return JSON.stringify({
"workbench.colorTheme": getCurrentTheme(),
"editor.wordBasedSuggestions": "off",
"editor.inlayHints.enabled": "offUnlessPressed",
"editor.quickSuggestionsDelay": 200,
});
}

function getCurrentTheme() {
return document.body.classList.contains("dark") ? 'Default Dark Modern' : 'Default Light Modern';
return document.body.classList.contains("dark")
? "Default Dark Modern"
: "Default Light Modern";
}

async function toggleEditorTheme() {
const vscodeUserConfiguration = JSON.parse(await getUserConfiguration()) as Record<string, string>;
vscodeUserConfiguration['workbench.colorTheme'] = getCurrentTheme();
wrapper.getMonacoEditorApp()?.updateUserConfiguration(JSON.stringify(vscodeUserConfiguration));
function toggleEditorTheme() {
wrapper
.getMonacoEditorApp()
?.updateUserConfiguration(getUserConfigurationJson());
}
document
.querySelector("#toggleTheme")!
Expand Down
8 changes: 4 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ if (showBuildPanel) {
}

const enableLsp = !params.isExplicitFalse("lsp");
let serverWorker: Worker;
let serverWorkerPromise: Promise<Worker>;
if (enableLsp) {
serverWorker = await createServer();
serverWorkerPromise = createServer();
} else {
setClangdStatus("disabled");
serverWorker = await new Promise<never>(() => {});
serverWorkerPromise = new Promise<never>(() => {});
}

const userConfig = await createUserConfig(code, serverWorker, enableLsp);
const userConfig = await createUserConfig(code, serverWorkerPromise, enableLsp);
await createEditor(document.getElementById("editor")!, userConfig);
if (runCodeNow) {
compileAndRun();
Expand Down
8 changes: 8 additions & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Use for example
// http://localhost:5173/?locale=zh-hans or
// http://localhost:5173/?locale=fr
// for a localised version
import { initLocalLoader } from "monaco-editor-wrapper/vscode/locale";
await initLocalLoader();

import("./main.ts");

0 comments on commit 8e535ef

Please sign in to comment.