-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.js
More file actions
289 lines (280 loc) · 11.2 KB
/
logic.js
File metadata and controls
289 lines (280 loc) · 11.2 KB
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
/**
* Retrieves the indentation unit (spaces or tabs) based on the editor's current file settings.
* @param {object} editor - The active text editor (or mock).
* @returns {string} The indentation unit (e.g., " " or "\t").
*/
function getIndentUnit(editor) {
const { tabSize, insertSpaces } = editor.options;
return insertSpaces ? " ".repeat(tabSize) : "\t";
}
/**
* Extracts the code part of a line, removing comments and trailing whitespace.
* @param {string} line - The input line of text.
* @returns {string} The code part of the line, with comments removed and trailing whitespace trimmed.
*/
function getCodePart(line) {
const index = line.indexOf('#');
const codePart = index >= 0 ? line.substring(0, index) : line;
return codePart.trimEnd();
}
/**
* Determines the indentation unit used in a given set of lines, limited to 1-4 spaces.
* Analyzes lines starting from the second line to infer the smallest number of leading spaces.
* Returns the default of 4 spaces if no valid indent is found or if the indent exceeds 4 spaces.
* @param {string[]} lines - An array of lines from the input text.
* @param {string} indentUnit - The indentation unit (spaces or tabs).
* @returns {string} The inferred indentation unit (e.g., " " for 2 spaces) or " " as default.
*/
function getGuessedIndentUnit(lines, indentUnit) {
const linesWithIndent = lines.slice(1).filter((line) => /^\s+/.test(line));
if (linesWithIndent.length === 0) {
return indentUnit;
}
const spaceCounts = linesWithIndent.map((line) => {
const match = line.match(/^ +/);
return match ? match[0].length : 0;
});
const minSpaceCount = Math.min(...spaceCounts);
return minSpaceCount > 0 && minSpaceCount <= 4
? " ".repeat(minSpaceCount)
: indentUnit;
}
/**
* Applies proper indentation to an array of text lines based on Python code structure.
* @param {string[]} lines - The array of text lines to indent.
* @param {number} baseIndentLevel - The base indentation level in units.
* @param {string} indentUnit - The indentation unit (spaces or tabs).
* @param {boolean} hasLostIndent - Indicates if the lines have lost their original indentation.
* @returns {string[]} The array of indented lines.
*/
function indentLines(lines, baseIndentLevel, indentUnit, hasLostIndent) {
const firstLine = lines[0];
const indentedLines = [indentUnit.repeat(baseIndentLevel) + firstLine.trim()];
let currentIndentLevel =
!firstLine.startsWith("#") && /[:({[]$/.test(getCodePart(firstLine)) ? 1 : 0;
let previousLineIndent = "";
let isPreviousLineIndentSet = false;
for (let i = 1; i < lines.length; i++) {
const line = lines[i];
const trimmedLine = line.trim();
if (!trimmedLine) {
indentedLines.push("");
continue;
}
let newIndentLevel;
if (hasLostIndent) {
newIndentLevel = baseIndentLevel + currentIndentLevel;
indentedLines.push(indentUnit.repeat(newIndentLevel) + trimmedLine);
if (!trimmedLine.startsWith("#") && getCodePart(trimmedLine).endsWith(':')) {
currentIndentLevel += 1;
}
const codePart = getCodePart(trimmedLine);
if (/^(return(\s+.*)?|pass|break|continue|raise(\s+.*)?|yield(\s+.*)?)$/.test(codePart)) {
currentIndentLevel = Math.max(0, currentIndentLevel - 1);
}
} else {
const currentIndent = line.match(/^\s*/)[0];
if (!isPreviousLineIndentSet) {
previousLineIndent = currentIndent;
const firstIndent = firstLine.match(/^\s*/)[0];
if (
firstIndent.length % indentUnit.length === 0 &&
firstIndent.length > currentIndent.length &&
!getCodePart(firstLine).endsWith(':')
) {
previousLineIndent = firstIndent;
if (/[:({[]$/.test(getCodePart(firstLine)))
previousLineIndent += indentUnit.repeat(1);
}
isPreviousLineIndentSet = true;
}
const indentDiff =
Math.floor((currentIndent.length - previousLineIndent.length) / indentUnit.length);
currentIndentLevel += indentDiff;
newIndentLevel = baseIndentLevel + currentIndentLevel;
indentedLines.push(
indentUnit.repeat(Math.max(0, newIndentLevel)) + trimmedLine
);
previousLineIndent = currentIndent;
}
}
return indentedLines;
}
/**
* Calculates the properly indented lines for pasting.
* normalization logic handles mixed indentation widths by using a stack
* to track indentation levels relative to the structure.
* @param {string[]} processedLines - Lines after preprocessing.
* @param {number} baseIndentLevel - The base indentation level for pasting.
* @param {string} indentUnit - The indentation unit (" ", "\t", etc.).
* @returns {string[]} The array of lines with correct indentation applied.
*/
function calculateIndentedLines(processedLines, baseIndentLevel, indentUnit) {
const normalizedLines = [];
const indentStack = [];
let maxLevel = 0;
for (const line of processedLines) {
if (!line.trim()) {
normalizedLines.push("");
continue;
}
const currentIndentStr = line.match(/^\s*/)[0];
const currentIndentLen = currentIndentStr.length;
if (indentStack.length === 0) {
indentStack.push(currentIndentLen);
} else {
let top = indentStack[indentStack.length - 1];
if (currentIndentLen > top) {
const unitLen = indentUnit.length;
let nextExpected = top + unitLen;
while (currentIndentLen > nextExpected) {
indentStack.push(nextExpected);
nextExpected += unitLen;
}
indentStack.push(currentIndentLen);
} else if (currentIndentLen < top) {
while (indentStack.length > 0 && indentStack[indentStack.length - 1] > currentIndentLen) {
indentStack.pop();
}
if (indentStack.length === 0 || indentStack[indentStack.length - 1] < currentIndentLen) {
indentStack.push(currentIndentLen);
}
}
}
const level = Math.max(0, indentStack.length - 1);
if (level > maxLevel) {
maxLevel = level;
}
const normalizedLine = indentUnit.repeat(level) + line.trim();
normalizedLines.push(normalizedLine);
}
const hasLostIndent = maxLevel === 0 && normalizedLines.length > 1;
return indentLines(
normalizedLines,
baseIndentLevel,
indentUnit,
hasLostIndent
);
}
/**
* Preprocesses the clipboard text for pasting.
* Removes leading/trailing whitespace, handles Python REPL prompts, and splits into lines.
* @param {string} clipboardText - The raw text from the clipboard.
* @returns {{lines: string[], separator: string}} An object containing the processed lines and the detected line separator.
*/
function preprocessClipboardText(clipboardText) {
const lineEndingPattern = clipboardText.includes("\r\n") ? "\\r\\n" : "\\n";
const processedText = clipboardText
.trimEnd()
.replace(new RegExp(`^(?:${lineEndingPattern})+`), '')
.replace(/^(>>> |\.\.\. )/gm, '');
const separator = clipboardText.includes("\r\n") ? "\r\n" : "\n";
const lines = processedText.split(separator);
return { lines, separator };
}
/**
* Determines the base indentation level for the current selection.
* @param {object} editor - The active text editor.
* @param {string} indentUnit - The indentation unit (spaces or tabs).
* @returns {number} The base indentation level in units.
*/
function getBaseIndentLevel(editor, indentUnit) {
const selection = editor.selection;
const startPos = selection.start;
const startLine = startPos.line;
const startCharacter = startPos.character;
const currentLine = editor.document.lineAt(startLine);
const currentIndent = currentLine.text.match(/^\s*/)[0];
if (
selection.isEmpty &&
currentIndent.length > 0 &&
currentIndent.length % indentUnit.length === 0
) {
return Math.floor(currentIndent.length / indentUnit.length);
} else if (!selection.isEmpty && startCharacter === 0) {
return Math.floor(currentIndent.length / indentUnit.length);
} else if (startCharacter % indentUnit.length !== 0) {
return Math.floor(currentIndent.length / indentUnit.length);
} else if (startCharacter % indentUnit.length === 0 && startCharacter !== 0) {
return Math.floor(startCharacter / indentUnit.length);
}
const prevLineNumber = startLine - 1;
if (prevLineNumber < 0) return 0;
const prevLine = editor.document.lineAt(prevLineNumber);
if (prevLine.text.trim() === "") return 0;
const prevIndent = prevLine.text.match(/^\s*/)[0];
const prevIndentLevel = Math.floor(prevIndent.length / indentUnit.length);
return getCodePart(prevLine.text).endsWith(':')
? prevIndentLevel + 1
: prevIndentLevel;
}
/**
* Adjusts the text to be inserted if pasting mid-line under specific conditions.
* Removes leading indent from the first line if pasting into non-whitespace content.
* @param {object} editor - The active text editor.
* @param {object} selection - The current selection.
* @param {string} insertText - The text intended for insertion.
* @returns {string} The potentially adjusted insert text.
*/
function adjustInsertTextForMidLinePaste(editor, selection, insertText) {
const pos = selection.start;
const startLine = selection.start.line;
const endLine = selection.end.line;
const endChar = selection.end.character;
const isSingleLine = startLine === endLine;
if (pos.character > 0) {
const line = editor.document.lineAt(pos.line).text;
const textBefore = line.substring(0, pos.character);
const textAfter = line.substring(pos.character);
const textBeforeIsWhitespace = /^\s*$/.test(textBefore);
const textAfterIsWhitespace = /^\s*$/.test(textAfter);
if (
isSingleLine &&
(!textBeforeIsWhitespace || (endChar !== line.length && !textAfterIsWhitespace))
) {
return insertText.replace(/^\s+/, "");
}
}
return insertText;
}
/**
* Calculates the lines to be dedented after a cut operation.
* @param {string[]} lines - The lines following the cut line.
* @param {number} baseIndentLevel - The base indentation level of the cut line.
* @param {string} indentUnit - The indentation unit.
* @returns {{lines: string[], count: number}} The dedented lines and the count of processed lines.
*/
function calculateDedentsForCut(lines, baseIndentLevel, indentUnit) {
const dedentedLines = [];
let count = 0;
for (const line of lines) {
if (!line.trim()) {
dedentedLines.push(line);
count++;
continue;
}
const currentIndent = line.match(/^\s*/)[0];
const currentIndentLevel = Math.floor(currentIndent.length / indentUnit.length);
if (currentIndentLevel <= baseIndentLevel) {
break;
}
const newIndent = currentIndent.slice(indentUnit.length);
dedentedLines.push(newIndent + line.slice(currentIndent.length));
count++;
}
return { lines: dedentedLines, count };
}
module.exports = {
getIndentUnit,
getCodePart,
getGuessedIndentUnit,
indentLines,
calculateIndentedLines,
preprocessClipboardText,
getBaseIndentLevel,
calculateIndentedLines,
preprocessClipboardText,
getBaseIndentLevel,
adjustInsertTextForMidLinePaste,
calculateDedentsForCut
};