@rohal12/spindle-lsp 0.7.0 → 0.7.1

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/dist/bin.js CHANGED
@@ -3172,6 +3172,50 @@ var init_prettier_bridge = __esm({
3172
3172
  });
3173
3173
 
3174
3174
  // src/plugins/format/placeholders.ts
3175
+ function scanSpindleTokens(text) {
3176
+ const matches = [];
3177
+ let i = 0;
3178
+ while (i < text.length) {
3179
+ if (text[i] === "[" && text[i + 1] === "[") {
3180
+ const closeIdx = text.indexOf("]]", i + 2);
3181
+ if (closeIdx !== -1) {
3182
+ const end = closeIdx + 2;
3183
+ matches.push({ start: i, end, token: text.slice(i, end) });
3184
+ i = end;
3185
+ continue;
3186
+ }
3187
+ }
3188
+ if (text[i] === "{" && !(i > 0 && text[i - 1] === "\\")) {
3189
+ const next = i + 1 < text.length ? text[i + 1] : "";
3190
+ let isToken = false;
3191
+ if (next === "/") {
3192
+ isToken = i + 2 < text.length && /[A-Za-z]/.test(text[i + 2]);
3193
+ } else if (next === "#" || next === ".") {
3194
+ isToken = i + 2 < text.length && /[a-zA-Z]/.test(text[i + 2]);
3195
+ } else if (/[A-Za-z]/.test(next)) {
3196
+ isToken = true;
3197
+ } else if (/[$_@%]/.test(next)) {
3198
+ isToken = i + 2 < text.length && /[a-zA-Z]/.test(text[i + 2]);
3199
+ }
3200
+ if (isToken) {
3201
+ let depth = 1;
3202
+ let j = i + 1;
3203
+ while (j < text.length && depth > 0) {
3204
+ if (text[j] === "{") depth++;
3205
+ else if (text[j] === "}") depth--;
3206
+ j++;
3207
+ }
3208
+ if (depth === 0) {
3209
+ matches.push({ start: i, end: j, token: text.slice(i, j) });
3210
+ i = j;
3211
+ continue;
3212
+ }
3213
+ }
3214
+ }
3215
+ i++;
3216
+ }
3217
+ return matches;
3218
+ }
3175
3219
  function isInsideAttribute(text, pos) {
3176
3220
  let inQuote = false;
3177
3221
  let quoteChar = "";
@@ -3208,13 +3252,15 @@ function replaceSpindleTokens(html) {
3208
3252
  const resultLines = [];
3209
3253
  for (const line of lines) {
3210
3254
  const trimmed = line.trim();
3211
- SPINDLE_TOKEN_REGEX.lastIndex = 0;
3212
- if (!SPINDLE_TOKEN_REGEX.test(trimmed)) {
3255
+ const found = scanSpindleTokens(trimmed);
3256
+ if (found.length === 0) {
3213
3257
  resultLines.push(line);
3214
3258
  continue;
3215
3259
  }
3216
- SPINDLE_TOKEN_REGEX.lastIndex = 0;
3217
- const withoutSpindle = trimmed.replace(SPINDLE_TOKEN_REGEX, "");
3260
+ let withoutSpindle = trimmed;
3261
+ for (let fi = found.length - 1; fi >= 0; fi--) {
3262
+ withoutSpindle = withoutSpindle.slice(0, found[fi].start) + withoutSpindle.slice(found[fi].end);
3263
+ }
3218
3264
  const hasHtml = HTML_TAG_REGEX.test(withoutSpindle);
3219
3265
  if (!hasHtml) {
3220
3266
  const idx = tokens.length;
@@ -3222,16 +3268,20 @@ function replaceSpindleTokens(html) {
3222
3268
  const indent = line.match(/^(\s*)/)?.[1] ?? "";
3223
3269
  resultLines.push(`${indent}<!--SP:${idx}-->`);
3224
3270
  } else {
3225
- SPINDLE_TOKEN_REGEX.lastIndex = 0;
3226
- const replaced = line.replace(SPINDLE_TOKEN_REGEX, (match, ...args2) => {
3227
- const matchOffset = args2[args2.length - 2];
3271
+ const indent = line.match(/^(\s*)/)?.[1] ?? "";
3272
+ const indentLen = indent.length;
3273
+ const assignments = found.map((t) => {
3274
+ const lineStart = t.start + indentLen;
3228
3275
  const idx = tokens.length;
3229
- tokens.push(match);
3230
- if (isInsideAttribute(line, matchOffset)) {
3231
- return `__SP${idx}__`;
3232
- }
3233
- return `<!--SP:${idx}-->`;
3276
+ tokens.push(t.token);
3277
+ const placeholder = isInsideAttribute(line, lineStart) ? `__SP${idx}__` : `<!--SP:${idx}-->`;
3278
+ return { start: lineStart, end: t.end + indentLen, placeholder };
3234
3279
  });
3280
+ let replaced = line;
3281
+ for (let ai = assignments.length - 1; ai >= 0; ai--) {
3282
+ const a = assignments[ai];
3283
+ replaced = replaced.slice(0, a.start) + a.placeholder + replaced.slice(a.end);
3284
+ }
3235
3285
  resultLines.push(replaced);
3236
3286
  }
3237
3287
  }
@@ -3245,11 +3295,10 @@ function restoreSpindleTokens(text, tokens) {
3245
3295
  }
3246
3296
  return result;
3247
3297
  }
3248
- var SPINDLE_TOKEN_REGEX, HTML_TAG_REGEX;
3298
+ var HTML_TAG_REGEX;
3249
3299
  var init_placeholders = __esm({
3250
3300
  "src/plugins/format/placeholders.ts"() {
3251
3301
  "use strict";
3252
- SPINDLE_TOKEN_REGEX = /(?:\{\/[A-Za-z][\w-]*\s*\}|\{(?:[#.][a-zA-Z][\w-]*\s*)*[A-Za-z][\w-]*(?:\s+(?:[^{}]|\{[^{}]*\})*)?\}|\{[$_@][a-zA-Z][\w.]*\}|\[\[(?:[^\]]*)\]\])/g;
3253
3302
  HTML_TAG_REGEX = /<\/?[a-zA-Z][\w-]*[\s>/]/;
3254
3303
  }
3255
3304
  });