aboutsummaryrefslogtreecommitdiff
path: root/src/shared/tabs.js
blob: bcf047941729c692030922dcb05b5e9b036857d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { localeQuotedDiagnostic, localeQuotedEscapedDiagnostic } from "./common.js";
import { UsageError } from "./diagnostics.js";

export const EXPAND_LONG_OPTIONS = ["tabs", "initial", "help", "version"];

export const UNEXPAND_LONG_OPTIONS = ["tabs", "all", "first-only", "help", "version"];

export function tabCommandMetaOption(program, args) {
  const longOptions = program === "expand" ? EXPAND_LONG_OPTIONS : UNEXPAND_LONG_OPTIONS;
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (arg === "--") return null;
    if (arg.startsWith("--")) {
      const option = normalizeTabCommandLongOption(arg, longOptions);
      const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2);
      if (!longOptions.includes(name)) return null;
      if (inlineValue != null) {
        if (name !== "tabs") return null;
        validateTabMetaOptionValue(program, inlineValue);
      }
      if (option === "--help" || option === "--version") return option;
      if (name === "tabs" && inlineValue == null) {
        validateTabMetaOptionValue(program, args[i + 1]);
        i++;
      }
      continue;
    }
    if (!arg.startsWith("-") || arg === "-") continue;
    if (/^-\d[\d,\s]*$/.test(arg)) continue;
    for (let j = 1; j < arg.length; j++) {
      const ch = arg[j];
      if (program === "expand" && ch === "i") continue;
      if (program === "unexpand" && ch === "a") continue;
      if (ch !== "t") return null;
      const value = arg.slice(j + 1) || args[i + 1];
      validateTabMetaOptionValue(program, value);
      if (!arg.slice(j + 1)) i++;
      break;
    }
  }
  return null;
}

export function validateTabMetaOptionValue(program, value) {
  try {
    parseTabStops(value, program);
  } catch (error) {
    if (error instanceof UsageError && ["tab size cannot be 0", "tab sizes must be ascending"].includes(error.message)) return;
    throw error;
  }
}

export function normalizeTabCommandLongOptions(args, longOptions) {
  const out = [];
  let end = false;
  for (const arg of args) {
    if (end || arg === "--" || !arg.startsWith("--")) {
      out.push(arg);
      if (arg === "--") end = true;
      continue;
    }
    out.push(normalizeTabCommandLongOption(arg, longOptions));
  }
  return out;
}

export function normalizeTabCommandLongOption(arg, longOptions) {
  const body = arg.slice(2);
  const eq = body.indexOf("=");
  const name = eq === -1 ? body : body.slice(0, eq);
  if (!name) return arg;
  const optionNames = name === "tab" || "tabs".startsWith(name) ? ["tabs"] : longOptions.filter((option) => option.startsWith(name));
  if (optionNames.length !== 1) return arg;
  return eq === -1 ? `--${optionNames[0]}` : `--${optionNames[0]}=${body.slice(eq + 1)}`;
}

