-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathformattingSolutionSearchv2.ts
389 lines (351 loc) · 11.2 KB
/
formattingSolutionSearchv2.ts
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* This file is a WIP of the second iteration of the Cypher formatter.
*/
import { Heap } from 'heap-js';
import CypherCmdLexer from '../generated-parser/CypherCmdLexer';
import {
Chunk,
isCommentBreak,
MAX_COL,
RegularChunk,
} from './formattingHelpersv2';
const errorMessage = `
Internal formatting error: An unexpected issue occurred while formatting.
This is likely a bug in the formatter itself. If possible, please report the issue
along with your input on GitHub:
https://github.com/neo4j/cypher-language-support.`.trim();
const INDENTATION = 2;
const showGroups = false;
export interface Split {
splitType: ' ' | '' | '\n' | '\n\n';
cost: number;
}
export interface Choice {
left: Chunk;
right: Chunk;
// The possible splits that the best first search can choose
possibleSplitChoices: Split[];
}
interface Group {
align: number;
breakCost: number;
}
export interface Decision {
indentation: number;
left: Chunk;
right: Chunk;
chosenSplit: Split;
}
export interface State {
activeGroups: Group[];
column: number;
choiceIndex: number;
baseIndentation: number;
cost: number;
overflowingCount: number;
edge: StateEdge;
}
interface StateEdge {
prevState: State;
decision: Decision;
}
export interface Result {
cost: number;
decisions: Decision[];
indentation: number;
}
interface FinalResultWithPos {
formattedString: string;
cursorPos: number;
}
type FinalResult = string | FinalResultWithPos;
const openingCharacters = [CypherCmdLexer.LPAREN, CypherCmdLexer.LBRACKET];
const standardSplits: Split[] = [
{ splitType: ' ', cost: 0 },
{ splitType: '\n', cost: 1 },
];
const doubleBreakStandardSplits: Split[] = [
{ splitType: ' ', cost: 0 },
{ splitType: '\n\n', cost: 1 },
];
const noSpaceSplits: Split[] = [
{ splitType: '', cost: 0 },
{ splitType: '\n', cost: 1 },
];
const noSpaceDoubleBreakSplits: Split[] = [
{ splitType: '', cost: 0 },
{ splitType: '\n\n', cost: 1 },
];
const noBreakSplit: Split[] = [{ splitType: ' ', cost: 0 }];
const noSpaceNoBreakSplit: Split[] = [{ splitType: '', cost: 0 }];
const onlyBreakSplit: Split[] = [{ splitType: '\n', cost: 0 }];
const onlyDoubleBreakSplit: Split[] = [{ splitType: '\n\n', cost: 0 }];
const emptyChunk: RegularChunk = {
type: 'REGULAR',
text: '',
groupsStarting: 0,
groupsEnding: 0,
modifyIndentation: 0,
};
export function doesNotWantSpace(chunk: Chunk, nextChunk: Chunk): boolean {
return (
nextChunk?.type !== 'COMMENT' &&
chunk.type === 'REGULAR' &&
(chunk.noSpace ||
(chunk.node && openingCharacters.includes(chunk.node.symbol.type)))
);
}
function getIndentations(curr: State, choice: Choice): [number, number] {
const currBaseIndent = curr.baseIndentation;
const nextBaseIndent =
currBaseIndent + choice.left.modifyIndentation * INDENTATION;
let finalIndent = curr.column === 0 ? currBaseIndent : 0;
if (curr.activeGroups.length > 0 && curr.column === 0) {
finalIndent = curr.activeGroups.at(-1).align;
}
// Align hard-break comments with the outermost group (usually the one that
// aligns things with a clause)
if (choice.left.type === 'COMMENT' && choice.left.breakBefore) {
const lastGroup = curr.activeGroups.at(0);
finalIndent = lastGroup ? lastGroup.align : nextBaseIndent;
}
return [nextBaseIndent, finalIndent];
}
// Very useful for debugging but not actually used in the code
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function stateToString(state: State) {
const result = reconstructBestPath(state);
const resultString = decisionsToFormatted(result.decisions);
return resultString;
}
function getNeighbourState(curr: State, choice: Choice, split: Split): State {
const isBreak = split.splitType === '\n' || split.splitType === '\n\n';
// A state has indentation, which is applied after a hard line break. However, if it has an
// active group and we decided to split within a line, the alignment of that group takes precedence
// over the base indentation.
const nextGroups = [...curr.activeGroups];
const [nextBaseIndent, finalIndent] = getIndentations(curr, choice);
const actualColumn = curr.column === 0 ? finalIndent : curr.column;
const splitLength = !isBreak ? split.splitType.length : 0;
const leftLength =
choice.left.type === 'COMMENT' || choice.left.type === 'REGULAR'
? choice.left.text.length
: 0;
const thisWordEnd = actualColumn + leftLength + splitLength;
// We don't consider comments nor an empty space as overflowing
const endWithoutCommentAndSplit =
choice.left.type === 'COMMENT'
? actualColumn - 1
: thisWordEnd - splitLength;
const overflowingCount = Math.max(0, endWithoutCommentAndSplit - MAX_COL);
for (let i = 0; i < choice.left.groupsStarting; i++) {
nextGroups.push({
align: actualColumn,
breakCost: Math.pow(10, nextGroups.length + 1),
});
}
for (let i = 0; i < choice.left.groupsEnding; i++) {
nextGroups.pop();
}
let extraCost = 0;
if (isBreak && nextGroups.length > 0) {
extraCost = nextGroups.at(-1).breakCost;
} else if (isBreak) {
extraCost = 1;
} else {
// Incentivize not breaking to avoid cases where we have longer lines after short
// ones.
extraCost = -1;
}
return {
activeGroups: nextGroups,
column: isBreak ? 0 : thisWordEnd,
choiceIndex: curr.choiceIndex + 1,
baseIndentation: nextBaseIndent,
cost: curr.cost + extraCost,
overflowingCount: curr.overflowingCount + overflowingCount,
edge: {
prevState: curr,
decision: {
indentation: finalIndent,
left: choice.left,
right: choice.right,
chosenSplit: split,
},
},
};
}
function reconstructBestPath(state: State): Result {
const decisions: Decision[] = [];
let currentState: State = state;
while (currentState.edge != null) {
decisions.push(currentState.edge.decision);
currentState = currentState.edge.prevState;
}
decisions.reverse();
return {
cost: state.cost,
decisions,
indentation: state.baseIndentation,
};
}
function getStateKey(state: State): string {
return `${state.column}-${state.choiceIndex}-${
state.activeGroups.at(-1)?.align
}`;
}
function bestFirstSolnSearch(
startingState: State,
choiceList: Choice[],
): Result {
const heap = new Heap<State>((a, b) => {
/**
* Best-first sorting logic:
*
* We want to find the solution with the lowest cost that overflows
* as little as possible.
*
* - We always prefer a solution that doesn't overflow to one that does,
* regardless of its cost. Given these two states:
*
* - A: cost 1000000000000, overflowingCount 0
* - B: cost 0, overflowingCount 1
*
* we would always pick A over B, despite B being significantly cheaper.
*
* - Breaking lines increases the cost of a state, while not breaking
* decreases it by one (see variable extraCost in getNeighbourState).
* - If we choose to not break too many times, we might go out of
* bounds however, which is always worse.
*/
if (a.overflowingCount !== b.overflowingCount) {
return a.overflowingCount - b.overflowingCount;
}
return a.cost - b.cost;
});
heap.push(startingState);
const seenStates = new Set<string>();
while (heap.size() > 0) {
const state = heap.pop();
// NOTE: This memoization is not perfect and can lead to suboptimal solutions.
// It's crucial for performance however.
const stateKey = getStateKey(state);
if (seenStates.has(stateKey)) {
continue;
}
seenStates.add(stateKey);
// We found a solution. Since we do best first, it has to be the best
// solution, so reconstruct that path of decisions
if (state.choiceIndex === choiceList.length) {
return reconstructBestPath(state);
}
const choice = choiceList[state.choiceIndex];
for (const split of choice.possibleSplitChoices) {
const neighbourState = getNeighbourState(state, choice, split);
heap.push(neighbourState);
}
}
throw new Error(errorMessage);
}
// Used for debugging only; it's very convenient to know where groups start and end
function addGroupStart(buffer: string[], decision: Decision) {
for (let i = 0; i < decision.left.groupsStarting; i++) {
buffer.push('[');
}
}
function addGroupEnd(buffer: string[], decision: Decision) {
for (let i = 0; i < decision.left.groupsEnding; i++) {
buffer.push(']');
}
}
function decisionsToFormatted(decisions: Decision[]): FinalResult {
const buffer: string[] = [];
let cursorPos = -1;
decisions.forEach((decision) => {
buffer.push(' '.repeat(decision.indentation));
const leftType = decision.left.type;
if (
(leftType === 'REGULAR' || leftType === 'COMMENT') &&
decision.left.isCursor
) {
cursorPos = buffer.join('').length;
}
if (showGroups) addGroupStart(buffer, decision);
buffer.push(
leftType === 'REGULAR' || leftType === 'COMMENT'
? decision.left.text
: '',
);
if (showGroups) addGroupEnd(buffer, decision);
buffer.push(decision.chosenSplit.splitType);
});
let result = buffer.join('').trimEnd();
if (decisions.at(-1).left.doubleBreak) {
result += '\n';
}
if (cursorPos === -1) {
return result;
}
return { formattedString: result, cursorPos: cursorPos };
}
function determineSplits(chunk: Chunk, nextChunk: Chunk): Split[] {
if (isCommentBreak(chunk, nextChunk)) {
return chunk.doubleBreak ? onlyDoubleBreakSplit : onlyBreakSplit;
}
if (chunk.type === 'REGULAR') {
const noSpace = doesNotWantSpace(chunk, nextChunk);
if (noSpace) {
if (chunk.noBreak) {
return noSpaceNoBreakSplit;
}
return chunk.doubleBreak ? noSpaceDoubleBreakSplits : noSpaceSplits;
}
if (chunk.noBreak) {
return noBreakSplit;
}
}
return chunk.doubleBreak ? doubleBreakStandardSplits : standardSplits;
}
function chunkListToChoices(chunkList: Chunk[]): Choice[] {
return chunkList.map((chunk, index) => {
return {
left: chunk,
right: index === chunkList.length - 1 ? emptyChunk : chunkList[index + 1],
possibleSplitChoices: determineSplits(chunk, chunkList[index + 1]),
};
});
}
export function buffersToFormattedString(
buffers: Chunk[][],
): FinalResultWithPos {
let formatted = '';
let indentation: number = 0;
let cursorPos = 0;
for (const chunkList of buffers) {
const choices: Choice[] = chunkListToChoices(chunkList);
// Indentation should carry over
const initialState: State = {
activeGroups: [],
column: 0,
choiceIndex: 0,
baseIndentation: indentation,
cost: 0,
overflowingCount: 0,
edge: null,
};
const result = bestFirstSolnSearch(initialState, choices);
indentation = result.indentation;
const formattingResult = decisionsToFormatted(result.decisions);
// Cursor is not in this chunkList
if (typeof formattingResult === 'string') {
formatted += formattingResult + '\n';
} else {
cursorPos = formatted.length + formattingResult.cursorPos;
formatted += formattingResult.formattedString + '\n';
}
}
if (indentation > 0) {
throw new Error(errorMessage);
}
return { formattedString: formatted.trimEnd(), cursorPos: cursorPos };
}