Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow creating .json files.
Also added the json language server so syntax highlighting and validation works with the ingame editor
  • Loading branch information
shyguy1412 authored Mar 6, 2024
1 parent 863ac2c commit d2dd691
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Paths/TextFilePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type TextFilePath = FilePath & WithTextExtension;

/** Check extension only */
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 */
Expand Down
6 changes: 6 additions & 0 deletions src/ScriptEditor/ScriptEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ export class ScriptEditor {
const source = (libSource + "").replace(/export /g, "");
monaco.languages.typescript.javascriptDefaults.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
loadThemes(monaco.editor.defineTheme);
sanitizeTheme(Settings.EditorTheme);
Expand Down
3 changes: 2 additions & 1 deletion src/ScriptEditor/ui/OpenScript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ContentFilePath } from "../../Paths/ContentFile";
import { editor, Position } from "monaco-editor";
import { makeModel } from "./utils";
import { hasTextExtension } from "../../Paths/TextFilePath";

type ITextModel = editor.ITextModel;

Expand All @@ -21,7 +22,7 @@ export class OpenScript {
this.hostname = hostname;
this.lastPosition = lastPosition;
this.model = model;
this.isTxt = path.endsWith(".txt");
this.isTxt = hasTextExtension(path);
}

regenerateModel(): void {
Expand Down
2 changes: 1 addition & 1 deletion src/ScriptEditor/ui/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function makeModel(hostname: string, filename: string, code: string) {
scheme: "file",
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
return editor.getModel(uri) ?? editor.createModel(code, language, uri);
}
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ module.exports = (env, argv) => {

return {
plugins: [
new MonacoWebpackPlugin({ languages: ["javascript", "typescript"] }),
new MonacoWebpackPlugin({ languages: ["javascript", "typescript", "json"] }),
new webpack.DefinePlugin({
"process.env.NODE_ENV": isDevelopment ? '"development"' : '"production"',
}),
Expand Down

0 comments on commit d2dd691

Please sign in to comment.