export function parseTabStops(value = "8", program = null) {
  const text = String(value).replace(/^["']|["']$/g, "").trim();
  if (text === "" || text === "," || text === "/" || text === "+") return [8];
  const parts = text.split(/[,\t ]+/).filter(Boolean);
  const diagnostics = [];
  let interval = null;
  let intervalMode = null;
  let zeroInterval = false;
  const numbers = [];
  let previous = 0;
  for (let index = 0; index < parts.length; index++) {
    let part = parts[index];
    if (part === "/" || part === "+") continue;
    if (part.includes("/") && !part.startsWith("/") && !part.startsWith("+/")) throw new UsageError("'/' specifier not at start of number: '/'");
    const matchedInterval = /^[/+]+(?=\/?\d)/.test(part);
    if (matchedInterval && part.startsWith("/") && index !== parts.length - 1) throw new UsageError("'/' specifier only allowed with the last value");
    part = part.replace(/^[/+]+(?=\/?\d)/, (prefix) => {
      intervalMode = prefix.includes("+") ? "+" : "/";
      return "";
    }).replace(/^\//, "");
    if (!/^\d+$/.test(part)) {
      const leadingDigits = part.match(/^\d+/)?.[0] ?? "";
      if (leadingDigits !== "" && Number(leadingDigits) > Number.MAX_SAFE_INTEGER) diagnostics.push(`tab stop is too large ${localeQuotedDiagnostic(leadingDigits)}`);
      const invalid = invalidTabSizeCharacters(part);
      if (diagnostics.length) {
        diagnostics.push(`tab size contains invalid character(s): ${localeQuotedEscapedDiagnostic(invalid)}`);
        continue;
      }
      throw new UsageError(`tab size contains invalid character(s): ${localeQuotedEscapedDiagnostic(invalid)}`);
    }
    const n = Number(part);
    if (n === 0) {
      if (matchedInterval) {
        zeroInterval = true;
        continue;
      }
      throw new UsageError("tab size cannot be 0");
    }
    if (n > Number.MAX_SAFE_INTEGER) {
      diagnostics.push(`tab stop is too large ${localeQuotedDiagnostic(part)}`);
      continue;
    }
    if (matchedInterval) interval = n;
    else {
      if (n <= previous) throw new UsageError("tab sizes must be ascending");
      numbers.push(n);
      previous = n;
    }
  }
  if (diagnostics.length) throw new UsageError(formatTabDiagnostics(diagnostics, program));
  const stops = numbers.length ? numbers : interval ? [interval] : zeroInterval ? [8] : [];
  if (interval) {
    stops.interval = interval;
    stops.intervalMode = intervalMode;
  }
  if (!stops.length || stops.some((n) => !Number.isInteger(n) || n <= 0)) throw new UsageError(`invalid tab size: ${value}`);
  return stops;
}

export function formatTabDiagnostics(diagnostics, program = null) {
  if (diagnostics.length <= 1 || !program) return diagnostics.join("\n");
  return diagnostics.map((message, index) => index === 0 ? message : `${program}: ${message}`).join("\n");
}

export function invalidTabSizeCharacters(part) {
  const stripped = String(part).replace(/^[+/]?\d*/, "").replace(/^[+/]/, "");
  return stripped || String(part);
}

export function normalizeTabArgs(args) {
  const out = [];
  const obsolete = [];
  const shortTabs = [];
  let scanning = true;
  const requireValue = (index, option) => {
    if (index + 1 < args.length) return args[index + 1];
    throw new UsageError(`option requires an argument -- '${option.slice(1)}'`, true);
  };
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (scanning && /^-\d[\d,\s]*$/.test(arg)) obsolete.push(arg.slice(1));
    else if (arg === "-t") {
      shortTabs.push(requireValue(i, arg));
      i++;
      scanning = false;
    } else if (/^-t.+/.test(arg)) {
      shortTabs.push(arg.slice(2));
      scanning = false;
    }
    else if (arg === "-a" || arg === "--all" || arg === "--first-only" || arg === "-i" || arg === "--initial") {
      out.push(arg);
    }
    else {
      scanning = false;
      out.push(arg);
    }
  }
  const tabSpecs = [...obsolete, ...shortTabs];
  return tabSpecs.length ? ["-t", tabSpecs.join(","), ...out] : out;
}

export function combinedTabSpec(longSpec, shortSpec) {
  if (longSpec != null && shortSpec != null) return `${longSpec},${shortSpec}`;
  return shortSpec ?? longSpec;
}

export function nextTabColumn(column, stops) {
  if (stops.length === 1 && !stops.interval) return column + (stops[0] - (column % stops[0]));
  for (const stop of stops) if (stop > column) return stop;
  if (stops.interval) {
    if (stops.intervalMode === "+" && stops.length) {
      const base = stops.at(-1);
      return base + Math.ceil((column + 1 - base) / stops.interval) * stops.interval;
    }
    return column + (stops.interval - (column % stops.interval));
  }
  return column + 1;
}