allow json (#1137)

Allow creating .json files.
Also added the json language server so syntax highlighting and validation works with the ingame editor
This commit is contained in:
Shy 2024-03-07 00:52:32 +01:00 committed by GitHub
parent 863ac2c8c0
commit d2dd6916b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 11 additions and 4 deletions

@ -7,7 +7,7 @@ export type TextFilePath = FilePath & WithTextExtension;
/** Check extension only */ /** Check extension only */
export function hasTextExtension(path: string): path is WithTextExtension { export function hasTextExtension(path: string): path is WithTextExtension {
return path.endsWith(".txt"); return path.endsWith(".txt") || path.endsWith(".json");
} }
/** Sanitize a player input, resolve any relative paths, and for imports add the correct extension if missing */ /** Sanitize a player input, resolve any relative paths, and for imports add the correct extension if missing */

@ -60,6 +60,12 @@ export class ScriptEditor {
const source = (libSource + "").replace(/export /g, ""); const source = (libSource + "").replace(/export /g, "");
monaco.languages.typescript.javascriptDefaults.addExtraLib(source, "netscript.d.ts"); monaco.languages.typescript.javascriptDefaults.addExtraLib(source, "netscript.d.ts");
monaco.languages.typescript.typescriptDefaults.addExtraLib(source, "netscript.d.ts"); monaco.languages.typescript.typescriptDefaults.addExtraLib(source, "netscript.d.ts");
monaco.languages.json.jsonDefaults.setModeConfiguration({
...monaco.languages.json.jsonDefaults.modeConfiguration,
//completion should be disabled because the
//json language server tries to load a schema by default
completionItems: false,
});
// Load themes // Load themes
loadThemes(monaco.editor.defineTheme); loadThemes(monaco.editor.defineTheme);
sanitizeTheme(Settings.EditorTheme); sanitizeTheme(Settings.EditorTheme);

@ -1,6 +1,7 @@
import type { ContentFilePath } from "../../Paths/ContentFile"; import type { ContentFilePath } from "../../Paths/ContentFile";
import { editor, Position } from "monaco-editor"; import { editor, Position } from "monaco-editor";
import { makeModel } from "./utils"; import { makeModel } from "./utils";
import { hasTextExtension } from "../../Paths/TextFilePath";
type ITextModel = editor.ITextModel; type ITextModel = editor.ITextModel;
@ -21,7 +22,7 @@ export class OpenScript {
this.hostname = hostname; this.hostname = hostname;
this.lastPosition = lastPosition; this.lastPosition = lastPosition;
this.model = model; this.model = model;
this.isTxt = path.endsWith(".txt"); this.isTxt = hasTextExtension(path);
} }
regenerateModel(): void { regenerateModel(): void {

@ -26,7 +26,7 @@ function makeModel(hostname: string, filename: string, code: string) {
scheme: "file", scheme: "file",
path: `${hostname}/${filename}`, path: `${hostname}/${filename}`,
}); });
const language = filename.endsWith(".txt") ? "plaintext" : "javascript"; const language = filename.endsWith(".txt") ? "plaintext" : filename.endsWith(".json") ? "json" : "javascript";
//if somehow a model already exist return it //if somehow a model already exist return it
return editor.getModel(uri) ?? editor.createModel(code, language, uri); return editor.getModel(uri) ?? editor.createModel(code, language, uri);
} }

@ -94,7 +94,7 @@ module.exports = (env, argv) => {
return { return {
plugins: [ plugins: [
new MonacoWebpackPlugin({ languages: ["javascript", "typescript"] }), new MonacoWebpackPlugin({ languages: ["javascript", "typescript", "json"] }),
new webpack.DefinePlugin({ new webpack.DefinePlugin({
"process.env.NODE_ENV": isDevelopment ? '"development"' : '"production"', "process.env.NODE_ENV": isDevelopment ? '"development"' : '"production"',
}), }),