Skip to content

Commit 7da73a1

Browse files
authored
Merge pull request #81 from hildjj/filePattern
Add filePattern option. Updated docs.
2 parents 3fb347a + e660ec1 commit 7da73a1

6 files changed

Lines changed: 93 additions & 57 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ See the [Marketplace](https://marketplace.visualstudio.com/items?itemName=PeggyJ
1717
merely informative ones. Some grammar compiler passes will give informative
1818
messages about optimizations. Those may be more annoying to you than
1919
helpful, depending on your approach to grammar writing.
20+
- `peggyLanguageServer.debounceMS` [default: *200*] Amount of time to wait
21+
after a file has changed before running. Longer times use less CPU but are
22+
less responsive.
23+
- `peggyLanguageServer.filePattern` [default: *%s/%s.js*] Convert a grammar
24+
name to a JS file to output, using this first `%s` as the directory and the
25+
second `%s` as the unsuffixed name of the grammar. This is useful in a per-
26+
workspace configuration file so that `import` and `require` work from the
27+
correct output directory.
2028

2129
## Syntax Highlighting
2230

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

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

70+
![Live Preview Menu](/images/LivePreviewMenu.png)
71+
72+
![Live Preview](/images/LivePreview.png)
73+
6274
## Problem Matchers
6375

6476
Report problems of your code in the Problems view when `peggy` is run in a task.

client/preview.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import * as peggy from "peggy";
33
import {
44
ExtensionContext,
55
OutputChannel,
6+
Selection,
67
Tab,
78
TabInputText,
9+
TextEditorRevealType,
810
Uri,
911
ViewColumn,
1012
commands,
@@ -13,11 +15,12 @@ import {
1315
} from "vscode";
1416
import { MemFS } from "../vendor/vscode-extension-samples/fileSystemProvider";
1517
import { debouncePromise } from "../common/debounce";
16-
import { fileURLToPath } from "url";
1718
import fromMem from "@peggyjs/from-mem";
19+
import { format as uformat } from "node:util";
1820

1921
const PEGGY_INPUT_SCHEME = "peggyjsin";
2022
const ID = "peggyLanguageServer";
23+
let filePattern = "%s/%s.js";
2124

2225
interface GrammarConfig {
2326
name: string;
@@ -57,7 +60,12 @@ async function executeAndDisplayResults(
5760
const input = input_document.getText();
5861
const fs = await import("node:fs");
5962
fs.writeFileSync("/tmp/u.txt", JSON.stringify(config));
60-
const filename = fileURLToPath(grammar_document.uri.toString());
63+
const parsedFilename = path.parse(grammar_document.uri.fsPath);
64+
const filename = path.resolve(uformat(
65+
filePattern,
66+
parsedFilename.dir,
67+
parsedFilename.name
68+
));
6169

6270
try {
6371
const grammar_text = grammar_document.getText();
@@ -227,12 +235,16 @@ export function activate(context: ExtensionContext): void {
227235
}
228236
});
229237

238+
const initialConfig = workspace.getConfiguration(ID);
239+
debounceExecution.wait = initialConfig.get("debounceMS");
240+
filePattern = initialConfig.get("filePattern");
230241
const configChanged = workspace.onDidChangeConfiguration(e => {
231242
if (e.affectsConfiguration(ID)) {
232-
debounceExecution.wait = workspace.getConfiguration(ID).get("debounceMS");
243+
const config = workspace.getConfiguration(ID);
244+
debounceExecution.wait = config.get("debounceMS");
245+
filePattern = config.get("filePattern");
233246
}
234247
});
235-
debounceExecution.wait = workspace.getConfiguration(ID).get("debounceMS");
236248

237249
context.subscriptions.push(
238250
configChanged,
@@ -253,13 +265,19 @@ export function activate(context: ExtensionContext): void {
253265
);
254266

255267
if (word_range !== null) {
268+
editor.selection = new Selection(word_range.start, word_range.end);
256269
const rule_name = editor.document.getText(word_range);
257270
const grammar_config = await trackGrammar(
258271
editor.document.uri,
259272
rule_name
260273
);
261274

262-
return debounceExecution(peggy_output, grammar_config);
275+
return debounceExecution(peggy_output, grammar_config).then(() => {
276+
editor.revealRange(
277+
word_range,
278+
TextEditorRevealType.InCenterIfOutsideViewport
279+
);
280+
});
263281
}
264282

265283
return null;

images/LivePreview.png

229 KB
Loading

images/LivePreviewMenu.png

71.6 KB
Loading

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@
4242
"peggyLanguageServer.debounceMS": {
4343
"description": "Time, in milliseconds, to debounce typing",
4444
"type": "integer",
45-
"default": 200
45+
"default": 200,
46+
"minimum": 0
47+
},
48+
"peggyLanguageServer.filePattern": {
49+
"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.",
50+
"type": "string",
51+
"default": "%s/%s.js"
4652
}
4753
}
4854
},
@@ -188,7 +194,7 @@
188194
"eslint-plugin-mocha": "11.1.0",
189195
"npm-run-all": "^4.1.5",
190196
"rimraf": "^6.0.1",
191-
"typescript": "^5.8.3",
197+
"typescript": "^5.9.2",
192198
"typescript-eslint": "8.38.0"
193199
},
194200
"dependencies": {
@@ -204,7 +210,7 @@
204210
"c8": "10.1.3"
205211
}
206212
},
207-
"packageManager": "pnpm@10.13.1",
213+
"packageManager": "pnpm@10.14.0",
208214
"engines": {
209215
"vscode": ">=1.102.0"
210216
}

pnpm-lock.yaml

Lines changed: 49 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)