Skip to content

Commit faf4955

Browse files
authored
Implement scroll by last line (#10057)
1 parent 11e46b3 commit faf4955

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

news/2 Fixes/9116.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove whitespace from code before pushing to the interactive window

src/datascience-ui/common/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,37 @@ export function splitMultilineString(source: nbformat.MultilineString): string[]
5454
return [];
5555
}
5656

57+
export function removeLinesFromFrontAndBack(code: string): string {
58+
const lines = code.splitLines({ trim: false, removeEmptyEntries: false });
59+
let foundNonEmptyLine = false;
60+
let lastNonEmptyLine = -1;
61+
let result: string[] = [];
62+
parseForComments(
63+
lines,
64+
(_s, i) => {
65+
result.push(lines[i]);
66+
lastNonEmptyLine = i;
67+
},
68+
(s, i) => {
69+
const trimmed = s.trim();
70+
if (foundNonEmptyLine || trimmed) {
71+
result.push(lines[i]);
72+
foundNonEmptyLine = true;
73+
}
74+
if (trimmed) {
75+
lastNonEmptyLine = i;
76+
}
77+
}
78+
);
79+
80+
// Remove empty lines off the bottom too
81+
if (lastNonEmptyLine < lines.length - 1) {
82+
result = result.slice(0, result.length - (lines.length - 1 - lastNonEmptyLine));
83+
}
84+
85+
return result.join('\n');
86+
}
87+
5788
// Strip out comment lines from code
5889
export function stripComments(str: string): string {
5990
let result: string = '';

src/datascience-ui/history-react/redux/reducers/creation.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { Identifiers } from '../../../../client/datascience/constants';
55
import { InteractiveWindowMessages } from '../../../../client/datascience/interactive-common/interactiveWindowTypes';
66
import { ICell, IDataScienceExtraSettings } from '../../../../client/datascience/types';
7+
import { removeLinesFromFrontAndBack } from '../../../common';
78
import { createCellVM, extractInputText, ICellViewModel, IMainState } from '../../../interactive-common/mainState';
89
import { createPostableAction } from '../../../interactive-common/redux/postOffice';
910
import { Helpers } from '../../../interactive-common/redux/reducers/helpers';
@@ -19,6 +20,14 @@ export namespace Creation {
1920
return true;
2021
}
2122

23+
function extractInputBlockText(cellVM: ICellViewModel, settings?: IDataScienceExtraSettings) {
24+
// Use the base function first
25+
const text = extractInputText(cellVM, settings);
26+
27+
// Then remove text on the front and back. We only do this for the interactive window
28+
return removeLinesFromFrontAndBack(text);
29+
}
30+
2231
export function alterCellVM(cellVM: ICellViewModel, settings?: IDataScienceExtraSettings, visible?: boolean, expanded?: boolean): ICellViewModel {
2332
if (cellVM.cell.data.cell_type === 'code') {
2433
// If we are already in the correct state, return back our initial cell vm
@@ -41,13 +50,13 @@ export namespace Creation {
4150
if (cellVM.inputBlockOpen !== expanded && cellVM.inputBlockCollapseNeeded && cellVM.inputBlockShow) {
4251
if (expanded) {
4352
// Expand the cell
44-
const newText = extractInputText(cellVM, settings);
53+
const newText = extractInputBlockText(cellVM, settings);
4554

4655
newCellVM.inputBlockOpen = true;
4756
newCellVM.inputBlockText = newText;
4857
} else {
4958
// Collapse the cell
50-
let newText = extractInputText(cellVM, settings);
59+
let newText = extractInputBlockText(cellVM, settings);
5160
if (newText.length > 0) {
5261
newText = newText.split('\n', 1)[0];
5362
newText = newText.slice(0, 255); // Slice to limit length, slicing past length is fine

src/test/datascience/cellFactory.unit.test.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'use strict';
44
import { assert } from 'chai';
55
import { generateCells } from '../../client/datascience/cellFactory';
6-
import { stripComments } from '../../datascience-ui/common';
6+
import { removeLinesFromFrontAndBack, stripComments } from '../../datascience-ui/common';
77

88
// tslint:disable: max-func-body-length
99
suite('Data Science CellFactory', () => {
@@ -150,4 +150,51 @@ class Pizza(object):
150150
nonComments = stripComments(multilineQuoteInFunc);
151151
assert.equal(nonComments.splitLines().length, 6, 'Splitting quote in func wrong number of lines');
152152
});
153+
154+
test('Line removal', () => {
155+
const entry1 = `# %% CELL
156+
157+
first line`;
158+
const expected1 = `# %% CELL
159+
first line`;
160+
const entry2 = `# %% CELL
161+
162+
first line
163+
164+
`;
165+
const expected2 = `# %% CELL
166+
first line`;
167+
const entry3 = `# %% CELL
168+
169+
first line
170+
171+
second line
172+
173+
`;
174+
const expected3 = `# %% CELL
175+
first line
176+
177+
second line`;
178+
179+
const entry4 = `
180+
181+
if (foo):
182+
print('stuff')
183+
184+
print('some more')
185+
186+
`;
187+
const expected4 = `if (foo):
188+
print('stuff')
189+
190+
print('some more')`;
191+
let removed = removeLinesFromFrontAndBack(entry1);
192+
assert.equal(removed, expected1);
193+
removed = removeLinesFromFrontAndBack(entry2);
194+
assert.equal(removed, expected2);
195+
removed = removeLinesFromFrontAndBack(entry3);
196+
assert.equal(removed, expected3);
197+
removed = removeLinesFromFrontAndBack(entry4);
198+
assert.equal(removed, expected4);
199+
});
153200
});

0 commit comments

Comments
 (0)