Skip to content

Commit 1bfaf2c

Browse files
committed
Very rough proof-of-concept
1 parent 00e2c59 commit 1bfaf2c

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

Modules/main.c

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,145 @@ pymain_import_readline(const PyConfig *config)
229229
}
230230

231231

232+
/*_command_dedent(wchar_t *command) */
233+
/*{ */
234+
/* // NEW CODE: */
235+
/* // Remove common leading whitespace from the string */
236+
/* // Handle dedenting the command */
237+
/* // */
238+
/* int cmdlen = wcslen(command); */
239+
/* fprintf(stderr, "COMMAND: %ls\n", command); */
240+
/* fprintf(stderr, "cmdlen: %d\n", cmdlen); */
241+
242+
/* int num_newlines = 0; */
243+
/* int num_spaces = 0; */
244+
/* for (int i = 0; i < cmdlen; i++) */
245+
/* { */
246+
/* if (wcsncmp(command + i, L"\n", 1) == 0){ */
247+
/* num_newlines++; */
248+
/* } */
249+
/* } */
250+
/* int* line_endloc = (int*) malloc(sizeof(int) * num_newlines + 1); */
251+
/* int* line_lens = (int*) malloc(sizeof(int) * num_newlines + 1); */
252+
/* int* line_nleadingspaces = (int*) malloc(sizeof(int) * num_newlines + 1);*/
253+
254+
/* int curr_line = 0; */
255+
/* int curr_line = 0; */
256+
/* for (int i = 0; i < cmdlen; i++) */
257+
/* { */
258+
/* if (wcsncmp(command + i, L"\n", 1) == 0){ */
259+
/* num_newlines++; */
260+
/* } */
261+
/* } */
262+
263+
/* for (int i = 0; i < cmdlen; i++) */
264+
/* { */
265+
/* if (wcsncmp(command + i, L"\n", 1) == 0){ */
266+
/* num_newlines++; */
267+
/* } */
268+
/* if (wcsncmp(command + i, L" ", 1) == 0){ */
269+
/* num_spaces++; */
270+
/* } */
271+
/* fprintf(stderr, "command[%d] = '%lc'\n", i, command[i]); */
272+
/* } */
273+
/* fprintf(stderr, "num_newlines: %d\n", num_newlines); */
274+
/* fprintf(stderr, "num_spaces: %d\n", num_spaces); */
275+
276+
/*} */
277+
278+
PyObject* _unicode_dedent(PyObject *unicode)
279+
{
280+
PyObject *lines = PyUnicode_Splitlines(unicode, 1);
281+
/*PyObject_Print(lines, stdout, 0);*/
282+
/*fprintf(stdout, "\n");*/
283+
284+
Py_ssize_t num_lines = PyObject_Length(lines);
285+
286+
PyObject* space = PyUnicode_FromWideChar(L" ", -1);
287+
PyObject* emptystr = PyUnicode_FromWideChar(L"", -1);
288+
PyObject* new_unicode;
289+
290+
// Initialize leading space to a large value to indicate
291+
// that it is uninitialized
292+
Py_ssize_t effective_inf = PyObject_Length(unicode) + 1;
293+
Py_ssize_t common_leading_spaces = effective_inf;
294+
295+
for (Py_ssize_t line_idx = 0; line_idx < num_lines; line_idx ++)
296+
{
297+
PyObject* index = PyLong_FromSsize_t(line_idx);
298+
PyObject* line = PyObject_GetItem(lines, index);
299+
Py_ssize_t line_len = PyObject_Length(line);
300+
301+
PyObject* striped_line = _PyUnicode_XStrip(line, 0, space);
302+
Py_ssize_t stripline_len = PyObject_Length(striped_line);
303+
304+
Py_ssize_t leading_spaces = line_len - stripline_len;
305+
306+
// On non-empty lines, see if the amount of leading whitespace is less
307+
// than current value. If so, update it.
308+
if (line_len > 1)
309+
{
310+
if (leading_spaces < common_leading_spaces) {
311+
common_leading_spaces = leading_spaces;
312+
}
313+
}
314+
315+
/*fprintf(stdout, "Index: %d\n", line_idx); */
316+
/*fprintf(stdout, "Line Length: %d\n", line_len); */
317+
/*fprintf(stdout, "Strip Line Length: %d\n", stripline_len); */
318+
/*fprintf(stdout, "leading_spaces: %d\n", leading_spaces); */
319+
/*fprintf(stdout, "common_leading_spaces: %d\n", common_leading_spaces);*/
320+
/*fprintf(stdout, "Line: "); */
321+
/*PyObject_Print(PyObject_Repr(line), stdout, 1);*/
322+
//fprintf(stdout, "\n");
323+
Py_DECREF(line);
324+
Py_DECREF(index);
325+
}
326+
327+
if (common_leading_spaces > 0 && common_leading_spaces < effective_inf){
328+
329+
// We found common leading whitespace, strip if off.
330+
PyObject* new_lines = PyList_New(num_lines);
331+
for (Py_ssize_t line_idx = 0; line_idx < num_lines; line_idx ++)
332+
{
333+
PyObject* index = PyLong_FromSsize_t(line_idx);
334+
PyObject* line = PyObject_GetItem(lines, index);
335+
Py_ssize_t end = PyObject_Length(line);
336+
Py_ssize_t start = common_leading_spaces;
337+
if (end <= 1){
338+
start = 0;
339+
}
340+
PyObject* new_line = PyUnicode_Substring(line, start, end);
341+
PyList_SetItem(new_lines, line_idx, new_line);
342+
Py_DECREF(line);
343+
Py_DECREF(index);
344+
}
345+
/*PyObject_Print(PyObject_Repr(new_lines), stdout, 0);*/
346+
//fprintf(stdout, "\n");
347+
348+
new_unicode = PyUnicode_Join(emptystr, new_lines);
349+
350+
Py_DECREF(unicode);
351+
/*PyObject_Print(PyObject_Repr(new_unicode), stdout, 0);*/
352+
}
353+
else{
354+
new_unicode = unicode;
355+
}
356+
357+
//fprintf(stderr, "num_lines: %d\n", num_lines);
358+
Py_DECREF(lines);
359+
return new_unicode;
360+
}
361+
362+
232363
static int
233364
pymain_run_command(wchar_t *command)
234365
{
235366
PyObject *unicode, *bytes;
236367
int ret;
237368

369+
//_command_dedent(wchar_t *command)
370+
238371
unicode = PyUnicode_FromWideChar(command, -1);
239372
if (unicode == NULL) {
240373
goto error;
@@ -244,6 +377,8 @@ pymain_run_command(wchar_t *command)
244377
return pymain_exit_err_print();
245378
}
246379

380+
unicode = _unicode_dedent(unicode);
381+
247382
bytes = PyUnicode_AsUTF8String(unicode);
248383
Py_DECREF(unicode);
249384
if (bytes == NULL) {

0 commit comments

Comments
 (0)