Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ See the [Marketplace](https://marketplace.visualstudio.com/items?itemName=PeggyJ
merely informative ones. Some grammar compiler passes will give informative
messages about optimizations. Those may be more annoying to you than
helpful, depending on your approach to grammar writing.
- `peggyLanguageServer.debounceMS` [default: *200*] Amount of time to wait
after a file has changed before running. Longer times use less CPU but are
less responsive.
- `peggyLanguageServer.filePattern` [default: *%s/%s.js*] Convert a grammar
name to a JS file to output, using this first `%s` as the directory and the
second `%s` as the unsuffixed name of the grammar. This is useful in a per-
workspace configuration file so that `import` and `require` work from the
correct output directory.

## Syntax Highlighting

Expand Down Expand Up @@ -59,6 +67,10 @@ of all of the rules in the Outline view.

Live edit and test your Grammars, optionally starting at the rule under cursor.

![Live Preview Menu](/images/LivePreviewMenu.png)

![Live Preview](/images/LivePreview.png)

## Problem Matchers

Report problems of your code in the Problems view when `peggy` is run in a task.
Expand Down
28 changes: 23 additions & 5 deletions client/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import * as peggy from "peggy";
import {
ExtensionContext,
OutputChannel,
Selection,
Tab,
TabInputText,
TextEditorRevealType,
Uri,
ViewColumn,
commands,
Expand All @@ -13,11 +15,12 @@ import {
} from "vscode";
import { MemFS } from "../vendor/vscode-extension-samples/fileSystemProvider";
import { debouncePromise } from "../common/debounce";
import { fileURLToPath } from "url";
import fromMem from "@peggyjs/from-mem";
import { format as uformat } from "node:util";

const PEGGY_INPUT_SCHEME = "peggyjsin";
const ID = "peggyLanguageServer";
let filePattern = "%s/%s.js";

interface GrammarConfig {
name: string;
Expand Down Expand Up @@ -57,7 +60,12 @@ async function executeAndDisplayResults(
const input = input_document.getText();
const fs = await import("node:fs");
fs.writeFileSync("/tmp/u.txt", JSON.stringify(config));
const filename = fileURLToPath(grammar_document.uri.toString());
const parsedFilename = path.parse(grammar_document.uri.fsPath);
const filename = path.resolve(uformat(
filePattern,
parsedFilename.dir,
parsedFilename.name
));

try {
const grammar_text = grammar_document.getText();
Expand Down Expand Up @@ -227,12 +235,16 @@ export function activate(context: ExtensionContext): void {
}
});

const initialConfig = workspace.getConfiguration(ID);
debounceExecution.wait = initialConfig.get("debounceMS");
filePattern = initialConfig.get("filePattern");
const configChanged = workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration(ID)) {
debounceExecution.wait = workspace.getConfiguration(ID).get("debounceMS");
const config = workspace.getConfiguration(ID);
debounceExecution.wait = config.get("debounceMS");
filePattern = config.get("filePattern");
}
});
debounceExecution.wait = workspace.getConfiguration(ID).get("debounceMS");

context.subscriptions.push(
configChanged,
Expand All @@ -253,13 +265,19 @@ export function activate(context: ExtensionContext): void {
);

if (word_range !== null) {
editor.selection = new Selection(word_range.start, word_range.end);
const rule_name = editor.document.getText(word_range);
const grammar_config = await trackGrammar(
editor.document.uri,
rule_name
);

return debounceExecution(peggy_output, grammar_config);
return debounceExecution(peggy_output, grammar_config).then(() => {
editor.revealRange(
word_range,
TextEditorRevealType.InCenterIfOutsideViewport
);
});
}

return null;
Expand Down
Binary file added images/LivePreview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/LivePreviewMenu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@
"peggyLanguageServer.debounceMS": {
"description": "Time, in milliseconds, to debounce typing",
"type": "integer",
"default": 200
"default": 200,
"minimum": 0
},
"peggyLanguageServer.filePattern": {
"markdownDescription": "Convert a grammar name to a JS file to output, using this first `%s` as the directory and the second `%s` as the unsuffixed name of the grammar.",
"type": "string",
"default": "%s/%s.js"
}
}
},
Expand Down Expand Up @@ -188,7 +194,7 @@
"eslint-plugin-mocha": "11.1.0",
"npm-run-all": "^4.1.5",
"rimraf": "^6.0.1",
"typescript": "^5.8.3",
"typescript": "^5.9.2",
"typescript-eslint": "8.38.0"
},
"dependencies": {
Expand All @@ -204,7 +210,7 @@
"c8": "10.1.3"
}
},
"packageManager": "pnpm@10.13.1",
"packageManager": "pnpm@10.14.0",
"engines": {
"vscode": ">=1.102.0"
}
Expand Down
98 changes: 49 additions & 49 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.