@puruslang/prettier-plugin-purus 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/index.js +46 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@puruslang/prettier-plugin-purus",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Prettier plugin for the Purus language",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -30,6 +30,6 @@
|
|
|
30
30
|
"url": "https://github.com/sponsors/otoneko1102"
|
|
31
31
|
},
|
|
32
32
|
"engines": {
|
|
33
|
-
"node": ">=
|
|
33
|
+
"node": ">=20"
|
|
34
34
|
}
|
|
35
35
|
}
|
package/src/index.js
CHANGED
|
@@ -228,6 +228,32 @@ function isEmptyLine(lineTokens) {
|
|
|
228
228
|
return lineTokens.every(t => t.type === "whitespace");
|
|
229
229
|
}
|
|
230
230
|
|
|
231
|
+
/**
|
|
232
|
+
* Returns "postfix", "prefix", or null for the backslash token at content[ti].
|
|
233
|
+
* Postfix: (ident | ] | number) \ (add|sub keyword) → x\add, x\sub
|
|
234
|
+
* Prefix: (add|sub keyword) \ ident → add\x, sub\x
|
|
235
|
+
*/
|
|
236
|
+
function isIncrDecrSlash(content, ti) {
|
|
237
|
+
if (content[ti].value !== "\\") return null;
|
|
238
|
+
let ni = ti + 1;
|
|
239
|
+
while (ni < content.length && content[ni].type === "whitespace") ni++;
|
|
240
|
+
let pi = ti - 1;
|
|
241
|
+
while (pi >= 0 && content[pi].type === "whitespace") pi--;
|
|
242
|
+
const nextTok = ni < content.length ? content[ni] : null;
|
|
243
|
+
const prevTok = pi >= 0 ? content[pi] : null;
|
|
244
|
+
if (nextTok && nextTok.type === "keyword" && (nextTok.value === "add" || nextTok.value === "sub")) {
|
|
245
|
+
if (prevTok && (prevTok.type === "ident" || prevTok.value === "]" || prevTok.type === "number")) {
|
|
246
|
+
return "postfix";
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (prevTok && prevTok.type === "keyword" && (prevTok.value === "add" || prevTok.value === "sub")) {
|
|
250
|
+
if (nextTok && nextTok.type === "ident") {
|
|
251
|
+
return "prefix";
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
|
|
231
257
|
function formatPurus(source, options = {}) {
|
|
232
258
|
const indent = options.tabWidth || 2;
|
|
233
259
|
const useTabs = options.useTabs || false;
|
|
@@ -273,22 +299,37 @@ function formatPurus(source, options = {}) {
|
|
|
273
299
|
// Normalize to single space between tokens, but not before [ or after [, or before ]
|
|
274
300
|
const next = content[ti + 1];
|
|
275
301
|
const prevChar = lineStr.length > 0 ? lineStr[lineStr.length - 1] : "";
|
|
276
|
-
|
|
302
|
+
// Suppress space before an incr/decr backslash
|
|
303
|
+
const nextIsIncrDecrSlash = next && next.value === "\\" && isIncrDecrSlash(content, ti + 1) !== null;
|
|
304
|
+
// Suppress space after an incr/decr backslash
|
|
305
|
+
let prevIsIncrDecrSlash = false;
|
|
306
|
+
{ let pi = ti - 1; while (pi >= 0 && content[pi].type === "whitespace") pi--;
|
|
307
|
+
prevIsIncrDecrSlash = pi >= 0 && content[pi].value === "\\" && isIncrDecrSlash(content, pi) !== null; }
|
|
308
|
+
if (lineStr.length > 0 && next && next.value !== "]" && next.value !== "[" && prevChar !== "[" && !nextIsIncrDecrSlash && !prevIsIncrDecrSlash) {
|
|
277
309
|
lineStr += " ";
|
|
278
310
|
}
|
|
279
311
|
} else {
|
|
280
312
|
if (ti > 0 && content[ti - 1].type !== "whitespace" && lineStr.length > 0) {
|
|
281
313
|
// Adjacent non-whitespace tokens - check if space needed
|
|
282
314
|
const prev = lineStr[lineStr.length - 1];
|
|
315
|
+
let suppressSpace = false;
|
|
283
316
|
if (tok.value === "." || prev === ".") {
|
|
284
|
-
// No space around dots
|
|
317
|
+
suppressSpace = true; // No space around dots
|
|
285
318
|
} else if (tok.value === "," || tok.value === ";") {
|
|
286
|
-
// No space before comma/semicolon
|
|
319
|
+
suppressSpace = true; // No space before comma/semicolon
|
|
287
320
|
} else if (tok.value === "[" || tok.value === "]" || prev === "[") {
|
|
288
|
-
// No space around brackets
|
|
321
|
+
suppressSpace = true; // No space around brackets
|
|
322
|
+
} else if (tok.value === "\\" && isIncrDecrSlash(content, ti) !== null) {
|
|
323
|
+
suppressSpace = true; // No space before incr/decr backslash
|
|
289
324
|
} else {
|
|
290
|
-
|
|
325
|
+
// No space after incr/decr backslash
|
|
326
|
+
let pi = ti - 1;
|
|
327
|
+
while (pi >= 0 && content[pi].type === "whitespace") pi--;
|
|
328
|
+
if (pi >= 0 && content[pi].value === "\\" && isIncrDecrSlash(content, pi) !== null) {
|
|
329
|
+
suppressSpace = true;
|
|
330
|
+
}
|
|
291
331
|
}
|
|
332
|
+
if (!suppressSpace) lineStr += " ";
|
|
292
333
|
}
|
|
293
334
|
lineStr += tok.value;
|
|
294
335
|
}
|